/** * @return a new {@link Spy} instance with an expectation initialized * @see #spy() * @see Spy#expect(int, Threads, Query) * @since 2.2 */ @Deprecated public static Spy expect(int allowedStatements, Query query, Threads threadMatcher) { return spy().expect(allowedStatements, query, threadMatcher); }
/** * @return a new {@link Spy} instance with an expectation initialized * @see #spy() * @see Spy#expect(int, Threads, Query) * @since 2.2 */ @Deprecated public static Spy expect(int allowedStatements, Threads threadMatcher, Query query) { return spy().expect(allowedStatements, threadMatcher, query); }
/** * @return a new {@link Spy} instance with an expectation initialized * @see #spy() * @see Spy#expect(int, Query) * @since 2.2 */ @Deprecated public static Spy expect(int allowedStatements, Query query) { return spy().expect(allowedStatements, query); }
/** * @return a new {@link Spy} instance with an expectation initialized * @see #spy() * @see Spy#expect(int, Threads, Query) * @since 2.2 */ @Deprecated public static Spy expect(int allowedStatements, Query query, Threads threadMatcher) { return spy().expect(allowedStatements, query, threadMatcher); }
/** * @return a new {@link Spy} instance with an expectation initialized * @see #spy() * @see Spy#expect(int) * @since 2.0 */ @Deprecated public static Spy expect(int allowedStatements) { return spy().expect(allowedStatements); }
/** * @return a new {@link Spy} instance with an expectation initialized * @see #spy() * @see Spy#expect(int, Threads) * @since 2.0 */ @Deprecated public static Spy expect(int allowedStatements, Threads threadMatcher) { return spy().expect(allowedStatements, threadMatcher); }
/** * @param expectation a {@link Spy.Expectation} implementation * @return a new {@link Spy} instance with given expectation * @see #spy() * @since 3.1 */ public static Spy expect(Spy.Expectation expectation) { return spy().expect(expectation); }
/** * @return a new {@link Spy} instance with an expectation initialized * @see #spy() * @see Spy#expect(int) * @since 2.0 */ @Deprecated public static Spy expect(int allowedStatements) { return spy().expect(allowedStatements); }
/** * @param expectation a {@link Spy.Expectation} implementation * @return a new {@link Spy} instance with given expectation * @see #spy() * @since 3.1 */ public static Spy expect(Spy.Expectation expectation) { return spy().expect(expectation); }
/** * @return a new {@link Spy} instance with an expectation initialized * @see #spy() * @see Spy#expect(int, Query) * @since 2.2 */ @Deprecated public static Spy expect(int allowedStatements, Query query) { return spy().expect(allowedStatements, query); }
/** * @return a new {@link Spy} instance with an expectation initialized * @see #spy() * @see Spy#expect(int, Threads) * @since 2.0 */ @Deprecated public static Spy expect(int allowedStatements, Threads threadMatcher) { return spy().expect(allowedStatements, threadMatcher); }
/** * @return a new {@link Spy} instance with an expectation initialized * @see #spy() * @see Spy#expect(int, Threads, Query) * @since 2.2 */ @Deprecated public static Spy expect(int allowedStatements, Threads threadMatcher, Query query) { return spy().expect(allowedStatements, threadMatcher, query); }
@Test public void testResourceApi() throws SQLException { // tag::testResourceApi[] final Connection connection = DriverManager.getConnection("sniffy:jdbc:h2:mem:", "sa", "sa"); // <1> try (@SuppressWarnings("unused") Spy s = Sniffy. // <2> expect(SqlQueries.atMostOneQuery()). expect(SqlQueries.noneQueries().otherThreads()); Statement statement = connection.createStatement()) { statement.execute("SELECT 1 FROM DUAL"); } // end::testResourceApi[] }
@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 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()); } }
@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()); } }