@Test public void testCastableFromProxy() throws SQLException { Connection con = new TestConnectionImpl(); try (Connection proxy = ConnectionWrapper.wrap(con, noOpEventListener, ConnectionInformation.fromTestConnection(con))) { // if the proxy implements the interface then true should be returned. assertTrue(proxy.isWrapperFor(Connection.class)); assertTrue(proxy.isWrapperFor(TestConnection.class)); assertTrue(proxy.isWrapperFor(Wrapper.class)); } }
@Test public void testCastableFromUnderlying() throws SQLException { Connection con = new TestConnectionImpl(); try (Connection proxy = ConnectionWrapper.wrap(con, noOpEventListener, ConnectionInformation.fromTestConnection(con))) { // if the underlying object extends the class (or matches the class) then true should be returned. assertTrue(proxy.isWrapperFor(TestConnectionImpl.class)); assertTrue(proxy.isWrapperFor(AbstractTestConnection.class)); } }
@Test public void testExecute() throws SQLException { String query = "select * from customers"; try (Connection connectionWrapper = // ConnectionWrapper.wrap( // this.connection, new SimpleJdbcEventListener() { @Override public void onBeforeAnyExecute(StatementInformation statementInformation) { assertThat("sql of statementInformation", statementInformation.getSql(), is(notNullValue())); } }, // ConnectionInformation.fromTestConnection(this.connection) // ) // ) { P6TestUtil.execute(connectionWrapper, query); } } }
@Test public void testProxyOfWrappedConnection() throws SQLException { // this will be the actual connection Connection con = new TestConnectionImpl(); // use a wrapper from DBCP to create a proxy of a proxy // Note: DBCP implements with JDBC 4.0 API so the Wrapper interface // is implemented here. DelegatingConnection underlying = new DelegatingConnection(con); try (Connection proxy = ConnectionWrapper.wrap(con, noOpEventListener, ConnectionInformation.fromTestConnection(con))) { // TestConnection is an interface of the wrapped underlying object. assertTrue(proxy.isWrapperFor(TestConnection.class)); // ResultSet is not implemented at all - false should be returned assertFalse(proxy.isWrapperFor(ResultSet.class)); } }
@Test public void testCastableFromUnderlying() throws SQLException { Connection con = new TestConnectionImpl(); try (Connection proxy = ConnectionWrapper.wrap(con, noOpEventListener, ConnectionInformation.fromTestConnection(con))) { // if the underlying object extends the class (or matches the class) then the underlying object should be returned. { AbstractTestConnection unwrapped = proxy.unwrap(AbstractTestConnection.class); assertFalse(AbstractWrapper.isProxy(unwrapped)); } { TestConnectionImpl unwrapped = proxy.unwrap(TestConnectionImpl.class); assertFalse(AbstractWrapper.isProxy(unwrapped)); } } }
when(mockedPooledConnection.getConnection()).thenReturn(mockedConnection); connectionInformation = ConnectionInformation.fromTestConnection(mockedConnection); statementInformation = new StatementInformation(connectionInformation); preparedStatementInformation = new PreparedStatementInformation(connectionInformation, "SELECT * FROM DUAL");
@Test public void testProxyOfWrappedConnection() throws SQLException { // this will be the actual connection Connection con = new TestConnectionImpl(); // use a wrapper from DBCP to create a proxy of a proxy // Note: DBCP implements with JDBC 4.0 API so the Wrapper interface // is implemented here. DelegatingConnection underlying = new DelegatingConnection(con); try (Connection proxy = ConnectionWrapper.wrap(con, noOpEventListener, ConnectionInformation.fromTestConnection(underlying))) { // TestConnection is an interface of the actual connection but not of the proxy. Unwrapping works // but a proxy is not returned { TestConnection unwrapped = proxy.unwrap(TestConnection.class); assertFalse(AbstractWrapper.isProxy(unwrapped)); } // ResultSet is not implemented at all - an exception will be thrown try { proxy.unwrap(ResultSet.class); fail("Expected exception not thrown"); } catch (SQLException e) { } } }
@Test public void testCastableFromProxy() throws SQLException { Connection con = new TestConnectionImpl(); @SuppressWarnings("resource") Connection proxy = ConnectionWrapper.wrap(con, noOpEventListener, ConnectionInformation.fromTestConnection(con)); // if the proxy implements the interface then the proxy should be returned { Connection unwrapped = proxy.unwrap(Connection.class); assertTrue(AbstractWrapper.isProxy(unwrapped)); } { Wrapper unwrapped = proxy.unwrap(Wrapper.class); assertTrue(AbstractWrapper.isProxy(unwrapped)); } { AutoCloseable unwrapped = proxy.unwrap(AutoCloseable.class); assertTrue(AbstractWrapper.isProxy(unwrapped)); } // TestConnectionImpl is not implemented by the proxy - proxy will not be returned { TestConnectionImpl unwrapped = proxy.unwrap(TestConnectionImpl.class); assertFalse(AbstractWrapper.isProxy(unwrapped)); } }
ConnectionInformation.fromTestConnection(this.connection) //
@Test public void testExecuteUpdate() throws SQLException { String query = "update customers set name='xyz' where id=1"; final int[] eventListenerRowCount = { 0 }; try (Connection connectionWrapper = // ConnectionWrapper.wrap( // this.connection, new JdbcEventListener() { @Override public void onAfterExecuteUpdate(StatementInformation statementInformation, long timeElapsedNanos, String sql, int rowCount, SQLException e) { eventListenerRowCount[0] = rowCount; } }, // ConnectionInformation.fromTestConnection(this.connection)) ) { int rowCount = P6TestUtil.executeUpdate(connectionWrapper, query); assertEquals(1, rowCount); assertEquals(1, eventListenerRowCount[0]); // validate logging assertTrue(super.getLastLogEntry().contains(query)); // validate that the sql executed against the db assertEquals(1, P6TestUtil.queryForInt(this.connection, "select count(*) from customers where id=1 and name='xyz'")); } }
@Test public void testServiceLoaderFromWrapConnection() throws Exception { final Connection connectionMock = mock(Connection.class); @SuppressWarnings("resource") final Connection connection = ConnectionWrapper.wrap(connectionMock, new DefaultJdbcEventListenerFactory().createJdbcEventListener(), ConnectionInformation.fromTestConnection(connectionMock)); assertTrue(connection instanceof ConnectionWrapper); ConnectionWrapper connectionWrapper = (ConnectionWrapper) connection; final JdbcEventListener eventListener = connectionWrapper.getEventListener(); assertTrue(eventListener instanceof CompoundJdbcEventListener); CompoundJdbcEventListener compoundJdbcEventListener = (CompoundJdbcEventListener) eventListener; final List<JdbcEventListener> eventListeners = compoundJdbcEventListener.getEventListeners(); assertTrue(containsClass(TestJdbcEventListener.class, eventListeners)); assertFalse(containsClass(JdbcEventListener.class, eventListeners)); assertTrue(containsClass(TestLoggingEventListener.class, eventListeners)); assertFalse(containsClass(LoggingEventListener.class, eventListeners)); }