@Test public void testBehaviourWithOnlyOrdinaryChecks() throws Exception { final TreeWalker treeWalkerSpy = spy(new TreeWalker()); final Class<?> classAstState = Class.forName("com.puppycrawl.tools.checkstyle.TreeWalker$AstState"); final PackageObjectFactory factory = new PackageObjectFactory( new HashSet<>(), Thread.currentThread().getContextClassLoader()); treeWalkerSpy.configure(createModuleConfig(TypeNameCheck.class)); treeWalkerSpy.setModuleFactory(factory); treeWalkerSpy.setupChild(createModuleConfig(TypeNameCheck.class)); final File file = temporaryFolder.newFile("file.java"); final List<String> lines = new ArrayList<>(); lines.add("class Test {}"); treeWalkerSpy.processFiltered(file, new FileText(file, lines)); verifyPrivate(treeWalkerSpy, times(1)).invoke("walk", any(DetailAST.class), any(FileContents.class), any(classAstState)); verifyPrivate(treeWalkerSpy, times(0)).invoke("getFilteredMessages", any(String.class), any(FileContents.class), any(DetailAST.class)); }
@Test public void testBehaviourWithOnlyCommentChecks() throws Exception { final TreeWalker treeWalkerSpy = spy(new TreeWalker()); final Class<?> classAstState = Class.forName("com.puppycrawl.tools.checkstyle.TreeWalker$AstState"); final PackageObjectFactory factory = new PackageObjectFactory( new HashSet<>(), Thread.currentThread().getContextClassLoader()); treeWalkerSpy.configure(createModuleConfig(CommentsIndentationCheck.class)); treeWalkerSpy.setModuleFactory(factory); treeWalkerSpy.setupChild(createModuleConfig(CommentsIndentationCheck.class)); final File file = temporaryFolder.newFile("file.java"); final List<String> lines = new ArrayList<>(); lines.add("class Test {}"); treeWalkerSpy.processFiltered(file, new FileText(file, lines)); verifyPrivate(treeWalkerSpy, times(1)).invoke("walk", any(DetailAST.class), any(FileContents.class), any(classAstState)); verifyPrivate(treeWalkerSpy, times(0)).invoke("getFilteredMessages", any(String.class), any(FileContents.class), any(DetailAST.class)); }
@Test public void testConnectSocketAddress() throws Exception { SocketAddress socketAddress = new InetSocketAddress(InetAddress.getLocalHost(), 123); sniffySocket.connect(socketAddress, 123); verifyPrivate(delegate).invoke("connect", socketAddress, 123); verifyNoMoreInteractions(delegate); }
@Test public void testGetOutputStream() throws Exception { ByteArrayOutputStream expected = new ByteArrayOutputStream(); when(delegate, "getOutputStream").thenReturn(expected); OutputStream actual = sniffySocket.getOutputStream(); verifyPrivate(delegate).invoke("getOutputStream"); verifyNoMoreInteractions(delegate); assertEquals(SnifferOutputStream.class, actual.getClass()); actual.write(1); assertArrayEquals(new byte[]{1}, expected.toByteArray()); }
@Test public void testGetInputStream() throws Exception { InputStream expected = new ByteArrayInputStream(new byte[]{1,2,3}); when(delegate, "getInputStream").thenReturn(expected); InputStream actual = sniffySocket.getInputStream(); verifyPrivate(delegate).invoke("getInputStream"); verifyNoMoreInteractions(delegate); assertEquals(SnifferInputStream.class, actual.getClass()); assertEquals(1, actual.read()); }
@Test public void testPrivateMethod() throws Exception { PowerMockito.spy(A.class); PowerMockito.when(A.class, "privateStaticMethod").thenReturn("abc"); String real = A.callPrivateStaticMethod(); Assert.assertEquals("callabc", real); PowerMockito.verifyPrivate(A.class).invoke("privateStaticMethod"); }
@Test public void testSetOption() throws Exception { int optId = 1; Object option = new Object(); sniffySocket.setOption(optId, option); verifyPrivate(delegate).invoke("setOption", optId, option); verifyNoMoreInteractions(delegate); }
@Test public void testConnectInetAddress() throws Exception { InetAddress inetAddress = InetAddress.getLoopbackAddress(); sniffySocket.connect(inetAddress, 123); verifyPrivate(delegate).invoke("connect", inetAddress, 123); verifyNoMoreInteractions(delegate); }
@Test public void testBindInetAddress() throws Exception { InetAddress inetAddress = InetAddress.getLoopbackAddress(); sniffySocket.bind(inetAddress, 123); verifyPrivate(delegate).invoke("bind", inetAddress, 123); verifyNoMoreInteractions(delegate); }
@Test @Features({"issues/219"}) public void testConnectWithDelayThreadSleeps() throws Exception { ConnectionsRegistry.INSTANCE.setSocketAddressStatus("localhost", 123, 1000); AtomicReference<Exception> exceptionReference = new AtomicReference<>(); Thread thread = new Thread(() -> { try { sniffySocket.connect("localhost", 123); } catch (IOException e) { exceptionReference.set(e); } }); assertNull(exceptionReference.get()); thread.start(); Thread.sleep(500); assertEquals(Thread.State.TIMED_WAITING, thread.getState()); thread.join(1000); verifyPrivate(delegate).invoke("connect", "localhost", 123); verifyNoMoreInteractions(delegate); }
@Test @Features({"issues/219"}) public void testConnectWithDelay() throws Exception { ConnectionsRegistry.INSTANCE.setSocketAddressStatus("localhost", 123, 10); doNothing().when(SnifferSocketImpl.class, "sleepImpl", anyInt()); sniffySocket.connect("localhost", 123); verifyPrivate(SnifferSocketImpl.class).invoke("sleepImpl", eq(10)); verifyPrivate(delegate).invoke("connect", "localhost", 123); verifyNoMoreInteractions(delegate); }
@Test public void testEstimateSendBufferSndBufOptionThrowsException() throws Exception { ByteArrayOutputStream expected = new ByteArrayOutputStream(); SnifferSocketImpl.defaultSendBufferSize = null; when(delegate, "getOutputStream").thenReturn(expected); when(delegate, "getOption", SO_SNDBUF).thenThrow(new SocketException()); OutputStream actual = sniffySocket.getOutputStream(); verifyPrivate(delegate).invoke("getOutputStream"); verifyNoMoreInteractions(ignoreStubs(delegate)); assertEquals(SnifferOutputStream.class, actual.getClass()); actual.write(3); assertEquals(3, (int) expected.toByteArray()[0]); assertEquals((Integer) 0, SnifferSocketImpl.defaultSendBufferSize); }
@Test public void testGetOption() throws Exception { Object expected = new Object(); when(delegate, "getOption", 1).thenReturn(expected); Object actual = sniffySocket.getOption(1); verifyPrivate(delegate).invoke("getOption", 1); verifyNoMoreInteractions(delegate); assertEquals(expected, actual); }
@Test public void testEstimateReceiveBufferRcvBufOptionThrowsException() throws Exception { InputStream expected = new ByteArrayInputStream(new byte[]{1,2,3}); SnifferSocketImpl.defaultReceiveBufferSize = null; when(delegate, "getInputStream").thenReturn(expected); when(delegate, "getOption", SO_RCVBUF).thenThrow(new SocketException()); InputStream actual = sniffySocket.getInputStream(); verifyPrivate(delegate).invoke("getInputStream"); verifyNoMoreInteractions(ignoreStubs(delegate)); assertEquals(SnifferInputStream.class, actual.getClass()); assertEquals(1, actual.read()); assertEquals((Integer) 0, SnifferSocketImpl.defaultReceiveBufferSize); }
@Test public void testSendUrgentDataThrowsIOException() throws Exception { IOException expected = new IOException(); when(delegate, "sendUrgentData", anyInt()).thenThrow(expected); try { sniffySocket.sendUrgentData(1); fail(); } catch (IOException actual) { assertEquals(expected, actual); } verifyPrivate(delegate).invoke("sendUrgentData",1); verifyNoMoreInteractions(delegate); }
@Test public void testEstimateSendBufferNoSndBufOption() throws Exception { ByteArrayOutputStream expected = new ByteArrayOutputStream(); SnifferSocketImpl.defaultSendBufferSize = null; when(delegate, "getOutputStream").thenReturn(expected); when(delegate, "getOption", SO_SNDBUF).thenReturn(null); OutputStream actual = sniffySocket.getOutputStream(); verifyPrivate(delegate).invoke("getOutputStream"); verifyNoMoreInteractions(ignoreStubs(delegate)); assertEquals(SnifferOutputStream.class, actual.getClass()); actual.write(3); assertEquals(3, (int) expected.toByteArray()[0]); assertEquals((Integer) 0, SnifferSocketImpl.defaultSendBufferSize); }
@Test public void testSendUrgentDataThrowsRuntimeException() throws Exception { RuntimeException expected = new RuntimeException(); when(delegate, "sendUrgentData", anyInt()).thenThrow(expected); try { sniffySocket.sendUrgentData(1); fail(); } catch (Exception actual) { assertEquals(expected, actual); } verifyPrivate(delegate).invoke("sendUrgentData",1); verifyNoMoreInteractions(delegate); }
@Test public void testEstimateReceiveBufferNoRcvBufOption() throws Exception { InputStream expected = new ByteArrayInputStream(new byte[]{1,2,3}); SnifferSocketImpl.defaultReceiveBufferSize = null; when(delegate, "getInputStream").thenReturn(expected); when(delegate, "getOption", SO_RCVBUF).thenReturn(null); InputStream actual = sniffySocket.getInputStream(); verifyPrivate(delegate).invoke("getInputStream"); verifyNoMoreInteractions(ignoreStubs(delegate)); assertEquals(SnifferInputStream.class, actual.getClass()); assertEquals(1, actual.read()); assertEquals((Integer) 0, SnifferSocketImpl.defaultReceiveBufferSize); }
@Test public void testSendUrgentData() throws Exception { sniffySocket.sendUrgentData(1); // TODO: insert timeout here and to similar methods? verifyPrivate(delegate).invoke("sendUrgentData",1); verifyNoMoreInteractions(delegate); }
@Test public void testShutdownInput() throws Exception { sniffySocket.shutdownInput(); verifyPrivate(delegate).invoke("shutdownInput"); verifyNoMoreInteractions(delegate); }