KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbcapi > maxfieldsize


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.maxfieldsize
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.tests.jdbcapi;
23 import java.sql.Connection JavaDoc;
24 import java.sql.DriverManager JavaDoc;
25 import java.sql.PreparedStatement JavaDoc;
26 import java.sql.ResultSetMetaData JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.Statement JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.sql.Types JavaDoc;
31 import java.sql.PreparedStatement JavaDoc;
32 import java.io.*;
33
34 import org.apache.derby.tools.ij;
35 import org.apache.derby.tools.JDBCDisplayUtil;
36 import org.apache.derbyTesting.functionTests.util.TestUtil;
37
38 /**
39  *This Program Test SetMaxFieldsize()/getMaxFieldsize().
40  *and the getXXX calls that are affected.
41  * @author - suresht
42  */

43
44 public class maxfieldsize {
45     
46    
47     public static ResultSet JavaDoc rs;
48
49     static final int START_SECOND_HALF = 5;
50     static final int NUM_EXECUTIONS = 2 * START_SECOND_HALF;
51
52     static final String JavaDoc c1_value="C1XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
53     static final String JavaDoc c2_value="C2XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
54     static final String JavaDoc c3_value="C3XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
55     static final String JavaDoc c4_value="C4XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
56     static final String JavaDoc c5_value="C5XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
57     static final String JavaDoc c6_value="C6XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
58     
59     static private boolean isDerbyNet = false;
60    
61     public static void main(String JavaDoc[] args) {
62         Connection JavaDoc conn;
63         Statement JavaDoc stmt;
64
65         // start by cleaning up, just in case
66
String JavaDoc[] testObjects = {"table tab1", "table tab2"};
67         
68
69         System.out.println("Test MaxFieldSize starting");
70
71         isDerbyNet = TestUtil.isNetFramework();
72         try
73         {
74             // use the ij utility to read the property file and
75
// make the initial connection.
76
ij.getPropertyArg(args);
77              conn = ij.startJBMS();
78              stmt = conn.createStatement();
79              TestUtil.cleanUpTest(stmt, testObjects);
80             //create a table, insert a row, do a select from the table,
81
stmt.execute("create table tab1("+
82                                            "c1 char(100) for bit data,"+
83                                            "c2 varchar(100) for bit data," +
84                                            "c3 long varchar for bit data,"+
85                                            "c4 char(100),"+
86                                            "c5 varchar(100),"+
87                                            "c6 long varchar)");
88             // load some data into this table ..
89
load_data(conn);
90
91             // read the data of each type with all the possible functions
92
int loop = 0;
93             while (loop < NUM_EXECUTIONS )
94             {
95                 if (loop == START_SECOND_HALF)
96                 {
97                     stmt.setMaxFieldSize(24);
98                 }
99
100                 System.out.println("Iteration #: " + loop);
101                 System.out.println("Max Field Size = " + stmt.getMaxFieldSize());
102                 rs = stmt.executeQuery("select * from tab1");
103                 while (rs.next())
104                 {
105                     for(int i=1 ; i < 7 ; i++)
106                     {
107                         System.out.println("Column #: " + i);
108                         switch (loop % START_SECOND_HALF)
109                         {
110                             case 0:
111                                 connectionJdbc20.get_using_object(rs, i);
112                                 break;
113
114                             case 1:
115                                 if (isDerbyNet)
116                                     System.out.println("beetle 5350 - JCC returns incorrect result for maxfieldsize()");
117                                 connectionJdbc20.get_using_string(rs, i);
118                                 break;
119
120                             case 2:
121                                 connectionJdbc20.get_using_ascii_stream(rs, i);
122                                 break;
123
124                             case 3:
125                                 if(i < 4 ) // only c1 , c2, c3
126
{
127                                     connectionJdbc20.get_using_binary_stream(rs, i);
128                                 }
129                                 else
130                                 {
131                                     System.out.println("SKIPPING");
132                                 }
133                                 break;
134
135                             case 4:
136                                 if(i < 4 ) // only c1 , c2, c3
137
{
138                                     connectionJdbc20.get_using_bytes(rs, i);
139                                 }
140                                 else
141                                 {
142                                     System.out.println("SKIPPING");
143                                 }
144                                 break;
145                         }
146                     }
147                 }
148                 rs.close();
149                 loop++;
150             }
151             // make sure that we throw exception for invalid values
152
try{
153                 // negative value should throw an exception
154
stmt.setMaxFieldSize(-200);
155             } catch (SQLException JavaDoc e) {
156                 if ((e.getMessage() != null &&
157                      e.getMessage().indexOf("Invalid maxFieldSize value") >= 0)
158                     || (e.getSQLState() != null &&
159                         (e.getSQLState().equals("XJ066"))))
160                     System.out.println("Expected Exception - Invalid maxFieldsize");
161                 else System.out.println("Unexpected FAILURE at " +e);
162             }
163             // zero is valid value -- which means unlimited
164
stmt.setMaxFieldSize(0);
165
166             // Do an external sort (forced via properties file),
167
// verifying that streams work correctly
168
System.out.println("Doing external sort");
169
170             testSort(conn, stmt);
171
172             TestUtil.cleanUpTest(stmt, testObjects);
173
174             stmt.close();
175             conn.close();
176
177         }
178         catch (SQLException JavaDoc e) {
179             dumpSQLExceptions(e);
180             e.printStackTrace();
181         }
182         catch (Throwable JavaDoc e) {
183             System.out.println("FAIL -- unexpected exception: "+e);
184             e.printStackTrace();
185         }
186
187         System.out.println("Test maxfieldsize finished");
188     }
189
190
191     static private void load_data(Connection JavaDoc conn) throws Exception JavaDoc{
192         PreparedStatement JavaDoc pstmt = null;
193
194         try{
195             pstmt = conn.prepareStatement(
196                      "insert into tab1 values(?,?,?,?,?,?)");
197
198             pstmt.setBytes(1, c1_value.getBytes("US-ASCII"));
199             pstmt.setBytes(2, c2_value.getBytes("US-ASCII"));
200             pstmt.setBytes(3, c3_value.getBytes("US-ASCII"));
201             pstmt.setString(4, c4_value);
202             pstmt.setString(5, c5_value);
203             pstmt.setString(6, c6_value);
204             pstmt.execute();
205         }
206         catch(SQLException JavaDoc e)
207         {
208             dumpSQLExceptions(e);
209             e.printStackTrace();
210         }
211         catch(Throwable JavaDoc e )
212         {
213             System.out.println("Fail -- unexpected exception ");
214             e.printStackTrace();
215         }
216         finally
217         {
218              pstmt.close();
219         }
220     }
221
222     private static void testSort(Connection JavaDoc conn, Statement JavaDoc stmt)
223         throws SQLException JavaDoc, java.io.UnsupportedEncodingException JavaDoc
224     {
225         PreparedStatement JavaDoc insertPStmt;
226
227         // Load up a 2nd table using streams where appropriate
228
stmt.execute("create table tab2("+
229                                        "c0 int, " +
230                                           "c1 char(100) for bit data,"+
231                                           "c2 varchar(100) for bit data," +
232                                           "c3 long varchar for bit data,"+
233                                           "c4 char(100),"+
234                                           "c5 varchar(100),"+
235                                           "c6 long varchar)");
236
237         // Populate the table
238
insertPStmt = conn.prepareStatement(
239                         "insert into tab2 values (?, ?, ?, ?, ?, ?, ?)");
240         for (int index = 0; index < 5000; index++)
241         {
242             insertPStmt.setInt(1, index);
243             insertPStmt.setBytes(2, c1_value.getBytes("US-ASCII"));
244             insertPStmt.setBytes(3, c2_value.getBytes("US-ASCII"));
245             insertPStmt.setBytes(4, c3_value.getBytes("US-ASCII"));
246             insertPStmt.setString(5, c4_value);
247             insertPStmt.setString(6, c5_value);
248             insertPStmt.setString(7, c6_value);
249             insertPStmt.executeUpdate();
250         }
251
252         insertPStmt.close();
253
254         // Do sort with maxFieldSize = 0
255
doSort(stmt);
256
257         // Set maxFieldSize to 24 and do another sort
258
stmt.setMaxFieldSize(24);
259         doSort(stmt);
260     }
261
262     private static void doSort(Statement JavaDoc stmt)
263         throws SQLException JavaDoc
264     {
265         System.out.println("Max Field Size = " + stmt.getMaxFieldSize());
266
267         try
268         {
269             /* Do a descending sort on 1st column, but only select
270              * out 1st and last 5 rows. This should test streaming to/from
271              * a work table.
272              */

273             rs = stmt.executeQuery("select * from tab2 order by c0 desc");
274             for (int index = 0; index < 5000; index++)
275             {
276                 rs.next();
277                 if (index < 5 || index >= 4995)
278                 {
279                     System.out.println("Iteration #: " + index);
280                     System.out.println("Column #1: " + rs.getInt(1));
281                     System.out.println("Column #2:");
282                     connectionJdbc20.get_using_binary_stream(rs, 2);
283                     System.out.println("Column #3:");
284                     connectionJdbc20.get_using_binary_stream(rs, 3);
285                     System.out.println("Column #4:");
286                     connectionJdbc20.get_using_binary_stream(rs, 4);
287                     System.out.println("Column #5:");
288                     connectionJdbc20.get_using_ascii_stream(rs, 5);
289                     System.out.println("Column #6:");
290                     connectionJdbc20.get_using_ascii_stream(rs, 6);
291                     System.out.println("Column #7:");
292                     connectionJdbc20.get_using_ascii_stream(rs, 7);
293                 }
294             }
295         }
296         catch (SQLException JavaDoc e)
297         {
298             throw e;
299         }
300         catch (Throwable JavaDoc e) {
301             System.out.println("FAIL -- unexpected exception: "+e);
302             e.printStackTrace();
303         }
304     }
305
306     static private void dumpSQLExceptions (SQLException JavaDoc se) {
307         System.out.println("FAIL -- unexpected exception");
308         while (se != null)
309         {
310             System.out.println("SQLSTATE("+se.getSQLState()+"): "+se);
311             se = se.getNextException();
312         }
313     }
314
315     static private void dumpExpectedSQLExceptions (SQLException JavaDoc se) {
316         System.out.println("PASS -- expected exception");
317         while (se != null)
318         {
319             System.out.println("SQLSTATE("+se.getSQLState()+"): "+se);
320             se = se.getNextException();
321         }
322     }
323
324     static private void cleanUp(Connection JavaDoc conn) throws SQLException JavaDoc
325     {
326         
327     }
328
329
330 }
331
Popular Tags