@Test public void testRecordQueriesThreadLocalPositive() throws Exception { Sniffer.execute(() -> { executeStatement(); Thread thread = new Thread(BaseTest::executeStatement); thread.start(); thread.join(); }).verifyAtMostOnce(Threads.CURRENT); }
@Test public void testRecordQueriesOtherThreadsPositive() throws Exception { Sniffer.execute(() -> { executeStatement(); Thread thread = new Thread(BaseTest::executeStatement); thread.start(); thread.join(); }).verifyAtMostOnce(Threads.OTHERS); }
@Test public void testRecordQueriesPositive() throws Exception { Sniffer.run(BaseTest::executeStatement).verifyAtMostOnce(); }
@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 testAtMostOnceUpdatePositive() throws Exception { Spy spy = Sniffy.spy(); executeStatement(Query.UPDATE); spy.verifyAtMostOnce(Query.UPDATE); spy = Sniffy.spy(); executeStatement(Query.DELETE); executeStatement(Query.UPDATE); spy.verifyAtMostOnce(Query.UPDATE); }
@Test(expected = WrongNumberOfQueriesError.class) public void testAtMostOnceUpdateOtherThreadNegative() throws Exception { Spy spy = Sniffy.spy(); executeStatementsInOtherThread(2, Query.UPDATE); spy.verifyAtMostOnce(Query.UPDATE, Threads.OTHERS); }
@Test public void testExecuteQueryStatement() throws ClassNotFoundException, SQLException { Spy spy = Sniffy.spy(); try (Connection connection = DriverManager.getConnection("sniffy:jdbc:h2:mem:", "sa", "sa"); Statement statement = connection.createStatement()) { statement.executeQuery("SELECT 1 FROM DUAL"); } assertEquals(1, spy.executedStatements()); spy.verifyAtMostOnce().reset().verifyNever(); }
@Test public void testExecuteQueryPreparedStatement() throws ClassNotFoundException, SQLException { Spy spy = Sniffy.spy(); try (Connection connection = DriverManager.getConnection("sniffy:jdbc:h2:mem:", "sa", "sa"); PreparedStatement preparedStatement = connection.prepareStatement("SELECT 1 FROM DUAL")) { preparedStatement.executeQuery(); } assertEquals(1, spy.executedStatements()); spy.verifyAtMostOnce().reset().verifyNever(); }
@Test(expected = WrongNumberOfQueriesError.class) public void testAtMostOnceUpdateNegative() throws Exception { Spy spy = Sniffy.spy(); executeStatements(2, Query.UPDATE); spy.verifyAtMostOnce(Query.UPDATE); }
@Test public void testExecutePreparedStatement() throws ClassNotFoundException, SQLException { Spy spy = Sniffy.spy(); try (Connection connection = DriverManager.getConnection("sniffy: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 testExecuteQueryStatement() throws ClassNotFoundException, SQLException { Spy spy = Sniffy.spy(); try (Connection connection = DriverManager.getConnection("sniffer:jdbc:h2:mem:", "sa", "sa"); Statement statement = connection.createStatement()) { statement.executeQuery("SELECT 1 FROM DUAL"); } assertEquals(1, spy.executedStatements()); spy.verifyAtMostOnce().reset().verifyNever(); }
@Test public void testExecuteQueryPreparedStatement() 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.executeQuery(); } assertEquals(1, spy.executedStatements()); spy.verifyAtMostOnce().reset().verifyNever(); }
@Test public void testExecuteIncorrectStatement() throws ClassNotFoundException, SQLException { Spy spy = Sniffy.spy(); try (Connection connection = DriverManager.getConnection("sniffy:jdbc:h2:mem:", "sa", "sa"); Statement statement = connection.createStatement()) { try { statement.execute("this is an incorrect SQL query"); } catch (SQLException e) { assertNotNull(e); } } assertEquals(1, spy.executedStatements()); spy.verifyAtMostOnce().reset().verifyNever(); }
@Test public void testExecuteIncorrectStatement() throws ClassNotFoundException, SQLException { Spy spy = Sniffy.spy(); try (Connection connection = DriverManager.getConnection("sniffer:jdbc:h2:mem:", "sa", "sa"); Statement statement = connection.createStatement()) { try { statement.execute("this is an incorrect SQL query"); } catch (SQLException e) { assertNotNull(e); } } assertEquals(1, spy.executedStatements()); spy.verifyAtMostOnce().reset().verifyNever(); }
@Test public void testCallStatement() throws ClassNotFoundException, SQLException { try (Connection connection = DriverManager.getConnection("sniffy:jdbc:h2:mem:", "sa", "sa")) { try (Statement statement = connection.createStatement()) { statement.execute("CREATE ALIAS IF NOT EXISTS TIMES_TWO FOR \"io.sniffy.MockDriverTest.timesTwo\""); } Spy spy = Sniffy.spy(); try (CallableStatement callableStatement = connection.prepareCall("CALL TIMES_TWO(?)")) { callableStatement.setInt(1, 1); callableStatement.execute(); } assertEquals(1, spy.executedStatements()); spy. verifyAtMostOnce(). reset(). verifyNever(); } }
@Test public void testCallStatement() throws ClassNotFoundException, SQLException { try (Connection connection = DriverManager.getConnection("sniffer:jdbc:h2:mem:", "sa", "sa")) { try (Statement statement = connection.createStatement()) { statement.execute("CREATE ALIAS IF NOT EXISTS TIMES_TWO FOR \"io.sniffy.MockDriverTest.timesTwo\""); } Spy spy = Sniffy.spy(); try (CallableStatement callableStatement = connection.prepareCall("CALL TIMES_TWO(?)")) { callableStatement.setInt(1, 1); callableStatement.execute(); } assertEquals(1, spy.executedStatements()); spy. verifyAtMostOnce(). reset(). verifyNever(); } }
@Test public void testAtMostOnceUpdateOtherThreadPositive() throws Exception { Spy spy = Sniffy.spy(); executeStatementInOtherThread(Query.SELECT); executeStatementInOtherThread(Query.UPDATE); executeStatements(5, Query.UPDATE); spy.verifyAtMostOnce(Threads.OTHERS, Query.UPDATE); }
@Test public void testExecuteStatement() throws ClassNotFoundException, SQLException { Spy spy = Sniffy.spy(); executeStatement(); assertEquals(1, spy.executedStatements()); spy.verifyAtMostOnce().reset().verifyNever(); }
@Test public void testExecuteStatement() throws ClassNotFoundException, SQLException { Spy spy = Sniffy.spy(); executeStatement(); assertEquals(1, spy.executedStatements()); spy.verifyAtMostOnce().reset().verifyNever(); }