Skip to content

Commit 7c50695

Browse files
author
Vladimir Kotal
committed
split the tests to avoid closing auto-closeable resource
1 parent 3b6be24 commit 7c50695

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

plugins/src/test/java/opengrok/auth/plugin/LdapServerTest.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void testSetGetUsername() {
7878
}
7979

8080
@Test
81-
public void testIsReachable() throws IOException, InterruptedException, URISyntaxException {
81+
public void testIsReachablePositive() throws IOException, InterruptedException, URISyntaxException {
8282
// Start simple TCP server on test port.
8383
InetAddress localhostAddr = InetAddress.getLoopbackAddress();
8484
try (ServerSocket serverSocket = new ServerSocket(0, 1)) {
@@ -115,17 +115,27 @@ public void testIsReachable() throws IOException, InterruptedException, URISynta
115115

116116
// Test reachability.
117117
boolean reachable = serverSpy.isReachable();
118-
serverSocket.close();
119-
thread.join(5000);
120-
thread.interrupt();
121118
assertTrue(reachable);
122119

123-
// Test non-reachability.
124-
reachable = serverSpy.isReachable();
125-
assertFalse(reachable);
120+
thread.interrupt();
121+
thread.join(5000);
126122
}
127123
}
128124

125+
@Test
126+
void testsReachableNegative() throws Exception {
127+
InetAddress localhostAddr = InetAddress.getLoopbackAddress();
128+
129+
// Mock getAddresses() to return single localhost IP address and getPort() to return the test port.
130+
LdapServer server = new LdapServer("ldaps://foo.bar.com");
131+
LdapServer serverSpy = Mockito.spy(server);
132+
Mockito.when(serverSpy.getAddresses(any())).thenReturn(new InetAddress[]{localhostAddr});
133+
// port 0 should not be reachable.
134+
doReturn(0).when(serverSpy).getPort();
135+
136+
assertFalse(serverSpy.isReachable());
137+
}
138+
129139
@Test
130140
public void testEmptyAddressArray() throws UnknownHostException {
131141
LdapServer server = new LdapServer("ldaps://foo.bar.com");

0 commit comments

Comments
 (0)