KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > lang > largeCodeGen


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

17
18
19 package org.apache.derbyTesting.functionTests.tests.lang;
20
21 import java.sql.*;
22 import java.util.Random JavaDoc;
23
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26
27 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
28 import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
29 import org.apache.derbyTesting.junit.JDBC;
30
31
32 // This test tries to push byte code generation to the limit.
33
// It has to be run with a large amount of memory which is set with jvmflags in
34
// largeCodeGen_app.properties
35
// There are only a few types of cases now. Other areas need to be tested such as large in clauses, etc.
36
//
37

38 public class largeCodeGen extends BaseJDBCTestCase
39 {
40     private static boolean TEST_QUERY_EXECUTION = true;
41     
42    
43     
44     public largeCodeGen(String JavaDoc name)
45     {
46         super(name);
47     }
48     
49     public static Test suite() {
50         TestSuite suite = new TestSuite();
51         
52         // Code generation test, just invoke on embedded
53
// as the main function is to test the byte code compiler.
54
if (usingEmbedded()) {
55             suite.addTestSuite(largeCodeGen.class);
56             return new CleanDatabaseTestSetup(suite);
57         }
58         return suite;
59     }
60        
61     protected void setUp() throws SQLException
62     {
63         getConnection().setAutoCommit(false);
64         Statement stmt = createStatement();
65         
66         String JavaDoc createSQL = "create table t0 " +
67         "(si smallint,i int, bi bigint, r real, f float, d double precision, n5_2 numeric(5,2), dec10_3 decimal(10,3), ch20 char(3),vc varchar(20), lvc long varchar)";
68         stmt.executeUpdate(createSQL);
69         stmt.executeUpdate("insert into t0 values(2,3,4,5.3,5.3,5.3,31.13,123456.123, 'one','one','one')");
70         stmt.close();
71         commit();
72     }
73     
74     protected void tearDown() throws Exception JavaDoc
75     {
76         Statement stmt = createStatement();
77         stmt.execute("DROP TABLE T0");
78         stmt.close();
79         commit();
80         super.tearDown();
81     }
82     
83     
84     /**
85      * Prepares and executes query against table t0 with n parameters
86      * The assumption is that the query will always return our one row
87      * of data inserted into the t0 table.
88      *
89      * @param testName
90      * @param sqlBuffer - StringBuffer with SQL Text
91      * @param numParams - Number of parameters
92      * @param paramValue - Parameter value
93      * @return true if the check fails
94      */

95     private boolean checkT0Query(String JavaDoc testName,
96                 StringBuffer JavaDoc sqlBuffer, int numParams, int paramValue) {
97         PreparedStatement ps;
98         try {
99             ps = prepareStatement(sqlBuffer.toString());
100             if (TEST_QUERY_EXECUTION)
101             {
102                 for (int i = 1; i <= numParams; i++)
103                 {
104                     ps.setInt(i, paramValue);
105                 }
106                 ResultSet rs = ps.executeQuery();
107                 rs.next();
108                 checkRowData(rs);
109                 rs.close();
110             }
111             ps.close();
112             commit();
113             return false;
114         }catch (SQLException e)
115         {
116             // The top level exception is expected to be
117
// the "user-friendly" query is too complex
118
// rather than some linkage error.
119
assertSQLState("42ZA0", e);
120             return true;
121             
122         }
123     }
124
125     /**
126      * Test many logical operators in the where clause.
127      */

128     public void testLogicalOperators() throws SQLException {
129          
130        int passCount = 0;
131          for (int count = 700; count <= 10000 ; count += 100)
132          {
133              // keep testing until it fails
134
if (logicalOperators(count))
135                  break;
136              
137              passCount = count;
138          }
139          
140         // svn 372388 trunk - passed @ 400
141
// Fix to DERBY-921 - passed @ 800
142
// DERBY-921 - support 32bit branch offsets
143
assertEquals("logical operators change from previous limit",
144                 800, passCount);
145         
146      
147          // 10,000 causes Stack overflow and database corruption
148
//testLogicalOperators(con, 10000);
149
}
150
151     
152     /**
153      * Tests numParam parameter markers in a where clause
154      *
155      * @param numOperands
156      */

157     private boolean logicalOperators(int numOperands) throws SQLException {
158         
159         // First with parameters
160
String JavaDoc pred = "(si = ? AND si = ? )";
161         String JavaDoc testName = "Logical operators with " + numOperands + " parameters";
162         StringBuffer JavaDoc sqlBuffer = new StringBuffer JavaDoc((numOperands * 20) + 512);
163         sqlBuffer.append("SELECT * FROM T0 WHERE " + pred );
164         for (int i = 2; i < numOperands; i+=2)
165         {
166             sqlBuffer.append(" OR " + pred);
167         }
168         return checkT0Query(testName, sqlBuffer, numOperands, 2);
169         
170         
171         
172         
173     }
174     
175     public void testInClause() throws SQLException {
176       
177         // DERBY-739 raised number of parameters from 2700 to 3400
178
// svn 372388 trunk - passed @ 3400
179
// So perform a quick check there.
180
assertFalse("IN clause with 3400 parameters ", inClause(3400));
181         
182         int passCount = 0;
183          for (int count = 97000; count <= 200000 ; count += 1000)
184          {
185              // keep testing until it fails.
186
if (inClause(count))
187                 break;
188              passCount = count;
189          }
190          
191         // fixes for DERBY-766 to split methods with individual statements
192
// bumps the limit to 98,000 parameters.
193
assertEquals("IN clause change from previous limit", 98000, passCount);
194     }
195     
196     /**
197      * Test in clause with many parameters
198      *
199      * @param con
200      * @param numParams - Number of parameters to test
201      * @return true if the test fails
202      * @throws SQLException
203      */

204     private boolean inClause(int numParams) throws SQLException {
205         String JavaDoc testName = "IN clause with " + numParams + " parameters";
206         StringBuffer JavaDoc sqlBuffer = new StringBuffer JavaDoc((numParams * 20) + 512);
207         sqlBuffer.append("SELECT * FROM T0 WHERE SI IN (" );
208         for (int i = 1; i < numParams; i++)
209         {
210             sqlBuffer.append("?, ");
211         }
212         sqlBuffer.append("?)");
213         return checkT0Query(testName, sqlBuffer, numParams, 2);
214     }
215     
216     public void testUnions() throws SQLException
217     {
218         String JavaDoc viewName = "v0";
219         Statement stmt = createStatement();
220         
221         StringBuffer JavaDoc createView = new StringBuffer JavaDoc("create view " + viewName +
222                                                    " as select * from t0 " );
223         for (int i = 1; i < 100; i ++)
224         {
225             createView.append(" UNION ALL (SELECT * FROM t0 )");
226         }
227         //System.out.println(createViewString);
228
stmt.executeUpdate(createView.toString());
229         commit();
230         
231        int passCount = 0;
232         for (int count = 1000; count <= 1000; count += 1000)
233         {
234             // keep testing until it fails
235
if (largeUnionSelect(viewName, count))
236                 break;
237             passCount = count;
238            
239         }
240         
241         // 10000 gives a different constant pool error
242
// DERBY-1315 gives out of memory error.
243
//assertTrue("10000 UNION passed!",
244
// largeUnionSelect(viewName, 10000));
245

246         createStatement().executeUpdate("DROP VIEW " + viewName);
247
248         // svn 372388 trunk - passed @ 900
249
// trunk now back to 700
250
//
251
assertEquals("UNION operators change from previous limit",
252                 1000, passCount);
253         
254         
255     }
256     
257     private boolean largeUnionSelect(String JavaDoc viewName,
258             int numUnions) throws SQLException
259     {
260
261         // There are 100 unions in each view so round to the nearest 100
262

263         String JavaDoc unionClause = " UNION ALL (SELECT * FROM " + viewName + ")";
264
265         StringBuffer JavaDoc selectSQLBuffer =
266             new StringBuffer JavaDoc(((numUnions/100) * unionClause.length()) + 512);
267         
268         selectSQLBuffer.append("select * from t0 ");
269         
270         for (int i = 1; i < numUnions/100;i++)
271         {
272             selectSQLBuffer.append(unionClause);
273         }
274         
275         try {
276         // Ready to execute the problematic query
277
String JavaDoc selectSQL = selectSQLBuffer.toString();
278         //System.out.println(selectSQL);
279
PreparedStatement pstmt = prepareStatement(selectSQL);
280         if (largeCodeGen.TEST_QUERY_EXECUTION)
281         {
282             ResultSet rs = pstmt.executeQuery();
283             int numRows = 0;
284             while (rs.next())
285             {
286                 numRows++;
287                 if ((numRows % 100) == 0)
288                 checkRowData(rs);
289             }
290             rs.close();
291             commit();
292         }
293         pstmt.close();
294         return false;
295      
296         } catch (SQLException sqle)
297         {
298             // The top level exception is expected to be
299
// the "user-friendly" query is too complex
300
// rather than some linkage error.
301
assertSQLState("42ZA0", sqle);
302
303             return true;
304             
305         }
306
307       }
308
309     // Check the data on the positioned row against what we inserted.
310
private static void checkRowData(ResultSet rs) throws SQLException
311     {
312         //" values(2,3,4,5.3,5.3,5.3,31.13,123456.123, 'one','one','one')");
313
String JavaDoc[] values = {"2", "3", "4", "5.3","5.3","5.3","31.13","123456.123",
314                            "one","one","one"};
315         for (int i = 1; i <= 11; i ++)
316         {
317             assertEquals("Result set data value: ",
318                     values[i-1], rs.getString(i));
319         }
320     }
321     
322     /**
323      * Test an INSERT statement with a large number of rows in the VALUES clause.
324      * Reported as DERBY-1714.
325      * @throws SQLException
326      *
327      */

328     public void testInsertValues() throws SQLException {
329        int passCount = 0;
330         for (int count = 1500; count <= 1700; count += 200) {
331             // keep testing until it fails
332
if (insertValues(count))
333                 break;
334             passCount = count;
335
336         }
337
338         // Final fixes for DERBY-766 pushed the limit to 1700
339
// Beyond that a StackOverflow occurs.
340
assertEquals("INSERT VALUES change from previous limit", 1700, passCount);
341     }
342
343     /**
344      * Create a large insert statement with rowCount rows all with
345      * constants. Prepare and execute it and then rollback to leave
346      * the table unchanged.
347      * @param rowCount
348      * @return
349      * @throws SQLException
350      */

351     private boolean insertValues(int rowCount) throws SQLException {
352         Random JavaDoc r = new Random JavaDoc(3457245435L);
353
354         StringBuffer JavaDoc insertSQL = new StringBuffer JavaDoc(
355                 "INSERT INTO T0(SI,I,BI,R,F,D,N5_2,DEC10_3,CH20,VC,LVC) VALUES\n");
356
357         for (int i = 0; i < rowCount; i++) {
358             if (i != 0)
359                 insertSQL.append(',');
360
361             insertSQL.append('(');
362
363             insertSQL.append(((short) r.nextInt()));
364             insertSQL.append(',');
365             insertSQL.append(i);
366             insertSQL.append(',');
367             insertSQL.append(r.nextLong());
368             insertSQL.append(',');
369
370             insertSQL.append(r.nextFloat());
371             insertSQL.append(',');
372             insertSQL.append(r.nextFloat());
373             insertSQL.append(',');
374             insertSQL.append(r.nextDouble());
375             insertSQL.append(',');
376
377             insertSQL.append("462.54");
378             insertSQL.append(',');
379             insertSQL.append("9324324.34");
380             insertSQL.append(',');
381
382             insertSQL.append('\'');
383             insertSQL.append("c");
384             insertSQL.append(r.nextInt() % 10);
385             insertSQL.append('\'');
386             insertSQL.append(',');
387
388             insertSQL.append('\'');
389             insertSQL.append("vc");
390             insertSQL.append(r.nextInt() % 1000000);
391             insertSQL.append('\'');
392             insertSQL.append(',');
393
394             insertSQL.append('\'');
395             insertSQL.append("lvc");
396             insertSQL.append(r.nextInt());
397             insertSQL.append('\'');
398
399             insertSQL.append(')');
400
401             insertSQL.append('\n');
402         }
403
404         try {
405             PreparedStatement ps = prepareStatement(insertSQL.toString());
406             assertEquals("Incorrect update count", rowCount, ps.executeUpdate());
407             ps.close();
408             rollback();
409             return false;
410         } catch (SQLException e) {
411             // The top level exception is expected to be
412
// the "user-friendly" query is too complex
413
// rather than some linkage error.
414
assertSQLState("42ZA0", e);
415         }
416
417         return true;
418     }
419 }
420
Popular Tags