Skip to content

Commit 2a43956

Browse files
author
Vladimir Kotal
committed
refactor, add test for closing unused servers
1 parent 8ef9460 commit 2a43956

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

plugins/src/test/java/opengrok/auth/plugin/ldap/LdapFacadeTest.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import static org.junit.Assert.assertEquals;
3838
import static org.junit.jupiter.api.Assertions.assertFalse;
3939
import static org.mockito.ArgumentMatchers.any;
40+
import static org.mockito.Mockito.times;
41+
import static org.mockito.Mockito.verify;
4042

4143
public class LdapFacadeTest {
4244
@Test
@@ -54,11 +56,15 @@ public void testSearchControlsConfig() {
5456
}
5557

5658
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;
6268
}
6369

6470
@Test
@@ -116,19 +122,9 @@ public void testToStringPositive() throws UnknownHostException {
116122
@Test
117123
public void testPrepareServersNegative() throws UnknownHostException {
118124
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)));
130127
LdapFacade facade = new LdapFacade(config);
131-
facade.prepareServers();
132128
assertFalse(facade.isConfigured());
133129
}
134130

@@ -139,4 +135,18 @@ public void testGetSearchDescription() {
139135
assertEquals("DN: foo, filter: bar",
140136
LdapFacade.getSearchDescription("foo", "bar", null));
141137
}
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+
}
142152
}

0 commit comments

Comments
 (0)