Преглед на файлове

UnboundIdContainer Random Port Support

Setting a port of 0 will now induce the container to select a random
port.

Fixes: gh-5920
Josh Cummings преди 6 години
родител
ревизия
b9ca1400e3

+ 14 - 1
ldap/src/integration-test/java/org/springframework/security/ldap/server/UnboundIdContainerTests.java

@@ -1,5 +1,5 @@
 /*
- * Copyright 2002-2017 the original author or authors.
+ * Copyright 2002-2018 the original author or authors.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -47,6 +47,19 @@ public class UnboundIdContainerTests {
 		}
 	}
 
+	@Test
+	public void afterPropertiesSetWhenPortIsZeroThenRandomPortIsSelected() throws Exception {
+		UnboundIdContainer server = new UnboundIdContainer("dc=springframework,dc=org", null);
+		server.setPort(0);
+
+		try {
+			server.afterPropertiesSet();
+			assertThat(server.getPort()).isNotEqualTo(0);
+		} finally {
+			server.destroy();
+		}
+	}
+
 	private List<Integer> getDefaultPorts(int count) throws IOException {
 		List<ServerSocket> connections = new ArrayList<>();
 		List<Integer> availablePorts = new ArrayList<>(count);

+ 2 - 1
ldap/src/main/java/org/springframework/security/ldap/server/UnboundIdContainer.java

@@ -1,5 +1,5 @@
 /*
- * Copyright 2002-2017 the original author or authors.
+ * Copyright 2002-2018 the original author or authors.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -103,6 +103,7 @@ public class UnboundIdContainer implements InitializingBean, DisposableBean, Lif
 			directoryServer.add(entry);
 			importLdif(directoryServer);
 			directoryServer.startListening();
+			this.port = directoryServer.getListenPort();
 			this.directoryServer = directoryServer;
 			this.running = true;
 		} catch (LDAPException ex) {