@Test public void testInstall() throws Exception { SnifferSocketImplFactory.uninstall(); SnifferSocketImplFactory.install(); try (Spy<?> s = Sniffy.spy()) { performSocketOperation(); Thread thread = new Thread(this::performSocketOperation); thread.start(); thread.join(); assertEquals(1, (long) s.getSocketOperations(CURRENT, null, true).entrySet().size()); s.getSocketOperations(CURRENT, null, true).values().stream().findAny().ifPresent((socketStats) -> { assertEquals(REQUEST.length, socketStats.bytesUp.intValue()); assertEquals(RESPONSE.length, socketStats.bytesDown.intValue()); }); assertEquals(1, s.getSocketOperations(OTHERS, null, true).entrySet().stream().count()); s.getSocketOperations(OTHERS, null, true).values().stream().findAny().ifPresent((socketStats) -> { assertEquals(REQUEST.length, socketStats.bytesUp.intValue()); assertEquals(RESPONSE.length, socketStats.bytesDown.intValue()); }); assertEquals(2, s.getSocketOperations(ANY, null, true).entrySet().stream().count()); s.getSocketOperations(OTHERS, null, true).values().stream().forEach((socketStats) -> { assertEquals(REQUEST.length, socketStats.bytesUp.intValue()); assertEquals(RESPONSE.length, socketStats.bytesDown.intValue()); }); } }
public static void install() throws IOException { try { Field factoryField = Socket.class.getDeclaredField("factory"); factoryField.setAccessible(true); SocketImplFactory currentSocketImplFactory = (SocketImplFactory) factoryField.get(null); if (null == currentSocketImplFactory || !SnifferSocketImplFactory.class.equals(currentSocketImplFactory.getClass())) { factoryField.set(null, null); Socket.setSocketImplFactory(new SnifferSocketImplFactory()); previousSocketImplFactory = currentSocketImplFactory; } } catch (IllegalAccessException e) { throw new IOException("Failed to initialize SnifferSocketImplFactory", e); } catch (NoSuchFieldException e) { throw new IOException("Failed to initialize SnifferSocketImplFactory", e); } }
SnifferSocketImplFactory implements SocketImplFactory { public static void install() throws IOException { try { Field factoryField = Socket.class.getDeclaredField("factory"); factoryField.setAccessible(true); SocketImplFactory currentSocketImplFactory = (SocketImplFactory) factoryField.get(null); if (null == currentSocketImplFactory || !SnifferSocketImplFactory.class.equals(currentSocketImplFactory.getClass())) { factoryField.set(null, null); Socket.setSocketImplFactory(new SnifferSocketImplFactory()); previousSocketImplFactory = currentSocketImplFactory; } } catch (IllegalAccessException e) { throw new IOException("Failed to initialize SnifferSocketImplFactory", e); } catch (NoSuchFieldException e) { throw new IOException("Failed to initialize SnifferSocketImplFactory", e); } } }
SnifferSocketImplFactory implements SocketImplFactory { public static void install() throws IOException { try { Field factoryField = Socket.class.getDeclaredField("factory"); factoryField.setAccessible(true); SocketImplFactory currentSocketImplFactory = (SocketImplFactory) factoryField.get(null); if (null == currentSocketImplFactory || !SnifferSocketImplFactory.class.equals(currentSocketImplFactory.getClass())) { factoryField.set(null, null); Socket.setSocketImplFactory(new SnifferSocketImplFactory()); previousSocketImplFactory = currentSocketImplFactory; } } catch (IllegalAccessException e) { throw new IOException("Failed to initialize SnifferSocketImplFactory", e); } catch (NoSuchFieldException e) { throw new IOException("Failed to initialize SnifferSocketImplFactory", e); } }  }
SnifferSocketImplFactory implements SocketImplFactory { public static void install() throws IOException { try { Field factoryField = Socket.class.getDeclaredField("factory"); factoryField.setAccessible(true); SocketImplFactory currentSocketImplFactory = (SocketImplFactory) factoryField.get(null); if (null == currentSocketImplFactory || !SnifferSocketImplFactory.class.equals(currentSocketImplFactory.getClass())) { factoryField.set(null, null); Socket.setSocketImplFactory(new SnifferSocketImplFactory()); previousSocketImplFactory = currentSocketImplFactory; } } catch (IllegalAccessException e) { throw new IOException("Failed to initialize SnifferSocketImplFactory", e); } catch (NoSuchFieldException e) { throw new IOException("Failed to initialize SnifferSocketImplFactory", e); } }  static void install(); static void uninstall(); @Override SocketImpl createSocketImpl(); }
SnifferSocketImplFactory implements SocketImplFactory { public static void install() throws IOException { try { Field factoryField = Socket.class.getDeclaredField("factory"); factoryField.setAccessible(true); SocketImplFactory currentSocketImplFactory = (SocketImplFactory) factoryField.get(null); if (null == currentSocketImplFactory || !SnifferSocketImplFactory.class.equals(currentSocketImplFactory.getClass())) { factoryField.set(null, null); Socket.setSocketImplFactory(new SnifferSocketImplFactory()); previousSocketImplFactory = currentSocketImplFactory; } } catch (IllegalAccessException e) { throw new IOException("Failed to initialize SnifferSocketImplFactory", e); } catch (NoSuchFieldException e) { throw new IOException("Failed to initialize SnifferSocketImplFactory", e); } }  static void install(); static void uninstall(); @Override SocketImpl createSocketImpl();  }