Repository: flex-blazeds
Updated Branches:
  refs/heads/master 45a58ab16 -> 9c8588434


- Refactored the TestServer that it finds a free port and uses that for testing.
- Made the TestServer run on a Mac (Hopefully Linux too)


Project: http://git-wip-us.apache.org/repos/asf/flex-blazeds/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-blazeds/commit/1cf3381a
Tree: http://git-wip-us.apache.org/repos/asf/flex-blazeds/tree/1cf3381a
Diff: http://git-wip-us.apache.org/repos/asf/flex-blazeds/diff/1cf3381a

Branch: refs/heads/master
Commit: 1cf3381ac0c8a2ed135e4463a719edb175eddf44
Parents: dab3e47
Author: Christofer Dutz <christofer.d...@codecentric.de>
Authored: Wed Aug 6 11:25:38 2014 +0200
Committer: Christofer Dutz <christofer.d...@codecentric.de>
Committed: Wed Aug 6 11:25:38 2014 +0200

----------------------------------------------------------------------
 .../io/amf/client/AMFConnectionTest.java        | 76 +++++++++++---------
 .../io/amf/client/AMFDataTypeTest.java          | 12 +++-
 .../java/flex/messaging/util/TestServer.java    | 52 +++++++++++---
 3 files changed, 92 insertions(+), 48 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-blazeds/blob/1cf3381a/modules/testsuite/src/test/java/flex/messaging/io/amf/client/AMFConnectionTest.java
----------------------------------------------------------------------
diff --git 
a/modules/testsuite/src/test/java/flex/messaging/io/amf/client/AMFConnectionTest.java
 
b/modules/testsuite/src/test/java/flex/messaging/io/amf/client/AMFConnectionTest.java
index 1db6025..75aba04 100644
--- 
a/modules/testsuite/src/test/java/flex/messaging/io/amf/client/AMFConnectionTest.java
+++ 
b/modules/testsuite/src/test/java/flex/messaging/io/amf/client/AMFConnectionTest.java
@@ -48,13 +48,14 @@ public class AMFConnectionTest extends TestCase
     private static final String DEFAULT_DESTINATION_ID = 
"amfConnectionTestService";
     private static final String DEFAULT_METHOD_NAME = "echoString";
     private static final String DEFAULT_METHOD_ARG = "echo me";
-    private static final String DEFAULT_URL = 
"http://localhost:8400/qa-regress/messagebroker/amf";;
+    private static final String DEFAULT_URL = 
"http://localhost:%s/qa-regress/messagebroker/amf";;
     private static final String DEFAULT_AMF_OPERATION = 
getOperationCall(DEFAULT_METHOD_NAME);
     private static final String FOO_STRING = "foo";
     private static final String BAR_STRING = "bar";
     private static final String UNEXPECTED_EXCEPTION_STRING = "Unexpected 
exception: ";
 
     private TestServer server;
+    private int serverPort;
 
     /**
      * Given a remote method name, returns the AMF connection call needed using
@@ -65,6 +66,10 @@ public class AMFConnectionTest extends TestCase
         return DEFAULT_DESTINATION_ID + "." + method;
     }
 
+    protected String getConnectionUrl() {
+        return String.format(DEFAULT_URL, serverPort);
+    }
+
 
     public AMFConnectionTest(String name)
     {
@@ -107,7 +112,8 @@ public class AMFConnectionTest extends TestCase
     protected void setUp() throws Exception
     {
         server = new TestServer();
-        if(!server.startServer("classpath:/WEB-INF/flex/services-config.xml")) 
{
+        serverPort = 
server.startServer("classpath:/WEB-INF/flex/services-config.xml");
+        if(serverPort == -1) {
             Assert.fail("Couldn't start server process");
         }
 
@@ -135,7 +141,7 @@ public class AMFConnectionTest extends TestCase
         // Connect to the remote url.
         try
         {
-            amfConnection.connect(DEFAULT_URL);
+            amfConnection.connect(getConnectionUrl());
         }
         catch (ClientStatusException cse)
         {
@@ -166,8 +172,8 @@ public class AMFConnectionTest extends TestCase
         AMFConnection amfConnection = new AMFConnection();
         try
         {
-            amfConnection.connect(DEFAULT_URL);
-            Assert.assertEquals(DEFAULT_URL, amfConnection.getUrl());
+            amfConnection.connect(getConnectionUrl());
+            Assert.assertEquals(getConnectionUrl(), amfConnection.getUrl());
         }
         catch (ClientStatusException cse)
         {
@@ -184,8 +190,8 @@ public class AMFConnectionTest extends TestCase
         AMFConnection amfConnection = new AMFConnection();
         try
         {
-            amfConnection.connect(DEFAULT_URL);
-            Assert.assertEquals(DEFAULT_URL, amfConnection.getUrl());
+            amfConnection.connect(getConnectionUrl());
+            Assert.assertEquals(getConnectionUrl(), amfConnection.getUrl());
         }
         catch (ClientStatusException cse)
         {
@@ -222,7 +228,7 @@ public class AMFConnectionTest extends TestCase
         AMFConnection amfConnection = new AMFConnection();
         try
         {
-            amfConnection.connect(DEFAULT_URL);
+            amfConnection.connect(getConnectionUrl());
         }
         catch (ClientStatusException cse)
         {
@@ -404,8 +410,8 @@ public class AMFConnectionTest extends TestCase
         AMFConnection amfConnection = new AMFConnection();
         try
         {
-            amfConnection.connect(DEFAULT_URL);
-            Assert.assertEquals(DEFAULT_URL, amfConnection.getUrl());
+            amfConnection.connect(getConnectionUrl());
+            Assert.assertEquals(getConnectionUrl(), amfConnection.getUrl());
             amfConnection.setObjectEncoding(MessageIOConstants.AMF0);
             retAMF = amfConnection.getObjectEncoding();
             Assert.assertEquals(MessageIOConstants.AMF0, retAMF);
@@ -426,8 +432,8 @@ public class AMFConnectionTest extends TestCase
         AMFConnection amfConnection = new AMFConnection();
         try
         {
-            amfConnection.connect(DEFAULT_URL);
-            Assert.assertEquals(DEFAULT_URL, amfConnection.getUrl());
+            amfConnection.connect(getConnectionUrl());
+            Assert.assertEquals(getConnectionUrl(), amfConnection.getUrl());
             AMFConnection.setDefaultObjectEncoding(MessageIOConstants.AMF3);
             retAMF = AMFConnection.getDefaultObjectEncoding();
             Assert.assertEquals(MessageIOConstants.AMF3, retAMF);
@@ -453,8 +459,8 @@ public class AMFConnectionTest extends TestCase
         AMFConnection amfConnection = new AMFConnection();
         try
         {
-            amfConnection.connect(DEFAULT_URL);
-            Assert.assertEquals(DEFAULT_URL, amfConnection.getUrl());
+            amfConnection.connect(getConnectionUrl());
+            Assert.assertEquals(getConnectionUrl(), amfConnection.getUrl());
             amfConnection.setAMFHeaderProcessor(setAMF);
             retAMF = amfConnection.getAMFHeaderProcessor();
             Assert.assertEquals(setAMF, retAMF);
@@ -476,8 +482,8 @@ public class AMFConnectionTest extends TestCase
         AMFConnection amfConnection = new AMFConnection();
         try
         {
-            amfConnection.connect(DEFAULT_URL);
-            Assert.assertEquals(DEFAULT_URL, amfConnection.getUrl());
+            amfConnection.connect(getConnectionUrl());
+            Assert.assertEquals(getConnectionUrl(), amfConnection.getUrl());
             amfConnection.addAmfHeader(FOO_STRING,val);
             retAMF = amfConnection.removeAmfHeader(FOO_STRING);
             Assert.assertTrue(retAMF);
@@ -499,8 +505,8 @@ public class AMFConnectionTest extends TestCase
         AMFConnection amfConnection = new AMFConnection();
         try
         {
-            amfConnection.connect(DEFAULT_URL);
-            Assert.assertEquals(DEFAULT_URL, amfConnection.getUrl());
+            amfConnection.connect(getConnectionUrl());
+            Assert.assertEquals(getConnectionUrl(), amfConnection.getUrl());
             amfConnection.addAmfHeader(FOO_STRING,true,val);
             retAMF = amfConnection.removeAmfHeader(FOO_STRING);
             Assert.assertTrue(retAMF);
@@ -522,8 +528,8 @@ public class AMFConnectionTest extends TestCase
         AMFConnection amfConnection = new AMFConnection();
         try
         {
-            amfConnection.connect(DEFAULT_URL);
-            Assert.assertEquals(DEFAULT_URL, amfConnection.getUrl());
+            amfConnection.connect(getConnectionUrl());
+            Assert.assertEquals(getConnectionUrl(), amfConnection.getUrl());
             amfConnection.addAmfHeader(FOO_STRING,true,val1);
             amfConnection.addAmfHeader(BAR_STRING,true,val2);
             amfConnection.removeAllAmfHeaders();
@@ -545,8 +551,8 @@ public class AMFConnectionTest extends TestCase
         AMFConnection amfConnection = new AMFConnection();
         try
         {
-            amfConnection.connect(DEFAULT_URL);
-            Assert.assertEquals(DEFAULT_URL, amfConnection.getUrl());
+            amfConnection.connect(getConnectionUrl());
+            Assert.assertEquals(getConnectionUrl(), amfConnection.getUrl());
             amfConnection.addHttpRequestHeader(FOO_STRING,BAR_STRING);
             retHttp = amfConnection.removeHttpRequestHeader(FOO_STRING);
             Assert.assertTrue(retHttp);
@@ -566,8 +572,8 @@ public class AMFConnectionTest extends TestCase
         AMFConnection amfConnection = new AMFConnection();
         try
         {
-            amfConnection.connect(DEFAULT_URL);
-            Assert.assertEquals(DEFAULT_URL, amfConnection.getUrl());
+            amfConnection.connect(getConnectionUrl());
+            Assert.assertEquals(getConnectionUrl(), amfConnection.getUrl());
             amfConnection.addHttpRequestHeader(FOO_STRING,BAR_STRING);
             amfConnection.addHttpRequestHeader(BAR_STRING,FOO_STRING);
             amfConnection.removeAllHttpRequestHeaders();
@@ -589,8 +595,8 @@ public class AMFConnectionTest extends TestCase
         AMFConnection amfConnection = new AMFConnection();
         try
         {
-            amfConnection.connect(DEFAULT_URL);
-            Assert.assertEquals(DEFAULT_URL, amfConnection.getUrl());
+            amfConnection.connect(getConnectionUrl());
+            Assert.assertEquals(getConnectionUrl(), amfConnection.getUrl());
             retAMF = amfConnection.removeAmfHeader(FOO_STRING);
             Assert.assertFalse(retAMF);
         }
@@ -609,8 +615,8 @@ public class AMFConnectionTest extends TestCase
         AMFConnection amfConnection = new AMFConnection();
         try
         {
-            amfConnection.connect(DEFAULT_URL);
-            Assert.assertEquals(DEFAULT_URL, amfConnection.getUrl());
+            amfConnection.connect(getConnectionUrl());
+            Assert.assertEquals(getConnectionUrl(), amfConnection.getUrl());
             amfConnection.removeAllAmfHeaders();
             Assert.assertTrue(true);
         }
@@ -630,8 +636,8 @@ public class AMFConnectionTest extends TestCase
         AMFConnection amfConnection = new AMFConnection();
         try
         {
-            amfConnection.connect(DEFAULT_URL);
-            Assert.assertEquals(DEFAULT_URL, amfConnection.getUrl());
+            amfConnection.connect(getConnectionUrl());
+            Assert.assertEquals(getConnectionUrl(), amfConnection.getUrl());
             retHttp = amfConnection.removeHttpRequestHeader(FOO_STRING);
             Assert.assertFalse(retHttp);
         }
@@ -650,8 +656,8 @@ public class AMFConnectionTest extends TestCase
         AMFConnection amfConnection = new AMFConnection();
         try
         {
-            amfConnection.connect(DEFAULT_URL);
-            Assert.assertEquals(DEFAULT_URL, amfConnection.getUrl());
+            amfConnection.connect(getConnectionUrl());
+            Assert.assertEquals(getConnectionUrl(), amfConnection.getUrl());
             amfConnection.removeAllHttpRequestHeaders();
             Assert.assertTrue(true);
         }
@@ -672,7 +678,7 @@ public class AMFConnectionTest extends TestCase
         try
         {
             AMFConnection amfConnection = new AMFConnection();
-            amfConnection.connect(DEFAULT_URL);
+            amfConnection.connect(getConnectionUrl());
 
             // First, make sure we get the strong type.
             Object result = amfConnection.call(getOperationCall(method));
@@ -696,7 +702,7 @@ public class AMFConnectionTest extends TestCase
         try
         {
             AmfTrace trace = new AmfTrace();
-            amfConnection.connect(DEFAULT_URL);
+            amfConnection.connect(getConnectionUrl());
             amfConnection.setAmfTrace(trace);
 
             String stringToEcho = DEFAULT_METHOD_ARG + 1;
@@ -766,7 +772,7 @@ public class AMFConnectionTest extends TestCase
     {
         AMFConnection amfConnection = new AMFConnection();
         // Connect.
-        amfConnection.connect(DEFAULT_URL);
+        amfConnection.connect(getConnectionUrl());
         // Make a remoting call and retrieve the result.
         Object result;
         if (methodArg == null)

http://git-wip-us.apache.org/repos/asf/flex-blazeds/blob/1cf3381a/modules/testsuite/src/test/java/flex/messaging/io/amf/client/AMFDataTypeTest.java
----------------------------------------------------------------------
diff --git 
a/modules/testsuite/src/test/java/flex/messaging/io/amf/client/AMFDataTypeTest.java
 
b/modules/testsuite/src/test/java/flex/messaging/io/amf/client/AMFDataTypeTest.java
index b88aa98..a107c28 100644
--- 
a/modules/testsuite/src/test/java/flex/messaging/io/amf/client/AMFDataTypeTest.java
+++ 
b/modules/testsuite/src/test/java/flex/messaging/io/amf/client/AMFDataTypeTest.java
@@ -42,11 +42,12 @@ public class AMFDataTypeTest extends TestCase
     private static final String DEFAULT_DESTINATION_ID = 
"amfConnectionTestService";
     private static final String DEFAULT_METHOD_NAME = "echoString";
     private static final String DEFAULT_METHOD_ARG = "echo me";
-    private static final String DEFAULT_URL = 
"http://localhost:8400/qa-regress/messagebroker/amf";;
+    private static final String DEFAULT_URL = 
"http://localhost:%s/qa-regress/messagebroker/amf";;
     private static final String DEFAULT_AMF_OPERATION = 
getOperationCall(DEFAULT_METHOD_NAME);
     private static final String UNEXPECTED_EXCEPTION_STRING = "Unexpected 
exception: ";
 
     private TestServer server;
+    private int serverPort;
 
     /**
      * Given a remote method name, returns the AMF connection call needed using
@@ -57,6 +58,10 @@ public class AMFDataTypeTest extends TestCase
         return DEFAULT_DESTINATION_ID + "." + method;
     }
 
+    protected String getConnectionUrl() {
+        return String.format(DEFAULT_URL, serverPort);
+    }
+
 
     public AMFDataTypeTest(String name)
     {
@@ -89,7 +94,8 @@ public class AMFDataTypeTest extends TestCase
     protected void setUp() throws Exception
     {
         server = new TestServer();
-        if(!server.startServer("classpath:/WEB-INF/flex/services-config.xml")) 
{
+        serverPort = 
server.startServer("classpath:/WEB-INF/flex/services-config.xml");
+        if(serverPort == -1) {
             Assert.fail("Couldn't start server process");
         }
 
@@ -465,7 +471,7 @@ public class AMFDataTypeTest extends TestCase
     {
         AMFConnection amfConnection = new AMFConnection();
         // Connect.
-        amfConnection.connect(DEFAULT_URL);
+        amfConnection.connect(getConnectionUrl());
         // Make a remoting call and retrieve the result.
         Object result;
         if (methodArg == null)

http://git-wip-us.apache.org/repos/asf/flex-blazeds/blob/1cf3381a/modules/testsuite/src/test/java/flex/messaging/util/TestServer.java
----------------------------------------------------------------------
diff --git 
a/modules/testsuite/src/test/java/flex/messaging/util/TestServer.java 
b/modules/testsuite/src/test/java/flex/messaging/util/TestServer.java
index ec075bf..c9d1d12 100644
--- a/modules/testsuite/src/test/java/flex/messaging/util/TestServer.java
+++ b/modules/testsuite/src/test/java/flex/messaging/util/TestServer.java
@@ -17,11 +17,13 @@
 package flex.messaging.util;
 
 import flex.messaging.MessageBrokerServlet;
+
 import org.eclipse.jetty.server.Server;
 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
@@ -33,28 +35,32 @@ public class TestServer {
 
     private Process serverProcess;
 
-    public boolean startServer(String configPath) {
+    public int startServer(String configPath) {
         // We can only start one server per instance of TestServer.
         if(serverProcess != null) {
-            return false;
+            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 ProcessBuilder processBuilder = new ProcessBuilder(path, "-cp", 
classpath,
-                TestServer.class.getCanonicalName(), "\"" + configPath + "\"");
+        final int port = findFreePort();
+        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(500);
+            return port;
         } catch (IOException e) {
-            return false;
+            return -1;
         } catch (InterruptedException e) {
-            return false;
+            return -1;
         }
-        return true;
     }
 
     public void stopServer() {
@@ -65,13 +71,15 @@ public class TestServer {
     }
 
     public static void main(String args[]) throws Exception {
-        if(args.length != 1) {
-            throw new Exception("Need exactly one argument containing th path 
to the configuration");
+        if(args.length != 2) {
+            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 Server server = new Server(8400);
+        final int port = Integer.valueOf(args[1]);
+        final Server server = new Server(port);
         final ServletContextHandler context = new 
ServletContextHandler(ServletContextHandler.SESSIONS);
         context.setContextPath("/qa-regress");
         final MessageBrokerServlet messageBrokerServlet = new 
MessageBrokerServlet();
@@ -93,4 +101,28 @@ public class TestServer {
         }
     }
 
+    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");
+    }
+
 }

Reply via email to