@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 @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 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 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); }
@Test public void testShutdownOutput() throws Exception { sniffySocket.shutdownOutput(); verifyPrivate(delegate).invoke("shutdownOutput"); verifyNoMoreInteractions(delegate); }
@Test public void testCreate() throws Exception { sniffySocket.create(true); verifyPrivate(delegate).invoke("create", true); verifyNoMoreInteractions(delegate); }
@Test public void testClose() throws Exception { sniffySocket.close(); verifyPrivate(delegate).invoke("close"); verifyNoMoreInteractions(delegate); }
@Test public void testSetPerformancePreferences() throws Exception { sniffySocket.setPerformancePreferences(1, 2, 3); verifyPrivate(delegate).invoke("setPerformancePreferences", 1, 2, 3); verifyNoMoreInteractions(delegate); }
@Test public void testSupportsUrgentData() throws Exception { when(delegate, "supportsUrgentData").thenReturn(true); boolean actual = sniffySocket.supportsUrgentData(); verifyPrivate(delegate).invoke("supportsUrgentData"); verifyNoMoreInteractions(delegate); assertEquals(true, actual); }
@Test public void testGetLocalPort() throws Exception { int expected = 42; when(delegate, "getLocalPort").thenReturn(expected); int actual = sniffySocket.getLocalPort(); verifyPrivate(delegate).invoke("getLocalPort"); verifyNoMoreInteractions(delegate); assertEquals(expected, actual); }
@Test public void testAvailable() throws Exception { int expected = 1; when(delegate, "available").thenReturn(expected); int actual = sniffySocket.available(); verifyPrivate(delegate).invoke("available"); verifyNoMoreInteractions(delegate); assertEquals(expected, actual); }
@Test public void testGetPort() throws Exception { int expected = 1; when(delegate, "getPort").thenReturn(expected); int actual = sniffySocket.getPort(); verifyPrivate(delegate).invoke("getPort"); verifyNoMoreInteractions(delegate); assertEquals(expected, actual); }
@Test public void testGetInetAddress() throws Exception { InetAddress expected = mock(InetAddress.class); when(delegate, "getInetAddress").thenReturn(expected); InetAddress actual = sniffySocket.getInetAddress(); verifyPrivate(delegate).invoke("getInetAddress"); verifyNoMoreInteractions(delegate); assertEquals(expected, actual); }