Skip to content

Commit

Permalink
Reserve LDAP port number for tests to avoid 'Address already in use' …
Browse files Browse the repository at this point in the history
…build failures
  • Loading branch information
leleuj committed Oct 9, 2019
1 parent 7cfec3a commit 7bf4652
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void test() {
properties.put(INDIRECTBASICAUTH_AUTHENTICATOR.concat(".2"), "testUsernamePassword");

properties.put(LDAP_TYPE, "direct");
properties.put(LDAP_URL, "ldap://localhost:" + PORT);
properties.put(LDAP_URL, "ldap://localhost:" + port);
properties.put(LDAP_USE_SSL, "false");
properties.put(LDAP_USE_START_TLS, "false");
properties.put(LDAP_DN_FORMAT, CN + "=%s," + BASE_PEOPLE_DN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public LdapClient() {
final ConnectionConfig connectionConfig = new ConnectionConfig();
connectionConfig.setConnectTimeout(Duration.ofMillis(500));
connectionConfig.setResponseTimeout(Duration.ofSeconds(1));
connectionConfig.setLdapUrl("ldap://localhost:" + LdapServer.PORT);
connectionConfig.setLdapUrl("ldap://localhost:" + LdapServer.port);

connectionFactory = new DefaultConnectionFactory();
((DefaultConnectionFactory) connectionFactory).setConnectionConfig(connectionConfig);
Expand Down
13 changes: 11 additions & 2 deletions pac4j-ldap/src/test/java/org/pac4j/ldap/test/tools/LdapServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@ public final class LdapServer implements TestsConstants {

public final static String BASE_DN = "dc=example,dc=com";
public final static String BASE_PEOPLE_DN = "ou=people,dc=example,dc=com";
public final static int PORT = 33389;
private final static int PORT = 33389;
public static int port = PORT;
public final static String CN = "cn";
public final static String SN = "sn";
public final static String ROLE = "role";
public final static String ROLE1 = "role1";
public final static String ROLE2 = "role2";

static {
final String sPort = System.getProperty("test.ldap.port");
System.out.println("Reserved LDAP port: " + sPort);
if (sPort != null) {
port = Integer.parseInt(sPort);
}
}

private InMemoryDirectoryServer ds;

public void start() {
Expand All @@ -30,7 +39,7 @@ public void start() {
dsConfig.setSchema(null);
dsConfig.setEnforceAttributeSyntaxCompliance(false);
dsConfig.setEnforceSingleStructuralObjectClass(false);
dsConfig.setListenerConfigs(new InMemoryListenerConfig("myListener", null, PORT, null, null, null));
dsConfig.setListenerConfigs(new InMemoryListenerConfig("myListener", null, port, null, null, null));
dsConfig.addAdditionalBindCredentials(CN + "=" + GOOD_USERNAME + "," + BASE_PEOPLE_DN, PASSWORD);
dsConfig.addAdditionalBindCredentials(CN + "=" + GOOD_USERNAME2 + "," + BASE_PEOPLE_DN, PASSWORD);
this.ds = new InMemoryDirectoryServer(dsConfig);
Expand Down
23 changes: 23 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,26 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>reserve-ports</id>
<phase>generate-sources</phase>
<goals>
<goal>reserve-network-port</goal>
</goals>
<configuration>
<portNames>
<portName>test.ldap.port</portName>
</portNames>
<minPortNumber>33389</minPortNumber>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
Expand All @@ -279,6 +299,9 @@
<include>**/*Test.java</include>
<include>**/*Tests.java</include>
</includes>
<systemPropertyVariables>
<test.ldap.port>${test.ldap.port}</test.ldap.port>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
Expand Down

0 comments on commit 7bf4652

Please sign in to comment.