@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(expected = WrongNumberOfQueriesError.class) public void testNeverInsertNegative() throws Exception { Spy spy = Sniffy.spy(); executeStatement(Query.INSERT); spy.verifyNever(Query.INSERT); }
@Test public void testRecordQueriesOtherThreadsNegative() throws Exception { try { Sniffer.execute(() -> { executeStatement(); Thread thread = new Thread(BaseTest::executeStatement); thread.start(); thread.join(); }).verifyNever(Threads.OTHERS); fail(); } catch (WrongNumberOfQueriesError e) { assertNotNull(e); } }
@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 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(expected = WrongNumberOfQueriesError.class) public void testNeverInsertOtherThreadNegative() throws Exception { Spy spy = Sniffy.spy(); executeStatementInOtherThread(Query.INSERT); spy.verifyNever(Query.INSERT, Threads.OTHERS); }
@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 testNeverInsertOtherThreadPositive() throws Exception { Spy spy = Sniffy.spy(); executeStatementInOtherThread(Query.SELECT); executeStatement(Query.INSERT); spy.verifyNever(Threads.OTHERS, Query.INSERT); }
@Test public void testRecordQueriesNegative() throws Exception { try { Sniffer.run(BaseTest::executeStatement).verifyNever(); fail(); } catch (WrongNumberOfQueriesError e) { assertNotNull(e); } }
@Test public void testRecordQueriesThreadLocalNegative() throws Exception { try { Sniffer.run(BaseTest::executeStatement).verifyNever(Threads.CURRENT); fail(); } catch (WrongNumberOfQueriesError e) { assertNotNull(e); } }
@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 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(); }