BoneCP implements Serializable, Closeable { public Connection getConnection() throws SQLException { return this.connectionStrategy.getConnection(); } BoneCP(BoneCPConfig config); synchronized void shutdown(); void close(); Connection getConnection(); ListenableFuture<Connection> getAsyncConnection(); boolean isConnectionHandleAlive(ConnectionHandle connection); int getTotalLeased(); int getTotalFree(); int getTotalCreatedConnections(); BoneCPConfig getConfig(); Statistics getStatistics(); AtomicBoolean getDbIsDown(); static final String MBEAN_CONFIG; static final String MBEAN_BONECP; }
@Test public void testGetConnection() throws SQLException, InterruptedException, IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException { expect(mockPartition.isUnableToCreateMoreTransactions()).andReturn(true).once(); expect(mockPartition.getFreeConnections()).andReturn(mockConnectionHandles).anyTimes(); expect(mockPartition.getAvailableConnections()).andReturn(1).anyTimes(); expect(mockConnectionHandles.poll()).andReturn(mockConnection).once(); mockConnection.renewConnection(); expectLastCall().once(); replay(mockPartition, mockConnectionHandles, mockConnection); assertEquals(mockConnection, testClass.getConnection()); verify(mockPartition, mockConnectionHandles, mockConnection); }
@Test public void testGetConnectionWithTimeout() throws SQLException, InterruptedException, IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException { expect(mockPartition.isUnableToCreateMoreTransactions()).andReturn(true).once(); expect(mockPartition.getFreeConnections()).andReturn(mockConnectionHandles).anyTimes(); expect(mockConnectionHandles.poll()).andReturn(null).anyTimes(); replay(mockPartition, mockConnectionHandles, mockConnection); try{ testClass.getConnection(); fail("Should have thrown an exception"); } catch (SQLException e){ } verify(mockPartition, mockConnectionHandles, mockConnection); }
@Test public void testGetConnectionOnShutdownPool() { testClass.poolShuttingDown = true; try{ testClass.getConnection(); fail("Should have thrown an exception"); } catch (SQLException e){ } }
@Test public void testGetConnectionUncheckedExceptionTriggeredWhileWaiting() throws NoSuchFieldException, IllegalAccessException { reset(mockPartition, mockConnectionHandles, mockConnection); expect(mockPartition.isUnableToCreateMoreTransactions()).andReturn(false).anyTimes(); expect(mockPartition.getFreeConnections()).andReturn(mockConnectionHandles).anyTimes(); expect(mockPartition.getMaxConnections()).andReturn(0).anyTimes(); replay(mockPartition, mockConnectionHandles, mockConnection, mockLock); try{ testClass.getConnection(); fail("Should have thrown an exception"); } catch (Throwable t){ } verify(mockPartition, mockConnectionHandles, mockConnection, mockLock); }
@Test public void testGetConnectionLimitsHit() throws SQLException { reset(mockPartition, mockConnectionHandles, mockConnection); expect(mockConfig.getPoolAvailabilityThreshold()).andReturn(0).anyTimes(); expect(mockPartition.isUnableToCreateMoreTransactions()).andReturn(false).anyTimes(); expect(mockPartition.getFreeConnections()).andReturn(mockConnectionHandles).anyTimes(); expect(mockPartition.getMaxConnections()).andReturn(10).anyTimes(); expect(mockPartition.getAvailableConnections()).andReturn(1).anyTimes(); BlockingQueue<Object> bq = new ArrayBlockingQueue<Object>(1); bq.add(new Object()); expect(mockPartition.getPoolWatchThreadSignalQueue()).andReturn(bq); expect(mockConnectionHandles.poll()).andReturn(mockConnection).once(); mockConnection.renewConnection(); expectLastCall().once(); replay(mockPartition, mockConnectionHandles, mockConnection); testClass.getConnection(); verify(mockPartition, mockConnectionHandles, mockConnection); }
@Test public void testGetConnectionConnectionQueueStarved() throws NoSuchFieldException, IllegalAccessException, InterruptedException, SQLException { reset(mockPartition, mockConnectionHandles, mockConnection); expect(mockPartition.isUnableToCreateMoreTransactions()).andReturn(true).once(); expect(mockPartition.getFreeConnections()).andReturn(mockConnectionHandles).anyTimes(); expect(mockConnectionHandles.poll(Long.MAX_VALUE, TimeUnit.MILLISECONDS)).andReturn(mockConnection).once(); mockConnection.renewConnection(); expectLastCall().once(); replay(mockPartition, mockConnectionHandles, mockConnection); assertEquals(mockConnection, testClass.getConnection()); verify(mockPartition, mockConnectionHandles, mockConnection); }
@Test public void testGetConnectionSimulateInterruptedException2() throws NoSuchFieldException, IllegalAccessException, InterruptedException { reset(mockPartition, mockConnectionHandles, mockConnection); expect(mockPartition.isUnableToCreateMoreTransactions()).andReturn(true).once(); expect(mockPartition.getFreeConnections()).andReturn(mockConnectionHandles).anyTimes(); expect(mockConnectionHandles.poll()).andReturn(null).once(); expect(mockConnectionHandles.poll(Long.MAX_VALUE, TimeUnit.MILLISECONDS)).andThrow(new InterruptedException()).once(); replay(mockPartition, mockConnectionHandles, mockConnection); try{ testClass.getConnection(); fail("Should have throw an SQL Exception"); } catch (SQLException e){ } verify(mockPartition, mockConnectionHandles, mockConnection); }
@Test public void testGetConnectionSimulateInterruptedExceptionWithNullReturn() throws NoSuchFieldException, IllegalAccessException, InterruptedException { reset(mockPartition, mockConnectionHandles, mockConnection); expect(mockPartition.isUnableToCreateMoreTransactions()).andReturn(true).once(); expect(mockPartition.getFreeConnections()).andReturn(mockConnectionHandles).anyTimes(); testClass.nullOnConnectionTimeout = true; expect(mockConnectionHandles.poll()).andReturn(null).once(); expect(mockConnectionHandles.poll(Long.MAX_VALUE, TimeUnit.MILLISECONDS)).andThrow(new InterruptedException()).once(); replay(mockPartition, mockConnectionHandles, mockConnection); try{ assertNull(testClass.getConnection()); } catch (SQLException e){ fail("Should not have throw an SQL Exception"); } verify(mockPartition, mockConnectionHandles, mockConnection); }
@Test public void testGetConnectionSimulateInterruptedException() throws NoSuchFieldException, IllegalAccessException, InterruptedException { reset(mockPartition, mockConnectionHandles, mockConnection); expect(mockPartition.getMaxConnections()).andReturn(100).anyTimes(); expect(mockPartition.getFreeConnections()).andReturn(mockConnectionHandles).anyTimes(); expect(mockConnectionHandles.poll(Long.MAX_VALUE, TimeUnit.MILLISECONDS)).andThrow(new InterruptedException()).once(); BlockingQueue<Object> bq = new ArrayBlockingQueue<Object>(1); bq.add(new Object()); expect(mockPartition.getPoolWatchThreadSignalQueue()).andReturn(bq); replay(mockPartition, mockConnectionHandles, mockConnection); try{ testClass.getConnection(); fail("Should have throw an SQL Exception"); } catch (SQLException e){ } verify(mockPartition, mockConnectionHandles, mockConnection); }
@Test public void testGetConnectionWithNullReturn() throws NoSuchFieldException, IllegalAccessException, InterruptedException { reset(mockPartition, mockConnectionHandles, mockConnection); expect(mockPartition.getMaxConnections()).andReturn(100).anyTimes(); expect(mockPartition.getFreeConnections()).andReturn(mockConnectionHandles).anyTimes(); expect(mockConnectionHandles.poll(Long.MAX_VALUE, TimeUnit.MILLISECONDS)).andReturn(null).once(); BlockingQueue<Object> bq = new ArrayBlockingQueue<Object>(1); bq.add(new Object()); testClass.nullOnConnectionTimeout = true; expect(mockPartition.getPoolWatchThreadSignalQueue()).andReturn(bq); replay(mockPartition, mockConnectionHandles, mockConnection); try{ assertNull(testClass.getConnection()); } catch (SQLException e){ fail("Should have throw an SQL Exception"); } verify(mockPartition, mockConnectionHandles, mockConnection); }
@Test public void testGetConnectionBlockOnUnavailable() throws InterruptedException, SQLException { reset(mockPartition, mockConnectionHandles, mockConnection); expect(mockPartition.isUnableToCreateMoreTransactions()).andReturn(true).once(); expect(mockPartition.getFreeConnections()).andReturn(mockConnectionHandles).anyTimes(); expect(mockPartition.getAvailableConnections()).andReturn(1).anyTimes(); expect(mockConnectionHandles.poll()).andReturn(null).once(); expect(mockConnectionHandles.poll(Long.MAX_VALUE, TimeUnit.MILLISECONDS)).andReturn(mockConnection).once(); mockConnection.renewConnection(); expectLastCall().once(); replay(mockPartition, mockConnectionHandles, mockConnection); assertEquals(mockConnection, testClass.getConnection()); verify(mockPartition, mockConnectionHandles, mockConnection); }