Author: veithen
Date: Sun Nov 23 13:15:47 2008
New Revision: 720044

URL: http://svn.apache.org/viewvc?rev=720044&view=rev
Log:
Testkit: added a port allocation mechanism.

Added:
    
webservices/commons/trunk/modules/transport/modules/testkit/src/main/java/org/apache/axis2/transport/testkit/util/PortAllocator.java
   (with props)
Modified:
    
webservices/commons/trunk/modules/transport/modules/tests/src/test/java/org/apache/axis2/transport/mail/GreenMailTestEnvironment.java

Added: 
webservices/commons/trunk/modules/transport/modules/testkit/src/main/java/org/apache/axis2/transport/testkit/util/PortAllocator.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/transport/modules/testkit/src/main/java/org/apache/axis2/transport/testkit/util/PortAllocator.java?rev=720044&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/transport/modules/testkit/src/main/java/org/apache/axis2/transport/testkit/util/PortAllocator.java
 (added)
+++ 
webservices/commons/trunk/modules/transport/modules/testkit/src/main/java/org/apache/axis2/transport/testkit/util/PortAllocator.java
 Sun Nov 23 13:15:47 2008
@@ -0,0 +1,59 @@
+/*
+ *  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 org.apache.axis2.transport.testkit.util;
+
+import org.apache.axis2.transport.testkit.tests.Setup;
+import org.apache.axis2.transport.testkit.tests.Transient;
+
+public class PortAllocator {
+    public static final PortAllocator INSTANCE = new PortAllocator();
+    
+    private static final int basePort = 9000;
+    
+    private @Transient boolean[] allocated;
+    
+    private PortAllocator() {
+    }
+    
+    @Setup @SuppressWarnings("unused")
+    private void setUp() {
+        allocated = new boolean[16];
+    }
+    
+    public int allocatePort() {
+        int len = allocated.length;
+        for (int i=0; i<len; i++) {
+            if (!allocated[i]) {
+                allocated[i] = true;
+                return basePort+i;
+            }
+        }
+        
+        boolean[] newAllocated = new boolean[len*2];
+        System.arraycopy(allocated, 0, newAllocated, 0, len);
+        newAllocated[len] = true;
+        allocated = newAllocated;
+        return basePort+len;
+    }
+    
+    public void releasePort(int port) {
+        allocated[port-basePort] = false;
+    }
+}

Propchange: 
webservices/commons/trunk/modules/transport/modules/testkit/src/main/java/org/apache/axis2/transport/testkit/util/PortAllocator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
webservices/commons/trunk/modules/transport/modules/tests/src/test/java/org/apache/axis2/transport/mail/GreenMailTestEnvironment.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/transport/modules/tests/src/test/java/org/apache/axis2/transport/mail/GreenMailTestEnvironment.java?rev=720044&r1=720043&r2=720044&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/transport/modules/tests/src/test/java/org/apache/axis2/transport/mail/GreenMailTestEnvironment.java
 (original)
+++ 
webservices/commons/trunk/modules/transport/modules/tests/src/test/java/org/apache/axis2/transport/mail/GreenMailTestEnvironment.java
 Sun Nov 23 13:15:47 2008
@@ -34,6 +34,7 @@
 import org.apache.axis2.transport.testkit.tests.TearDown;
 import org.apache.axis2.transport.testkit.tests.Transient;
 import org.apache.axis2.transport.testkit.util.LogManager;
+import org.apache.axis2.transport.testkit.util.PortAllocator;
 import org.apache.axis2.transport.testkit.util.ServerUtil;
 import org.apache.axis2.transport.testkit.util.tcpmon.Tunnel;
 
@@ -46,17 +47,10 @@
 
 @Name("greenmail")
 public class GreenMailTestEnvironment extends MailTestEnvironment {
-    private static final ServerSetup SMTP =
-            new ServerSetup(7025, "127.0.0.1", ServerSetup.PROTOCOL_SMTP);
-    
-    private static final ServerSetup POP3 =
-            new ServerSetup(7110, "127.0.0.1", ServerSetup.PROTOCOL_POP3);
-    
-    private static final ServerSetup IMAP =
-            new ServerSetup(7143, "127.0.0.1", ServerSetup.PROTOCOL_IMAP);
-    
     private final String protocol;
-    private final ServerSetup storeServerSetup;
+    private @Transient PortAllocator portAllocator;
+    private @Transient ServerSetup smtpServerSetup;
+    private @Transient ServerSetup storeServerSetup;
     private @Transient LogManager logManager;
     private @Transient GreenMail greenMail;
     private @Transient Tunnel smtpTunnel;
@@ -65,24 +59,20 @@
 
     public GreenMailTestEnvironment(String protocol) {
         this.protocol = protocol;
-        if (protocol.equals("pop3")) {
-            storeServerSetup = POP3;
-        } else if (protocol.equals("imap")) {
-            storeServerSetup = IMAP;
-        } else {
-            throw new IllegalArgumentException();
-        }
     }
 
     @Setup @SuppressWarnings("unused")
-    private void setUp(LogManager logManager) throws Exception {
+    private void setUp(LogManager logManager, PortAllocator portAllocator) 
throws Exception {
         this.logManager = logManager;
-        greenMail = new GreenMail(new ServerSetup[] { SMTP, storeServerSetup 
});
+        this.portAllocator = portAllocator;
+        smtpServerSetup = new ServerSetup(portAllocator.allocatePort(), 
"127.0.0.1", ServerSetup.PROTOCOL_SMTP);
+        storeServerSetup = new ServerSetup(portAllocator.allocatePort(), 
"127.0.0.1", protocol);
+        greenMail = new GreenMail(new ServerSetup[] { smtpServerSetup, 
storeServerSetup });
         greenMail.start();
-        smtpTunnel = new Tunnel(new InetSocketAddress("127.0.0.1", 7025));
+        smtpTunnel = new Tunnel(new InetSocketAddress("127.0.0.1", 
smtpServerSetup.getPort()));
         smtpTunnel.start();
         unallocatedAccounts = new LinkedList<Account>();
-        ServerUtil.waitForServer(SMTP.getPort());
+        ServerUtil.waitForServer(smtpServerSetup.getPort());
         ServerUtil.waitForServer(storeServerSetup.getPort());
     }
 
@@ -90,6 +80,8 @@
     private void tearDown() throws Exception {
         greenMail.stop();
         accountNumber = 1;
+        portAllocator.releasePort(smtpServerSetup.getPort());
+        portAllocator.releasePort(storeServerSetup.getPort());
     }
     
     @Override


Reply via email to