Author: matthieu
Date: Fri Dec 11 10:04:13 2015
New Revision: 1719299

URL: http://svn.apache.org/viewvc?rev=1719299&view=rev
Log:
JAMES-1639 Test random port

Modified:
    
james/project/trunk/server/container/jetty/src/main/java/org/apache/james/http/jetty/JettyHttpServer.java
    
james/project/trunk/server/container/jetty/src/test/java/org/apache/james/http/jetty/JettyHttpServerTest.java

Modified: 
james/project/trunk/server/container/jetty/src/main/java/org/apache/james/http/jetty/JettyHttpServer.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/container/jetty/src/main/java/org/apache/james/http/jetty/JettyHttpServer.java?rev=1719299&r1=1719298&r2=1719299&view=diff
==============================================================================
--- 
james/project/trunk/server/container/jetty/src/main/java/org/apache/james/http/jetty/JettyHttpServer.java
 (original)
+++ 
james/project/trunk/server/container/jetty/src/main/java/org/apache/james/http/jetty/JettyHttpServer.java
 Fri Dec 11 10:04:13 2015
@@ -19,13 +19,18 @@
 
 package org.apache.james.http.jetty;
 
+import java.io.Closeable;
+
 import org.eclipse.jetty.server.Server;
 import org.eclipse.jetty.server.ServerConnector;
 import org.eclipse.jetty.servlet.ServletHandler;
 import org.eclipse.jetty.servlet.ServletHolder;
 
-public class JettyHttpServer {
+import com.google.common.base.Throwables;
+
+public class JettyHttpServer implements Closeable {
     
+    @SuppressWarnings("resource")
     public static JettyHttpServer start(Configuration configuration) throws 
Exception {
         return new JettyHttpServer(configuration).start();
     }
@@ -59,5 +64,15 @@ public class JettyHttpServer {
     public int getPort() {
         return serverConnector.getLocalPort();
     }
+
+    
+    @Override
+    public void close() {
+        try {
+            stop();
+        } catch (Exception e) {
+            Throwables.propagate(e);
+        }
+    }
     
 }

Modified: 
james/project/trunk/server/container/jetty/src/test/java/org/apache/james/http/jetty/JettyHttpServerTest.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/container/jetty/src/test/java/org/apache/james/http/jetty/JettyHttpServerTest.java?rev=1719299&r1=1719298&r2=1719299&view=diff
==============================================================================
--- 
james/project/trunk/server/container/jetty/src/test/java/org/apache/james/http/jetty/JettyHttpServerTest.java
 (original)
+++ 
james/project/trunk/server/container/jetty/src/test/java/org/apache/james/http/jetty/JettyHttpServerTest.java
 Fri Dec 11 10:04:13 2015
@@ -20,6 +20,7 @@
 package org.apache.james.http.jetty;
 
 import static com.jayway.restassured.RestAssured.when;
+import static org.assertj.core.api.Assertions.assertThat;
 
 import java.io.IOException;
 
@@ -33,6 +34,7 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
+import com.google.common.io.Closeables;
 import com.jayway.restassured.RestAssured;
 
 public class JettyHttpServerTest {
@@ -53,19 +55,28 @@ public class JettyHttpServerTest {
     }
     
     private JettyHttpServer testee;
+    private Configuration.Builder configurationBuilder;
     
     @Before
     public void setup() {
+        configurationBuilder = Configuration.builder().randomPort();
     }
     
     @After
     public void teardown() throws Exception {
-        testee.stop();
+        Closeables.close(testee, false);
     }
     
     @Test
+    public void shouldStartOnRandomPort() throws Exception {
+        try (JettyHttpServer first = 
JettyHttpServer.start(configurationBuilder.build());
+             JettyHttpServer second = 
JettyHttpServer.start(configurationBuilder.build())) {
+            assertThat(first.getPort()).isNotEqualTo(second.getPort());
+        }
+    }
+    @Test
     public void shouldReturn404WhenNoServletConfigured() throws Exception {
-        testee = JettyHttpServer.start(Configuration.defaultConfiguration());
+        testee = JettyHttpServer.start(configurationBuilder.build());
         RestAssured.port = testee.getPort();
         when()
             .get("/")
@@ -78,8 +89,7 @@ public class JettyHttpServerTest {
     public void 
shouldLetConfiguredServletHandleIncomingRequestWhenServletConfigured() throws 
Exception {
         ServletMethod getHandler = (req, resp) -> 
resp.getWriter().append("served").close();
         
-        testee = JettyHttpServer.start(Configuration
-                                        .builder()
+        testee = JettyHttpServer.start(configurationBuilder
                                         .serve("/")
                                         .with(get(getHandler)).build());
         
@@ -98,8 +108,7 @@ public class JettyHttpServerTest {
         ServletMethod fooGetHandler = (req, resp) -> 
resp.getWriter().append("served").close();
         ServletMethod barGetMethod = (req, resp) -> resp.sendError(400, 
"should not be called");
         
-        testee = JettyHttpServer.start(Configuration
-                                        .builder()
+        testee = JettyHttpServer.start(configurationBuilder
                                         .serve("/foo")
                                         .with(get(fooGetHandler))
                                         .serve("/bar")



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to