From 46cdeca5433bc28e3f99e392762cdcdb90ef9fe2 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Fri, 12 Aug 2016 14:02:39 +1000 Subject: [PATCH] Use dynamic ports for OSGi tests --- jetty-osgi/test-jetty-osgi-context/pom.xml | 2 +- jetty-osgi/test-jetty-osgi-webapp/pom.xml | 2 +- .../osgi/test/TestJettyOSGiBootCore.java | 13 +++++-- .../eclipse/jetty/osgi/test/TestOSGiUtil.java | 38 +++++++++++++++++++ 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/jetty-osgi/test-jetty-osgi-context/pom.xml b/jetty-osgi/test-jetty-osgi-context/pom.xml index 460a0a2754f..a992bfe51ae 100644 --- a/jetty-osgi/test-jetty-osgi-context/pom.xml +++ b/jetty-osgi/test-jetty-osgi-context/pom.xml @@ -6,7 +6,7 @@ 4.0.0 test-jetty-osgi-context - Jetty :: OSGi :: Context + Jetty :: OSGi :: Test Context Test Jetty OSGi bundle with a ContextHandler http://www.eclipse.org/jetty diff --git a/jetty-osgi/test-jetty-osgi-webapp/pom.xml b/jetty-osgi/test-jetty-osgi-webapp/pom.xml index 7a26dd256d3..2201e8c22e6 100644 --- a/jetty-osgi/test-jetty-osgi-webapp/pom.xml +++ b/jetty-osgi/test-jetty-osgi-webapp/pom.xml @@ -7,7 +7,7 @@ 4.0.0 test-jetty-osgi-webapp - Jetty :: OSGi :: WebApp + Jetty :: OSGi :: Test WebApp Test Jetty OSGi Webapp bundle http://www.eclipse.org/jetty diff --git a/jetty-osgi/test-jetty-osgi/src/test/java/org/eclipse/jetty/osgi/test/TestJettyOSGiBootCore.java b/jetty-osgi/test-jetty-osgi/src/test/java/org/eclipse/jetty/osgi/test/TestJettyOSGiBootCore.java index 2608f57c84d..696c886592f 100644 --- a/jetty-osgi/test-jetty-osgi/src/test/java/org/eclipse/jetty/osgi/test/TestJettyOSGiBootCore.java +++ b/jetty-osgi/test-jetty-osgi/src/test/java/org/eclipse/jetty/osgi/test/TestJettyOSGiBootCore.java @@ -47,10 +47,16 @@ public class TestJettyOSGiBootCore { private static final String LOG_LEVEL = "WARN"; - // TODO these should be dynamic - public static final int DEFAULT_HTTP_PORT = 9876; - public static final int DEFAULT_SSL_PORT = 9877; + public static final int DEFAULT_HTTP_PORT=TestOSGiUtil.findFreePort("jetty.http.port"); + public static final int DEFAULT_SSL_PORT=TestOSGiUtil.findFreePort("jetty.ssl.port"); + static + { + System.err.println("DEFAULT_HTTP_PORT="+DEFAULT_HTTP_PORT); + System.err.println("DEFAULT_SSL_PORT="+DEFAULT_SSL_PORT); + } + + @Inject private BundleContext bundleContext; @@ -175,4 +181,5 @@ public class TestJettyOSGiBootCore { TestOSGiUtil.testHttpServiceGreetings(bundleContext, "http", DEFAULT_HTTP_PORT); } + } diff --git a/jetty-osgi/test-jetty-osgi/src/test/java/org/eclipse/jetty/osgi/test/TestOSGiUtil.java b/jetty-osgi/test-jetty-osgi/src/test/java/org/eclipse/jetty/osgi/test/TestOSGiUtil.java index 8b3a27d71a3..e8bf386ba9c 100644 --- a/jetty-osgi/test-jetty-osgi/src/test/java/org/eclipse/jetty/osgi/test/TestOSGiUtil.java +++ b/jetty-osgi/test-jetty-osgi/src/test/java/org/eclipse/jetty/osgi/test/TestOSGiUtil.java @@ -19,6 +19,7 @@ package org.eclipse.jetty.osgi.test; import java.io.IOException; +import java.net.ServerSocket; import java.util.HashMap; import java.util.Map; @@ -184,4 +185,41 @@ public class TestOSGiUtil client.stop(); } } + + public static int findFreePort(String systemProperty) + { + String freeport = System.getProperty(systemProperty); + if (freeport!=null) + return Integer.valueOf(freeport); + + try (ServerSocket socket = new ServerSocket(0)) + { + socket.setReuseAddress(true); + int port = socket.getLocalPort(); + System.setProperty(systemProperty,Integer.toString(port)); + return port; + } + catch (IOException e) + { + throw new RuntimeException(e); + } + } + + + public static void main(String... args) + { + int freeport = TestOSGiUtil.findFreePort("test"); + System.err.println("Found Free port="+freeport); + + + try (ServerSocket socket = new ServerSocket(TestOSGiUtil.findFreePort("test"))) + { + System.err.println("reused port="+socket.getLocalPort()); + } + catch (IOException e) + { + e.printStackTrace(); + } + + } }