@Test(expected = WrongNumberOfQueriesError.class) public void testNeverInsertNegative() throws Exception { try (Spy ignored = Sniffer.expectNever(INSERT)) { executeStatement(INSERT); } }
@Test public void testNeverInsertPositive() throws Exception { try (Spy ignored = Sniffer.expectNever(INSERT)) { executeStatement(SELECT); } }
@Test(expected = WrongNumberOfQueriesError.class) public void testNeverInsertOtherThreadNegative() throws Exception { try (Spy ignored = Sniffer.expectNever(INSERT, Threads.OTHERS)) { executeStatementInOtherThread(INSERT); } }
@Test public void testNeverInsertOtherThreadPositive() throws Exception { try (Spy ignored = Sniffer.expectNever(Threads.OTHERS, INSERT)) { executeStatementInOtherThread(SELECT); executeStatement(INSERT); } }
@Test public void testTryWithResourceApi_Never() throws Exception { try { try (Spy ignored = Sniffer.expectNever()) { executeStatement(); throw new RuntimeException("This is a test exception"); } } catch (RuntimeException e) { assertEquals("This is a test exception", e.getMessage()); assertNotNull(e.getSuppressed()); assertEquals(1, e.getSuppressed().length); assertTrue(WrongNumberOfQueriesError.class.isAssignableFrom(e.getSuppressed()[0].getClass())); } }
@Test public void testTryWithResourceApi_NeverOtherThread() throws Exception { try { try (Spy ignored = Sniffer.expectNever(Threads.OTHERS)) { executeStatementInOtherThread(); throw new RuntimeException("This is a test exception"); } } catch (Exception e) { assertEquals("This is a test exception", e.getMessage()); assertNotNull(e.getSuppressed()); assertEquals(1, e.getSuppressed().length); assertTrue(WrongNumberOfQueriesError.class.isAssignableFrom(e.getSuppressed()[0].getClass())); } }
try (Connection connection = DriverManager.getConnection("sniffer:jdbc:h2:mem:", "sa", "sa")) { connection.createStatement().execute("CREATE TEMPORARY TABLE TEMPORARY_TABLE (BAZ VARCHAR(255))"); try (@SuppressWarnings("unused") Spy spy = Sniffer.expectNever(); PreparedStatement preparedStatement = connection.prepareStatement( "INSERT INTO TEMPORARY_TABLE (BAZ) VALUES (?)")) {
@Test public void testStatementBatchIsLogged() throws Exception { try (Connection connection = DriverManager.getConnection("sniffer:jdbc:h2:mem:", "sa", "sa")) { connection.createStatement().execute("CREATE TEMPORARY TABLE TEMPORARY_TABLE (BAZ VARCHAR(255))"); try (@SuppressWarnings("unused") Spy spy = Sniffer.expectNever(); Statement statement = connection.createStatement()) { statement.addBatch("INSERT INTO TEMPORARY_TABLE (BAZ) VALUES ('foo')"); statement.addBatch("INSERT INTO TEMPORARY_TABLE (BAZ) VALUES (LOWER('bar'))"); statement.executeBatch(); } } catch (WrongNumberOfQueriesError e) { assertNotNull(e); assertEquals(0, e.getMinimumQueries()); assertEquals(0, e.getMaximumQueries()); assertEquals(1, e.getNumQueries()); assertEquals(1, e.getExecutedStatements().size()); assertEquals(1, e.getExecutedSqls().size()); assertEquals(Threads.CURRENT, e.getThreadMatcher()); assertTrue(e.getMessage().contains("INSERT INTO TEMPORARY_TABLE (BAZ) VALUES ('foo')")); assertTrue(e.getMessage().contains("INSERT INTO TEMPORARY_TABLE (BAZ) VALUES (LOWER('bar'))")); } }