Repository: james-project
Updated Branches:
  refs/heads/master 36b0ef6e8 -> 993f444fd


Run POP3 tests on a random port


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/1c733137
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/1c733137
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/1c733137

Branch: refs/heads/master
Commit: 1c73313774a63b50a9e3924c3e4c0838495a784f
Parents: 52316f6
Author: Antoine Duprat <[email protected]>
Authored: Thu Jun 16 14:28:03 2016 +0200
Committer: Antoine Duprat <[email protected]>
Committed: Tue Dec 20 16:48:50 2016 +0100

----------------------------------------------------------------------
 protocols/api/pom.xml                           |  4 +
 .../api/utils/ProtocolServerUtils.java          | 41 +++++++++
 .../protocols/netty/AbstractAsyncServer.java    | 13 +--
 .../protocols/pop3/AbstractPOP3SServerTest.java |  8 +-
 .../protocols/pop3/AbstractPOP3ServerTest.java  | 89 +++++++++-----------
 .../pop3/AbstractStartTlsPOP3ServerTest.java    | 11 ++-
 .../pop3/netty/NettyPOP3SServerTest.java        |  7 +-
 .../pop3/netty/NettyPOP3ServerTest.java         |  7 +-
 server/protocols/protocols-pop3/pom.xml         |  6 ++
 .../apache/james/pop3server/POP3ServerTest.java | 50 ++++++-----
 .../james/pop3server/POP3TestConfiguration.java |  8 +-
 11 files changed, 150 insertions(+), 94 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/1c733137/protocols/api/pom.xml
----------------------------------------------------------------------
diff --git a/protocols/api/pom.xml b/protocols/api/pom.xml
index 81013c5..39bfaad 100644
--- a/protocols/api/pom.xml
+++ b/protocols/api/pom.xml
@@ -35,6 +35,10 @@
 
     <dependencies>
         <dependency>
+            <groupId>com.google.guava</groupId>
+            <artifactId>guava</artifactId>
+        </dependency>
+        <dependency>
             <groupId>commons-configuration</groupId>
             <artifactId>commons-configuration</artifactId>
         </dependency>

http://git-wip-us.apache.org/repos/asf/james-project/blob/1c733137/protocols/api/src/test/java/org/apache/james/protocols/api/utils/ProtocolServerUtils.java
----------------------------------------------------------------------
diff --git 
a/protocols/api/src/test/java/org/apache/james/protocols/api/utils/ProtocolServerUtils.java
 
b/protocols/api/src/test/java/org/apache/james/protocols/api/utils/ProtocolServerUtils.java
new file mode 100644
index 0000000..2c3ff9a
--- /dev/null
+++ 
b/protocols/api/src/test/java/org/apache/james/protocols/api/utils/ProtocolServerUtils.java
@@ -0,0 +1,41 @@
+/****************************************************************
+ * 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.james.protocols.api.utils;
+
+import java.net.InetSocketAddress;
+import java.util.List;
+
+import org.apache.james.protocols.api.ProtocolServer;
+
+import com.google.common.base.Preconditions;
+
+public class ProtocolServerUtils {
+
+    private final ProtocolServer server;
+
+    public ProtocolServerUtils(ProtocolServer server) {
+        this.server = server;
+    }
+
+    public InetSocketAddress retrieveBindedAddress() {
+        List<InetSocketAddress> listenAddresses = server.getListenAddresses();
+        Preconditions.checkState(listenAddresses.size() == 1, "Unexpected 
number of binded addresses");
+        return listenAddresses.get(0);
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/1c733137/protocols/netty/src/main/java/org/apache/james/protocols/netty/AbstractAsyncServer.java
----------------------------------------------------------------------
diff --git 
a/protocols/netty/src/main/java/org/apache/james/protocols/netty/AbstractAsyncServer.java
 
b/protocols/netty/src/main/java/org/apache/james/protocols/netty/AbstractAsyncServer.java
index 256711e..50ea0be 100644
--- 
a/protocols/netty/src/main/java/org/apache/james/protocols/netty/AbstractAsyncServer.java
+++ 
b/protocols/netty/src/main/java/org/apache/james/protocols/netty/AbstractAsyncServer.java
@@ -28,6 +28,7 @@ import java.util.concurrent.Executors;
 
 import org.apache.james.protocols.api.ProtocolServer;
 import org.jboss.netty.bootstrap.ServerBootstrap;
+import org.jboss.netty.channel.Channel;
 import org.jboss.netty.channel.ChannelPipelineFactory;
 import org.jboss.netty.channel.group.ChannelGroup;
 import org.jboss.netty.channel.group.DefaultChannelGroup;
@@ -35,6 +36,8 @@ import 
org.jboss.netty.channel.socket.ServerSocketChannelFactory;
 import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
 import org.jboss.netty.util.ExternalResourceReleasable;
 
+import com.google.common.collect.ImmutableList;
+
 /**
  * Abstract base class for Servers which want to use async io
  *
@@ -135,17 +138,17 @@ public abstract class AbstractAsyncServer implements 
ProtocolServer{
         bootstrap.releaseExternalResources();
         started = false;
     }
-    
-    
-    
 
-    
     /*
      * (non-Javadoc)
      * @see org.apache.james.protocols.api.ProtocolServer#getListenAddresses()
      */
     public synchronized List<InetSocketAddress> getListenAddresses() {
-        return addresses;
+        ImmutableList.Builder<InetSocketAddress> builder = 
ImmutableList.builder();
+        for (Channel channel : ImmutableList.copyOf(channels.iterator())) {
+            builder.add((InetSocketAddress) channel.getLocalAddress());
+        }
+        return builder.build();
     }
     
     

http://git-wip-us.apache.org/repos/asf/james-project/blob/1c733137/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/AbstractPOP3SServerTest.java
----------------------------------------------------------------------
diff --git 
a/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/AbstractPOP3SServerTest.java
 
b/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/AbstractPOP3SServerTest.java
index 7ea9bec..6d7984c 100644
--- 
a/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/AbstractPOP3SServerTest.java
+++ 
b/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/AbstractPOP3SServerTest.java
@@ -18,8 +18,6 @@
  ****************************************************************/
 package org.apache.james.protocols.pop3;
 
-import java.net.InetSocketAddress;
-
 import org.apache.commons.net.pop3.POP3Client;
 import org.apache.commons.net.pop3.POP3SClient;
 import org.apache.james.protocols.api.Encryption;
@@ -39,10 +37,10 @@ public abstract class AbstractPOP3SServerTest extends 
AbstractPOP3ServerTest {
     }
     
     @Override
-    protected ProtocolServer createServer(Protocol protocol, InetSocketAddress 
address) {
-        return createEncryptedServer(protocol, 
address,Encryption.createTls(BogusSslContextFactory.getServerContext()));
+    protected ProtocolServer createServer(Protocol protocol) {
+        return createEncryptedServer(protocol, 
Encryption.createTls(BogusSslContextFactory.getServerContext()));
     }
     
-    protected abstract ProtocolServer createEncryptedServer(Protocol protocol, 
InetSocketAddress address, Encryption enc);
+    protected abstract ProtocolServer createEncryptedServer(Protocol protocol, 
Encryption enc);
 
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/1c733137/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/AbstractPOP3ServerTest.java
----------------------------------------------------------------------
diff --git 
a/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/AbstractPOP3ServerTest.java
 
b/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/AbstractPOP3ServerTest.java
index 6eb3b85..edbf803 100644
--- 
a/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/AbstractPOP3ServerTest.java
+++ 
b/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/AbstractPOP3ServerTest.java
@@ -18,7 +18,7 @@
  ****************************************************************/
 package org.apache.james.protocols.pop3;
 
-import static org.assertj.core.api.Assertions.*;
+import static org.assertj.core.api.Assertions.assertThat;
 
 import java.io.BufferedReader;
 import java.io.IOException;
@@ -35,7 +35,7 @@ import org.apache.james.protocols.api.Protocol;
 import org.apache.james.protocols.api.ProtocolServer;
 import org.apache.james.protocols.api.handler.WiringException;
 import org.apache.james.protocols.api.utils.MockLogger;
-import org.apache.james.protocols.api.utils.TestUtils;
+import org.apache.james.protocols.api.utils.ProtocolServerUtils;
 import org.apache.james.protocols.pop3.core.AbstractApopCmdHandler;
 import org.apache.james.protocols.pop3.core.AbstractPassCmdHandler;
 import org.apache.james.protocols.pop3.mailbox.Mailbox;
@@ -53,7 +53,7 @@ public abstract class AbstractPOP3ServerTest {
         return new POP3Protocol(new POP3ProtocolHandlerChain(handler), new 
POP3Configuration(), new MockLogger());
     }
     
-    protected abstract ProtocolServer createServer(Protocol protocol, 
InetSocketAddress address);
+    protected abstract ProtocolServer createServer(Protocol protocol);
     
     protected POP3Client createClient() {
         return new POP3Client();
@@ -61,15 +61,14 @@ public abstract class AbstractPOP3ServerTest {
     
     @Test
     public void testInvalidAuth() throws Exception {
-        InetSocketAddress address = new InetSocketAddress("127.0.0.1", 
TestUtils.getFreePort());
-        
         ProtocolServer server = null;
         try {
-            server = createServer(createProtocol(new TestPassCmdHandler()), 
address);
+            server = createServer(createProtocol(new TestPassCmdHandler()));
             server.bind();
             
             POP3Client client =  createClient();
-            client.connect(address.getAddress().getHostAddress(), 
address.getPort());
+            InetSocketAddress bindedAddress = new 
ProtocolServerUtils(server).retrieveBindedAddress();
+            client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
             
             assertThat(client.login("invalid", "invalid")).isFalse();
             assertThat(client.logout()).isTrue();
@@ -81,22 +80,21 @@ public abstract class AbstractPOP3ServerTest {
         }
         
     }
-    
+
     @Test
     public void testEmptyInbox() throws Exception {
-        InetSocketAddress address = new InetSocketAddress("127.0.0.1", 
TestUtils.getFreePort());
-        
         ProtocolServer server = null;
         try {
             String identifier = "id";
             TestPassCmdHandler handler = new TestPassCmdHandler();
             
             handler.add("valid", new MockMailbox(identifier));
-            server = createServer(createProtocol(handler), address);
+            server = createServer(createProtocol(handler));
             server.bind();
             
             POP3Client client =  createClient();
-            client.connect(address.getAddress().getHostAddress(), 
address.getPort());
+            InetSocketAddress bindedAddress = new 
ProtocolServerUtils(server).retrieveBindedAddress();
+            client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
             
             assertThat(client.login("valid", "valid")).isTrue();
             POP3MessageInfo[] info = client.listMessages();
@@ -116,19 +114,18 @@ public abstract class AbstractPOP3ServerTest {
     
     @Test
     public void testInboxWithMessages() throws Exception {
-        InetSocketAddress address = new InetSocketAddress("127.0.0.1", 
TestUtils.getFreePort());
-        
         ProtocolServer server = null;
         try {
             String identifier = "id";
             TestPassCmdHandler handler = new TestPassCmdHandler();
             
             handler.add("valid", new MockMailbox(identifier, MESSAGE1, 
MESSAGE2));
-            server = createServer(createProtocol(handler), address);
+            server = createServer(createProtocol(handler));
             server.bind();
             
             POP3Client client =  createClient();
-            client.connect(address.getAddress().getHostAddress(), 
address.getPort());
+            InetSocketAddress bindedAddress = new 
ProtocolServerUtils(server).retrieveBindedAddress();
+            client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
             
             assertThat(client.login("valid", "valid")).isTrue();
             POP3MessageInfo[] info = client.listMessages();
@@ -173,19 +170,18 @@ public abstract class AbstractPOP3ServerTest {
     
     @Test
     public void testRetr() throws Exception {
-        InetSocketAddress address = new InetSocketAddress("127.0.0.1", 
TestUtils.getFreePort());
-        
         ProtocolServer server = null;
         try {
             String identifier = "id";
             TestPassCmdHandler factory = new TestPassCmdHandler();
             
             factory.add("valid", new MockMailbox(identifier, MESSAGE1, 
MESSAGE2));
-            server = createServer(createProtocol(factory), address);
+            server = createServer(createProtocol(factory));
             server.bind();
             
             POP3Client client =  createClient();
-            client.connect(address.getAddress().getHostAddress(), 
address.getPort());
+            InetSocketAddress bindedAddress = new 
ProtocolServerUtils(server).retrieveBindedAddress();
+            client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
             
             assertThat(client.login("valid", "valid")).isTrue();
             Reader reader = client.retrieveMessage(1);
@@ -216,19 +212,18 @@ public abstract class AbstractPOP3ServerTest {
     
     @Test
     public void testTop() throws Exception {
-        InetSocketAddress address = new InetSocketAddress("127.0.0.1", 
TestUtils.getFreePort());
-        
         ProtocolServer server = null;
         try {
             String identifier = "id";
             TestPassCmdHandler factory = new TestPassCmdHandler();
             
             factory.add("valid", new MockMailbox(identifier, MESSAGE1, 
MESSAGE2));
-            server = createServer(createProtocol(factory), address);
+            server = createServer(createProtocol(factory));
             server.bind();
             
             POP3Client client =  createClient();
-            client.connect(address.getAddress().getHostAddress(), 
address.getPort());
+            InetSocketAddress bindedAddress = new 
ProtocolServerUtils(server).retrieveBindedAddress();
+            client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
             
             assertThat(client.login("valid", "valid")).isTrue();
             Reader reader = client.retrieveMessageTop(1, 1000);
@@ -262,19 +257,18 @@ public abstract class AbstractPOP3ServerTest {
     
     @Test
     public void testDele() throws Exception {
-        InetSocketAddress address = new InetSocketAddress("127.0.0.1", 
TestUtils.getFreePort());
-        
         ProtocolServer server = null;
         try {
             String identifier = "id";
             TestPassCmdHandler factory = new TestPassCmdHandler();
             
             factory.add("valid", new MockMailbox(identifier, MESSAGE1, 
MESSAGE2));
-            server = createServer(createProtocol(factory), address);
+            server = createServer(createProtocol(factory));
             server.bind();
             
             POP3Client client =  createClient();
-            client.connect(address.getAddress().getHostAddress(), 
address.getPort());
+            InetSocketAddress bindedAddress = new 
ProtocolServerUtils(server).retrieveBindedAddress();
+            client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
             
             assertThat(client.login("valid", "valid")).isTrue();
             POP3MessageInfo[] info = client.listMessages();
@@ -297,7 +291,7 @@ public abstract class AbstractPOP3ServerTest {
             // logout so the messages get expunged
             assertThat(client.logout()).isTrue();
 
-            client.connect(address.getAddress().getHostAddress(), 
address.getPort());
+            client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
   
             assertThat(client.login("valid", "valid")).isTrue();
             info = client.listMessages();
@@ -315,19 +309,18 @@ public abstract class AbstractPOP3ServerTest {
     
     @Test
     public void testNoop() throws Exception {
-        InetSocketAddress address = new InetSocketAddress("127.0.0.1", 
TestUtils.getFreePort());
-        
         ProtocolServer server = null;
         try {
             String identifier = "id";
             TestPassCmdHandler factory = new TestPassCmdHandler();
             
             factory.add("valid", new MockMailbox(identifier));
-            server = createServer(createProtocol(factory), address);
+            server = createServer(createProtocol(factory));
             server.bind();
             
             POP3Client client =  createClient();
-            client.connect(address.getAddress().getHostAddress(), 
address.getPort());
+            InetSocketAddress bindedAddress = new 
ProtocolServerUtils(server).retrieveBindedAddress();
+            client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
             
             assertThat(client.login("valid", "valid")).isTrue();
             assertThat(client.noop()).isTrue();
@@ -343,19 +336,18 @@ public abstract class AbstractPOP3ServerTest {
     
     @Test
     public void testRset() throws Exception {
-        InetSocketAddress address = new InetSocketAddress("127.0.0.1", 
TestUtils.getFreePort());
-        
         ProtocolServer server = null;
         try {
             String identifier = "id";
             TestPassCmdHandler factory = new TestPassCmdHandler();
             
             factory.add("valid", new MockMailbox(identifier, MESSAGE1));
-            server = createServer(createProtocol(factory), address);
+            server = createServer(createProtocol(factory));
             server.bind();
             
             POP3Client client =  createClient();
-            client.connect(address.getAddress().getHostAddress(), 
address.getPort());
+            InetSocketAddress bindedAddress = new 
ProtocolServerUtils(server).retrieveBindedAddress();
+            client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
             
             assertThat(client.login("valid", "valid")).isTrue();
             assertThat(client.listMessages().length).isEqualTo(1);
@@ -378,19 +370,18 @@ public abstract class AbstractPOP3ServerTest {
     
     @Test
     public void testStat() throws Exception {
-        InetSocketAddress address = new InetSocketAddress("127.0.0.1", 
TestUtils.getFreePort());
-        
         ProtocolServer server = null;
         try {
             String identifier = "id";
             TestPassCmdHandler factory = new TestPassCmdHandler();
             
             factory.add("valid", new MockMailbox(identifier, MESSAGE1, 
MESSAGE2));
-            server = createServer(createProtocol(factory), address);
+            server = createServer(createProtocol(factory));
             server.bind();
             
             POP3Client client =  createClient();
-            client.connect(address.getAddress().getHostAddress(), 
address.getPort());
+            InetSocketAddress bindedAddress = new 
ProtocolServerUtils(server).retrieveBindedAddress();
+            client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
             
             assertThat(client.login("valid", "valid")).isTrue();
             POP3MessageInfo info = client.status();
@@ -407,20 +398,19 @@ public abstract class AbstractPOP3ServerTest {
     }
     @Test
     public void testDifferentStates() throws Exception {
-        InetSocketAddress address = new InetSocketAddress("127.0.0.1", 
TestUtils.getFreePort());
-        
         ProtocolServer server = null;
         try {
             String identifier = "id";
             TestPassCmdHandler factory = new TestPassCmdHandler();
             
             factory.add("valid", new MockMailbox(identifier, MESSAGE1, 
MESSAGE2));
-            server = createServer(createProtocol(factory), address);
+            server = createServer(createProtocol(factory));
             server.bind();
             
             POP3Client client =  createClient();
             
-            client.connect(address.getAddress().getHostAddress(), 
address.getPort());
+            InetSocketAddress bindedAddress = new 
ProtocolServerUtils(server).retrieveBindedAddress();
+            client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
             assertThat(client.listMessages()).isNull();
             assertThat(client.listUniqueIdentifiers()).isNull();
             assertThat(client.deleteMessage(1)).isFalse();
@@ -430,7 +420,7 @@ public abstract class AbstractPOP3ServerTest {
             assertThat(client.reset()).isFalse();
             client.logout();
             
-            client.connect(address.getAddress().getHostAddress(), 
address.getPort());
+            client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
 
             assertThat(client.login("valid", "valid")).isTrue();
             assertThat(client.listMessages()).isNotNull();
@@ -458,16 +448,15 @@ public abstract class AbstractPOP3ServerTest {
     
     @Test
     public void testAPop() throws Exception {
-        InetSocketAddress address = new InetSocketAddress("127.0.0.1", 
TestUtils.getFreePort());
-        
         ProtocolServer server = null;
         try {
             TestApopCmdHandler handler = new TestApopCmdHandler();
-            server = createServer(createProtocol(handler), address);
+            server = createServer(createProtocol(handler));
             server.bind();
             
             POP3Client client =  createClient();
-            client.connect(address.getAddress().getHostAddress(), 
address.getPort());
+            InetSocketAddress bindedAddress = new 
ProtocolServerUtils(server).retrieveBindedAddress();
+            client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
             String welcomeMessage = client.getReplyString();
             
             // check for valid syntax that include all info needed for APOP

http://git-wip-us.apache.org/repos/asf/james-project/blob/1c733137/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/AbstractStartTlsPOP3ServerTest.java
----------------------------------------------------------------------
diff --git 
a/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/AbstractStartTlsPOP3ServerTest.java
 
b/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/AbstractStartTlsPOP3ServerTest.java
index 81d2229..e3fe5d3 100644
--- 
a/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/AbstractStartTlsPOP3ServerTest.java
+++ 
b/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/AbstractStartTlsPOP3ServerTest.java
@@ -32,7 +32,7 @@ import org.apache.james.protocols.api.handler.WiringException;
 import org.apache.james.protocols.api.utils.BogusSslContextFactory;
 import org.apache.james.protocols.api.utils.BogusTrustManagerFactory;
 import org.apache.james.protocols.api.utils.MockLogger;
-import org.apache.james.protocols.api.utils.TestUtils;
+import org.apache.james.protocols.api.utils.ProtocolServerUtils;
 import org.apache.james.protocols.pop3.core.AbstractPassCmdHandler;
 import org.apache.james.protocols.pop3.utils.MockMailbox;
 import org.apache.james.protocols.pop3.utils.TestPassCmdHandler;
@@ -40,6 +40,9 @@ import org.junit.Test;
 
 public abstract class AbstractStartTlsPOP3ServerTest {
 
+    private static final String LOCALHOST_IP = "127.0.0.1";
+    private static final int RANDOM_PORT = 0;
+
     private POP3Protocol createProtocol(AbstractPassCmdHandler handler) throws 
WiringException {
         return new POP3Protocol(new POP3ProtocolHandlerChain(handler), new 
POP3Configuration(), new MockLogger());
     }
@@ -55,7 +58,7 @@ public abstract class AbstractStartTlsPOP3ServerTest {
     
     @Test
     public void testStartTls() throws Exception {
-        InetSocketAddress address = new InetSocketAddress("127.0.0.1", 
TestUtils.getFreePort());
+        InetSocketAddress address = new InetSocketAddress(LOCALHOST_IP, 
RANDOM_PORT);
         
         ProtocolServer server = null;
         try {
@@ -67,7 +70,8 @@ public abstract class AbstractStartTlsPOP3ServerTest {
             server.bind();
             
             POP3SClient client = createClient();
-            client.connect(address.getAddress().getHostAddress(), 
address.getPort());
+            InetSocketAddress bindedAddress = new 
ProtocolServerUtils(server).retrieveBindedAddress();
+            client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
             
             // TODO: Make use of client.capa() once possible
             //       See NET-438
@@ -97,5 +101,4 @@ public abstract class AbstractStartTlsPOP3ServerTest {
         }
         
     }
-
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/1c733137/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/netty/NettyPOP3SServerTest.java
----------------------------------------------------------------------
diff --git 
a/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/netty/NettyPOP3SServerTest.java
 
b/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/netty/NettyPOP3SServerTest.java
index c1ac359..b3bf155 100644
--- 
a/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/netty/NettyPOP3SServerTest.java
+++ 
b/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/netty/NettyPOP3SServerTest.java
@@ -28,13 +28,16 @@ import 
org.apache.james.protocols.pop3.AbstractPOP3SServerTest;
 
 public class NettyPOP3SServerTest extends AbstractPOP3SServerTest {
 
+    private static final String LOCALHOST_IP = "127.0.0.1";
+    private static final int RANDOM_PORT = 0;
+
     @Override
-    protected ProtocolServer createEncryptedServer(Protocol protocol, 
InetSocketAddress address, Encryption enc) {
+    protected ProtocolServer createEncryptedServer(Protocol protocol, 
Encryption enc) {
         NettyServer server = NettyServer.builder()
                 .protocol(protocol)
                 .secure(enc)
                 .build();
-        server.setListenAddresses(address);
+        server.setListenAddresses(new InetSocketAddress(LOCALHOST_IP, 
RANDOM_PORT));
         return server;
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/1c733137/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/netty/NettyPOP3ServerTest.java
----------------------------------------------------------------------
diff --git 
a/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/netty/NettyPOP3ServerTest.java
 
b/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/netty/NettyPOP3ServerTest.java
index 8bf701d..39d88e2 100644
--- 
a/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/netty/NettyPOP3ServerTest.java
+++ 
b/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/netty/NettyPOP3ServerTest.java
@@ -27,12 +27,15 @@ import 
org.apache.james.protocols.pop3.AbstractPOP3ServerTest;
 
 public class NettyPOP3ServerTest extends AbstractPOP3ServerTest{
 
+    private static final String LOCALHOST_IP = "127.0.0.1";
+    private static final int RANDOM_PORT = 0;
+
     @Override
-    protected ProtocolServer createServer(Protocol protocol, InetSocketAddress 
address) {
+    protected ProtocolServer createServer(Protocol protocol) {
         NettyServer server =  NettyServer.builder()
                 .protocol(protocol)
                 .build();
-        server.setListenAddresses(address);
+        server.setListenAddresses(new InetSocketAddress(LOCALHOST_IP, 
RANDOM_PORT));
         return server;
     }
     

http://git-wip-us.apache.org/repos/asf/james-project/blob/1c733137/server/protocols/protocols-pop3/pom.xml
----------------------------------------------------------------------
diff --git a/server/protocols/protocols-pop3/pom.xml 
b/server/protocols/protocols-pop3/pom.xml
index f7163d3..85b8ae0 100644
--- a/server/protocols/protocols-pop3/pom.xml
+++ b/server/protocols/protocols-pop3/pom.xml
@@ -47,6 +47,12 @@
         </dependency>
         <dependency>
             <groupId>org.apache.james.protocols</groupId>
+            <artifactId>protocols-api</artifactId>
+            <scope>test</scope>
+            <type>test-jar</type>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.james.protocols</groupId>
             <artifactId>protocols-pop3</artifactId>
         </dependency>
         <dependency>

http://git-wip-us.apache.org/repos/asf/james-project/blob/1c733137/server/protocols/protocols-pop3/src/test/java/org/apache/james/pop3server/POP3ServerTest.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/protocols-pop3/src/test/java/org/apache/james/pop3server/POP3ServerTest.java
 
b/server/protocols/protocols-pop3/src/test/java/org/apache/james/pop3server/POP3ServerTest.java
index 730237e..233e652 100644
--- 
a/server/protocols/protocols-pop3/src/test/java/org/apache/james/pop3server/POP3ServerTest.java
+++ 
b/server/protocols/protocols-pop3/src/test/java/org/apache/james/pop3server/POP3ServerTest.java
@@ -27,6 +27,7 @@ import static org.junit.Assert.assertTrue;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.Reader;
+import java.net.InetSocketAddress;
 import java.util.Arrays;
 import java.util.Date;
 import java.util.List;
@@ -54,8 +55,8 @@ import org.apache.james.mailbox.store.StoreMailboxManager;
 import org.apache.james.mailbox.store.mail.model.DefaultMessageId;
 import org.apache.james.mailbox.store.mail.model.impl.MessageParser;
 import org.apache.james.pop3server.netty.POP3Server;
+import org.apache.james.protocols.api.utils.ProtocolServerUtils;
 import org.apache.james.protocols.lib.POP3BeforeSMTPHelper;
-import org.apache.james.protocols.lib.PortUtil;
 import org.apache.james.protocols.lib.mock.MockProtocolHandlerLoader;
 import org.apache.james.user.api.UsersRepository;
 import org.apache.james.user.api.UsersRepositoryException;
@@ -69,7 +70,6 @@ import org.slf4j.LoggerFactory;
 
 public class POP3ServerTest {
 
-    private final int pop3Port = PortUtil.getNonPrivilegedPort();
     private POP3TestConfiguration pop3Configuration;
     private final InMemoryUsersRepository usersRepository = new 
InMemoryUsersRepository();
     private POP3Client pop3Client = null;
@@ -86,7 +86,7 @@ public class POP3ServerTest {
     public void setUp() throws Exception {
         setUpServiceManager();
         setUpPOP3Server();
-        pop3Configuration = new POP3TestConfiguration(pop3Port);
+        pop3Configuration = new POP3TestConfiguration();
     }
 
     @After
@@ -110,7 +110,8 @@ public class POP3ServerTest {
         finishSetUp(pop3Configuration);
 
         pop3Client = new POP3Client();
-        pop3Client.connect("127.0.0.1", pop3Port);
+        InetSocketAddress bindedAddress = new 
ProtocolServerUtils(pop3Server).retrieveBindedAddress();
+        pop3Client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
 
         usersRepository.addUser("known", "test2");
 
@@ -124,7 +125,8 @@ public class POP3ServerTest {
         finishSetUp(pop3Configuration);
 
         pop3Client = new POP3Client();
-        pop3Client.connect("127.0.0.1", pop3Port);
+        InetSocketAddress bindedAddress = new 
ProtocolServerUtils(pop3Server).retrieveBindedAddress();
+        pop3Client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
 
         pop3Client.login("unknown", "test");
         assertEquals(0, pop3Client.getState());
@@ -136,7 +138,8 @@ public class POP3ServerTest {
         finishSetUp(pop3Configuration);
 
         pop3Client = new POP3Client();
-        pop3Client.connect("127.0.0.1", pop3Port);
+        InetSocketAddress bindedAddress = new 
ProtocolServerUtils(pop3Server).retrieveBindedAddress();
+        pop3Client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
 
         usersRepository.addUser("foo", "bar");
 
@@ -184,7 +187,8 @@ public class POP3ServerTest {
         finishSetUp(pop3Configuration);
 
         pop3Client = new POP3Client();
-        pop3Client.connect("127.0.0.1", pop3Port);
+        InetSocketAddress bindedAddress = new 
ProtocolServerUtils(pop3Server).retrieveBindedAddress();
+        pop3Client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
 
         pop3Client.sendCommand("unkn");
         assertEquals(0, pop3Client.getState());
@@ -199,7 +203,8 @@ public class POP3ServerTest {
         usersRepository.addUser("foo", "bar");
 
         pop3Client = new POP3Client();
-        pop3Client.connect("127.0.0.1", pop3Port);
+        InetSocketAddress bindedAddress = new 
ProtocolServerUtils(pop3Server).retrieveBindedAddress();
+        pop3Client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
 
         pop3Client.sendCommand("uidl");
         assertEquals(0, pop3Client.getState());
@@ -217,7 +222,7 @@ public class POP3ServerTest {
         }
         setupTestMails(session, mailboxManager.getMailbox(mailboxPath, 
session));
 
-        pop3Client.connect("127.0.0.1", pop3Port);
+        pop3Client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
         pop3Client.login("foo", "bar");
 
         list = pop3Client.listUniqueIdentifiers();
@@ -237,7 +242,8 @@ public class POP3ServerTest {
         usersRepository.addUser("foo", "bar");
 
         pop3Client = new POP3Client();
-        pop3Client.connect("127.0.0.1", pop3Port);
+        InetSocketAddress bindedAddress = new 
ProtocolServerUtils(pop3Server).retrieveBindedAddress();
+        pop3Client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
 
         pop3Client.sendCommand("noop");
         assertEquals(0, pop3Client.getState());
@@ -289,7 +295,8 @@ public class POP3ServerTest {
         finishSetUp(pop3Configuration);
 
         pop3Client = new POP3Client();
-        pop3Client.connect("127.0.0.1", pop3Port);
+        InetSocketAddress bindedAddress = new 
ProtocolServerUtils(pop3Server).retrieveBindedAddress();
+        pop3Client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
 
         usersRepository.addUser("foo2", "bar2");
 
@@ -342,7 +349,7 @@ public class POP3ServerTest {
         pop3Client.logout();
         //m_pop3Protocol.disconnect();
 
-        pop3Client.connect("127.0.0.1", pop3Port);
+        pop3Client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
 
         pop3Client.login("foo2", "bar2");
         assertEquals(1, pop3Client.getState());
@@ -377,7 +384,8 @@ public class POP3ServerTest {
         finishSetUp(pop3Configuration);
 
         pop3Client = new POP3Client();
-        pop3Client.connect("127.0.0.1", pop3Port);
+        InetSocketAddress bindedAddress = new 
ProtocolServerUtils(pop3Server).retrieveBindedAddress();
+        pop3Client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
 
         usersRepository.addUser("foo2", "bar2");
 
@@ -407,7 +415,7 @@ public class POP3ServerTest {
         pop3Client.sendCommand("quit");
         pop3Client.disconnect();
 
-        pop3Client.connect("127.0.0.1", pop3Port);
+        pop3Client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
 
         pop3Client.login("foo2", "bar2");
         assertEquals(1, pop3Client.getState());
@@ -426,7 +434,8 @@ public class POP3ServerTest {
         finishSetUp(pop3Configuration);
 
         pop3Client = new POP3Client();
-        pop3Client.connect("127.0.0.1", pop3Port);
+        InetSocketAddress bindedAddress = new 
ProtocolServerUtils(pop3Server).retrieveBindedAddress();
+        pop3Client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
 
         usersRepository.addUser("foo2", "bar2");
 
@@ -454,7 +463,7 @@ public class POP3ServerTest {
         assertEquals(msgCount, statInfo.number);
 
         POP3Client m_pop3Protocol2 = new POP3Client();
-        m_pop3Protocol2.connect("127.0.0.1", pop3Port);
+        m_pop3Protocol2.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
         m_pop3Protocol2.login("foo2", "bar2");
         assertEquals(1, m_pop3Protocol2.getState());
 
@@ -547,7 +556,8 @@ public class POP3ServerTest {
         finishSetUp(pop3Configuration);
 
         pop3Client = new POP3Client();
-        pop3Client.connect("127.0.0.1", pop3Port);
+        InetSocketAddress bindedAddress = new 
ProtocolServerUtils(pop3Server).retrieveBindedAddress();
+        pop3Client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
 
         String pass = "password";
         usersRepository.addUser("foo", pass);
@@ -563,7 +573,8 @@ public class POP3ServerTest {
         finishSetUp(pop3Configuration);
 
         pop3Client = new POP3Client();
-        pop3Client.connect("127.0.0.1", pop3Port);
+        InetSocketAddress bindedAddress = new 
ProtocolServerUtils(pop3Server).retrieveBindedAddress();
+        pop3Client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
 
         String pass = "password";
         usersRepository.addUser("foo", pass);
@@ -631,7 +642,8 @@ public class POP3ServerTest {
         finishSetUp(pop3Configuration);
 
         pop3Client = new POP3Client();
-        pop3Client.connect("127.0.0.1", pop3Port);
+        InetSocketAddress bindedAddress = new 
ProtocolServerUtils(pop3Server).retrieveBindedAddress();
+        pop3Client.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
 
         usersRepository.addUser("foo6", "bar6");
         MailboxSession session = mailboxManager.login("foo6", "bar6", 
LoggerFactory.getLogger("Test"));

http://git-wip-us.apache.org/repos/asf/james-project/blob/1c733137/server/protocols/protocols-pop3/src/test/java/org/apache/james/pop3server/POP3TestConfiguration.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/protocols-pop3/src/test/java/org/apache/james/pop3server/POP3TestConfiguration.java
 
b/server/protocols/protocols-pop3/src/test/java/org/apache/james/pop3server/POP3TestConfiguration.java
index 81a899a..64c9e39 100644
--- 
a/server/protocols/protocols-pop3/src/test/java/org/apache/james/pop3server/POP3TestConfiguration.java
+++ 
b/server/protocols/protocols-pop3/src/test/java/org/apache/james/pop3server/POP3TestConfiguration.java
@@ -25,15 +25,9 @@ import org.apache.james.pop3server.core.CoreCmdHandlerLoader;
 @SuppressWarnings("serial")
 public class POP3TestConfiguration extends DefaultConfigurationBuilder {
 
-    private final int pop3ListenerPort;
-
-    public POP3TestConfiguration(int pop3ListenerPort) {
-        this.pop3ListenerPort = pop3ListenerPort;
-    }
-
     public void init() {
         addProperty("[@enabled]", true);
-        addProperty("bind", "127.0.0.1:" + this.pop3ListenerPort);
+        addProperty("bind", "127.0.0.1:0");
         addProperty("helloName", "myMailServer");
         addProperty("connectiontimeout", "360000");
         addProperty("handlerchain.[@coreHandlersPackage]", 
CoreCmdHandlerLoader.class.getName());


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

Reply via email to