1 20 21 package org.apache.derbyTesting.functionTests.tests.jdbc4; 22 23 import org.apache.derbyTesting.functionTests.util.SQLStateConstants; 24 import org.apache.derbyTesting.junit.BaseJDBCTestCase; 25 26 import junit.framework.*; 27 28 import java.sql.*; 29 30 33 public class StatementTest 34 extends BaseJDBCTestCase { 35 36 37 private Statement stmt = null; 38 39 44 public StatementTest(String name) { 45 super(name); 46 } 47 48 54 protected void setUp() 55 throws SQLException { 56 getConnection().setAutoCommit(false); 57 stmt = createStatement(); 59 assertFalse("First statement must be open initially", 60 stmt.isClosed()); 61 } 62 63 68 protected void tearDown() 69 throws Exception { 70 if (stmt != null) { 72 stmt.close(); 73 } 74 75 super.tearDown(); 76 } 77 78 82 public void testIsClosedBasic() 83 throws SQLException { 84 ResultSet rs = stmt.executeQuery("select count(*) from stmtTable"); 85 assertFalse("Statement should still be open", stmt.isClosed()); 86 rs.close(); 87 assertFalse("Statement should be open after ResultSet has been " + 88 "closed", stmt.isClosed()); 89 stmt.close(); 90 assertTrue("Statement should be closed, close() has been called", 91 stmt.isClosed()); 92 } 93 94 98 public void testIsClosedWithTwoStatementsOnSameConnection() 99 throws SQLException { 100 Statement stmt2 = createStatement(); 102 assertFalse("Second statement must be open initially", 103 stmt2.isClosed()); 104 assertFalse("First statement should not be closed when " + 105 "creating a second statement", stmt.isClosed()); 106 ResultSet rs = stmt2.executeQuery("select count(*) from stmtTable"); 107 assertFalse("Second statement should be open after call to " + 108 "execute()", stmt2.isClosed()); 109 assertFalse("First statement should be open after call to " + 110 "second statment's execute()", stmt.isClosed()); 111 stmt2.close(); 112 assertTrue("Second statement should be closed, close() has " + 113 "been called!", stmt2.isClosed()); 114 assertFalse("First statement should be open after call to " + 115 "second statment's close()", stmt.isClosed()); 116 } 117 118 122 public void testIsClosedWhenClosingConnection() 123 throws SQLException { 124 Statement stmt2 = createStatement(); 126 assertFalse("Second statement must be open initially", 127 stmt2.isClosed()); 128 stmt2.execute("select count(*) from stmtTable"); 130 assertFalse("Second statement should be open after call to " + 131 "execute()", stmt2.isClosed()); 132 rollback(); 135 Connection con = getConnection(); 136 con.close(); 137 assertTrue("Connection should be closed after close()", 138 con.isClosed()); 139 assertTrue("First statement should be closed, as parent " + 140 "connection has been closed", stmt.isClosed()); 141 assertTrue("Second statement should be closed, as parent " + 142 "connection has been closed", stmt2.isClosed()); 143 } 144 145 155 public void testIsClosedWhenClosingConnectionInInvalidState() 156 throws SQLException { 157 stmt.executeQuery("select count(*) from stmtTable"); 158 Connection con = stmt.getConnection(); 160 try { 161 con.close(); 162 fail("Invalid transaction state exception was not thrown"); 163 } catch (SQLException sqle) { 164 String expectedState; 167 if ( this.usingDerbyNetClient() ) 168 expectedState = SQLStateConstants.INVALID_TRANSACTION_STATE_ACTIVE_SQL_TRANSACTION; 169 else 170 expectedState = SQLStateConstants.INVALID_TRANSACTION_STATE_NO_SUBCLASS; 171 172 if ( ! expectedState.equals(sqle.getSQLState()) ) 173 { 174 System.err.println("ERROR: Unexpected SQL State encountered; " 175 + "got " + sqle.getSQLState() + ", expected " 176 + expectedState + 177 ". Unexpected exception message is " + sqle.getMessage()); 178 179 throw sqle; 180 } 181 } 182 assertFalse("Statement should still be open, because " + 183 "Connection.close() failed", stmt.isClosed()); 184 assertFalse("Connection should still be open", con.isClosed()); 185 con.commit(); 187 con.close(); 188 assertTrue("Connection should be closed after close()", 189 con.isClosed()); 190 assertTrue("Statement should be closed, because " + 191 "the connection has been closed", stmt.isClosed()); 192 stmt.close(); 193 assertTrue("Statement should still be closed", stmt.isClosed()); 194 } 195 196 200 public void testStatementExecuteAfterConnectionClose() 201 throws SQLException { 202 Connection con = stmt.getConnection(); 203 con.close(); 204 assertTrue("Connection should be closed after close()", 205 con.isClosed()); 206 try { 207 stmt.executeQuery("select count(*) from stmtTable"); 208 } catch (SQLException sqle) { 209 assertEquals("Unexpected SQL state for performing " + 210 "operations on a closed statement.", 211 SQLStateConstants.CONNECTION_EXCEPTION_CONNECTION_DOES_NOT_EXIST, 212 sqle.getSQLState()); 213 } 214 assertTrue("Statement should be closed, because " + 215 "the connection has been closed", stmt.isClosed()); 216 } 217 218 public void testIsWrapperForStatement() throws SQLException { 219 assertTrue(stmt.isWrapperFor(Statement.class)); 220 } 221 222 public void testIsNotWrapperForPreparedStatement() throws SQLException { 223 assertFalse(stmt.isWrapperFor(PreparedStatement.class)); 224 } 225 226 public void testIsNotWrapperForCallableStatement() throws SQLException { 227 assertFalse(stmt.isWrapperFor(CallableStatement.class)); 228 } 229 230 public void testIsNotWrapperForResultSet() throws SQLException { 231 assertFalse(stmt.isWrapperFor(ResultSet.class)); 232 } 233 234 public void testUnwrapStatement() throws SQLException { 235 Statement stmt2 = stmt.unwrap(Statement.class); 236 assertSame("Unwrap returned wrong object.", stmt, stmt2); 237 } 238 239 public void testUnwrapPreparedStatement() { 240 try { 241 PreparedStatement ps = stmt.unwrap(PreparedStatement.class); 242 fail("Unwrap didn't fail."); 243 } catch (SQLException e) { 244 assertSQLState("XJ128", e); 245 } 246 } 247 248 public void testUnwrapCallableStatement() { 249 try { 250 CallableStatement cs = stmt.unwrap(CallableStatement.class); 251 fail("Unwrap didn't fail."); 252 } catch (SQLException e) { 253 assertSQLState("XJ128", e); 254 } 255 } 256 257 public void testUnwrapResultSet() throws SQLException { 258 try { 259 ResultSet rs = stmt.unwrap(ResultSet.class); 260 fail("Unwrap didn't fail."); 261 } catch (SQLException e) { 262 assertSQLState("XJ128", e); 263 } 264 } 265 266 269 public void testPoolable() throws SQLException { 270 assertFalse("Statement cannot be poolable by default", 271 stmt.isPoolable()); 272 stmt.setPoolable(true); 273 assertTrue("Statement must be poolable", stmt.isPoolable()); 274 275 stmt.setPoolable(false); 276 assertFalse("Statement cannot be poolable", stmt.isPoolable()); 277 } 278 279 282 public static Test suite() { 283 TestSuite suite = new TestSuite("StatementTest suite"); 284 suite.addTest(new StatementTestSetup( 286 new TestSuite(StatementTest.class))); 287 288 return suite; 289 } 290 291 } | Popular Tags |