37
37
import static org .junit .Assert .assertEquals ;
38
38
import static org .junit .jupiter .api .Assertions .assertFalse ;
39
39
import static org .mockito .ArgumentMatchers .any ;
40
+ import static org .mockito .Mockito .times ;
41
+ import static org .mockito .Mockito .verify ;
40
42
41
43
public class LdapFacadeTest {
42
44
@ Test
@@ -54,11 +56,15 @@ public void testSearchControlsConfig() {
54
56
}
55
57
56
58
private LdapServer getSpyLdapServer (String name ) throws UnknownHostException {
57
- LdapServer server1 = new LdapServer (name );
58
- LdapServer serverSpy1 = Mockito .spy (server1 );
59
- Mockito .when (serverSpy1 .getAddresses (any ())).thenReturn (new InetAddress []{InetAddress .getLocalHost ()});
60
- Mockito .when (serverSpy1 .isWorking ()).thenReturn (true );
61
- return serverSpy1 ;
59
+ return getSpyLdapServer (name , true );
60
+ }
61
+
62
+ private LdapServer getSpyLdapServer (String name , boolean working ) throws UnknownHostException {
63
+ LdapServer server = new LdapServer (name );
64
+ LdapServer serverSpy = Mockito .spy (server );
65
+ Mockito .when (serverSpy .getAddresses (any ())).thenReturn (new InetAddress []{InetAddress .getLocalHost ()});
66
+ Mockito .when (serverSpy .isWorking ()).thenReturn (working );
67
+ return serverSpy ;
62
68
}
63
69
64
70
@ Test
@@ -116,19 +122,9 @@ public void testToStringPositive() throws UnknownHostException {
116
122
@ Test
117
123
public void testPrepareServersNegative () throws UnknownHostException {
118
124
Configuration config = new Configuration ();
119
-
120
- LdapServer server1 = new LdapServer ("ldap://foo.com" );
121
- LdapServer serverSpy1 = Mockito .spy (server1 );
122
- Mockito .when (serverSpy1 .getAddresses (any ())).thenReturn (new InetAddress []{InetAddress .getLocalHost ()});
123
- Mockito .when (serverSpy1 .isReachable ()).thenReturn (false );
124
-
125
- LdapServer server2 = new LdapServer ("ldap://bar.com" );
126
- LdapServer serverSpy2 = Mockito .spy (server2 );
127
- Mockito .when (serverSpy2 .getAddresses (any ())).thenReturn (new InetAddress []{});
128
-
129
- config .setServers (Arrays .asList (serverSpy1 , serverSpy2 ));
125
+ config .setServers (Arrays .asList (getSpyLdapServer ("ldap://foo.com" , false ),
126
+ getSpyLdapServer ("ldap://bar.com" , true )));
130
127
LdapFacade facade = new LdapFacade (config );
131
- facade .prepareServers ();
132
128
assertFalse (facade .isConfigured ());
133
129
}
134
130
@@ -139,4 +135,18 @@ public void testGetSearchDescription() {
139
135
assertEquals ("DN: foo, filter: bar" ,
140
136
LdapFacade .getSearchDescription ("foo" , "bar" , null ));
141
137
}
138
+
139
+ @ Test
140
+ public void testPrepareServersCloseUnused () throws UnknownHostException {
141
+ Configuration config = new Configuration ();
142
+
143
+ LdapServer server1 = getSpyLdapServer ("ldap://foo.com" );
144
+ LdapServer server2 = getSpyLdapServer ("ldap://bar.com" );
145
+ LdapServer [] servers = {server1 , server2 };
146
+ config .setServers (Arrays .asList (servers ));
147
+
148
+ LdapFacade facade = new LdapFacade (config );
149
+ verify (server1 , times (0 )).close ();
150
+ verify (server2 ).close ();
151
+ }
142
152
}
0 commit comments