@Test public void testGetObjectInstance() throws Exception { config.setAcquireIncrement(5); config.setMinConnectionsPerPartition(30); config.setMaxConnectionsPerPartition(100); config.setPartitionCount(1); Reference mockRef = createNiceMock(Reference.class); Enumeration<RefAddr> mockEnum = createNiceMock(Enumeration.class); RefAddr mockRefAddr = createNiceMock(RefAddr.class); expect(mockRef.getAll()).andReturn(mockEnum).anyTimes(); expect(mockEnum.hasMoreElements()).andReturn(true).times(2); expect(mockEnum.nextElement()).andReturn(mockRefAddr).anyTimes(); expect(mockRefAddr.getType()) .andReturn("driverClassName") .once() .andReturn("password") .times(2); expect(mockRefAddr.getContent()) .andReturn("com.jolbox.bonecp.MockJDBCDriver") .once() .andReturn("abcdefgh") .once(); replay(mockRef, mockEnum, mockRefAddr); BoneCPDataSource dsb = new BoneCPDataSource(); BoneCPDataSource result = (BoneCPDataSource) dsb.getObjectInstance(mockRef, null, null, null); assertEquals("abcdefgh", result.getPassword()); verify(mockRef, mockEnum, mockRefAddr); }
public static void main(String[] args) throws Exception { final Config config = Config.loadFromDisk("nexus-importer"); final HTTPCache http = HttpClient.createHttpCache(config); final XmlParser xmlParser = new XmlParser(); final BoneCPDataSource boneCp = config.createBoneCp(); XmlParser.debugXml = false; ObjectManager<NexusServerDto, ActorRef<NexusServer>> serverManager = new ObjectManager<>( "Nexus server", Collections.<NexusServerDto>emptySet(), new ObjectFactory<NexusServerDto, ActorRef<NexusServer>>() { public ActorRef<NexusServer> create(NexusServerDto server) { final NexusClient client = new NexusClient(http, server.url); String name = server.name; return ObjectUtil.threadedActor( name, config.nexusUpdateInterval, boneCp, "Nexus Server: " + name, new NexusServer(client, server, xmlParser)); } }); final AtomicBoolean shouldRun = new AtomicBoolean(true); config.addShutdownHook(currentThread(), shouldRun); while (shouldRun.get()) { try { List<NexusServerDto> newKeys; try (Connection c = boneCp.getConnection()) { newKeys = new NexusDao(c).selectServer(); } serverManager.update(newKeys); } catch (SQLException e) { e.printStackTrace(System.out); } synchronized (shouldRun) { shouldRun.wait(60 * 1000); } } serverManager.close(); }
@Test public void testMultithreadMultiPartition() throws InterruptedException, SQLException { CommonTestUtils.logTestInfo("Test multiple threads hitting a multiple partitions concurrently"); config.setAcquireIncrement(5); config.setMinConnectionsPerPartition(10); config.setMaxConnectionsPerPartition(25); config.setPartitionCount(5); config.setReleaseHelperThreads(0); BoneCPDataSource dsb = new BoneCPDataSource(config); dsb.setDriverClass("com.jolbox.bonecp.MockJDBCDriver"); CommonTestUtils.startThreadTest(100, 1000, dsb, 0, false); assertEquals(0, dsb.getTotalLeased()); dsb.close(); CommonTestUtils.logPass(); }
@Test public void testMultithreadSinglePartition() throws InterruptedException, SQLException { CommonTestUtils.logTestInfo("Test multiple threads hitting a single partition concurrently"); config.setAcquireIncrement(5); config.setMinConnectionsPerPartition(30); config.setMaxConnectionsPerPartition(100); config.setPartitionCount(1); BoneCPDataSource dsb = new BoneCPDataSource(config); dsb.setDriverClass("org.hsqldb.jdbcDriver"); CommonTestUtils.startThreadTest(100, 100, dsb, 0, false); assertEquals(0, dsb.getTotalLeased()); dsb.close(); CommonTestUtils.logPass(); }
@Test public void testMultithreadMultiPartitionWithRandomWorkDelay() throws InterruptedException, SQLException { CommonTestUtils.logTestInfo( "Test multiple threads hitting a partition and doing some work of random duration on each connection"); config.setAcquireIncrement(5); config.setMinConnectionsPerPartition(10); config.setMaxConnectionsPerPartition(25); config.setPartitionCount(5); BoneCPDataSource dsb = new BoneCPDataSource(config); dsb.setDriverClass("com.jolbox.bonecp.MockJDBCDriver"); CommonTestUtils.startThreadTest(100, 10, dsb, -50, false); assertEquals(0, dsb.getTotalLeased()); dsb.close(); CommonTestUtils.logPass(); }
@Test public void testMultithreadMultiPartitionWithConstantWorkDelay() throws InterruptedException, SQLException { CommonTestUtils.logTestInfo( "Test multiple threads hitting a partition and doing some work on each connection"); config.setAcquireIncrement(1); config.setMinConnectionsPerPartition(10); config.setMaxConnectionsPerPartition(10); config.setPartitionCount(1); BoneCPDataSource dsb = new BoneCPDataSource(config); dsb.setDriverClass("org.hsqldb.jdbcDriver"); CommonTestUtils.startThreadTest(15, 10, dsb, 50, false); assertEquals(0, dsb.getTotalLeased()); dsb.close(); CommonTestUtils.logPass(); }
/** * Tests general methods. * * @throws CloneNotSupportedException */ @Test public void testCloneEqualsHashCode() throws CloneNotSupportedException { config.setAcquireIncrement(5); config.setMinConnectionsPerPartition(30); config.setMaxConnectionsPerPartition(100); config.setPartitionCount(1); BoneCPDataSource dsb = new BoneCPDataSource(config); BoneCPDataSource clone = (BoneCPDataSource) dsb.clone(); assertTrue(clone.hasSameConfiguration(dsb)); assertFalse(clone.hasSameConfiguration(null)); assertTrue(clone.hasSameConfiguration(dsb)); clone.setJdbcUrl("something else"); assertFalse(clone.hasSameConfiguration(dsb)); }
/** * Mostly for code coverage. * * @throws IOException * @throws NoSuchMethodException * @throws SecurityException * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException * @throws ClassNotFoundException */ @Test public void testDataSource() throws SQLException, IOException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, ClassNotFoundException { config.setAcquireIncrement(5); config.setMinConnectionsPerPartition(30); config.setMaxConnectionsPerPartition(100); config.setPartitionCount(1); BoneCPDataSource dsb = new BoneCPDataSource(config); dsb.setPartitionCount(1); dsb.setAcquireRetryDelay(-1); dsb.setAcquireRetryAttempts(0); dsb.setMaxConnectionsPerPartition(100); dsb.setMinConnectionsPerPartition(30); dsb.setTransactionRecoveryEnabled(true); dsb.setConnectionHook(new CoverageHook()); dsb.setLazyInit(false); dsb.setStatementsCachedPerConnection(30); dsb.setStatementsCacheSize(30); dsb.setReleaseHelperThreads(0); dsb.setDriverClass("com.jolbox.bonecp.MockJDBCDriver"); dsb.isWrapperFor(String.class); dsb.setIdleMaxAge(0L); dsb.setAcquireIncrement(5); dsb.setIdleConnectionTestPeriod(0L); dsb.setConnectionTestStatement("test"); dsb.setInitSQL(CommonTestUtils.TEST_QUERY); dsb.setCloseConnectionWatch(true); dsb.setLogStatementsEnabled(false); dsb.getConnection().close(); assertNotNull(dsb.getConfig()); assertNotNull(dsb.toString()); dsb.setConnectionHookClassName("bad class name"); assertEquals("bad class name", dsb.getConnectionHookClassName()); assertNull(dsb.getConnectionHook()); dsb.setConnectionHookClassName("com.jolbox.bonecp.hooks.CustomHook"); assertTrue(dsb.getConnectionHook() instanceof CustomHook); File tmp = File.createTempFile("bonecp", ""); dsb.setLogWriter(new PrintWriter(tmp)); assertNotNull(dsb.getLogWriter()); try { dsb.setLoginTimeout(0); fail("Should throw exception"); } catch (UnsupportedOperationException e) { // do nothing } try { dsb.getLoginTimeout(); fail("Should throw exception"); } catch (UnsupportedOperationException e) { // do nothing } Connection c = dsb.getConnection("test", "test"); assertNotNull(c); BoneCPDataSource dsb2 = new BoneCPDataSource(); // empty constructor test dsb2.setDriverClass("inexistent"); try { dsb2.getConnection(); fail("Should fail"); } catch (SQLException e) { // do nothing } assertNull(dsb.unwrap(String.class)); assertEquals("com.jolbox.bonecp.MockJDBCDriver", dsb.getDriverClass()); dsb.setClassLoader(getClass().getClassLoader()); dsb.loadClass("java.lang.String"); assertEquals(getClass().getClassLoader(), dsb.getClassLoader()); }