/** * 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); }
/** * Tests various getter/setters. * * @throws IllegalArgumentException * @throws IllegalAccessException * @throws SecurityException * @throws NoSuchFieldException */ @SuppressWarnings("deprecation") @Test public void testSettersGetters() throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException { ConnectionPartition mockPartition = createNiceMock(ConnectionPartition.class); testClass.setOriginatingPartition(mockPartition); assertEquals(mockPartition, testClass.getOriginatingPartition()); testClass.setConnectionLastReset(123); assertEquals(testClass.getConnectionLastReset(), 123); testClass.setConnectionLastUsed(456); assertEquals(testClass.getConnectionLastUsed(), 456); Field field = testClass.getClass().getDeclaredField("possiblyBroken"); field.setAccessible(true); field.setBoolean(testClass, true); assertTrue(testClass.isPossiblyBroken()); Object debugHandle = new Object(); testClass.setDebugHandle(debugHandle); assertEquals(debugHandle, testClass.getDebugHandle()); testClass.setInternalConnection(mockConnection); assertEquals(mockConnection, testClass.getInternalConnection()); assertEquals(mockConnection, testClass.getRawConnection()); field = testClass.getClass().getDeclaredField("logicallyClosed"); field.setAccessible(true); field.setBoolean(testClass, true); assertTrue(testClass.isClosed()); testClass.setLogStatementsEnabled(true); assertTrue(testClass.isLogStatementsEnabled()); assertEquals(testClass.getPool(), mockPool); ArrayList<ReplayLog> testLog = new ArrayList<ReplayLog>(); testClass.setReplayLog(testLog); assertEquals(testClass.getReplayLog(), testLog); testClass.setInReplayMode(true); assertTrue(testClass.isInReplayMode()); testClass.setInReplayMode(false); testClass.threadUsingConnection = Thread.currentThread(); assertEquals(Thread.currentThread(), testClass.getThreadUsingConnection()); }