NonRegisteringDriver implements Driver { @Override public Connection connect(@Nullable String url, @Nullable Properties info) throws SQLException { return connect(url, info, TraceOptions.of(info)); } NonRegisteringDriver(); @VisibleForTesting  NonRegisteringDriver(TraceService traceService); @Override boolean acceptsURL(@Nullable String url); @Override Connection connect(@Nullable String url, @Nullable Properties info); @Override int getMajorVersion(); @Override int getMinorVersion(); @Override DriverPropertyInfo[] getPropertyInfo(@Nullable String url, @Nullable Properties info); @Override Logger getParentLogger(); @Override boolean jdbcCompliant();  }
@Test public void connect() throws Exception { when(mockRealDriver.connect(any(String.class), any(Properties.class))) .thenReturn(mockRealConnection); Properties properties = new Properties(); Connection conn = driver.connect("jdbc:stackdriver:mockdriver?user=root&password=123456", properties); conn.close(); assertThat(conn).isNotNull(); InOrder inOrder = inOrder(mockTraceService, mockTraceSpan, mockRealDriver, mockRealConnection); inOrder.verify(mockTraceService).open("JDBC.Driver#connect"); inOrder .verify(mockTraceSpan) .annotate(Label.DATABASE_URL, Optional.of("jdbc:mockdriver?<...>")); inOrder.verify(mockRealDriver).connect("jdbc:mockdriver?user=root&password=123456", properties); inOrder.verify(mockTraceSpan).close(); inOrder.verify(mockRealConnection).close(); verifyNoMoreInteractions(mockTraceService, mockTraceSpan, mockRealDriver, mockRealConnection); }
@Test public void connect_disabled() throws Exception { when(mockRealDriver.connect(any(String.class), any(Properties.class))) .thenReturn(mockRealConnection); Properties properties = new Properties() { { put("stackdriver.trace.enabled", "false"); } }; Connection conn = driver.connect("jdbc:stackdriver:mockdriver?user=root&password=123456", properties); conn.close(); assertThat(conn).isNotNull(); InOrder inOrder = inOrder(mockTraceService, mockRealDriver, mockRealConnection); inOrder.verify(mockRealDriver).connect("jdbc:mockdriver?user=root&password=123456", properties); inOrder.verify(mockRealConnection).close(); verifyNoMoreInteractions( mockTraceService, mockRealDriver, mockRealConnection); }
@Test public void connect_url_scrubbed() throws Exception { when(mockRealDriver.connect(any(String.class), any(Properties.class))) .thenReturn(mockRealConnection); Properties properties = new Properties(); TraceOptions traceOptions = TraceOptions.builder() .setEnabled(true) .setSqlScrubber(Scrubbers.KEEP) .setUrlScrubber( new Scrubbers.Scrubber() { @Override public Optional<String> apply(String input) { return Optional.of("#scrubbed URL#"); } }) .build(); Connection conn = driver.connect( "jdbc:stackdriver:mockdriver?user=root&password=123456", properties, traceOptions); conn.close(); assertThat(conn).isNotNull(); InOrder inOrder = inOrder(mockTraceService, mockTraceSpan, mockRealDriver, mockRealConnection); inOrder.verify(mockTraceService).open("JDBC.Driver#connect"); inOrder.verify(mockTraceSpan).annotate(Label.DATABASE_URL, Optional.of("#scrubbed URL#")); inOrder.verify(mockRealDriver).connect("jdbc:mockdriver?user=root&password=123456", properties); inOrder.verify(mockTraceSpan).close(); inOrder.verify(mockRealConnection).close(); verifyNoMoreInteractions(mockTraceService, mockTraceSpan, mockRealDriver, mockRealConnection); }
@Test public void connect_fails() throws Exception { when(mockRealDriver.connect(eq("jdbc:mockdriver"), any(Properties.class))) .thenThrow(new SQLException()); Properties info = new Properties(); try { driver.connect("jdbc:stackdriver:mockdriver", info); fail("expected SQLException"); } catch (SQLException expected) { } InOrder inOrder = inOrder(mockTraceService, mockTraceSpan, mockRealDriver); inOrder.verify(mockTraceService).open("JDBC.Driver#connect"); inOrder.verify(mockTraceSpan).annotate(Label.DATABASE_URL, Optional.of("jdbc:mockdriver")); inOrder.verify(mockRealDriver).connect("jdbc:mockdriver", info); inOrder.verify(mockTraceSpan).close(); }