1 21 22 package org.apache.derbyTesting.functionTests.tests.jdbc4; 23 24 import java.sql.*; 25 import javax.sql.*; 26 import junit.framework.*; 27 28 import org.apache.derbyTesting.functionTests.util.TestDataSourceFactory; 29 import org.apache.derbyTesting.junit.BaseJDBCTestCase; 30 31 import java.util.Enumeration ; 32 33 37 public class StatementEventsTest extends BaseJDBCTestCase { 38 39 44 private boolean xa; 45 50 private boolean callable; 51 52 53 private Statement closedStatement; 54 55 private int closedCount; 56 57 private Statement errorStatement; 58 59 private int errorCount; 60 61 65 private PooledConnection pooledConnection; 66 67 private Connection connection; 68 69 74 public StatementEventsTest(String name) { 75 super(name); 76 } 77 78 84 private void setXA(boolean xa) { 85 this.xa = xa; 86 } 87 88 95 private void setCallable(boolean callable) { 96 this.callable = callable; 97 } 98 99 104 public String getName() { 105 return super.getName() + (xa ? "_xa" : "_pooled") + 106 (callable ? "_callable" : "_prepared"); 107 } 108 109 111 117 public void setUp() throws SQLException { 118 if (xa) { 119 XADataSource ds = TestDataSourceFactory.getXADataSource(); 120 pooledConnection = ds.getXAConnection(); 121 } else { 122 ConnectionPoolDataSource ds = 123 TestDataSourceFactory.getConnectionPoolDataSource(); 124 pooledConnection = ds.getPooledConnection(); 125 } 126 StatementEventListener listener = new StatementEventListener() { 127 public void statementClosed(StatementEvent event) { 128 closedStatement = event.getStatement(); 129 closedCount++; 130 } 131 public void statementErrorOccurred(StatementEvent event) { 132 errorStatement = event.getStatement(); 133 errorCount++; 134 } 135 }; 136 pooledConnection.addStatementEventListener(listener); 137 connection = pooledConnection.getConnection(); 138 } 139 140 145 public void tearDown() throws SQLException { 146 connection.close(); 147 pooledConnection.close(); 148 } 149 150 156 public static Test suite() { 157 TestSuite suites = new TestSuite(); 158 boolean[] truefalse = new boolean[] { true, false }; 159 for (boolean xa : truefalse) { 160 for (boolean callable : truefalse) { 161 suites.addTest(new Suite(xa, callable)); 162 } 163 } 164 return suites; 165 } 166 167 171 private static class Suite extends TestSuite { 172 private Suite(boolean xa, boolean callable) { 173 super(StatementEventsTest.class); 174 for (Enumeration e = tests(); e.hasMoreElements(); ) { 175 StatementEventsTest test = 176 (StatementEventsTest) e.nextElement(); 177 test.setXA(xa); 178 test.setCallable(callable); 179 } 180 } 181 } 182 183 185 193 private PreparedStatement prepare(String sql) throws SQLException { 194 if (callable) { 195 return connection.prepareCall(sql); 196 } 197 return connection.prepareStatement(sql); 198 } 199 200 202 207 public void testCloseEvent() throws SQLException { 208 PreparedStatement ps = prepare("VALUES (1)"); 209 ps.close(); 210 assertSame("Close event raised on wrong statement.", 211 ps, closedStatement); 212 assertEquals("Incorrect close count.", 1, closedCount); 213 } 214 215 224 public void testCloseEventOnClosedConnection() throws SQLException { 225 PreparedStatement ps = prepare("VALUES (1)"); 226 connection.close(); 227 if (usingDerbyNetClient()) { 228 assertSame("Close event raised on wrong statement.", 229 ps, closedStatement); 230 assertEquals("Incorrect close count.", 1, closedCount); 231 } else if (usingEmbedded()) { 232 assertNull("Didn't expect close event.", closedStatement); 233 assertEquals("Incorrect close count.", 0, closedCount); 234 } else { 235 fail("Unknown framework."); 236 } 237 } 238 239 245 public void testErrorEventOnClosedConnection() throws SQLException { 246 PreparedStatement ps = prepare("VALUES (1)"); 247 connection.close(); 248 try { 249 ps.execute(); 250 fail("No exception thrown."); 251 } catch (SQLException e) { 252 assertSQLState("Unexpected SQL state.", "08003", e); 253 assertSame("Error event raised on wrong statement.", 254 ps, errorStatement); 255 assertEquals("Incorrect error count.", 1, errorCount); 256 } 257 } 258 } 259 | Popular Tags |