29
29
import java .io .IOException ;
30
30
import java .net .InetAddress ;
31
31
import java .net .ServerSocket ;
32
- import java .net .Socket ;
33
32
import java .net .URISyntaxException ;
34
33
import java .net .UnknownHostException ;
35
34
36
35
import static org .junit .jupiter .api .Assertions .assertEquals ;
37
36
import static org .junit .jupiter .api .Assertions .assertFalse ;
38
- import static org .junit .jupiter .api .Assertions .assertNotNull ;
39
37
import static org .junit .jupiter .api .Assertions .assertNull ;
40
38
import static org .junit .jupiter .api .Assertions .assertTrue ;
41
39
import static org .mockito .ArgumentMatchers .any ;
42
40
import static org .mockito .Mockito .doReturn ;
43
41
44
- public class LdapServerTest {
42
+ class LdapServerTest {
45
43
46
44
@ Test
47
- public void testInvalidURI () {
45
+ void testInvalidURI () {
48
46
LdapServer server = new LdapServer ("foo:/\\ /\\ foo.bar" );
49
47
assertFalse (server .isReachable ());
50
48
}
51
49
52
50
@ Test
53
- public void testGetPort () throws URISyntaxException {
51
+ void testGetPort () throws URISyntaxException {
54
52
LdapServer server = new LdapServer ("ldaps://foo.bar" );
55
53
assertEquals (636 , server .getPort ());
56
54
@@ -62,7 +60,7 @@ public void testGetPort() throws URISyntaxException {
62
60
}
63
61
64
62
@ Test
65
- public void testSetGetUsername () {
63
+ void testSetGetUsername () {
66
64
LdapServer server = new LdapServer ();
67
65
68
66
assertNull (server .getUsername ());
@@ -78,47 +76,21 @@ public void testSetGetUsername() {
78
76
}
79
77
80
78
@ Test
81
- public void testIsReachablePositive () throws IOException , InterruptedException , URISyntaxException {
79
+ void testIsReachablePositive () throws IOException , URISyntaxException {
82
80
// Start simple TCP server on test port.
83
81
InetAddress loopbackAddress = InetAddress .getLoopbackAddress ();
84
82
try (ServerSocket serverSocket = new ServerSocket (0 , 1 )) {
85
- Thread thread = new Thread (() -> {
86
- try {
87
- while (true ) {
88
- Socket client = serverSocket .accept ();
89
- client .close ();
90
- }
91
- } catch (IOException e ) {
92
- e .printStackTrace ();
93
- }
94
- });
95
-
96
83
int testPort = serverSocket .getLocalPort ();
97
- thread .start ();
98
- Socket socket = null ;
99
- for (int i = 0 ; i < 3 ; i ++) {
100
- try {
101
- socket = new Socket (loopbackAddress , testPort );
102
- } catch (IOException e ) {
103
- Thread .sleep (1000 );
104
- }
105
- }
106
-
107
- assertNotNull (socket );
108
- assertTrue (socket .isConnected ());
109
84
110
85
// Mock getAddresses() to return single localhost IP address and getPort() to return the test port.
111
86
LdapServer server = new LdapServer ("ldaps://foo.bar.com" );
112
87
LdapServer serverSpy = Mockito .spy (server );
113
- Mockito .when (serverSpy .getAddresses (any ())). thenReturn ( new InetAddress []{ loopbackAddress } );
88
+ doReturn ( new InetAddress [] { loopbackAddress }) .when (serverSpy ) .getAddresses (any ());
114
89
doReturn (testPort ).when (serverSpy ).getPort ();
115
90
116
91
// Test reachability.
117
92
boolean reachable = serverSpy .isReachable ();
118
93
assertTrue (reachable );
119
-
120
- thread .interrupt ();
121
- thread .join (5000 );
122
94
}
123
95
}
124
96
@@ -129,23 +101,23 @@ void testsReachableNegative() throws Exception {
129
101
// Mock getAddresses() to return single localhost IP address and getPort() to return the test port.
130
102
LdapServer server = new LdapServer ("ldaps://foo.bar.com" );
131
103
LdapServer serverSpy = Mockito .spy (server );
132
- Mockito .when (serverSpy .getAddresses (any ())). thenReturn ( new InetAddress []{ loopbackAddress } );
104
+ doReturn ( new InetAddress []{ loopbackAddress }) .when (serverSpy ) .getAddresses (any ());
133
105
// port 0 should not be reachable.
134
106
doReturn (0 ).when (serverSpy ).getPort ();
135
107
136
108
assertFalse (serverSpy .isReachable ());
137
109
}
138
110
139
111
@ Test
140
- public void testEmptyAddressArray () throws UnknownHostException {
112
+ void testEmptyAddressArray () throws UnknownHostException {
141
113
LdapServer server = new LdapServer ("ldaps://foo.bar.com" );
142
114
LdapServer serverSpy = Mockito .spy (server );
143
- Mockito .when (serverSpy .getAddresses (any ())). thenReturn ( new InetAddress []{} );
115
+ doReturn ( new InetAddress []{}) .when (serverSpy ) .getAddresses (any ());
144
116
assertFalse (serverSpy .isReachable ());
145
117
}
146
118
147
119
@ Test
148
- public void testToString () {
120
+ void testToString () {
149
121
LdapServer server = new LdapServer ("ldaps://foo.bar.com" , "foo" , "bar" );
150
122
server .setConnectTimeout (2000 );
151
123
server .setReadTimeout (1000 );
0 commit comments