/** * @return a new {@link Spy} instance * @since 2.0 */ @Deprecated public static <T extends Spy<T>> Spy<? extends Spy<T>> spy() { return Sniffy.<T>spy(); }
/** * Execute the {@link Runnable#run()} method, record the SQL queries * and return the {@link Spy} object with stats * @param runnable code to test * @return statistics on executed queries * @since 3.1 */ public static Spy run(Runnable runnable) { return spy().run(runnable); }
/** * @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); }
@Test public void hasSpiesFromOtherThreads() throws Exception { try (@SuppressWarnings("unused") Spy spy = Sniffy.spy()) { AtomicBoolean hasSpies = new AtomicBoolean(); Thread thread = new Thread(() -> hasSpies.set(Sniffy.hasSpies())); thread.start(); thread.join(); assertTrue(hasSpies.get()); } }
@Test public void testExecuteStatementThrowsException() throws ClassNotFoundException, SQLException { Spy spy = Sniffy.spy(); try (Connection connection = DriverManager.getConnection("sniffy:jdbc:h2:mem:", "sa", "sa"); Statement statement = connection.createStatement()) { statement.execute("SELECT 1 FROM DUAL_HUAL"); } catch (Exception e) { assertFalse(InvocationTargetException.class.isAssignableFrom(e.getClass())); assertTrue(SQLException.class.isAssignableFrom(e.getClass())); } assertEquals(1, spy.executedStatements()); }
@Test public void testExecuteStatementThrowsException() throws ClassNotFoundException, SQLException { Spy spy = Sniffy.spy(); try (Connection connection = DriverManager.getConnection("sniffer:jdbc:h2:mem:", "sa", "sa"); Statement statement = connection.createStatement()) { statement.execute("SELECT 1 FROM DUAL_HUAL"); } catch (Exception e) { assertFalse(InvocationTargetException.class.isAssignableFrom(e.getClass())); assertTrue(SQLException.class.isAssignableFrom(e.getClass())); } assertEquals(1, spy.executedStatements()); }
@Test public void testSpyRemovedOnClose() throws Exception { Spy spy = Sniffy.spy(); spy.close(); Sniffy.registeredSpies.stream(). filter(spyReference -> spyReference.get() == spy). forEach(spyReference -> fail("Spy was not removed from Sniffer observers")); }
@Test public void testSpyExecutedStatements_StackTraceTracked() throws Exception { Spy<?> spy = Sniffy.spy(); executeStatement(); Collection<StatementMetaData> statements = spy.getExecutedStatements(Threads.ANY, false).keySet(); Iterator<StatementMetaData> statementsIt = statements.iterator(); StatementMetaData statementMetaData = statementsIt.next(); assertNotNull(statementMetaData.stackTrace); assertTrue(statementMetaData.stackTrace.contains("testSpyExecutedStatements_StackTraceTracked")); assertFalse(statementsIt.hasNext()); }
@Test public void testExecutePreparedStatement() throws ClassNotFoundException, SQLException { Spy spy = Sniffy.spy(); try (Connection connection = DriverManager.getConnection("sniffer:jdbc:h2:mem:", "sa", "sa"); PreparedStatement preparedStatement = connection.prepareStatement("SELECT 1 FROM DUAL")) { preparedStatement.execute(); } assertEquals(1, spy.executedStatements()); spy.verifyAtMostOnce().reset().verifyNever(); }
@Test public void testNeverInsertPositive() throws Exception { Spy spy = Sniffy.spy(); executeStatement(Query.SELECT); spy.verifyNever(Query.INSERT); }
@Test public void testBetweenOtherOtherThreadPositive() throws Exception { Spy spy = Sniffy.spy(); executeStatementsInOtherThread(2, Query.OTHER); spy.verifyBetween(2, 4, Threads.OTHERS, Query.OTHER); }
@Test(expected = WrongNumberOfQueriesError.class) public void testAtMostOnceUpdateOtherThreadNegative() throws Exception { Spy spy = Sniffy.spy(); executeStatementsInOtherThread(2, Query.UPDATE); spy.verifyAtMostOnce(Query.UPDATE, Threads.OTHERS); }
@Test(expected = WrongNumberOfQueriesError.class) public void testAtMostMergeOtherThreadNegative() throws Exception { Spy spy = Sniffy.spy(); executeStatementsInOtherThread(3, Query.MERGE); spy.verifyAtMost(2, Query.MERGE, Threads.OTHERS); }
@Test(expected = WrongNumberOfQueriesError.class) public void testNeverInsertNegative() throws Exception { Spy spy = Sniffy.spy(); executeStatement(Query.INSERT); spy.verifyNever(Query.INSERT); }
@Test public void testBetweenOtherPositive() throws Exception { Spy spy = Sniffy.spy(); executeStatements(3, Query.OTHER); spy.verifyBetween(2, 4, Query.OTHER); }
@Test public void testSpyExecutedStatements() throws Exception { Spy spy = Sniffy.spy(); executeStatement(); assertEquals(1, spy.executedStatements(Threads.ANY)); }
@Test public void testSpyExecutedStatementsCurrentThread() throws Exception { Spy spy = Sniffy.spy(); executeStatement(); assertEquals(1, spy.executedStatements()); }
@Test public void testNeverInsertOtherThreadPositive() throws Exception { Spy spy = Sniffy.spy(); executeStatementInOtherThread(Query.SELECT); executeStatement(Query.INSERT); spy.verifyNever(Threads.OTHERS, Query.INSERT); }
@Test public void testAtMostMergeOtherThreadPositive() throws Exception { Spy spy = Sniffy.spy(); executeStatementInOtherThread(Query.SELECT); executeStatementInOtherThread(Query.MERGE); executeStatements(5, Query.MERGE); spy.verifyAtMost(2, Threads.OTHERS, Query.MERGE); }