From 4bf5d0fc132ba4712f89009b4d23a2023f0ccbc1 Mon Sep 17 00:00:00 2001 From: Hadrian Zbarcea Date: Tue, 6 Jan 2015 01:23:05 -0500 Subject: [PATCH] Test cleanup: use available port instead of hardcoded --- .../transport/ws/WSTransportTest.java | 34 +++++++++++++++---- .../transport/wss/WSSTransportTest.java | 5 +-- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/activemq-http/src/test/java/org/apache/activemq/transport/ws/WSTransportTest.java b/activemq-http/src/test/java/org/apache/activemq/transport/ws/WSTransportTest.java index b133e71319..58df4ccad1 100644 --- a/activemq-http/src/test/java/org/apache/activemq/transport/ws/WSTransportTest.java +++ b/activemq-http/src/test/java/org/apache/activemq/transport/ws/WSTransportTest.java @@ -21,11 +21,14 @@ import static org.junit.Assert.assertTrue; import java.io.File; import java.io.IOException; +import java.net.ServerSocket; import java.net.Socket; import java.net.URI; import java.net.URISyntaxException; import java.net.UnknownHostException; +import javax.net.ServerSocketFactory; + import org.apache.activemq.broker.BrokerFactory; import org.apache.activemq.broker.BrokerService; import org.apache.activemq.spring.SpringSslContext; @@ -60,6 +63,7 @@ public class WSTransportTest { private File profileDir; private String stompUri; + private int proxyPort = 0; protected String wsUri; private StompConnection stompConnection = new StompConnection(); @@ -101,16 +105,33 @@ public class WSTransportTest { context.setServer(server); server.setHandler(context); - server.setConnectors(new Connector[] { - connector - }); + server.setConnectors(new Connector[] { connector }); server.start(); return server; } - protected Connector createJettyConnector() { + protected int getProxyPort() { + if (proxyPort == 0) { + ServerSocket ss = null; + try { + ss = ServerSocketFactory.getDefault().createServerSocket(0); + proxyPort = ss.getLocalPort(); + } catch (IOException e) { // ignore + } finally { + try { + if (ss != null ) { + ss.close(); + } + } catch (IOException e) { // ignore + } + } + } + return proxyPort; + } + +protected Connector createJettyConnector() { SelectChannelConnector connector = new SelectChannelConnector(); - connector.setPort(8080); + connector.setPort(getProxyPort()); return connector; } @@ -208,7 +229,8 @@ public class WSTransportTest { } protected String getTestURI() { - return "http://localhost:8080/websocket.html#" + wsUri; + int port = getProxyPort(); + return "http://localhost:" + port + "/websocket.html#" + wsUri; } public void doTestWebSockets(WebDriver driver) throws Exception { diff --git a/activemq-http/src/test/java/org/apache/activemq/transport/wss/WSSTransportTest.java b/activemq-http/src/test/java/org/apache/activemq/transport/wss/WSSTransportTest.java index 13fab43cb9..775c0ab331 100644 --- a/activemq-http/src/test/java/org/apache/activemq/transport/wss/WSSTransportTest.java +++ b/activemq-http/src/test/java/org/apache/activemq/transport/wss/WSSTransportTest.java @@ -30,7 +30,7 @@ public class WSSTransportTest extends WSTransportTest { contextFactory.setKeyStorePassword("password"); contextFactory.setTrustStore("src/test/resources/client.keystore"); contextFactory.setTrustStorePassword("password"); - sslConnector.setPort(8080); + sslConnector.setPort(getProxyPort()); return sslConnector; } @@ -41,6 +41,7 @@ public class WSSTransportTest extends WSTransportTest { @Override protected String getTestURI() { - return "https://localhost:8080/websocket.html#wss://localhost:61623"; + int port = getProxyPort(); + return "https://localhost:" + port + "/websocket.html#wss://localhost:61623"; } }