1 20 21 package org.apache.derbyTesting.functionTests.tests.jdbc4; 22 23 import java.lang.reflect.InvocationTargetException ; 24 import java.lang.reflect.Method ; 25 import java.sql.CallableStatement ; 26 import java.sql.SQLClientInfoException ; 27 import java.sql.Connection ; 28 import java.sql.PreparedStatement ; 29 import java.sql.ResultSet ; 30 import java.sql.SQLException ; 31 import java.sql.SQLFeatureNotSupportedException ; 32 import java.sql.Statement ; 33 import java.util.Arrays ; 34 import java.util.Properties ; 35 import javax.sql.ConnectionPoolDataSource ; 36 import javax.sql.DataSource ; 37 import javax.sql.PooledConnection ; 38 import javax.sql.XAConnection ; 39 import javax.sql.XADataSource ; 40 import junit.extensions.TestSetup; 41 import junit.framework.Test; 42 import junit.framework.TestSuite; 43 import org.apache.derbyTesting.functionTests.util.TestDataSourceFactory; 44 import org.apache.derbyTesting.junit.BaseJDBCTestCase; 45 import org.apache.derbyTesting.junit.TestConfiguration; 46 47 53 public class ClosedObjectTest extends BaseJDBCTestCase { 54 55 private final Method method_; 56 58 private final ObjectDecorator decorator_; 59 60 private String name_; 61 62 68 public ClosedObjectTest(Method method, ObjectDecorator decorator) { 69 super("testClosedObjects"); 70 method_ = method; 71 decorator_ = decorator; 72 name_ = method.getDeclaringClass().getName() + "." + method.getName(); 74 } 75 76 81 public String getName() { 82 return name_; 83 } 84 85 91 public void testClosedObjects() throws Throwable { 92 try { 93 Object object = decorator_.getClosedObject(); 94 95 name_ = object.getClass() + "." + method_.getName(); 97 98 method_.invoke(object, 99 getNullArguments(method_.getParameterTypes())); 100 assertFalse("No exception was thrown", 101 decorator_.expectsException(method_)); 102 } catch (InvocationTargetException ite) { 103 try { 104 throw ite.getCause(); 105 } catch (SQLFeatureNotSupportedException fnse) { 106 } catch (SQLException sqle) { 109 decorator_.checkException(method_, sqle); 110 } 111 } 112 } 113 114 122 public static Test suite() { 123 TestSuite topSuite = new TestSuite(); 124 125 TestSuite dsSuite = new TestSuite(); 126 DataSourceDecorator dsDecorator = new DataSourceDecorator(dsSuite); 127 topSuite.addTest(dsDecorator); 128 fillDataSourceSuite(dsSuite, dsDecorator); 129 130 TestSuite poolSuite = new TestSuite(); 131 PoolDataSourceDecorator poolDecorator = 132 new PoolDataSourceDecorator(poolSuite); 133 topSuite.addTest(poolDecorator); 134 fillDataSourceSuite(poolSuite, poolDecorator); 135 136 TestSuite xaSuite = new TestSuite(); 137 XADataSourceDecorator xaDecorator = new XADataSourceDecorator(xaSuite); 138 topSuite.addTest(xaDecorator); 139 fillDataSourceSuite(xaSuite, xaDecorator); 140 141 return topSuite; 142 } 143 144 154 private static void fillDataSourceSuite(TestSuite suite, 155 DataSourceDecorator dsDecorator) 156 { 157 TestSuite rsSuite = new TestSuite(); 158 ResultSetObjectDecorator rsDecorator = 159 new ResultSetObjectDecorator(rsSuite, dsDecorator); 160 suite.addTest(rsDecorator); 161 fillObjectSuite(rsSuite, rsDecorator, ResultSet .class); 162 163 TestSuite stmtSuite = new TestSuite(); 164 StatementObjectDecorator stmtDecorator = 165 new StatementObjectDecorator(stmtSuite, dsDecorator); 166 suite.addTest(stmtDecorator); 167 fillObjectSuite(stmtSuite, stmtDecorator, Statement .class); 168 169 TestSuite psSuite = new TestSuite(); 170 PreparedStatementObjectDecorator psDecorator = 171 new PreparedStatementObjectDecorator(psSuite, dsDecorator); 172 suite.addTest(psDecorator); 173 fillObjectSuite(psSuite, psDecorator, PreparedStatement .class); 174 175 TestSuite csSuite = new TestSuite(); 176 CallableStatementObjectDecorator csDecorator = 177 new CallableStatementObjectDecorator(csSuite, dsDecorator); 178 suite.addTest(csDecorator); 179 fillObjectSuite(csSuite, csDecorator, CallableStatement .class); 180 181 TestSuite connSuite = new TestSuite(); 182 ConnectionObjectDecorator connDecorator = 183 new ConnectionObjectDecorator(connSuite, dsDecorator); 184 suite.addTest(connDecorator); 185 fillObjectSuite(connSuite, connDecorator, Connection .class); 186 } 187 188 196 private static void fillObjectSuite(TestSuite suite, 197 ObjectDecorator decorator, 198 Class iface) 199 { 200 for (Method m : iface.getMethods()) { 201 ClosedObjectTest cot = new ClosedObjectTest(m, decorator); 202 suite.addTest(cot); 203 } 204 } 205 206 214 private static Object [] getNullArguments(Class [] params) { 215 Object [] args = new Object [params.length]; 216 for (int i = 0; i < params.length; i++) { 217 args[i] = getNullValueForType(params[i]); 218 } 219 return args; 220 } 221 222 231 private static Object getNullValueForType(Class type) { 232 if (!type.isPrimitive()) { 233 return null; 234 } 235 if (type == Boolean.TYPE) { 236 return Boolean.FALSE; 237 } 238 if (type == Character.TYPE) { 239 return new Character ((char) 0); 240 } 241 if (type == Byte.TYPE) { 242 return new Byte ((byte) 0); 243 } 244 if (type == Short.TYPE) { 245 return new Short ((short) 0); 246 } 247 if (type == Integer.TYPE) { 248 return new Integer (0); 249 } 250 if (type == Long.TYPE) { 251 return new Long (0L); 252 } 253 if (type == Float.TYPE) { 254 return new Float (0f); 255 } 256 if (type == Double.TYPE) { 257 return new Double (0d); 258 } 259 fail("Don't know how to handle type " + type); 260 return null; } 262 263 267 private static abstract class ObjectDecorator extends TestSetup { 268 269 private final DataSourceDecorator decorator_; 270 271 protected Object object_; 272 273 279 public ObjectDecorator(Test test, DataSourceDecorator decorator) { 280 super(test); 281 decorator_ = decorator; 282 } 283 284 289 public Object getClosedObject() { 290 return object_; 291 } 292 293 302 public boolean expectsException(Method method) { 303 final String [] exceptionLessMethods = { 304 "close", 305 "isClosed", 306 "isValid", 307 }; 308 for (String name : exceptionLessMethods) { 309 if (name.equals(method.getName())) return false; 310 } 311 return true; 312 } 313 314 322 public final void checkException(Method method, SQLException sqle) 323 throws SQLException 324 { 325 if (!expectsException(method)) { 326 throw sqle; 327 } 328 checkSQLState(method, sqle); 329 } 330 331 338 protected abstract void checkSQLState(Method method, 339 SQLException sqle) 340 throws SQLException ; 341 342 348 protected Connection createConnection() throws SQLException { 349 return decorator_.newConnection(); 350 } 351 352 358 protected Statement createStatement() throws SQLException { 359 return decorator_.getConnection().createStatement(); 360 } 361 362 369 protected PreparedStatement prepareStatement(String sql) 370 throws SQLException 371 { 372 return decorator_.getConnection().prepareStatement(sql); 373 } 374 375 382 protected CallableStatement prepareCall(String call) 383 throws SQLException 384 { 385 return decorator_.getConnection().prepareCall(call); 386 } 387 } 388 389 392 private static class ResultSetObjectDecorator extends ObjectDecorator { 393 394 private Statement stmt_; 395 396 402 public ResultSetObjectDecorator(Test test, 403 DataSourceDecorator decorator) { 404 super(test, decorator); 405 } 406 407 412 public void setUp() throws SQLException { 413 stmt_ = createStatement(); 414 ResultSet rs = stmt_.executeQuery("VALUES(1)"); 415 rs.close(); 416 object_ = rs; 417 } 418 419 424 public void tearDown() throws SQLException { 425 stmt_.close(); 426 } 427 428 436 protected void checkSQLState(Method method, SQLException sqle) 437 throws SQLException 438 { 439 if (sqle.getSQLState().equals("XCL16")) { 440 } else { 442 throw sqle; 444 } 445 } 446 } 447 448 451 private static class StatementObjectDecorator extends ObjectDecorator { 452 458 public StatementObjectDecorator(Test test, 459 DataSourceDecorator decorator) { 460 super(test, decorator); 461 } 462 463 468 public void setUp() throws SQLException { 469 Statement stmt = createStatement(); 470 stmt.close(); 471 object_ = stmt; 472 } 473 474 483 protected void checkSQLState(Method method, SQLException sqle) 484 throws SQLException 485 { 486 String sqlState = sqle.getSQLState(); 487 if (sqlState.equals("XJ012")) { 488 } else { 490 throw sqle; 492 } 493 } 494 } 495 496 499 private static class PreparedStatementObjectDecorator 500 extends StatementObjectDecorator 501 { 502 509 public PreparedStatementObjectDecorator(Test test, 510 DataSourceDecorator decorator) 511 { 512 super(test, decorator); 513 } 514 515 520 public void setUp() throws SQLException { 521 PreparedStatement ps = prepareStatement("VALUES(1)"); 522 ps.close(); 523 object_ = ps; 524 } 525 526 536 protected void checkSQLState(Method method, SQLException sqle) 537 throws SQLException 538 { 539 if (method.getDeclaringClass() == Statement .class && 540 sqle.getSQLState().equals("XJ016")) { 541 } else { 544 super.checkSQLState(method, sqle); 545 } 546 547 } 548 } 549 550 553 private static class CallableStatementObjectDecorator 554 extends PreparedStatementObjectDecorator 555 { 556 563 public CallableStatementObjectDecorator(Test test, 564 DataSourceDecorator decorator) 565 { 566 super(test, decorator); 567 } 568 569 574 public void setUp() throws SQLException { 575 CallableStatement cs = 576 prepareCall("CALL SYSCS_UTIL.SYSCS_SET_RUNTIMESTATISTICS(1)"); 577 cs.close(); 578 object_ = cs; 579 } 580 } 581 582 585 private static class ConnectionObjectDecorator extends ObjectDecorator { 586 592 public ConnectionObjectDecorator(Test test, 593 DataSourceDecorator decorator) { 594 super(test, decorator); 595 } 596 597 602 public void setUp() throws SQLException { 603 Connection conn = createConnection(); 604 conn.rollback(); conn.close(); 606 object_ = conn; 607 } 608 609 619 protected void checkSQLState(Method method, SQLException sqle) 620 throws SQLException 621 { 622 if (sqle instanceof SQLClientInfoException && 623 method.getName().equals("setClientInfo") && 624 Arrays.asList(method.getParameterTypes()) 625 .equals(Arrays.asList(new Class [] { Properties .class }))) { 626 } else if (sqle.getSQLState().equals("08003")) { 629 } else { 631 throw sqle; 633 } 634 } 635 } 636 637 641 private static class DataSourceDecorator extends TestSetup { 642 643 private Connection connection_; 644 645 650 public DataSourceDecorator(Test test) { 651 super(test); 652 } 653 654 659 public final void setUp() throws SQLException { 660 connection_ = newConnection(); 661 } 662 663 668 public final Connection getConnection() { 669 return connection_; 670 } 671 672 678 public final Connection newConnection() throws SQLException { 679 Connection conn = newConnection_(); 680 conn.setAutoCommit(false); 681 return conn; 682 } 683 684 689 public final void tearDown() throws SQLException { 690 connection_.rollback(); 691 connection_.close(); 692 } 693 694 700 protected Connection newConnection_() throws SQLException { 701 DataSource ds = TestDataSourceFactory.getDataSource(); 702 return ds.getConnection(TestConfiguration.getCurrent().getUserName(), 703 TestConfiguration.getCurrent().getUserPassword()); 704 } 705 } 706 707 711 private static class PoolDataSourceDecorator extends DataSourceDecorator { 712 717 public PoolDataSourceDecorator(Test test) { 718 super(test); 719 } 720 721 728 protected Connection newConnection_() throws SQLException { 729 ConnectionPoolDataSource ds = TestDataSourceFactory.getConnectionPoolDataSource(); 730 PooledConnection pc = 731 ds.getPooledConnection(TestConfiguration.getCurrent().getUserName(), 732 TestConfiguration.getCurrent().getUserPassword()); 733 return pc.getConnection(); 734 } 735 } 736 737 741 private static class XADataSourceDecorator extends DataSourceDecorator { 742 747 public XADataSourceDecorator(Test test) { 748 super(test); 749 } 750 751 757 protected Connection newConnection_() throws SQLException { 758 XADataSource ds = TestDataSourceFactory.getXADataSource(); 759 XAConnection xac = ds.getXAConnection(TestConfiguration.getCurrent().getUserName(), 760 TestConfiguration.getCurrent().getUserPassword()); 761 return xac.getConnection(); 762 } 763 } 764 } 765 | Popular Tags |