JdbcServiceList extends ServiceList implements ConnectedService { @Override public void setConnection(AdaptrisConnection c) { this.connection = c; } JdbcServiceList();  JdbcServiceList(Collection<Service> serviceList);  JdbcServiceList(Service... services); @Override void prepare(); @Override AdaptrisConnection getConnection(); @Override void setConnection(AdaptrisConnection c);  }
@Test public void testSetConnection() { JdbcConnection connection = new JdbcConnection(); JdbcServiceList list = new JdbcServiceList(); assertNull(list.getConnection()); list.setConnection(connection); assertEquals(connection, list.connection()); assertEquals(connection, list.getConnection()); list.setConnection(null); assertEquals(null, list.getConnection()); assertNull(list.connection()); }
@Test public void testServiceList_SqlConnectionInObjectMetadata() throws Exception { createDatabase(); JdbcServiceList service = createServiceCollection(); service.setConnection(createJdbcConnection()); AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(); execute(service, msg); assertTrue(msg.getObjectHeaders().containsKey(JdbcConstants.OBJ_METADATA_DATABASE_CONNECTION_KEY)); Connection sqlCon = (Connection) msg.getObjectHeaders().get(JdbcConstants.OBJ_METADATA_DATABASE_CONNECTION_KEY); }
@Test public void testServiceList_SequenceNumber_Commit_AutoCommit() throws Exception { createDatabase(); JdbcServiceList service = createServiceCollection(); service.setConnection(createJdbcConnection()); AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(); service.add(createSequenceNumberService(null, getName(), SequenceNumberCase.DEFAULT_ID)); execute(service, msg); doStandardAssertions(msg); assertTrue(msg.getObjectHeaders().containsKey(JdbcConstants.OBJ_METADATA_DATABASE_CONNECTION_KEY)); Connection sqlCon = (Connection) msg.getObjectHeaders().get(JdbcConstants.OBJ_METADATA_DATABASE_CONNECTION_KEY); }
@Test public void testServiceList_PooledConnection() throws Exception { int maxServices = 5; final int iterations = 5; int poolsize = maxServices - 1; createDatabase(); List<Service> serviceList = new ArrayList<Service>(); String name = Thread.currentThread().getName(); Thread.currentThread().setName(getName()); JdbcPooledConnection conn = PooledConnectionHelper.createPooledConnection(PROPERTIES.getProperty(CFG_JDBC_DRIVER), PROPERTIES.getProperty(CFG_JDBC_URL), poolsize); try { GuidGenerator guid = new GuidGenerator(); for (int i = 0; i < maxServices; i++) { JdbcServiceList service = createServiceCollection(createSequenceNumberService(conn, guid.safeUUID()), createSequenceNumberService(conn, guid.safeUUID())); service.setConnection(conn); serviceList.add(service); start(service); } PooledConnectionHelper.executeTest(serviceList, iterations, new PooledConnectionHelper.MessageCreator() { @Override public AdaptrisMessage createMsgForPooledConnectionTest() throws Exception { return AdaptrisMessageFactory.getDefaultInstance().newMessage(); } }); assertEquals(0, conn.currentBusyConnectionCount()); assertEquals(poolsize, conn.currentIdleConnectionCount()); assertEquals(poolsize, conn.currentConnectionCount()); } finally { stop(serviceList.toArray(new ComponentLifecycle[0])); Thread.currentThread().setName(name); } }
@Test public void testServiceList_AdvancedPooledConnection() throws Exception { int maxServices = 5; final int iterations = 5; int poolsize = maxServices - 1; createDatabase(); List<Service> serviceList = new ArrayList<Service>(); String name = Thread.currentThread().getName(); Thread.currentThread().setName(getName()); AdvancedJdbcPooledConnection conn = PooledConnectionHelper.createAdvancedPooledConnection(PROPERTIES.getProperty(CFG_JDBC_DRIVER), PROPERTIES.getProperty(CFG_JDBC_URL), poolsize); try { GuidGenerator guid = new GuidGenerator(); for (int i = 0; i < maxServices; i++) { JdbcServiceList service = createServiceCollection(createSequenceNumberService(conn, guid.safeUUID()), createSequenceNumberService(conn, guid.safeUUID())); service.setConnection(conn); serviceList.add(service); start(service); } PooledConnectionHelper.executeTest(serviceList, iterations, new PooledConnectionHelper.MessageCreator() { @Override public AdaptrisMessage createMsgForPooledConnectionTest() throws Exception { return AdaptrisMessageFactory.getDefaultInstance().newMessage(); } }); assertEquals(0, conn.currentBusyConnectionCount()); assertEquals(poolsize, conn.currentIdleConnectionCount()); assertEquals(poolsize, conn.currentConnectionCount()); } finally { stop(serviceList.toArray(new ComponentLifecycle[0])); Thread.currentThread().setName(name); } }
@Test public void testServiceList_SequenceNumber_NoAutoCommit() throws Exception { createDatabase(); JdbcServiceList service = createServiceCollection(); DatabaseConnection c = createJdbcConnection(); c.setAutoCommit(false); service.setConnection(c); AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(); service.add(createSequenceNumberService(null, getName(), SequenceNumberCase.DEFAULT_ID)); execute(service, msg); doStandardAssertions(msg); assertTrue(msg.getObjectHeaders().containsKey(JdbcConstants.OBJ_METADATA_DATABASE_CONNECTION_KEY)); Connection sqlCon = (Connection) msg.getObjectHeaders().get(JdbcConstants.OBJ_METADATA_DATABASE_CONNECTION_KEY); }
@Test public void testServiceList_ExceptionRollsback() throws Exception { createDatabase(); JdbcServiceList service = createServiceCollection(); DatabaseConnection c = createJdbcConnection(); c.setAutoCommit(false); c.setDebugMode(true); service.setConnection(c); AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(); String oldName = Thread.currentThread().getName(); Thread.currentThread().setName("testServiceList_ExceptionRollsback"); try { service.add(createSequenceNumberService(null, getName(), SequenceNumberCase.DEFAULT_ID)); service.add(new NullService() { @Override public void doService(AdaptrisMessage msg) throws ServiceException { throw new ServiceException("testServiceList_ExceptionRollsback throws an Exception"); } }); try { execute(service, msg); } catch (ServiceException expected) { } assertTrue(msg.getObjectHeaders().containsKey(JdbcConstants.OBJ_METADATA_DATABASE_CONNECTION_KEY)); Connection conn = (Connection) msg.getObjectHeaders().get(JdbcConstants.OBJ_METADATA_DATABASE_CONNECTION_KEY); assertTrue(conn.isClosed()); assertEquals(-1, getCurrentSequenceNumber(SequenceNumberCase.DEFAULT_ID)); } finally { Thread.currentThread().setName(oldName); } }
@Test public void testServiceList_RuntimeExceptionRollsback() throws Exception { createDatabase(); JdbcServiceList service = createServiceCollection(); DatabaseConnection c = createJdbcConnection(); c.setAutoCommit(false); service.setConnection(c); AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(); service.add(createSequenceNumberService(null, getName(), SequenceNumberCase.DEFAULT_ID)); service.add(new NullService() { @Override public void doService(AdaptrisMessage msg) throws ServiceException { throw new RuntimeException("testServiceList_RuntimeExceptionRollsback"); } }); try { execute(service, msg); } catch (ServiceException expected) { } assertTrue(msg.getObjectHeaders().containsKey(JdbcConstants.OBJ_METADATA_DATABASE_CONNECTION_KEY)); Connection conn = (Connection) msg.getObjectHeaders().get(JdbcConstants.OBJ_METADATA_DATABASE_CONNECTION_KEY); assertTrue(conn.isClosed()); assertEquals(-1, getCurrentSequenceNumber(SequenceNumberCase.DEFAULT_ID)); }