use of org.apache.derbyTesting.functionTests.tests.jdbcapi.AssertEventCatcher in project derby by apache.
the class DataSourceTest method testConnectionErrorEvent.
/**
* Test case for DERBY-3172
* When the Derby engine is shutdown or Network Server is brought down, any
* api on JDBC Connection object should generate a Connection error event.
*/
public void testConnectionErrorEvent() throws SQLException, Exception {
AssertEventCatcher aes12 = new AssertEventCatcher(12);
// Get the correct ConnectionPoolDataSource object
ConnectionPoolDataSource ds = J2EEDataSource.getConnectionPoolDataSource();
PooledConnection pc = ds.getPooledConnection();
// Add a connection event listener to ConnectionPoolDataSource
pc.addConnectionEventListener(aes12);
Connection conn = pc.getConnection();
dropTable(conn, "TAB1");
// No event should have been generated at this point
assertFalse(aes12.didConnectionClosedEventHappen());
assertFalse(aes12.didConnectionErrorEventHappen());
aes12.resetState();
// mode we are running in.
if (usingEmbedded()) {
getTestConfiguration().shutdownDatabase();
} else {
getTestConfiguration().stopNetworkServer();
}
// before shutdown and they all should generate connection error event.
try {
conn.createArrayOf("junk", null);
} catch (SQLException e) {
assertSQLState("0A000", e);
}
try {
conn.createNClob();
} catch (SQLException e) {
assertSQLState("0A000", e);
}
try {
conn.createSQLXML();
} catch (SQLException e) {
assertSQLState("0A000", e);
}
try {
conn.createStruct("junk", null);
} catch (SQLException e) {
assertSQLState("0A000", e);
}
try {
conn.createBlob();
} catch (SQLException e) {
// meaning No current connection
if (usingEmbedded())
assertSQLState("08003", e);
else
assertSQLState("08006", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.createClob();
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.getClientInfo();
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.getClientInfo("junk");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.setClientInfo(null);
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.setClientInfo("junk1", "junk2");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.isWrapperFor(this.getClass());
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
if (usingEmbedded())
assertTrue(aes12.didConnectionErrorEventHappen());
else
// We do not make any call on underneath JDBC Connection
// object for isWrapperFor and hence never get Connection
// Error event
assertFalse(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.unwrap(this.getClass());
} catch (SQLException e) {
if (usingEmbedded())
assertSQLState("08003", e);
else
// We do not make any call on underneath JDBC Connection
// object for unwrap and hence never get Connection
// closed exception. Instead we got exception because
// client driver code is trying to unwrap this.getClass
// and it can't do that
assertSQLState("XJ128", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
if (usingEmbedded())
assertTrue(aes12.didConnectionErrorEventHappen());
else
// We do not make any call on underneath JDBC Connection
// object for isWrapperFor and hence never get Connection
// Error event
assertFalse(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.isValid(5);
} catch (SQLException e) {
assertSQLState("08003", e);
}
if (usingEmbedded())
assertTrue(aes12.didConnectionClosedEventHappen());
else
assertFalse(aes12.didConnectionClosedEventHappen());
// As per the JDBC definition, an exception and hence an event is raised
// for isValid only if the param value is illegal
assertFalse(aes12.didConnectionErrorEventHappen());
aes12.resetState();
Class<?> clazz;
if (usingEmbedded()) {
clazz = Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
clazz.getConstructor().newInstance();
} else {
getTestConfiguration().startNetworkServer();
}
// Get a new connection to the database
conn = getConnection();
conn.close();
}
Aggregations