Author: veithen
Date: Sun Sep 14 04:41:30 2008
New Revision: 695199

URL: http://svn.apache.org/viewvc?rev=695199&view=rev
Log:
Mail transport tests:
* Make sure that the test cases only start sending messages when the test 
server is up and running (avoiding connection refused errors seen on some 
platforms).
* The fix in revision 695095 also corrects a synchronization issue in 
MailTransportListener that prevented us from reusing resources between test 
cases. This has been changed.

Added:
    
synapse/trunk/java/modules/transports/src/test/java/org/apache/synapse/transport/testkit/util/ServerUtil.java
Modified:
    
synapse/trunk/java/modules/transports/src/test/java/org/apache/synapse/transport/mail/GreenMailTestEnvironment.java
    
synapse/trunk/java/modules/transports/src/test/java/org/apache/synapse/transport/mail/MailTransportListenerTest.java

Modified: 
synapse/trunk/java/modules/transports/src/test/java/org/apache/synapse/transport/mail/GreenMailTestEnvironment.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/src/test/java/org/apache/synapse/transport/mail/GreenMailTestEnvironment.java?rev=695199&r1=695198&r2=695199&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/transports/src/test/java/org/apache/synapse/transport/mail/GreenMailTestEnvironment.java
 (original)
+++ 
synapse/trunk/java/modules/transports/src/test/java/org/apache/synapse/transport/mail/GreenMailTestEnvironment.java
 Sun Sep 14 04:41:30 2008
@@ -29,6 +29,7 @@
 
 import org.apache.synapse.transport.testkit.name.Name;
 import org.apache.synapse.transport.testkit.util.LogManager;
+import org.apache.synapse.transport.testkit.util.ServerUtil;
 
 import com.icegreen.greenmail.store.FolderListener;
 import com.icegreen.greenmail.store.MailFolder;
@@ -56,6 +57,8 @@
         greenMail = new GreenMail(new ServerSetup[] { SMTP, POP3 });
         greenMail.start();
         unallocatedAccounts = new LinkedList<Account>();
+        ServerUtil.waitForServer(SMTP.getPort());
+        ServerUtil.waitForServer(POP3.getPort());
     }
 
     @SuppressWarnings("unused")

Modified: 
synapse/trunk/java/modules/transports/src/test/java/org/apache/synapse/transport/mail/MailTransportListenerTest.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/src/test/java/org/apache/synapse/transport/mail/MailTransportListenerTest.java?rev=695199&r1=695198&r2=695199&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/transports/src/test/java/org/apache/synapse/transport/mail/MailTransportListenerTest.java
 (original)
+++ 
synapse/trunk/java/modules/transports/src/test/java/org/apache/synapse/transport/mail/MailTransportListenerTest.java
 Sun Sep 14 04:41:30 2008
@@ -32,7 +32,7 @@
 
 public class MailTransportListenerTest extends TestCase {
     public static TestSuite suite() throws Exception {
-        TransportTestSuite suite = new 
TransportTestSuite(MailTransportListenerTest.class, false);
+        TransportTestSuite suite = new 
TransportTestSuite(MailTransportListenerTest.class);
         
         // TODO: these test don't work; need more analysis why this is so
         suite.addExclude("(&(messageType=SOAP12)(data=Latin1))");

Added: 
synapse/trunk/java/modules/transports/src/test/java/org/apache/synapse/transport/testkit/util/ServerUtil.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/src/test/java/org/apache/synapse/transport/testkit/util/ServerUtil.java?rev=695199&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/transports/src/test/java/org/apache/synapse/transport/testkit/util/ServerUtil.java
 (added)
+++ 
synapse/trunk/java/modules/transports/src/test/java/org/apache/synapse/transport/testkit/util/ServerUtil.java
 Sun Sep 14 04:41:30 2008
@@ -0,0 +1,63 @@
+/*
+ *  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.synapse.transport.testkit.util;
+
+import java.net.ConnectException;
+import java.net.InetAddress;
+import java.net.Socket;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * Utility class with methods useful when working with test servers.
+ */
+public class ServerUtil {
+    private static final Log log = LogFactory.getLog(ServerUtil.class);
+    
+    private ServerUtil() {}
+    
+    /**
+     * Wait until the server listening on a given TCP port is ready to accept
+     * connections.
+     * 
+     * @param port The TCP port the server listens on.
+     * @throws Exception
+     */
+    public static void waitForServer(int port) throws Exception {
+        InetAddress localhost = InetAddress.getByAddress(new byte[] { 127, 0, 
0, 1 });
+        int attempts = 0;
+        Socket socket = null;
+        while (socket == null) {
+            attempts++;
+            try {
+                socket = new Socket(localhost, port);
+            } catch (ConnectException ex) {
+                if (attempts < 10) {
+                    Thread.sleep(50);
+                } else {
+                    throw ex;
+                }
+            }
+        }
+        log.debug("Server on port " + port + " ready after " + attempts + " 
connection attempts");
+        socket.close();
+    }
+}


Reply via email to