Skip to content

Commit 0d082e9

Browse files
committed
Add access method that gives access to the listening port for the embedded LDAP server and thus resolves #2
1 parent c6378d3 commit 0d082e9

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

src/main/java/org/zapodot/junit/ldap/EmbeddedLdapRule.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,17 @@ public interface EmbeddedLdapRule extends TestRule {
4747
* @throws NamingException if a LDAP failure happens during DirContext creation
4848
*/
4949
DirContext dirContext() throws NamingException;
50+
51+
/**
52+
* Gives access to the listening port for the currently running embedded LDAP server.
53+
* This will make it easier to use other integration
54+
*
55+
* Note: the listening address is always <em>0.0.0.0</em> which means the server will listen to all available NIC-s.
56+
* This may change in a future release (should ideally only be listening on the loopback device)
57+
*
58+
* @return the port number that the embedded server is listening to
59+
*/
60+
int embeddedServerPort();
61+
62+
5063
}

src/main/java/org/zapodot/junit/ldap/internal/EmbeddedLdapRuleImpl.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ public DirContext dirContext() throws NamingException {
9494
return ContextProxyFactory.asDelegatingDirContext(createOrGetInitialDirContext());
9595
}
9696

97+
@Override
98+
public int embeddedServerPort() {
99+
if(isStarted) {
100+
return inMemoryDirectoryServer.getListenPort();
101+
} else {
102+
throw new IllegalStateException("The embedded server must be started prior to accessing the listening port");
103+
}
104+
}
105+
97106
private InitialDirContext createOrGetInitialDirContext() throws NamingException {
98107
if (isStarted) {
99108
if (initialDirContext == null) {
@@ -111,7 +120,7 @@ private Hashtable<String, String> createLdapEnvironment() {
111120
environment.put(LdapContext.CONTROL_FACTORIES, DefaultResponseControlFactory.class.getName());
112121
environment.put(Context.PROVIDER_URL, String.format("ldap://%s:%s",
113122
"localhost",
114-
inMemoryDirectoryServer.getListenPort()));
123+
embeddedServerPort()));
115124
environment.put(Context.INITIAL_CONTEXT_FACTORY, LdapCtxFactory.class.getName());
116125
if (authenticationConfiguration != null) {
117126
environment.putAll(authenticationConfiguration.toAuthenticationEnvironment());

src/test/java/org/zapodot/junit/ldap/EmbeddedLdapRuleTest.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99
import javax.naming.NamingEnumeration;
1010
import javax.naming.directory.DirContext;
1111
import javax.naming.directory.SearchControls;
12-
1312
import java.util.Arrays;
1413

15-
import static org.junit.Assert.assertEquals;
16-
import static org.junit.Assert.assertNotNull;
14+
import static org.junit.Assert.*;
1715

1816
public class EmbeddedLdapRuleTest {
1917

@@ -48,7 +46,9 @@ public void testRawLdapConnection() throws Exception {
4846
ldapConnection.close();
4947
}
5048
ldapConnection = embeddedLdapRule.unsharedLdapConnection();
51-
final SearchResultEntry entry = ldapConnection.searchForEntry(new SearchRequest(dn, SearchScope.BASE, "(objectClass=person)"));
49+
final SearchResultEntry entry = ldapConnection.searchForEntry(new SearchRequest(dn,
50+
SearchScope.BASE,
51+
"(objectClass=person)"));
5252
assertNotNull(entry);
5353
}
5454

@@ -76,4 +76,17 @@ public void testContextClose() throws Exception {
7676
assertNotNull(context.getNameInNamespace());
7777

7878
}
79+
80+
@Test
81+
public void testEmbeddedServerPort() throws Exception {
82+
assertTrue(embeddedLdapRule.embeddedServerPort() > 0);
83+
84+
}
85+
86+
@Test(expected = IllegalStateException.class)
87+
public void testNoPortAssignedYet() throws Exception {
88+
final EmbeddedLdapRule embeddedLdapRule = new EmbeddedLdapRuleBuilder().build();
89+
embeddedLdapRule.embeddedServerPort();
90+
91+
}
7992
}

0 commit comments

Comments
 (0)