/** * @return a new {@link Spy} instance * @since 2.0 */ @Deprecated public static <T extends Spy<T>> Spy<? extends Spy<T>> spy() { return Sniffy.<T>spy(); }
private static void load() { try { DriverManager.registerDriver(INSTANCE); } catch (SQLException e) { e.printStackTrace(); } Sniffy.initialize(); }
/** * Execute the {@link Sniffer.Executable#execute()} method, record the SQL queries * and return the {@link Spy} object with stats * @param executable code to test * @return statistics on executed queries * @throws RuntimeException if underlying code under test throws an Exception * @since 2.0 */ @Deprecated public static Spy execute(Executable executable) { return Sniffy.execute(executable); }
protected Object invokeTarget(Method method, Object[] args) throws Throwable { long start = System.currentTimeMillis(); try { Sniffy.enterJdbcMethod(); return invokeTargetImpl(method, args); } finally { Sniffy.exitJdbcMethod(method, System.currentTimeMillis() - start); } }
protected Object invokeTargetAndRecord(Method method, Object[] args) throws Throwable { if (Sniffy.hasSpies()) { long start = System.currentTimeMillis(); try { Sniffy.enterJdbcMethod(); Object result = invokeTargetImpl(method, args); if (Boolean.TRUE.equals(result)) { Sniffy.readDatabaseRow(method, System.currentTimeMillis() - start, statementMetaData); } return result; } finally { Sniffy.exitJdbcMethod(method, System.currentTimeMillis() - start); } } else { return invokeTargetImpl(method, args); } } }
int rowsUpdated = 0; try { Sniffy.enterJdbcMethod(); checkConnectionAllowed(true); Object result = invokeTargetImpl(method, args); } finally { long elapsedTime = System.currentTimeMillis() - start; Sniffy.logSqlTime(sql, elapsedTime); if (Sniffy.hasSpies()) { String stackTrace = printStackTrace(getTraceForProxiedMethod(method)); lastStatementMetaData = Sniffy.executeStatement(sql, elapsedTime, stackTrace, rowsUpdated); } else { Sniffer.executedStatementsGlobalCounter.incrementAndGet();
@Test public void testUninstall() throws Exception { Sniffy.initialize(); SnifferSocketImplFactory.uninstall(); try (Spy<?> s = Sniffy.spy()) { performSocketOperation(); assertTrue(s.getSocketOperations(CURRENT, null, true).isEmpty()); } SnifferSocketImplFactory.install(); }
@Test public void testGetXAConnectionWithCredentialsWithSocketOperation() throws Exception { JdbcDataSource h2DataSource = new JdbcDataSource(); h2DataSource.setURL("jdbc:h2:mem:"); JdbcDataSource targetDataSource = Mockito.spy(h2DataSource); when(targetDataSource.getXAConnection(anyString(), anyString())).then(invocation -> { Sniffy.logSocket(1, new InetSocketAddress(InetAddress.getLoopbackAddress(), 9876), 2, 3, 4); return invocation.callRealMethod(); }); SniffyDataSource sniffyDataSource = new SniffyDataSource(targetDataSource); try (Spy spy = Sniffy.expect(SqlQueries.exactQueries(1)).expect(TcpConnections.none()); Connection connection = sniffyDataSource.getXAConnection("sa","sa").getConnection()) { assertNotNull(connection); assertTrue(Proxy.isProxyClass(connection.getClass())); try (Statement statement = connection.createStatement()) { statement.execute("SELECT 1 FROM DUAL"); statement.getResultSet().next(); } assertTrue(spy.getSocketOperations(Threads.CURRENT, null, false).isEmpty()); } }
@Test public void hasNotSpiesFromOtherThreads() throws Exception { try (@SuppressWarnings("unused") CurrentThreadSpy spy = Sniffy.spyCurrentThread()) { AtomicBoolean hasSpies = new AtomicBoolean(); Thread thread = new Thread(() -> hasSpies.set(Sniffy.hasSpies())); thread.start(); thread.join(); assertFalse(hasSpies.get()); } }
public static void exitJdbcMethod(Method method, long elapsedTime, Method implMethod) { if (Sniffy.hasSpies()) { Thread.currentThread().getId() ); notifyListeners( statementMetaData, elapsedTime,
@Test @Features("issues/292") public void testLruGlobalSqlStats() throws Exception { Sniffy.getGlobalSqlStats().clear(); for (int i = 0; i < Sniffy.TOP_SQL_CAPACITY; i++) { executeSelectStatements(i, 2 + i % 100); } assertEquals(Sniffy.TOP_SQL_CAPACITY, Sniffy.getGlobalSqlStats().size()); assertNotNull(Sniffy.getGlobalSqlStats().get(String.format("SELECT %d FROM DUAL", 0))); executeSelectStatements(Sniffy.TOP_SQL_CAPACITY + 1000, 1); assertEquals(Sniffy.TOP_SQL_CAPACITY, Sniffy.getGlobalSqlStats().size()); assertTrue(Sniffy.getGlobalSqlStats().containsKey(String.format("SELECT %d FROM DUAL", Sniffy.TOP_SQL_CAPACITY + 1000))); assertTrue(Sniffy.getGlobalSqlStats().containsKey(String.format("SELECT %d FROM DUAL", 0))); assertFalse(Sniffy.getGlobalSqlStats().containsKey(String.format("SELECT %d FROM DUAL", 1))); }
this.httpServletResponse = httpServletResponse; spy = Sniffy.spyCurrentThread();
@Test public void testGetMockConnectionMakesTcp() throws ClassNotFoundException, SQLException { TestDriver testDriver = TestDriver.getSpy(); final InetSocketAddress loopbackAddress = new InetSocketAddress(getLoopbackAddress(), 4242); try { doAnswer(invocation -> { Sniffy.logSocket(1, loopbackAddress, 100, 2, 3); return invocation.callRealMethod(); }).when(testDriver).connect(any(), any()); try (Connection connection = DriverManager.getConnection("sniffy:jdbc:h2spy:mem:", "sa", "sa")) { assertNotNull(connection); assertTrue(Proxy.isProxyClass(connection.getClass())); } } finally { Mockito.reset(testDriver); } try (Connection connection = DriverManager.getConnection("sniffy:jdbc:h2spy:mem:", "sa", "sa")) { assertNotNull(connection); assertTrue(Proxy.isProxyClass(connection.getClass())); } }
/** * Execute the {@link Callable#call()} method, record the SQL queries * and return the {@link Spy.SpyWithValue} object with stats * @param callable code to test * @param <V> type of return value * @return statistics on executed queries * @throws Exception if underlying code under test throws an Exception * @since 2.0 */ @SuppressWarnings("unchecked") @Deprecated public static <V> Spy.SpyWithValue<V> call(Callable<V> callable) throws Exception { return Sniffy.call(callable); }
protected Object invokeTarget(Method method, Object[] args) throws Throwable { long start = System.currentTimeMillis(); try { Sniffy.enterJdbcMethod(); return invokeTargetImpl(method, args); } finally { Sniffy.exitJdbcMethod(method, System.currentTimeMillis() - start); } }
protected Object invokeTargetAndRecord(Method method, Object[] args) throws Throwable { if (Sniffy.hasSpies()) { long start = System.currentTimeMillis(); try { Sniffy.enterJdbcMethod(); Object result = invokeTargetImpl(method, args); if (Boolean.TRUE.equals(result)) { Sniffy.readDatabaseRow(method, System.currentTimeMillis() - start, statementMetaData); } return result; } finally { Sniffy.exitJdbcMethod(method, System.currentTimeMillis() - start); } } else { return invokeTargetImpl(method, args); } } }
int rowsUpdated = 0; try { Sniffy.enterJdbcMethod(); checkConnectionAllowed(true); Object result = invokeTargetImpl(method, args); } finally { long elapsedTime = System.currentTimeMillis() - start; Sniffy.logSqlTime(sql, elapsedTime); if (Sniffy.hasSpies()) { String stackTrace = printStackTrace(getTraceForProxiedMethod(method)); lastStatementMetaData = Sniffy.executeStatement(sql, elapsedTime, stackTrace, rowsUpdated); } else { Sniffer.executedStatementsGlobalCounter.incrementAndGet();
@Test public void testGetConnectionWithCredentialsWithSocketOperation() throws Exception { JdbcDataSource h2DataSource = new JdbcDataSource(); h2DataSource.setURL("jdbc:h2:mem:"); JdbcDataSource targetDataSource = Mockito.spy(h2DataSource); when(targetDataSource.getConnection(anyString(), anyString())).then(invocation -> { Sniffy.logSocket(1, new InetSocketAddress(InetAddress.getLoopbackAddress(), 9876), 2, 3, 4); return invocation.callRealMethod(); }); SniffyDataSource sniffyDataSource = new SniffyDataSource(targetDataSource); try (Spy spy = Sniffy.expect(SqlQueries.exactQueries(1)).expect(TcpConnections.none()); Connection connection = sniffyDataSource.getConnection("sa","sa")) { assertNotNull(connection); assertTrue(Proxy.isProxyClass(connection.getClass())); try (Statement statement = connection.createStatement()) { statement.execute("SELECT 1 FROM DUAL"); statement.getResultSet().next(); } assertTrue(spy.getSocketOperations(Threads.CURRENT, null, false).isEmpty()); } }