Skip to content

Commit

Permalink
added a new test to ensure exceptions are bubbled up to connectionman…
Browse files Browse the repository at this point in the history
…ager and connectionpool interfaces when a connection fails to be created.
  • Loading branch information
jennifer_andre authored and jennifer_andre committed Jan 31, 2011
1 parent d118137 commit 6adc2fa
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
24 changes: 23 additions & 1 deletion tests/com/opower/util/powerpool/ConnectionManagerTest.java
Expand Up @@ -37,11 +37,33 @@ public void tearDown() throws Exception {
context.assertIsSatisfied();
}

@Test
public final void testRequestConnectionThrowsExceptionIfNoneCanBeMade() throws SQLException {
ConnectionManagerImpl manager = new ConnectionManagerImpl(dummySettings);
context.checking(new Expectations() {{
oneOf(dummySettings).getNewConnectionProvider(); will(returnValue(dummyConnectionProvider));
oneOf(dummyConnectionProvider).newConnection(); will(throwException(new SQLException("Failed to create a connection")));
atLeast(1).of(dummySettings).getMaximumConnections(); will(returnValue(1));
}});
String message ="";
try {
Connection connection = manager.requestConnection();
} catch (SQLException e) {
// a bit redundant..
message = e.getMessage();

}
Assert.assertEquals("Failed to create a connection",message);

assertEquals(0, manager.usedConnections.size());
assertEquals(0, manager.idleConnections.size());

}
@Test
public final void testRequestConnectionWhenEmptyAddsOneConnectionToQueue() throws SQLException {
ConnectionManagerImpl manager = new ConnectionManagerImpl(dummySettings);
context.checking(new Expectations() {{
atLeast(1).of(dummySettings).getNewConnectionProvider(); will(returnValue(dummyConnectionProvider));
oneOf(dummySettings).getNewConnectionProvider(); will(returnValue(dummyConnectionProvider));
oneOf(dummyConnectionProvider).newConnection(); will(returnValue(dummyConnection));
atLeast(1).of(dummySettings).getMaximumConnections(); will(returnValue(1));
}});
Expand Down
20 changes: 20 additions & 0 deletions tests/com/opower/util/powerpool/SimpleConnectionPoolTest.java
Expand Up @@ -84,6 +84,26 @@ public void testConnectionReturnedIsAProxiedConnection() throws SQLException {
context.assertIsSatisfied();
}

@Test
public void testExceptionIsThrownWhenNoConnectionCanBeCreated() throws SQLException {
final SimpleConnectionPool pool = new SimpleConnectionPool( dummyManager, dummyWrapper, Monitor.DUMMY_MONITOR);

context.checking(new Expectations() {{
oneOf(dummyManager).requestConnection(); will(throwException(new SQLException("Failed to create a connection")));


}});
String message ="";
try {
Connection pooledConnection = pool.getConnection();
} catch (SQLException e) {
// a bit redundant..
message = e.getMessage();

}
Assert.assertEquals("Failed to create a connection",message);

}

@Test
public void testReturningAConnectionCallsManagerToFreeConnection() throws SQLException {
Expand Down

0 comments on commit 6adc2fa

Please sign in to comment.