Repository: flex-blazeds Updated Branches: refs/heads/develop 2fe078b10 -> d8aba36dc
- Completely rewrote the way the test-server is started Project: http://git-wip-us.apache.org/repos/asf/flex-blazeds/repo Commit: http://git-wip-us.apache.org/repos/asf/flex-blazeds/commit/d8aba36d Tree: http://git-wip-us.apache.org/repos/asf/flex-blazeds/tree/d8aba36d Diff: http://git-wip-us.apache.org/repos/asf/flex-blazeds/diff/d8aba36d Branch: refs/heads/develop Commit: d8aba36dced5a4ffd587abf5610e7df8f65fafc7 Parents: 2fe078b Author: Christofer Dutz <[email protected]> Authored: Wed Jun 29 16:44:59 2016 +0200 Committer: Christofer Dutz <[email protected]> Committed: Wed Jun 29 16:44:59 2016 +0200 ---------------------------------------------------------------------- testsuite/pom.xml | 2 +- .../io/amf/client/AMFConnectionIT.java | 12 +-- .../messaging/io/amf/client/AMFDataTypeIT.java | 12 +-- .../java/flex/messaging/util/TestServer.java | 89 ++------------------ .../flex/messaging/util/TestServerWrapper.java | 78 +++++++++++++++++ 5 files changed, 96 insertions(+), 97 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flex-blazeds/blob/d8aba36d/testsuite/pom.xml ---------------------------------------------------------------------- diff --git a/testsuite/pom.xml b/testsuite/pom.xml index c45d786..5ebcd39 100644 --- a/testsuite/pom.xml +++ b/testsuite/pom.xml @@ -63,7 +63,7 @@ limitations under the License. <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> - <version>3.8.2</version> + <version>4.12</version> <scope>test</scope> </dependency> </dependencies> http://git-wip-us.apache.org/repos/asf/flex-blazeds/blob/d8aba36d/testsuite/src/test/java/flex/messaging/io/amf/client/AMFConnectionIT.java ---------------------------------------------------------------------- diff --git a/testsuite/src/test/java/flex/messaging/io/amf/client/AMFConnectionIT.java b/testsuite/src/test/java/flex/messaging/io/amf/client/AMFConnectionIT.java index b0d2830..26eaf7c 100644 --- a/testsuite/src/test/java/flex/messaging/io/amf/client/AMFConnectionIT.java +++ b/testsuite/src/test/java/flex/messaging/io/amf/client/AMFConnectionIT.java @@ -21,6 +21,7 @@ import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.Proxy; +import flex.messaging.util.TestServerWrapper; import junit.extensions.TestSetup; import junit.framework.Assert; import junit.framework.Test; @@ -29,7 +30,6 @@ import junit.framework.TestSuite; import amfclient.ClientCustomType; -import flex.messaging.util.TestServer; import flex.messaging.MessageException; import flex.messaging.messages.RemotingMessage; import flex.messaging.io.amf.ASObject; @@ -55,7 +55,7 @@ public class AMFConnectionIT extends TestCase private static final String BAR_STRING = "bar"; private static final String UNEXPECTED_EXCEPTION_STRING = "Unexpected exception: "; - private static TestServer server; + private static TestServerWrapper serverWrapper; private static int serverPort; /** @@ -109,8 +109,8 @@ public class AMFConnectionIT extends TestCase return new TestSetup(suite) { protected void setUp() throws Exception { - server = new TestServer(); - serverPort = server.startServer("classpath:/WEB-INF/flex/services-config.xml"); + serverWrapper = new TestServerWrapper(); + serverPort = serverWrapper.startServer("classpath:/WEB-INF/flex/services-config.xml"); if(serverPort == -1) { Assert.fail("Couldn't start server process"); } @@ -119,8 +119,8 @@ public class AMFConnectionIT extends TestCase "amfclient.ClientCustomType" /* client type */); } protected void tearDown() throws Exception { - server.stopServer(); - server = null; + serverWrapper.stopServer(); + serverWrapper = null; } }; } http://git-wip-us.apache.org/repos/asf/flex-blazeds/blob/d8aba36d/testsuite/src/test/java/flex/messaging/io/amf/client/AMFDataTypeIT.java ---------------------------------------------------------------------- diff --git a/testsuite/src/test/java/flex/messaging/io/amf/client/AMFDataTypeIT.java b/testsuite/src/test/java/flex/messaging/io/amf/client/AMFDataTypeIT.java index 778134b..724a407 100644 --- a/testsuite/src/test/java/flex/messaging/io/amf/client/AMFDataTypeIT.java +++ b/testsuite/src/test/java/flex/messaging/io/amf/client/AMFDataTypeIT.java @@ -19,6 +19,7 @@ package flex.messaging.io.amf.client; import java.util.Date; import java.util.List; +import flex.messaging.util.TestServerWrapper; import junit.extensions.TestSetup; import org.w3c.dom.Document; @@ -29,7 +30,6 @@ import junit.framework.TestSuite; import amfclient.ClientCustomType; -import flex.messaging.util.TestServer; import flex.messaging.io.amf.client.exceptions.ClientStatusException; import flex.messaging.io.amf.client.exceptions.ServerStatusException; import flex.messaging.util.XMLUtil; @@ -47,7 +47,7 @@ public class AMFDataTypeIT extends TestCase private static final String DEFAULT_AMF_OPERATION = getOperationCall(DEFAULT_METHOD_NAME); private static final String UNEXPECTED_EXCEPTION_STRING = "Unexpected exception: "; - private static TestServer server; + private static TestServerWrapper serverWrapper; private static int serverPort; /** @@ -92,8 +92,8 @@ public class AMFDataTypeIT extends TestCase return new TestSetup(suite) { protected void setUp() throws Exception { - server = new TestServer(); - serverPort = server.startServer("classpath:/WEB-INF/flex/services-config.xml"); + serverWrapper = new TestServerWrapper(); + serverPort = serverWrapper.startServer("classpath:/WEB-INF/flex/services-config.xml"); if(serverPort == -1) { Assert.fail("Couldn't start server process"); } @@ -102,8 +102,8 @@ public class AMFDataTypeIT extends TestCase "amfclient.ClientCustomType" /* client type */); } protected void tearDown() throws Exception { - server.stopServer(); - server = null; + serverWrapper.stopServer(); + serverWrapper = null; } }; } http://git-wip-us.apache.org/repos/asf/flex-blazeds/blob/d8aba36d/testsuite/src/test/java/flex/messaging/util/TestServer.java ---------------------------------------------------------------------- diff --git a/testsuite/src/test/java/flex/messaging/util/TestServer.java b/testsuite/src/test/java/flex/messaging/util/TestServer.java index beb9320..197db97 100644 --- a/testsuite/src/test/java/flex/messaging/util/TestServer.java +++ b/testsuite/src/test/java/flex/messaging/util/TestServer.java @@ -19,71 +19,25 @@ package flex.messaging.util; import flex.messaging.MessageBrokerServlet; import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; -import java.io.IOException; -import java.net.ServerSocket; - /** * Simple little wrapper starting up a BlazeDS server in a separate VM useful for unit testing the * features that need different singletons in client and server. */ public class TestServer { - private Process serverProcess; - - public int startServer(String configPath) { - // We can only start one server per instance of TestServer. - if(serverProcess != null) { - return -1; - } - - final String separator = System.getProperty("file.separator"); - final String classpath = System.getProperty("java.class.path"); - final String path = System.getProperty("java.home") + separator + "bin" + separator + "java"; - final int port = findFreePort(); - System.out.print("Starting test-server on port: " + port + " ... "); - final ProcessBuilder processBuilder = new ProcessBuilder(path, - /*"-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005",*/ - "-cp", /*"\"" +*/ classpath /*+ "\""*/, - TestServer.class.getCanonicalName(), /*"\"" +*/ configPath /*+ "\""*/, - Integer.toString(port)); - processBuilder.redirectErrorStream(true); - try { - serverProcess = processBuilder.start(); - // Give the server some time to ramp up. - Thread.sleep(3000); - System.out.println("STARTED"); - return port; - } catch (IOException e) { - System.out.println("ERROR: " + e.toString()); - return -1; - } catch (InterruptedException e) { - System.out.println("ERROR: " + e.toString()); - return -1; - } - } - - public void stopServer() { - if(serverProcess != null) { - System.out.print("Stopping test-server ... "); - // Send a signal to the server process to make itself shut down. - serverProcess.destroy(); - System.out.println("STOPPED"); - } - } - public static void main(String args[]) throws Exception { - if(args.length != 2) { + if(args.length != 1) { throw new Exception("Need exactly two argument containing th path to the configuration " + "followed by the port number the server should use"); } final String configPath = args[0]; // Setup a minimal servlet context for hosting our message broker servlet. - final int port = Integer.valueOf(args[1]); - final Server server = new Server(port); + final Server server = new Server(0); final ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); context.setContextPath("/qa-regress"); final MessageBrokerServlet messageBrokerServlet = new MessageBrokerServlet(); @@ -93,40 +47,7 @@ public class TestServer { server.setHandler(context); server.start(); - // Wait for the process to receive a single from the other vm. - while(true) { - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - // Shut down the server. - server.stop(); - return; - } - } + int port = ((ServerConnector) server.getConnectors()[0]).getLocalPort(); + System.out.println("Port:" + port); } - - private static int findFreePort() { - ServerSocket socket = null; - try { - socket = new ServerSocket(0); - socket.setReuseAddress(true); - int port = socket.getLocalPort(); - try { - socket.close(); - } catch (IOException e) { - // Ignore IOException on close() - } - return port; - } catch (IOException e) { - } finally { - if (socket != null) { - try { - socket.close(); - } catch (IOException e) { - } - } - } - throw new IllegalStateException("Could not find a free TCP/IP port to start embedded Jetty HTTP Server on"); - } - } http://git-wip-us.apache.org/repos/asf/flex-blazeds/blob/d8aba36d/testsuite/src/test/java/flex/messaging/util/TestServerWrapper.java ---------------------------------------------------------------------- diff --git a/testsuite/src/test/java/flex/messaging/util/TestServerWrapper.java b/testsuite/src/test/java/flex/messaging/util/TestServerWrapper.java new file mode 100644 index 0000000..cbde0e4 --- /dev/null +++ b/testsuite/src/test/java/flex/messaging/util/TestServerWrapper.java @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package flex.messaging.util; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +/** + * Simple little wrapper starting up a BlazeDS server in a separate VM useful for unit testing the + * features that need different singletons in client and server. + */ +public class TestServerWrapper { + + private Process serverProcess; + + public int startServer(String configPath) { + // We can only start one server per instance of TestServer. + if(serverProcess != null) { + return -1; + } + + final String separator = System.getProperty("file.separator"); + final String classpath = System.getProperty("java.class.path"); + final String path = System.getProperty("java.home") + separator + "bin" + separator + "java"; + System.out.print("Starting test-server"); + final ProcessBuilder processBuilder = new ProcessBuilder(path, + /*"-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005",*/ + "-cp", /*"\"" +*/ classpath /*+ "\""*/, + TestServer.class.getCanonicalName(), /*"\"" +*/ configPath /*+ "\""*/); + processBuilder.redirectErrorStream(true); + try { + serverProcess = processBuilder.start(); + + BufferedReader in = new BufferedReader(new InputStreamReader(serverProcess.getInputStream())); + + String line; + while((line = in.readLine()) != null) { + if(line.startsWith("Port:")) { + // Read the process output and extract the port + // number the server started on. + int port = Integer.parseInt(line.substring(5)); + System.out.println("STARTED on port " + port); + return port; + } + } + + return -1; + } catch (IOException e) { + System.out.println("ERROR: " + e.toString()); + return -1; + } + } + + public void stopServer() { + if(serverProcess != null) { + System.out.print("Stopping test-server ... "); + // Send a signal to the server process to make itself shut down. + serverProcess.destroy(); + System.out.println("STOPPED"); + } + } + +}
