/** * Tests fake exceptions, connection should be shutdown if the scheduler was marked as going down. * Same test except just used to check for a spurious interrupted exception (should be logged). * * @throws SQLException * @throws InterruptedException * @throws NoSuchFieldException * @throws SecurityException * @throws IllegalAccessException * @throws IllegalArgumentException */ @Test public void testExceptionOnCloseConnection() throws SQLException, InterruptedException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { ArrayBlockingQueue<ConnectionHandle> fakeFreeConnections = new ArrayBlockingQueue<ConnectionHandle>(1); fakeFreeConnections.add(mockConnection); config.setIdleConnectionTestPeriod(1); expect(mockPool.getConfig()).andReturn(config).anyTimes(); expect(mockConnectionPartition.getFreeConnections()).andReturn(fakeFreeConnections).anyTimes(); expect(mockConnectionPartition.getMinConnections()).andReturn(10).once(); expect(mockConnection.isPossiblyBroken()).andReturn(false); expect(mockConnection.getConnectionLastUsed()).andReturn(0L); expect(mockPool.isConnectionHandleAlive((ConnectionHandle) anyObject())) .andReturn(false) .anyTimes(); // connection should be closed mockConnection.internalClose(); expectLastCall().andThrow(new SQLException()); replay(mockPool, mockConnection, mockConnectionPartition, mockExecutor, mockLogger); this.testClass = new ConnectionTesterThread(mockConnectionPartition, mockExecutor, mockPool); Field loggerField = this.testClass.getClass().getDeclaredField("logger"); loggerField.setAccessible(true); loggerField.set(this.testClass, mockLogger); this.testClass.run(); verify(mockPool, mockConnectionPartition, mockExecutor, mockConnection, mockLogger); }
/** * Tests fake exceptions, connection should be shutdown if the scheduler was marked as going down. * Mostly for code coverage. * * @throws SQLException * @throws InterruptedException */ @Test public void testInterruptedException() throws SQLException, InterruptedException { ArrayBlockingQueue<ConnectionHandle> fakeFreeConnections = new ArrayBlockingQueue<ConnectionHandle>(1); fakeFreeConnections.add(mockConnection); config.setIdleConnectionTestPeriod(1); expect(mockPool.getConfig()).andReturn(config).anyTimes(); expect(mockConnectionPartition.getFreeConnections()).andReturn(fakeFreeConnections).anyTimes(); expect(mockConnectionPartition.getMinConnections()).andReturn(10).once(); expect(mockConnection.isPossiblyBroken()).andReturn(false); expect(mockConnection.getConnectionLastUsed()).andReturn(0L); expect(mockPool.isConnectionHandleAlive((ConnectionHandle) anyObject())) .andReturn(true) .anyTimes(); expect(mockExecutor.isShutdown()).andReturn(true); mockPool.releaseInAnyFreePartition( (ConnectionHandle) anyObject(), (ConnectionPartition) anyObject()); expectLastCall().andThrow(new InterruptedException()); // connection should be closed mockConnection.internalClose(); mockPool.postDestroyConnection(mockConnection); expectLastCall().once(); replay(mockPool, mockConnection, mockConnectionPartition, mockExecutor); this.testClass = new ConnectionTesterThread(mockConnectionPartition, mockExecutor, mockPool); this.testClass.run(); verify(mockPool, mockConnectionPartition, mockExecutor, mockConnection); }
@Before public void beforeTest() { config.setJdbcUrl(CommonTestUtils.url); config.setUsername(CommonTestUtils.username); config.setPassword(CommonTestUtils.password); config.setIdleConnectionTestPeriod(10000); config.setIdleMaxAge(0); config.setStatementsCacheSize(0); config.setReleaseHelperThreads(0); config.setStatementsCachedPerConnection(30); }
/** * Mock setup. * * @throws ClassNotFoundException */ @BeforeClass public static void setup() throws ClassNotFoundException { mockPool = createNiceMock(BoneCP.class); mockConnectionPartition = createNiceMock(ConnectionPartition.class); mockExecutor = createNiceMock(ScheduledExecutorService.class); mockConnection = createNiceMock(ConnectionHandle.class); mockLogger = createNiceMock(Logger.class); makeThreadSafe(mockLogger, true); config = new BoneCPConfig(); config.setIdleMaxAge(100); config.setIdleConnectionTestPeriod(100); }