You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shiro.apache.org by jb...@apache.org on 2012/01/17 17:09:45 UTC

svn commit: r1232454 - in /shiro/trunk/samples/web/src/test/java/org/apache/shiro/test: AbstractContainerTest.java ContainerIntegrationTest.java

Author: jbunting
Date: Tue Jan 17 16:09:45 2012
New Revision: 1232454

URL: http://svn.apache.org/viewvc?rev=1232454&view=rev
Log:
SHIRO-338: Modified AbstractContainerTest to attempt to select an unused port for the container to listen on.


Modified:
    shiro/trunk/samples/web/src/test/java/org/apache/shiro/test/AbstractContainerTest.java
    shiro/trunk/samples/web/src/test/java/org/apache/shiro/test/ContainerIntegrationTest.java

Modified: shiro/trunk/samples/web/src/test/java/org/apache/shiro/test/AbstractContainerTest.java
URL: http://svn.apache.org/viewvc/shiro/trunk/samples/web/src/test/java/org/apache/shiro/test/AbstractContainerTest.java?rev=1232454&r1=1232453&r2=1232454&view=diff
==============================================================================
--- shiro/trunk/samples/web/src/test/java/org/apache/shiro/test/AbstractContainerTest.java (original)
+++ shiro/trunk/samples/web/src/test/java/org/apache/shiro/test/AbstractContainerTest.java Tue Jan 17 16:09:45 2012
@@ -27,26 +27,42 @@ import org.mortbay.jetty.Server;
 import org.mortbay.jetty.nio.SelectChannelConnector;
 import org.mortbay.jetty.webapp.WebAppContext;
 
+import java.net.BindException;
+
 public abstract class AbstractContainerTest {
-    protected static PauseableServer server;
+    public static final int MAX_PORT = 9200;
 
-    protected static final int port = 9180;
+    protected static PauseableServer server;
 
-    protected static final String BASEURI = "http://localhost:" + port + "/";
+    private static int port = 9180;
 
     protected final WebClient webClient = new WebClient();
 
     @BeforeClass
     public static void startContainer() throws Exception {
-        if (server == null) {
-            server = new PauseableServer();
-            Connector connector = new SelectChannelConnector();
-            connector.setPort(port);
-            server.setConnectors(new Connector[]{connector});
-            server.setHandler(new WebAppContext("src/main/webapp", "/"));
-            server.start();
-            assertTrue(server.isStarted());
+        while (server == null && port < MAX_PORT) {
+            try {
+                server = createAndStartServer(port);
+            } catch (BindException e) {
+                System.err.printf("Unable to listen on port %d.  Trying next port.", port);
+                port++;
+            }
         }
+        assertTrue(server.isStarted());
+    }
+
+    private static PauseableServer createAndStartServer(final int port) throws Exception {
+        PauseableServer server = new PauseableServer();
+        Connector connector = new SelectChannelConnector();
+        connector.setPort(port);
+        server.setConnectors(new Connector[]{connector});
+        server.setHandler(new WebAppContext("src/main/webapp", "/"));
+        server.start();
+        return server;
+    }
+
+    protected static String getBaseUri() {
+        return "http://localhost:" + port + "/";
     }
 
     @Before

Modified: shiro/trunk/samples/web/src/test/java/org/apache/shiro/test/ContainerIntegrationTest.java
URL: http://svn.apache.org/viewvc/shiro/trunk/samples/web/src/test/java/org/apache/shiro/test/ContainerIntegrationTest.java?rev=1232454&r1=1232453&r2=1232454&view=diff
==============================================================================
--- shiro/trunk/samples/web/src/test/java/org/apache/shiro/test/ContainerIntegrationTest.java (original)
+++ shiro/trunk/samples/web/src/test/java/org/apache/shiro/test/ContainerIntegrationTest.java Tue Jan 17 16:09:45 2012
@@ -36,7 +36,7 @@ public class ContainerIntegrationTest ex
     @Before
     public void logOut() throws IOException {
         // Make sure we are logged out
-        final HtmlPage homePage = webClient.getPage(BASEURI);
+        final HtmlPage homePage = webClient.getPage(getBaseUri());
         try {
             homePage.getAnchorByHref("/logout").click();
         }
@@ -48,7 +48,7 @@ public class ContainerIntegrationTest ex
     @Test
     public void logIn() throws FailingHttpStatusCodeException, MalformedURLException, IOException, InterruptedException {
 
-        HtmlPage page = webClient.getPage(BASEURI + "login.jsp");
+        HtmlPage page = webClient.getPage(getBaseUri() + "login.jsp");
         HtmlForm form = page.getFormByName("loginform");
         form.<HtmlInput>getInputByName("username").setValueAttribute("root");
         form.<HtmlInput>getInputByName("password").setValueAttribute("secret");
@@ -59,7 +59,7 @@ public class ContainerIntegrationTest ex
 
     @Test
     public void logInAndRememberMe() throws Exception {
-        HtmlPage page = webClient.getPage(BASEURI + "login.jsp");
+        HtmlPage page = webClient.getPage(getBaseUri() + "login.jsp");
         HtmlForm form = page.getFormByName("loginform");
         form.<HtmlInput>getInputByName("username").setValueAttribute("root");
         form.<HtmlInput>getInputByName("password").setValueAttribute("secret");
@@ -68,7 +68,7 @@ public class ContainerIntegrationTest ex
         page = form.<HtmlInput>getInputByName("submit").click();
         server.stop();
         server.start();
-        page = webClient.getPage(BASEURI);
+        page = webClient.getPage(getBaseUri());
         // page.getAnchorByHref("/logout");
         WebAssert.assertLinkPresentWithText(page, "Log out");
         page = page.getAnchorByHref("/account").click();