1 17 18 19 package org.apache.derbyTesting.functionTests.tests.lang; 20 21 import java.sql.*; 22 import java.util.Random ; 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 38 public class largeCodeGen extends BaseJDBCTestCase 39 { 40 private static boolean TEST_QUERY_EXECUTION = true; 41 42 43 44 public largeCodeGen(String name) 45 { 46 super(name); 47 } 48 49 public static Test suite() { 50 TestSuite suite = new TestSuite(); 51 52 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 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 75 { 76 Statement stmt = createStatement(); 77 stmt.execute("DROP TABLE T0"); 78 stmt.close(); 79 commit(); 80 super.tearDown(); 81 } 82 83 84 95 private boolean checkT0Query(String testName, 96 StringBuffer 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 assertSQLState("42ZA0", e); 120 return true; 121 122 } 123 } 124 125 128 public void testLogicalOperators() throws SQLException { 129 130 int passCount = 0; 131 for (int count = 700; count <= 10000 ; count += 100) 132 { 133 if (logicalOperators(count)) 135 break; 136 137 passCount = count; 138 } 139 140 assertEquals("logical operators change from previous limit", 144 800, passCount); 145 146 147 } 150 151 152 157 private boolean logicalOperators(int numOperands) throws SQLException { 158 159 String pred = "(si = ? AND si = ? )"; 161 String testName = "Logical operators with " + numOperands + " parameters"; 162 StringBuffer sqlBuffer = new StringBuffer ((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 assertFalse("IN clause with 3400 parameters ", inClause(3400)); 181 182 int passCount = 0; 183 for (int count = 97000; count <= 200000 ; count += 1000) 184 { 185 if (inClause(count)) 187 break; 188 passCount = count; 189 } 190 191 assertEquals("IN clause change from previous limit", 98000, passCount); 194 } 195 196 204 private boolean inClause(int numParams) throws SQLException { 205 String testName = "IN clause with " + numParams + " parameters"; 206 StringBuffer sqlBuffer = new StringBuffer ((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 viewName = "v0"; 219 Statement stmt = createStatement(); 220 221 StringBuffer createView = new StringBuffer ("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 stmt.executeUpdate(createView.toString()); 229 commit(); 230 231 int passCount = 0; 232 for (int count = 1000; count <= 1000; count += 1000) 233 { 234 if (largeUnionSelect(viewName, count)) 236 break; 237 passCount = count; 238 239 } 240 241 246 createStatement().executeUpdate("DROP VIEW " + viewName); 247 248 assertEquals("UNION operators change from previous limit", 252 1000, passCount); 253 254 255 } 256 257 private boolean largeUnionSelect(String viewName, 258 int numUnions) throws SQLException 259 { 260 261 263 String unionClause = " UNION ALL (SELECT * FROM " + viewName + ")"; 264 265 StringBuffer selectSQLBuffer = 266 new StringBuffer (((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 String selectSQL = selectSQLBuffer.toString(); 278 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 assertSQLState("42ZA0", sqle); 302 303 return true; 304 305 } 306 307 } 308 309 private static void checkRowData(ResultSet rs) throws SQLException 311 { 312 String [] 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 328 public void testInsertValues() throws SQLException { 329 int passCount = 0; 330 for (int count = 1500; count <= 1700; count += 200) { 331 if (insertValues(count)) 333 break; 334 passCount = count; 335 336 } 337 338 assertEquals("INSERT VALUES change from previous limit", 1700, passCount); 341 } 342 343 351 private boolean insertValues(int rowCount) throws SQLException { 352 Random r = new Random (3457245435L); 353 354 StringBuffer insertSQL = new StringBuffer ( 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 assertSQLState("42ZA0", e); 415 } 416 417 return true; 418 } 419 } 420 | Popular Tags |