Make unittests more robust (no hardcoded port, special filename handling)

git-svn-id: https://svn.apache.org/repos/asf/geronimo/javamail/trunk@1642058 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/store/imap/AuthenticationTest.java b/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/store/imap/AuthenticationTest.java
index 77b0948..ff09eb8 100644
--- a/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/store/imap/AuthenticationTest.java
+++ b/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/store/imap/AuthenticationTest.java
@@ -33,6 +33,7 @@
 import junit.framework.TestCase;
 
 import org.apache.geronimo.mail.util.Base64;
+import org.apache.james.protocols.lib.PortUtil;
 
 public class AuthenticationTest extends TestCase {
 
@@ -48,12 +49,13 @@
     
     public void testAuthenticatePlain() throws Exception {
 
+        final int listenerPort = PortUtil.getNonPrivilegedPort();
         //greenmail does not have AUTHENTICATE "PLAIN" support
         FakeImapAuthPlainServer fs = new FakeImapAuthPlainServer(null, "user", "pass");
-        fs.startServer();
+        fs.startServer(listenerPort);
         // Setup JavaMail session
         Properties props = new Properties();
-        props.setProperty("mail.imap.port", "5111");
+        props.setProperty("mail.imap.port", String.valueOf(listenerPort));
         props.setProperty("mail.debug", String.valueOf(true));
         props.setProperty("mail.debug.auth", String.valueOf(true));
 
@@ -67,12 +69,13 @@
 
     public void testAuthenticatePlainFail() throws Exception {
 
+        final int listenerPort = PortUtil.getNonPrivilegedPort();
         //greenmail does not have AUTHENTICATE "PLAIN" support
         FakeImapAuthPlainServer fs = new FakeImapAuthPlainServer(null, "user", "pass");
-        fs.startServer();
+        fs.startServer(listenerPort);
         // Setup JavaMail session
         Properties props = new Properties();
-        props.setProperty("mail.imap.port", "5111");
+        props.setProperty("mail.imap.port", String.valueOf(listenerPort));
         props.setProperty("mail.debug", String.valueOf(true));
         props.setProperty("mail.debug.auth", String.valueOf(true));
         Session session = Session.getInstance(props);
@@ -89,12 +92,13 @@
 
     public void testAuthenticatePlainAuthzid() throws Exception {
 
+        final int listenerPort = PortUtil.getNonPrivilegedPort();
         //greenmail does not have AUTHENTICATE "PLAIN" support
         FakeImapAuthPlainServer fs = new FakeImapAuthPlainServer("authzid", "user", "pass");
-        fs.startServer();
+        fs.startServer(listenerPort);
         // Setup JavaMail session
         Properties props = new Properties();
-        props.setProperty("mail.imap.port", "5111");
+        props.setProperty("mail.imap.port", String.valueOf(listenerPort));
         props.setProperty("mail.debug", String.valueOf(true));
         props.setProperty("mail.debug.auth", String.valueOf(true));
         props.setProperty("mail.imap.sasl.authorizationid", "authzid");
@@ -123,9 +127,9 @@
             this.authzid = authzid==null?"":authzid;
         }
 
-        void startServer() throws IOException {
+        void startServer(int port) throws IOException {
 
-            serverSocket = new ServerSocket(5111);
+            serverSocket = new ServerSocket(port);
             this.setDaemon(false);
             this.start();
 
diff --git a/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/testserver/MailServer.java b/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/testserver/MailServer.java
index 0506249..5feb700 100644
--- a/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/testserver/MailServer.java
+++ b/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/testserver/MailServer.java
@@ -19,13 +19,17 @@
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.net.InetAddress;
 import java.net.Socket;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLDecoder;
 import java.net.UnknownHostException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Date;
@@ -355,30 +359,36 @@
         return queue;
     }
 
-    public static File getAbsoluteFilePathFromClassPath(final String fileNameFromClasspath) {
+    public static File getAbsoluteFilePathFromClassPath(final String fileNameFromClasspath) throws FileNotFoundException {
 
         File configFile = null;
         final URL configURL = MailServer.class.getClassLoader().getResource(fileNameFromClasspath);
         if (configURL != null) {
             try {
-                configFile = new File(URLDecoder.decode(configURL.getFile(), "UTF-8"));
-            } catch (final UnsupportedEncodingException e) {
-                return null;
+                configFile = new File(configURL.toURI());
+            } catch (URISyntaxException e) {
+                configFile = new File(configURL.getPath());
             }
 
-            if (configFile.exists() && configFile.canRead()) {
+            //Java 7 only
+            /*if(!configFile.exists()) {
+                try {
+                    configFile = Paths.get(configURL.toURI()).toFile();
+                } catch (URISyntaxException e) {
+                    throw new FileNotFoundException("Failed to load " + fileNameFromClasspath+ " due to "+e);
+                }
+            }*/
+
+            if (configFile.exists()) {
                 return configFile;
             } else {
-
-                System.out.println("Cannot read from {}, maybe the file does not exists? " + configFile.getAbsolutePath());
+                throw new FileNotFoundException("Cannot read from "+configFile.getAbsolutePath()+" (original resource was "+fileNameFromClasspath+", URL: "+configURL+"), because the file does not exist");
             }
-
+            
         } else {
-            System.out.println("Failed to load " + fileNameFromClasspath);
+            throw new FileNotFoundException("Failed to load " + fileNameFromClasspath+", because resource cannot be found within the classpath");
         }
 
-        return null;
-
     }
 
     public static abstract class AbstractTestConfiguration extends DefaultConfigurationBuilder {
@@ -392,7 +402,7 @@
             return listenerPort;
         }
 
-        public AbstractTestConfiguration enableSSL(final boolean enableStartTLS, final boolean enableSSL) {
+        public AbstractTestConfiguration enableSSL(final boolean enableStartTLS, final boolean enableSSL) throws FileNotFoundException {
             addProperty("tls.[@startTLS]", enableStartTLS);
             addProperty("tls.[@socketTLS]", enableSSL);
             addProperty("tls.keystore", "file://" + getAbsoluteFilePathFromClassPath("dummykeystore.jks").getAbsolutePath());
@@ -406,7 +416,8 @@
             addProperty("bind", "127.0.0.1:" + this.listenerPort);
             addProperty("connectiontimeout", "360000");
             //addProperty("jmxName", getServertype().name()+"on"+this.listenerPort);
-
+            addProperty("helloName", "jamesserver");
+            addProperty("helloName.[@autodetect]", false);
         }
 
     }