/** * Closing a connection handle should release that connection back in the pool and mark it as * closed. * * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InvocationTargetException * @throws NoSuchMethodException * @throws SQLException */ @Test public void testClose() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, SQLException { Field field = testClass.getClass().getDeclaredField("doubleCloseCheck"); field.setAccessible(true); field.set(testClass, true); testClass.renewConnection(); mockPool.releaseConnection((Connection) anyObject()); expectLastCall().once().andThrow(new SQLException()).once(); replay(mockPool); testClass.close(); // logically mark the connection as closed field = testClass.getClass().getDeclaredField("logicallyClosed"); field.setAccessible(true); Assert.assertTrue(field.getBoolean(testClass)); assertTrue(testClass.isClosed()); testClass.renewConnection(); try { testClass.close(); // 2nd time should throw an exception fail("Should have thrown an exception"); } catch (Throwable t) { // do nothing. } verify(mockPool); }
/** * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalArgumentException * @throws IllegalAccessException * @throws SQLException */ @Test public void testDoubleClose() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, SQLException { Field field = testClass.getClass().getDeclaredField("doubleCloseCheck"); field.setAccessible(true); field.set(testClass, true); field = testClass.getClass().getDeclaredField("logicallyClosed"); field.setAccessible(true); field.set(testClass, true); field = testClass.getClass().getDeclaredField("doubleCloseException"); field.setAccessible(true); field.set(testClass, "fakeexception"); mockLogger.error((String) anyObject(), anyObject()); expectLastCall().once(); mockPool.releaseConnection((Connection) anyObject()); expectLastCall().once().andThrow(new SQLException()).once(); replay(mockLogger, mockPool); testClass.close(); }