@Test public void testStatementUsingRelativeForward() throws Exception { // given // when container.executeAppUnderTest(ExecuteStatementAndUseRelativeForward.class); // then Trace trace = container.getTraceService().getLastTrace(); assertThat(trace.getSpans()).hasSize(2); Span jdbcSpan = trace.getSpans().get(1); assertThat(jdbcSpan.getMessage().getText()) .startsWith("jdbc execution: select * from employee => 3 rows [connection: "); }
// this test validates that lastJdbcMessageSupplier is cleared so that its numRows won't be // updated if the plugin is re-enabled in the middle of iterating over a different result set // (see related comments in StatementAspect) @Test public void testDisableReEnableMidIterating() throws Exception { // given // when container.executeAppUnderTest(ExecuteStatementDisableReEnableMidIterating.class); // then Trace trace = container.getTraceService().getLastTrace(); assertThat(trace.getSpans()).hasSize(2); assertThat(trace.getSpans().get(1).getMessage().getText()) .startsWith( "jdbc execution: select * from employee where name like ? ['nomatch%'] => 0 rows" + " [connection: "); }
@Test public void testCallableStatement() throws Exception { // given container.getConfigService().setPluginProperty(PLUGIN_ID, "captureBindParameters", true); // when container.executeAppUnderTest(ExecuteCallableStatement.class); // then Trace trace = container.getTraceService().getLastTrace(); assertThat(trace.getSpans()).hasSize(2); Span jdbcSpan = trace.getSpans().get(1); assertThat(jdbcSpan.getMessage().getText()) .startsWith( "jdbc execution: insert into employee values (?, ?) ['jane', NULL] [connection: "); }
@Test public void testPreparedStatementWithoutBindParameters() throws Exception { // given container.getConfigService().setPluginProperty(PLUGIN_ID, "captureBindParameters", false); // when container.executeAppUnderTest(ExecutePreparedStatementAndIterateOverResults.class); // then Trace trace = container.getTraceService().getLastTrace(); assertThat(trace.getSpans()).hasSize(2); Span jdbcSpan = trace.getSpans().get(1); assertThat(jdbcSpan.getMessage().getText()) .startsWith( "jdbc execution: select * from employee where name like ? => 1 row [connection: "); }
@Test public void testPreparedStatement() throws Exception { // given // when container.executeAppUnderTest(ExecutePreparedStatementAndIterateOverResults.class); // then Trace trace = container.getTraceService().getLastTrace(); assertThat(trace.getSpans()).hasSize(2); Span jdbcSpan = trace.getSpans().get(1); assertThat(jdbcSpan.getMessage().getText()) .startsWith( "jdbc execution: select * from employee" + " where name like ? ['john%'] => 1 row [connection: "); }
@Test public void testMetadataMetricEnabledSpan() throws Exception { // given container.getConfigService().setPluginProperty(PLUGIN_ID, "captureDatabaseMetaDataSpans", true); // when container.executeAppUnderTest(AccessMetaData.class); // then Trace trace = container.getTraceService().getLastTrace(); assertThat(trace.getSpans()).hasSize(2); assertThat(trace.getSpans().get(1).getMessage().getText()) .startsWith("jdbc metadata:" + " DatabaseMetaData.getTables() [connection: "); assertThat(trace.getMetrics().size()).isEqualTo(2); assertThat(trace.getMetrics().get(0).getName()).isEqualTo("mock trace marker"); assertThat(trace.getMetrics().get(1).getName()).isEqualTo("jdbc metadata"); }
@Test public void testResultSetValueMetric() throws Exception { // given container.getConfigService().setPluginProperty(PLUGIN_ID, "captureResultSetGet", true); // when container.executeAppUnderTest(ExecuteStatementAndIterateOverResults.class); // then Trace trace = container.getTraceService().getLastTrace(); boolean found = false; for (Metric metric : trace.getMetrics()) { if (metric.getName().equals("jdbc resultset value")) { found = true; break; } } assertThat(found).isTrue(); }
@Test public void testBatchPreparedStatement() throws Exception { // given container.getConfigService().setPluginProperty(PLUGIN_ID, "captureBindParameters", true); // when container.executeAppUnderTest(ExecuteBatchPreparedStatement.class); // then Trace trace = container.getTraceService().getLastTrace(); assertThat(trace.getSpans()).hasSize(3); assertThat(trace.getSpans().get(1).getMessage().getText()) .startsWith( "jdbc execution: 2 x" + " insert into employee (name) values (?) ['huckle'] ['sally'] [connection: "); assertThat(trace.getSpans().get(2).getMessage().getText()) .startsWith( "jdbc execution: 2 x" + " insert into employee (name) values (?) ['lowly'] ['pig will'] [connection: "); }
@Test public void testRollback() throws Exception { // given // when container.executeAppUnderTest(ExecuteJdbcRollback.class); // then Trace trace = container.getTraceService().getLastTrace(); assertThat(trace.getSpans()).hasSize(3); Span jdbcInsertSpan = trace.getSpans().get(1); assertThat(jdbcInsertSpan.getMessage().getText()) .startsWith( "jdbc execution: insert into employee (name) values ('john doe') [connection: "); Span jdbcCommitSpan = trace.getSpans().get(2); assertThat(jdbcCommitSpan.getMessage().getText()).startsWith("jdbc rollback [connection: "); assertThat(trace.getMetrics()).hasSize(4); // ordering is by total desc, so not fixed (though root span will be first since it // encompasses all other timings) assertThat(trace.getMetrics().get(0).getName()).isEqualTo("mock trace marker"); assertThat(trace.getMetricNames()) .containsOnly("mock trace marker", "jdbc execute", "jdbc rollback", "jdbc statement close"); }
@After public void afterEachTest() throws Exception { container.checkAndReset(); }
@AfterClass public static void tearDown() throws Exception { container.close(); }