protected void logSocket(long millis, int bytesDown, int bytesUp) { if (Sniffy.hasSpies() && null != address && (millis > 0 || bytesDown > 0 || bytesUp > 0)) { Sniffy.logSocket(id, address, millis, bytesDown, bytesUp); } }
protected void logSocket(long millis, int bytesDown, int bytesUp) { if (Sniffy.hasSpies() && null != address && (millis > 0 || bytesDown > 0 || bytesUp > 0)) { Sniffy.logSocket(id, address, millis, bytesDown, bytesUp); } }
@Test public void testGetMockConnectionMakesTcp() throws ClassNotFoundException, SQLException { TestDriver testDriver = TestDriver.getSpy(); final InetSocketAddress loopbackAddress = new InetSocketAddress(getLoopbackAddress(), 4242); try { doAnswer(invocation -> { Sniffy.logSocket(1, loopbackAddress, 100, 2, 3); return invocation.callRealMethod(); }).when(testDriver).connect(any(), any()); try (Connection connection = DriverManager.getConnection("sniffy:jdbc:h2spy:mem:", "sa", "sa")) { assertNotNull(connection); assertTrue(Proxy.isProxyClass(connection.getClass())); } } finally { Mockito.reset(testDriver); } try (Connection connection = DriverManager.getConnection("sniffy:jdbc:h2spy:mem:", "sa", "sa")) { assertNotNull(connection); assertTrue(Proxy.isProxyClass(connection.getClass())); } }
@Test public void testGetConnectionWithCredentialsWithSocketOperation() throws Exception { JdbcDataSource h2DataSource = new JdbcDataSource(); h2DataSource.setURL("jdbc:h2:mem:"); JdbcDataSource targetDataSource = Mockito.spy(h2DataSource); when(targetDataSource.getConnection(anyString(), anyString())).then(invocation -> { Sniffy.logSocket(1, new InetSocketAddress(InetAddress.getLoopbackAddress(), 9876), 2, 3, 4); return invocation.callRealMethod(); }); SniffyDataSource sniffyDataSource = new SniffyDataSource(targetDataSource); try (Spy spy = Sniffy.expect(SqlQueries.exactQueries(1)).expect(TcpConnections.none()); Connection connection = sniffyDataSource.getConnection("sa","sa")) { assertNotNull(connection); assertTrue(Proxy.isProxyClass(connection.getClass())); try (Statement statement = connection.createStatement()) { statement.execute("SELECT 1 FROM DUAL"); statement.getResultSet().next(); } assertTrue(spy.getSocketOperations(Threads.CURRENT, null, false).isEmpty()); } }
@Test public void testGetXAConnectionWithCredentialsWithSocketOperation() throws Exception { JdbcDataSource h2DataSource = new JdbcDataSource(); h2DataSource.setURL("jdbc:h2:mem:"); JdbcDataSource targetDataSource = Mockito.spy(h2DataSource); when(targetDataSource.getXAConnection(anyString(), anyString())).then(invocation -> { Sniffy.logSocket(1, new InetSocketAddress(InetAddress.getLoopbackAddress(), 9876), 2, 3, 4); return invocation.callRealMethod(); }); SniffyDataSource sniffyDataSource = new SniffyDataSource(targetDataSource); try (Spy spy = Sniffy.expect(SqlQueries.exactQueries(1)).expect(TcpConnections.none()); Connection connection = sniffyDataSource.getXAConnection("sa","sa").getConnection()) { assertNotNull(connection); assertTrue(Proxy.isProxyClass(connection.getClass())); try (Statement statement = connection.createStatement()) { statement.execute("SELECT 1 FROM DUAL"); statement.getResultSet().next(); } assertTrue(spy.getSocketOperations(Threads.CURRENT, null, false).isEmpty()); } }
@Test public void testGetPooledConnectionWithCredentialsWithSocketOperation() throws Exception { JdbcDataSource h2DataSource = new JdbcDataSource(); h2DataSource.setURL("jdbc:h2:mem:"); JdbcDataSource targetDataSource = Mockito.spy(h2DataSource); when(targetDataSource.getXAConnection(anyString(), anyString())).then(invocation -> { Sniffy.logSocket(1, new InetSocketAddress(InetAddress.getLoopbackAddress(), 9876), 2, 3, 4); return invocation.callRealMethod(); }); SniffyDataSource sniffyDataSource = new SniffyDataSource(targetDataSource); try (Spy spy = Sniffy.expect(SqlQueries.exactQueries(1)).expect(TcpConnections.none()); Connection connection = sniffyDataSource.getPooledConnection("sa","sa").getConnection()) { assertNotNull(connection); assertTrue(Proxy.isProxyClass(connection.getClass())); try (Statement statement = connection.createStatement()) { statement.execute("SELECT 1 FROM DUAL"); statement.getResultSet().next(); } assertTrue(spy.getSocketOperations(Threads.CURRENT, null, false).isEmpty()); } }
@Test public void testGetConnectionWithSocketOperation() throws Exception { JdbcDataSource h2DataSource = new JdbcDataSource(); h2DataSource.setURL("jdbc:h2:mem:"); JdbcDataSource targetDataSource = Mockito.spy(h2DataSource); when(targetDataSource.getConnection()).then(invocation -> { Sniffy.logSocket(1, new InetSocketAddress(InetAddress.getLoopbackAddress(), 9876), 2, 3, 4); return invocation.callRealMethod(); }); SniffyDataSource sniffyDataSource = new SniffyDataSource(targetDataSource); try (Spy spy = Sniffy.expect(SqlQueries.exactQueries(1)).expect(TcpConnections.none()); Connection connection = sniffyDataSource.getConnection()) { assertNotNull(connection); assertTrue(Proxy.isProxyClass(connection.getClass())); try (Statement statement = connection.createStatement()) { statement.execute("SELECT 1 FROM DUAL"); statement.getResultSet().next(); } assertTrue(spy.getSocketOperations(Threads.CURRENT, null, false).isEmpty()); } }
@Test public void testGetPooledConnectionWithSocketOperation() throws Exception { JdbcDataSource h2DataSource = new JdbcDataSource(); h2DataSource.setURL("jdbc:h2:mem:"); JdbcDataSource targetDataSource = Mockito.spy(h2DataSource); when(targetDataSource.getXAConnection()).then(invocation -> { Sniffy.logSocket(1, new InetSocketAddress(InetAddress.getLoopbackAddress(), 9876), 2, 3, 4); return invocation.callRealMethod(); }); SniffyDataSource sniffyDataSource = new SniffyDataSource(targetDataSource); try (Spy spy = Sniffy.expect(SqlQueries.exactQueries(1)).expect(TcpConnections.none()); Connection connection = sniffyDataSource.getPooledConnection().getConnection()) { assertNotNull(connection); assertTrue(Proxy.isProxyClass(connection.getClass())); try (Statement statement = connection.createStatement()) { statement.execute("SELECT 1 FROM DUAL"); statement.getResultSet().next(); } assertTrue(spy.getSocketOperations(Threads.CURRENT, null, false).isEmpty()); } }
@Test public void testGetXAConnectionWithSocketOperation() throws Exception { JdbcDataSource h2DataSource = new JdbcDataSource(); h2DataSource.setURL("jdbc:h2:mem:"); JdbcDataSource targetDataSource = Mockito.spy(h2DataSource); when(targetDataSource.getXAConnection()).then(invocation -> { Sniffy.logSocket(1, new InetSocketAddress(InetAddress.getLoopbackAddress(), 9876), 2, 3, 4); return invocation.callRealMethod(); }); SniffyDataSource sniffyDataSource = new SniffyDataSource(targetDataSource); try (Spy spy = Sniffy.expect(SqlQueries.exactQueries(1)).expect(TcpConnections.none()); Connection connection = sniffyDataSource.getXAConnection().getConnection()) { assertNotNull(connection); assertTrue(Proxy.isProxyClass(connection.getClass())); try (Statement statement = connection.createStatement()) { statement.execute("SELECT 1 FROM DUAL"); statement.getResultSet().next(); } assertTrue(spy.getSocketOperations(Threads.CURRENT, null, false).isEmpty()); } }