KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > util > StreamUtil


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.util.StreamUtil
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.util;
23
24 import org.apache.derby.iapi.services.sanity.SanityManager;
25 import org.apache.derby.iapi.error.StandardException;
26 import org.apache.derby.iapi.db.*;
27 import java.sql.*;
28 import java.io.ByteArrayInputStream JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.math.BigDecimal JavaDoc;
31 import java.math.BigInteger JavaDoc;
32
33 /**
34  * Methods for stream columns
35  */

36 public class StreamUtil
37 {
38
39     public static void insertAsciiColumn
40     (
41         Connection conn,
42         String JavaDoc stmtText,
43         int colNumber,
44         String JavaDoc value,
45         int length
46     )
47         throws Throwable JavaDoc
48     {
49         PreparedStatement ps = conn.prepareStatement(stmtText);
50         setAsciiColumn(ps, colNumber, value.charAt(0), length);
51         ps.setInt(colNumber + 1, length);
52         ps.execute();
53     }
54     
55     public static void insertBinaryColumn
56     (
57         Connection conn,
58         String JavaDoc stmtText,
59         int colNumber,
60         String JavaDoc value,
61         int length
62     )
63         throws Throwable JavaDoc
64     {
65         PreparedStatement ps = conn.prepareStatement(stmtText);
66         setBinaryColumn(ps, colNumber, value.charAt(0), length);
67         ps.setInt(colNumber + 1, length);
68         ps.execute();
69     }
70
71     /**
72      * Set a particular column to whatever you
73      * wish.
74      */

75     private static void setAsciiColumn
76     (
77         PreparedStatement ps,
78         int colNumber,
79         char value,
80         int length
81     ) throws SQLException
82     {
83         byte[] barray = new byte[length];
84         for (int i = 0; i < length; i++)
85         {
86             barray[i] = (byte)value;
87         }
88         ps.setAsciiStream(colNumber, new ByteArrayInputStream JavaDoc(barray), length);
89     }
90
91     /**
92      * Set a particular column to whatever you
93      * wish.
94      */

95     private static void setBinaryColumn
96     (
97         PreparedStatement ps,
98         int colNumber,
99         char value,
100         int length
101     ) throws SQLException
102     {
103         byte[] barray = new byte[length];
104         for (int i = 0; i < length; i++)
105         {
106             barray[i] = (byte)value;
107         }
108         ps.setBinaryStream(colNumber, new ByteArrayInputStream JavaDoc(barray), length);
109     }
110     
111     public static int getAsciiColumn
112     (
113         int whichRS, // 0 means old, 1 means new
114
int colNumber,
115         String JavaDoc value
116     ) throws Throwable JavaDoc
117     {
118         System.out.println("\ngetAsciiColumn() called");
119         ResultSet rs = getRowSet(whichRS);
120         if( rs == null)
121             return 0;
122         while (rs.next())
123         {
124             InputStream JavaDoc in = rs.getAsciiStream(colNumber);
125             int readlen = drainAndValidateStream(in, value.charAt(0));
126             if (readlen != rs.getInt(4))
127                 throw new Exception JavaDoc("INCORRECT READ LENGTH " + readlen + " <> " + rs.getInt(4));
128         }
129         return 1;
130     }
131
132     private static ResultSet getRowSet( int whichRS)
133         throws Throwable JavaDoc
134     {
135         TriggerExecutionContext tec = org.apache.derby.iapi.db.Factory.getTriggerExecutionContext();
136         if( tec == null)
137         {
138             System.out.println( "Not in a trigger.");
139             return null;
140         }
141         
142         return (whichRS == 0) ? tec.getOldRowSet() : tec.getNewRowSet();
143     }
144     
145     public static int getBinaryColumn
146     (
147         int whichRS, // 0 means old, 1 means new
148
int colNumber,
149         String JavaDoc value
150     ) throws Throwable JavaDoc
151     {
152         System.out.println("\ngetBinaryColumn() called");
153         ResultSet rs = getRowSet(whichRS);
154         if( rs == null)
155             return 0;
156         while (rs.next())
157         {
158             InputStream JavaDoc in = rs.getBinaryStream(colNumber);
159             int readlen = drainAndValidateStream(in, value.charAt(0));
160
161             if (readlen != rs.getInt(4))
162                 throw new Exception JavaDoc("INCORRECT READ LENGTH " + readlen + " <> " + rs.getInt(4));
163         }
164         return 1;
165     }
166
167     private static int drainAndValidateStream(InputStream JavaDoc in, char value)
168         throws Throwable JavaDoc
169     {
170         byte[] buf = new byte[1024];
171         int inputLength = 0;
172         while(true)
173         {
174             int size = 0;
175             try
176             {
177                 size = in.read(buf);
178             } catch(Throwable JavaDoc t)
179             {
180                 System.out.println("Got exception on byte "+inputLength+". Rethrowing...");
181                 throw t;
182             }
183             if (size == -1)
184                 break;
185
186             for (int i = 0; i < size; i++)
187             {
188                 if (buf[i] != (byte)value)
189                 {
190                     throw new Throwable JavaDoc("TEST ERROR: byte "+(i+inputLength)+" not what is expected. It is '"+(char)buf[i]+"' rather than '"+value+"'");
191                 }
192             }
193             inputLength += size;
194         }
195         // System.out.println("...read "+inputLength+" bytes");
196
return inputLength;
197     }
198 }
199
Popular Tags