james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ieu...@apache.org
Subject svn commit: r1452287 - in /james/server/trunk/queue: queue-activemq/src/test/java/org/apache/james/queue/activemq/ queue-api/src/main/java/org/apache/james/queue/api/ queue-file/src/main/java/org/apache/james/queue/file/ queue-jms/src/test/java/org/apa...
Date Mon, 04 Mar 2013 13:11:35 GMT
Author: ieugen
Date: Mon Mar  4 13:11:35 2013
New Revision: 1452287

URL: http://svn.apache.org/r1452287
Log:
JMS queue and ActiveMQ queue test speed-up

* start the broker just once

Added:
    james/server/trunk/queue/queue-jms/src/test/java/org/apache/james/queue/jms/AbstractJMSMailQueueTest.java
      - copied, changed from r1452285, james/server/trunk/queue/queue-jms/src/test/java/org/apache/james/queue/jms/JMSMailQueueTest.java
Modified:
    james/server/trunk/queue/queue-activemq/src/test/java/org/apache/james/queue/activemq/ActiveMQMailQueueBlobTest.java
    james/server/trunk/queue/queue-activemq/src/test/java/org/apache/james/queue/activemq/ActiveMQMailQueueTest.java
    james/server/trunk/queue/queue-api/src/main/java/org/apache/james/queue/api/MailQueue.java
    james/server/trunk/queue/queue-file/src/main/java/org/apache/james/queue/file/FileMailQueue.java
    james/server/trunk/queue/queue-file/src/main/java/org/apache/james/queue/file/FileMailQueueFactory.java
    james/server/trunk/queue/queue-jms/src/test/java/org/apache/james/queue/jms/JMSMailQueueTest.java

Modified: james/server/trunk/queue/queue-activemq/src/test/java/org/apache/james/queue/activemq/ActiveMQMailQueueBlobTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/queue/queue-activemq/src/test/java/org/apache/james/queue/activemq/ActiveMQMailQueueBlobTest.java?rev=1452287&r1=1452286&r2=1452287&view=diff
==============================================================================
--- james/server/trunk/queue/queue-activemq/src/test/java/org/apache/james/queue/activemq/ActiveMQMailQueueBlobTest.java
(original)
+++ james/server/trunk/queue/queue-activemq/src/test/java/org/apache/james/queue/activemq/ActiveMQMailQueueBlobTest.java
Mon Mar  4 13:11:35 2013
@@ -18,18 +18,22 @@
  ****************************************************************/
 package org.apache.james.queue.activemq;
 
+import org.apache.activemq.ActiveMQConnectionFactory;
+import org.apache.james.filesystem.api.FileSystem;
+import org.apache.james.queue.jms.JMSMailQueue;
+
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
-import org.apache.activemq.ActiveMQConnectionFactory;
-import org.apache.james.filesystem.api.FileSystem;
 
 public class ActiveMQMailQueueBlobTest extends ActiveMQMailQueueTest {
 
     public final static String BASE_DIR = "file://target/james-test";
     private MyFileSystem fs;
 
+    private JMSMailQueue queue;
+
     @Override
     protected ActiveMQConnectionFactory createConnectionFactory() {
         ActiveMQConnectionFactory factory = super.createConnectionFactory();
@@ -48,15 +52,23 @@ public class ActiveMQMailQueueBlobTest e
         super.setUp();
     }
 
-    @Override
     public void tearDown() throws Exception {
-        super.tearDown();
         if (fs != null) {
             fs.destroy();
         }
     }
 
     @Override
+    public JMSMailQueue getQueue() {
+        return queue;
+    }
+
+    @Override
+    public void setQueue(JMSMailQueue queue) {
+        this.queue = queue;
+    }
+
+    @Override
     protected boolean useBlobMessages() {
         return true;
     }

Modified: james/server/trunk/queue/queue-activemq/src/test/java/org/apache/james/queue/activemq/ActiveMQMailQueueTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/queue/queue-activemq/src/test/java/org/apache/james/queue/activemq/ActiveMQMailQueueTest.java?rev=1452287&r1=1452286&r2=1452287&view=diff
==============================================================================
--- james/server/trunk/queue/queue-activemq/src/test/java/org/apache/james/queue/activemq/ActiveMQMailQueueTest.java
(original)
+++ james/server/trunk/queue/queue-activemq/src/test/java/org/apache/james/queue/activemq/ActiveMQMailQueueTest.java
Mon Mar  4 13:11:35 2013
@@ -20,19 +20,49 @@ package org.apache.james.queue.activemq;
 
 import org.apache.activemq.broker.BrokerPlugin;
 import org.apache.activemq.broker.BrokerService;
+import org.apache.activemq.broker.region.policy.PolicyEntry;
+import org.apache.activemq.broker.region.policy.PolicyMap;
 import org.apache.activemq.plugin.StatisticsBrokerPlugin;
+import org.apache.james.queue.jms.AbstractJMSMailQueueTest;
 import org.apache.james.queue.jms.JMSMailQueue;
-import org.apache.james.queue.jms.JMSMailQueueTest;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import javax.jms.ConnectionFactory;
+import java.util.Arrays;
 
-public class ActiveMQMailQueueTest extends JMSMailQueueTest {
+public abstract class ActiveMQMailQueueTest extends AbstractJMSMailQueueTest {
 
-    @Override
-    protected BrokerService createBroker() throws Exception {
-        BrokerService broker = super.createBroker();
+    private static BrokerService brokerService;
+
+    @BeforeClass
+    public static void setupBroker() throws Exception {
+        brokerService = createBroker();
+        brokerService.start();
+    }
+
+    @AfterClass
+    public static void tearDownBroker() throws Exception {
+        if (brokerService != null) {
+            brokerService.stop();
+        }
+    }
+
+    protected static BrokerService createBroker() throws Exception {
+        BrokerService broker = new BrokerService();
+        broker.setPersistent(false);
+        broker.setUseJmx(false);
+        broker.addConnector("tcp://127.0.0.1:61616");
+
+        // Enable priority support
+        PolicyMap pMap = new PolicyMap();
+        PolicyEntry entry = new PolicyEntry();
+        entry.setPrioritizedMessages(true);
+        entry.setQueue(QUEUE_NAME);
+        pMap.setPolicyEntries(Arrays.asList(entry));
+        broker.setDestinationPolicy(pMap);
         // Enable statistics
         broker.setPlugins(new BrokerPlugin[]{new StatisticsBrokerPlugin()});
         broker.setEnableStatistics(true);
@@ -42,14 +72,11 @@ public class ActiveMQMailQueueTest exten
 
     @Override
     protected JMSMailQueue createQueue(ConnectionFactory factory, String queueName) {
-        Logger log = LoggerFactory.getLogger("MockLog");
-        // slf4j can't set programmatically any log level. It's just a facade
-        // log.setLevel(SimpleLog.LOG_LEVEL_DEBUG);
+        Logger log = LoggerFactory.getLogger(ActiveMQMailQueueTest.class);
         return new ActiveMQMailQueue(factory, queueName, useBlobMessages(), log);
     }
 
     protected boolean useBlobMessages() {
         return false;
     }
-
 }

Modified: james/server/trunk/queue/queue-api/src/main/java/org/apache/james/queue/api/MailQueue.java
URL: http://svn.apache.org/viewvc/james/server/trunk/queue/queue-api/src/main/java/org/apache/james/queue/api/MailQueue.java?rev=1452287&r1=1452286&r2=1452287&view=diff
==============================================================================
--- james/server/trunk/queue/queue-api/src/main/java/org/apache/james/queue/api/MailQueue.java
(original)
+++ james/server/trunk/queue/queue-api/src/main/java/org/apache/james/queue/api/MailQueue.java
Mon Mar  4 13:11:35 2013
@@ -107,7 +107,6 @@ public interface MailQueue {
     }
 
     /**
-     * 
      *
      */
     public interface MailQueueItem {

Modified: james/server/trunk/queue/queue-file/src/main/java/org/apache/james/queue/file/FileMailQueue.java
URL: http://svn.apache.org/viewvc/james/server/trunk/queue/queue-file/src/main/java/org/apache/james/queue/file/FileMailQueue.java?rev=1452287&r1=1452286&r2=1452287&view=diff
==============================================================================
--- james/server/trunk/queue/queue-file/src/main/java/org/apache/james/queue/file/FileMailQueue.java
(original)
+++ james/server/trunk/queue/queue-file/src/main/java/org/apache/james/queue/file/FileMailQueue.java
Mon Mar  4 13:11:35 2013
@@ -18,6 +18,17 @@
  ****************************************************************/
 package org.apache.james.queue.file;
 
+import com.google.common.io.Closeables;
+import org.apache.james.core.MimeMessageCopyOnWriteProxy;
+import org.apache.james.core.MimeMessageSource;
+import org.apache.james.lifecycle.api.Disposable;
+import org.apache.james.lifecycle.api.LifecycleUtil;
+import org.apache.james.queue.api.ManageableMailQueue;
+import org.apache.mailet.Mail;
+import org.slf4j.Logger;
+
+import javax.mail.MessagingException;
+import javax.mail.util.SharedFileInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -38,24 +49,14 @@ import java.util.concurrent.ScheduledExe
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicLong;
 
-import javax.mail.MessagingException;
-import javax.mail.util.SharedFileInputStream;
-
-import org.slf4j.Logger;
-import org.apache.james.core.MimeMessageCopyOnWriteProxy;
-import org.apache.james.core.MimeMessageSource;
-import org.apache.james.lifecycle.api.Disposable;
-import org.apache.james.lifecycle.api.LifecycleUtil;
-import org.apache.james.queue.api.ManageableMailQueue;
-import org.apache.mailet.Mail;
-
 /**
  * {@link ManageableMailQueue} implementation which use the fs to store {@link Mail}'s
- * 
- * On create of the {@link FileMailQueue} the {@link #init()} will get called. This takes
care of 
+ * <p/>
+ * On create of the {@link FileMailQueue} the {@link #init()} will get called. This takes
care of
  * loading the needed meta-data into memory for fast access.
  */
 public class FileMailQueue implements ManageableMailQueue {
+
     private final ConcurrentHashMap<String, FileItem> keyMappings = new ConcurrentHashMap<String,
FileMailQueue.FileItem>();
     private final BlockingQueue<String> inmemoryQueue = new LinkedBlockingQueue<String>();
     private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
@@ -63,7 +64,7 @@ public class FileMailQueue implements Ma
     private final String queueDirName;
     private final File queueDir;
     private final Logger log;
-    
+
     private final boolean sync;
     private final static String MSG_EXTENSION = ".msg";
     private final static String OBJECT_EXTENSION = ".obj";
@@ -77,16 +78,16 @@ public class FileMailQueue implements Ma
         this.queueDirName = queueDir.getAbsolutePath();
         init();
     }
-    
+
     private void init() throws IOException {
-        
+
         for (int i = 1; i <= SPLITCOUNT; i++) {
 
             File qDir = new File(queueDir, Integer.toString(i));
             if (!qDir.exists() && !qDir.mkdirs()) {
-                throw new IOException("Unable to create queue directory " +  qDir);
+                throw new IOException("Unable to create queue directory " + qDir);
             }
-            
+
             String[] files = qDir.list(new FilenameFilter() {
                 @Override
                 public boolean accept(File dir, String name) {
@@ -156,7 +157,7 @@ public class FileMailQueue implements Ma
             }
         }
     }
-    
+
     @Override
     public void enQueue(final Mail mail, long delay, TimeUnit unit) throws MailQueueException
{
         final String key = mail.getName() + "-" + COUNTER.incrementAndGet();
@@ -166,9 +167,9 @@ public class FileMailQueue implements Ma
         try {
             int i = (int) (Math.random() * SPLITCOUNT + 1);
 
-            
+
             String name = queueDirName + "/" + i + "/" + key;
-            
+
             final FileItem item = new FileItem(name + OBJECT_EXTENSION, name + MSG_EXTENSION);
             if (delay > 0) {
                 mail.setAttribute(NEXT_DELIVERY, System.currentTimeMillis() + unit.toMillis(delay));
@@ -179,33 +180,33 @@ public class FileMailQueue implements Ma
             oout.flush();
             if (sync) foout.getFD().sync();
             out = new FileOutputStream(item.getMessageFile());
-           
+
             mail.getMessage().writeTo(out);
             out.flush();
             if (sync) out.getFD().sync();
-            
+
             keyMappings.put(key, item);
 
             if (delay > 0) {
                 // The message should get delayed so schedule it for later
                 scheduler.schedule(new Runnable() {
-                    
+
                     @Override
                     public void run() {
-                        try {                           
+                        try {
                             inmemoryQueue.put(key);
 
                         } catch (InterruptedException e) {
                             Thread.currentThread().interrupt();
                             throw new RuntimeException("Unable to init", e);
-                        }                                
+                        }
                     }
                 }, delay, unit);
-            
+
             } else {
                 inmemoryQueue.put(key);
             }
-            
+
             //TODO: Think about exception handling in detail
         } catch (FileNotFoundException e) {
             throw new MailQueueException("Unable to enqueue mail", e);
@@ -255,7 +256,7 @@ public class FileMailQueue implements Ma
             String k = null;
             while (item == null) {
                 k = inmemoryQueue.take();
-                
+
                 item = keyMappings.get(k);
 
             }
@@ -292,7 +293,7 @@ public class FileMailQueue implements Ma
                         LifecycleUtil.dispose(mail);
                     }
                 };
-                
+
                 // TODO: Think about exception handling in detail
             } catch (FileNotFoundException e) {
                 throw new MailQueueException("Unable to dequeue", e);
@@ -302,7 +303,7 @@ public class FileMailQueue implements Ma
                 throw new MailQueueException("Unable to dequeue", e);
             } catch (MessagingException e) {
                 throw new MailQueueException("Unable to dequeue", e);
-                
+
             } finally {
                 if (oin != null) {
                     try {
@@ -319,7 +320,7 @@ public class FileMailQueue implements Ma
         }
     }
 
-    private final class FileMimeMessageSource extends MimeMessageSource implements Disposable{
+    private final class FileMimeMessageSource extends MimeMessageSource implements Disposable
{
 
         private File file;
         private SharedFileInputStream in;
@@ -328,7 +329,7 @@ public class FileMailQueue implements Ma
             this.file = file;
             this.in = new SharedFileInputStream(file);
         }
-        
+
         @Override
         public String getSourceId() {
             return file.getAbsolutePath();
@@ -336,9 +337,10 @@ public class FileMailQueue implements Ma
 
         /**
          * Get an input stream to retrieve the data stored in the temporary file
-         * 
+         *
          * @return a <code>BufferedInputStream</code> containing the data
          */
+        @Override
         public InputStream getInputStream() throws IOException {
             return in.newStream(0, -1);
         }
@@ -348,19 +350,14 @@ public class FileMailQueue implements Ma
             return file.length();
         }
 
-        /**
-         */
+        @Override
         public void dispose() {
-            try {
-                in.close();
-            } catch (IOException e) {
-            }
-            
+            Closeables.closeQuietly(in);
             file = null;
         }
-        
+
     }
-    
+
     /**
      * Helper class which is used to reference the path to the object and msg file
      */
@@ -372,33 +369,32 @@ public class FileMailQueue implements Ma
             this.objectfile = objectfile;
             this.messagefile = messagefile;
         }
-        
-        
+
         public String getObjectFile() {
             return objectfile;
         }
-        
+
         public String getMessageFile() {
             return messagefile;
         }
-        
+
         public void delete() throws MailQueueException {
             File msgFile = new File(getMessageFile());
             File objectFile = new File(getObjectFile());
-            
+
             if (objectFile.exists()) {
                 if (!objectFile.delete()) {
                     throw new MailQueueException("Unable to delete mail");
-                } 
+                }
             }
             if (msgFile.exists()) {
                 if (!msgFile.delete()) {
                     log.debug("Remove of msg file for mail failed");
                 }
-                
+
             }
         }
-        
+
     }
 
     @Override
@@ -410,7 +406,7 @@ public class FileMailQueue implements Ma
     public long flush() throws MailQueueException {
         Iterator<String> keys = keyMappings.keySet().iterator();
         long i = 0;
-        while(keys.hasNext()) {
+        while (keys.hasNext()) {
             String key = keys.next();
             if (!inmemoryQueue.contains(key)) {
                 inmemoryQueue.add(key);
@@ -424,11 +420,11 @@ public class FileMailQueue implements Ma
     public long clear() throws MailQueueException {
         final Iterator<Entry<String, FileItem>> items = keyMappings.entrySet().iterator();
         long count = 0;
-        while(items.hasNext()) {
+        while (items.hasNext()) {
             Entry<String, FileItem> entry = items.next();
             FileItem item = entry.getValue();
             String key = entry.getKey();
-           
+
             item.delete();
             keyMappings.remove(key);
             count++;
@@ -439,23 +435,23 @@ public class FileMailQueue implements Ma
 
     /**
      * TODO: implement me
-     * 
+     *
      * @see ManageableMailQueue#remove(org.apache.james.queue.api.ManageableMailQueue.Type,
String)
      */
     @Override
     public long remove(Type type, String value) throws MailQueueException {
         switch (type) {
-        case Name:
-            FileItem item = keyMappings.remove(value);
-            if (item != null) {
-                item.delete();
-                return 1;
-            } else {
-                return 0;
-            }
-            
-        default:
-            break;
+            case Name:
+                FileItem item = keyMappings.remove(value);
+                if (item != null) {
+                    item.delete();
+                    return 1;
+                } else {
+                    return 0;
+                }
+
+            default:
+                break;
         }
         throw new MailQueueException("Not supported yet");
 
@@ -479,7 +475,7 @@ public class FileMailQueue implements Ma
                     item = null;
                     return vitem;
                 } else {
-                    
+
                     throw new NoSuchElementException();
                 }
             }
@@ -534,5 +530,5 @@ public class FileMailQueue implements Ma
             }
         };
     }
-    
+
 }

Modified: james/server/trunk/queue/queue-file/src/main/java/org/apache/james/queue/file/FileMailQueueFactory.java
URL: http://svn.apache.org/viewvc/james/server/trunk/queue/queue-file/src/main/java/org/apache/james/queue/file/FileMailQueueFactory.java?rev=1452287&r1=1452286&r2=1452287&view=diff
==============================================================================
--- james/server/trunk/queue/queue-file/src/main/java/org/apache/james/queue/file/FileMailQueueFactory.java
(original)
+++ james/server/trunk/queue/queue-file/src/main/java/org/apache/james/queue/file/FileMailQueueFactory.java
Mon Mar  4 13:11:35 2013
@@ -18,45 +18,44 @@
  ****************************************************************/
 package org.apache.james.queue.file;
 
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
-
-import javax.inject.Inject;
-
 import org.apache.james.filesystem.api.FileSystem;
 import org.apache.james.lifecycle.api.LogEnabled;
 import org.apache.james.queue.api.MailQueue;
 import org.apache.james.queue.api.MailQueueFactory;
 import org.slf4j.Logger;
 
+import javax.inject.Inject;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
 /**
  * {@link MailQueueFactory} implementation which returns {@link FileMailQueue} instances
  */
-public class FileMailQueueFactory implements MailQueueFactory, LogEnabled{
+public class FileMailQueueFactory implements MailQueueFactory, LogEnabled {
 
     private final Map<String, MailQueue> queues = new HashMap<String, MailQueue>();
     private FileSystem fs;
     private Logger log;
     private boolean sync = true;
-    
+
     @Inject
     public void setFileSystem(FileSystem fs) {
         this.fs = fs;
     }
-    
+
     /**
      * If <code>true</code> the later created {@link FileMailQueue} will call
<code>fsync</code> after each message {@link FileMailQueue#enQueue(org.apache.mailet.Mail)}
call. This
      * is needed to be fully RFC conform but gives a performance penalty. If you are brave
enough you man set it to <code>false</code>
-     * 
+     * <p/>
      * The default is <code>true</code>
-     * 
+     *
      * @param sync
      */
     public void setSync(boolean sync) {
         this.sync = sync;
     }
-    
+
     @Override
     public MailQueue getQueue(String name) {
         MailQueue queue = queues.get(name);

Copied: james/server/trunk/queue/queue-jms/src/test/java/org/apache/james/queue/jms/AbstractJMSMailQueueTest.java
(from r1452285, james/server/trunk/queue/queue-jms/src/test/java/org/apache/james/queue/jms/JMSMailQueueTest.java)
URL: http://svn.apache.org/viewvc/james/server/trunk/queue/queue-jms/src/test/java/org/apache/james/queue/jms/AbstractJMSMailQueueTest.java?p2=james/server/trunk/queue/queue-jms/src/test/java/org/apache/james/queue/jms/AbstractJMSMailQueueTest.java&p1=james/server/trunk/queue/queue-jms/src/test/java/org/apache/james/queue/jms/JMSMailQueueTest.java&r1=1452285&r2=1452287&rev=1452287&view=diff
==============================================================================
--- james/server/trunk/queue/queue-jms/src/test/java/org/apache/james/queue/jms/JMSMailQueueTest.java
(original)
+++ james/server/trunk/queue/queue-jms/src/test/java/org/apache/james/queue/jms/AbstractJMSMailQueueTest.java
Mon Mar  4 13:11:35 2013
@@ -19,9 +19,6 @@
 package org.apache.james.queue.jms;
 
 import org.apache.activemq.ActiveMQConnectionFactory;
-import org.apache.activemq.broker.BrokerService;
-import org.apache.activemq.broker.region.policy.PolicyEntry;
-import org.apache.activemq.broker.region.policy.PolicyMap;
 import org.apache.james.core.MailImpl;
 import org.apache.james.protocols.smtp.MailAddressException;
 import org.apache.james.queue.api.MailQueue.MailQueueItem;
@@ -29,7 +26,6 @@ import org.apache.james.queue.api.Manage
 import org.apache.james.queue.api.ManageableMailQueue.MailQueueIterator;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
-import org.junit.After;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
@@ -52,56 +48,36 @@ import java.util.Properties;
 import java.util.UUID;
 import java.util.concurrent.TimeUnit;
 
-public class JMSMailQueueTest {
+/**
+ * Basic JMS test class. Extend this class and start the JMS broker in the super class,
+ * Create a queue and implement the getter and setter for the tests to run.
+ */
+public abstract class AbstractJMSMailQueueTest {
 
     protected final static String QUEUE_NAME = "test";
-    protected JMSMailQueue queue;
-    private BrokerService broker;
 
-    @Before
-    public void setUp() throws Exception {
-        broker = createBroker();
-        broker.start();
+    public abstract JMSMailQueue getQueue();
 
-        ConnectionFactory connectionFactory = createConnectionFactory();
-        queue = createQueue(connectionFactory, QUEUE_NAME);
-    }
+    public abstract void setQueue(JMSMailQueue queue);
 
     protected ActiveMQConnectionFactory createConnectionFactory() {
         return new ActiveMQConnectionFactory("vm://localhost?create=false");
     }
 
-    protected BrokerService createBroker() throws Exception {
-        BrokerService aBroker = new BrokerService();
-        aBroker.setPersistent(false);
-        aBroker.setUseJmx(false);
-        aBroker.addConnector("tcp://127.0.0.1:61616");
-
-        // Enable priority support
-        PolicyMap pMap = new PolicyMap();
-        PolicyEntry entry = new PolicyEntry();
-        entry.setPrioritizedMessages(true);
-        entry.setQueue(QUEUE_NAME);
-        pMap.setPolicyEntries(Arrays.asList(entry));
-        aBroker.setDestinationPolicy(pMap);
-
-        return aBroker;
-    }
-
     protected JMSMailQueue createQueue(ConnectionFactory factory, String queueName) {
-        Logger log = LoggerFactory.getLogger(JMSMailQueueTest.class);
+        Logger log = LoggerFactory.getLogger(AbstractJMSMailQueueTest.class);
         return new JMSMailQueue(factory, queueName, log);
     }
 
-    @After
-    public void tearDown() throws Exception {
-        if (broker != null) {
-            broker.stop();
-        }
+    @Before
+    public void setUp() throws Exception {
+        ConnectionFactory connectionFactory = createConnectionFactory();
+        setQueue(createQueue(connectionFactory, QUEUE_NAME));
     }
 
     @Test
     public void testFIFO() throws MessagingException, InterruptedException, IOException,
MailAddressException {
+        final JMSMailQueue queue = getQueue();
         // should be empty
         assertEquals(0, queue.getSize());
 
@@ -111,7 +87,7 @@ public class JMSMailQueueTest {
         queue.enQueue(mail);
         queue.enQueue(mail2);
 
-        Thread.sleep(200);
+        TimeUnit.MILLISECONDS.sleep(200);
 
         assertEquals(2, queue.getSize());
 
@@ -119,7 +95,7 @@ public class JMSMailQueueTest {
         checkMail(mail, item.getMail());
         item.done(false);
 
-        Thread.sleep(200);
+        TimeUnit.MILLISECONDS.sleep(200);
 
         // ok we should get the same email again
         assertEquals(2, queue.getSize());
@@ -127,14 +103,14 @@ public class JMSMailQueueTest {
         checkMail(mail, item2.getMail());
         item2.done(true);
 
-        Thread.sleep(200);
+        TimeUnit.MILLISECONDS.sleep(200);
 
         assertEquals(1, queue.getSize());
         MailQueueItem item3 = queue.deQueue();
         checkMail(mail2, item3.getMail());
         item3.done(true);
 
-        Thread.sleep(200);
+        TimeUnit.MILLISECONDS.sleep(200);
 
         // should be empty
         assertEquals(0, queue.getSize());
@@ -142,6 +118,7 @@ public class JMSMailQueueTest {
 
     @Test
     public void testDelayedDeQueue() throws MessagingException, InterruptedException, IOException,
MailAddressException {
+        final JMSMailQueue queue = getQueue();
         // should be empty
         assertEquals(0, queue.getSize());
 
@@ -152,7 +129,7 @@ public class JMSMailQueueTest {
         queue.enQueue(mail, 3, TimeUnit.SECONDS);
         queue.enQueue(mail2);
 
-        Thread.sleep(200);
+        TimeUnit.MILLISECONDS.sleep(200);
 
         assertEquals(2, queue.getSize());
 
@@ -161,7 +138,7 @@ public class JMSMailQueueTest {
         checkMail(mail2, item.getMail());
         item.done(true);
 
-        Thread.sleep(200);
+        TimeUnit.MILLISECONDS.sleep(200);
 
         assertEquals(1, queue.getSize());
         MailQueueItem item2 = queue.deQueue();
@@ -169,7 +146,7 @@ public class JMSMailQueueTest {
         checkMail(mail, item2.getMail());
         item2.done(true);
         assertTrue(dequeueTime >= 2000);
-        Thread.sleep(200);
+        TimeUnit.MILLISECONDS.sleep(200);
 
         // should be empty
         assertEquals(0, queue.getSize());
@@ -177,6 +154,7 @@ public class JMSMailQueueTest {
 
     @Test
     public void testFlush() throws MessagingException, InterruptedException, IOException,
MailAddressException {
+        final JMSMailQueue queue = getQueue();
         // should be empty
         assertEquals(0, queue.getSize());
 
@@ -185,7 +163,7 @@ public class JMSMailQueueTest {
         long enqueueTime = System.currentTimeMillis();
         queue.enQueue(mail, 30, TimeUnit.SECONDS);
 
-        Thread.sleep(200);
+        TimeUnit.MILLISECONDS.sleep(200);
 
         assertEquals(1, queue.getSize());
 
@@ -195,7 +173,7 @@ public class JMSMailQueueTest {
             public void run() {
                 try {
                     // wait for 2 seconds then flush the queue
-                    Thread.sleep(4000);
+                    TimeUnit.MILLISECONDS.sleep(4000);
                     assertEquals(1, queue.flush());
                 } catch (Exception e) {
                     throw new RuntimeException(e);
@@ -220,6 +198,8 @@ public class JMSMailQueueTest {
 
     @Test
     public void testRemoveWithRecipient() throws MessagingException, InterruptedException,
MailAddressException {
+        final JMSMailQueue queue = getQueue();
+
         assertEquals(0, queue.getSize());
 
         Mail mail = createMail();
@@ -231,12 +211,12 @@ public class JMSMailQueueTest {
         queue.enQueue(mail);
         queue.enQueue(mail2);
 
-        Thread.sleep(200);
+        TimeUnit.MILLISECONDS.sleep(200);
 
         assertEquals(2, queue.getSize());
         assertEquals(1, queue.remove(ManageableMailQueue.Type.Recipient, "remove@me1"));
 
-        Thread.sleep(200);
+        TimeUnit.MILLISECONDS.sleep(200);
         assertEquals(1, queue.getSize());
 
         assertEquals(1, queue.remove(ManageableMailQueue.Type.Recipient, "remove@me2"));
@@ -246,6 +226,7 @@ public class JMSMailQueueTest {
 
     @Test
     public void testRemoveWithSender() throws MessagingException, InterruptedException, MailAddressException
{
+        final JMSMailQueue queue = getQueue();
         assertEquals(0, queue.getSize());
 
         MailImpl mail = createMail();
@@ -257,12 +238,12 @@ public class JMSMailQueueTest {
         queue.enQueue(mail);
         queue.enQueue(mail2);
 
-        Thread.sleep(200);
+        TimeUnit.MILLISECONDS.sleep(200);
 
         assertEquals(2, queue.getSize());
         assertEquals(1, queue.remove(ManageableMailQueue.Type.Sender, "remove@me1"));
 
-        Thread.sleep(200);
+        TimeUnit.MILLISECONDS.sleep(200);
         assertEquals(1, queue.getSize());
 
         assertEquals(1, queue.remove(ManageableMailQueue.Type.Sender, "remove@me2"));
@@ -272,6 +253,7 @@ public class JMSMailQueueTest {
 
     @Test
     public void testRemoveWithName() throws MessagingException, InterruptedException, MailAddressException
{
+        final JMSMailQueue queue = getQueue();
         assertEquals(0, queue.getSize());
 
         MailImpl mail = createMail();
@@ -283,12 +265,12 @@ public class JMSMailQueueTest {
         queue.enQueue(mail);
         queue.enQueue(mail2);
 
-        Thread.sleep(200);
+        TimeUnit.MILLISECONDS.sleep(200);
 
         assertEquals(2, queue.getSize());
         assertEquals(1, queue.remove(ManageableMailQueue.Type.Name, "remove@me1"));
 
-        Thread.sleep(200);
+        TimeUnit.MILLISECONDS.sleep(200);
         assertEquals(1, queue.getSize());
 
         assertEquals(1, queue.remove(ManageableMailQueue.Type.Name, "remove@me2"));
@@ -348,6 +330,7 @@ public class JMSMailQueueTest {
 
     @Test
     public void testPrioritySupport() throws InterruptedException, MessagingException, IOException,
MailAddressException {
+        final JMSMailQueue queue = getQueue();
         // should be empty
         assertEquals(0, queue.getSize());
 
@@ -358,7 +341,7 @@ public class JMSMailQueueTest {
         queue.enQueue(mail);
         queue.enQueue(mail2);
 
-        Thread.sleep(200);
+        TimeUnit.MILLISECONDS.sleep(200);
 
         assertEquals(2, queue.getSize());
 
@@ -368,14 +351,14 @@ public class JMSMailQueueTest {
         checkMail(mail2, item2.getMail());
         item2.done(true);
 
-        Thread.sleep(200);
+        TimeUnit.MILLISECONDS.sleep(200);
 
         assertEquals(1, queue.getSize());
         MailQueueItem item3 = queue.deQueue();
         checkMail(mail, item3.getMail());
         item3.done(true);
 
-        Thread.sleep(200);
+        TimeUnit.MILLISECONDS.sleep(200);
 
         // should be empty
         assertEquals(0, queue.getSize());
@@ -383,6 +366,7 @@ public class JMSMailQueueTest {
 
     @Test
     public void testBrowse() throws MessagingException, InterruptedException, IOException,
MailAddressException {
+        final JMSMailQueue queue = getQueue();
         // should be empty
         assertEquals(0, queue.getSize());
 
@@ -392,7 +376,7 @@ public class JMSMailQueueTest {
         queue.enQueue(mail);
         queue.enQueue(mail2);
 
-        Thread.sleep(200);
+        TimeUnit.MILLISECONDS.sleep(200);
 
         assertEquals(2, queue.getSize());
 
@@ -406,7 +390,7 @@ public class JMSMailQueueTest {
         MailQueueItem item2 = queue.deQueue();
         checkMail(mail, item2.getMail());
         item2.done(true);
-        Thread.sleep(200);
+        TimeUnit.MILLISECONDS.sleep(200);
 
         assertEquals(1, queue.getSize());
         it = queue.browse();

Modified: james/server/trunk/queue/queue-jms/src/test/java/org/apache/james/queue/jms/JMSMailQueueTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/queue/queue-jms/src/test/java/org/apache/james/queue/jms/JMSMailQueueTest.java?rev=1452287&r1=1452286&r2=1452287&view=diff
==============================================================================
--- james/server/trunk/queue/queue-jms/src/test/java/org/apache/james/queue/jms/JMSMailQueueTest.java
(original)
+++ james/server/trunk/queue/queue-jms/src/test/java/org/apache/james/queue/jms/JMSMailQueueTest.java
Mon Mar  4 13:11:35 2013
@@ -1,77 +1,32 @@
-/****************************************************************
- * 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.queue.jms;
 
-import org.apache.activemq.ActiveMQConnectionFactory;
 import org.apache.activemq.broker.BrokerService;
 import org.apache.activemq.broker.region.policy.PolicyEntry;
 import org.apache.activemq.broker.region.policy.PolicyMap;
-import org.apache.james.core.MailImpl;
-import org.apache.james.protocols.smtp.MailAddressException;
-import org.apache.james.queue.api.MailQueue.MailQueueItem;
-import org.apache.james.queue.api.ManageableMailQueue;
-import org.apache.james.queue.api.ManageableMailQueue.MailQueueIterator;
-import org.apache.mailet.Mail;
-import org.apache.mailet.MailAddress;
-import org.junit.After;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-import org.junit.Before;
-import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import javax.jms.ConnectionFactory;
-import javax.mail.MessagingException;
-import javax.mail.Session;
-import javax.mail.internet.MimeMessage;
-import java.io.IOException;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+
 import java.util.Arrays;
-import java.util.Date;
-import java.util.Enumeration;
-import java.util.Iterator;
-import java.util.Properties;
-import java.util.UUID;
-import java.util.concurrent.TimeUnit;
-
-public class JMSMailQueueTest {
-
-    protected final static String QUEUE_NAME = "test";
-    protected JMSMailQueue queue;
-    private BrokerService broker;
 
-    @Before
-    public void setUp() throws Exception {
+public class JMSMailQueueTest extends AbstractJMSMailQueueTest {
+
+    private JMSMailQueue queue;
+    private static BrokerService broker;
+
+    @BeforeClass
+    public static void setUpBroker() throws Exception {
         broker = createBroker();
         broker.start();
-
-        ConnectionFactory connectionFactory = createConnectionFactory();
-        queue = createQueue(connectionFactory, QUEUE_NAME);
     }
 
-    protected ActiveMQConnectionFactory createConnectionFactory() {
-        return new ActiveMQConnectionFactory("vm://localhost?create=false");
+    @AfterClass
+    public static void tearDownBroker() throws Exception {
+        if (broker != null) {
+            broker.stop();
+        }
     }
 
-    protected BrokerService createBroker() throws Exception {
+    protected static BrokerService createBroker() throws Exception {
         BrokerService aBroker = new BrokerService();
         aBroker.setPersistent(false);
         aBroker.setUseJmx(false);
@@ -88,330 +43,13 @@ public class JMSMailQueueTest {
         return aBroker;
     }
 
-    protected JMSMailQueue createQueue(ConnectionFactory factory, String queueName) {
-        Logger log = LoggerFactory.getLogger(JMSMailQueueTest.class);
-        return new JMSMailQueue(factory, queueName, log);
-    }
-
-    @After
-    public void tearDown() throws Exception {
-        if (broker != null) {
-            broker.stop();
-        }
-    }
-
-    @Test
-    public void testFIFO() throws MessagingException, InterruptedException, IOException,
MailAddressException {
-        // should be empty
-        assertEquals(0, queue.getSize());
-
-        Mail mail = createMail();
-        Mail mail2 = createMail();
-
-        queue.enQueue(mail);
-        queue.enQueue(mail2);
-
-        Thread.sleep(200);
-
-        assertEquals(2, queue.getSize());
-
-        MailQueueItem item = queue.deQueue();
-        checkMail(mail, item.getMail());
-        item.done(false);
-
-        Thread.sleep(200);
-
-        // ok we should get the same email again
-        assertEquals(2, queue.getSize());
-        MailQueueItem item2 = queue.deQueue();
-        checkMail(mail, item2.getMail());
-        item2.done(true);
-
-        Thread.sleep(200);
-
-        assertEquals(1, queue.getSize());
-        MailQueueItem item3 = queue.deQueue();
-        checkMail(mail2, item3.getMail());
-        item3.done(true);
-
-        Thread.sleep(200);
-
-        // should be empty
-        assertEquals(0, queue.getSize());
-    }
-
-    @Test
-    public void testDelayedDeQueue() throws MessagingException, InterruptedException, IOException,
MailAddressException {
-        // should be empty
-        assertEquals(0, queue.getSize());
-
-        Mail mail = createMail();
-        Mail mail2 = createMail();
-
-        long enqueueTime = System.currentTimeMillis();
-        queue.enQueue(mail, 3, TimeUnit.SECONDS);
-        queue.enQueue(mail2);
-
-        Thread.sleep(200);
-
-        assertEquals(2, queue.getSize());
-
-        // as we enqueued the mail with delay we should get mail2 first
-        MailQueueItem item = queue.deQueue();
-        checkMail(mail2, item.getMail());
-        item.done(true);
-
-        Thread.sleep(200);
-
-        assertEquals(1, queue.getSize());
-        MailQueueItem item2 = queue.deQueue();
-        long dequeueTime = System.currentTimeMillis() - enqueueTime;
-        checkMail(mail, item2.getMail());
-        item2.done(true);
-        assertTrue(dequeueTime >= 2000);
-        Thread.sleep(200);
-
-        // should be empty
-        assertEquals(0, queue.getSize());
-    }
-
-    @Test
-    public void testFlush() throws MessagingException, InterruptedException, IOException,
MailAddressException {
-        // should be empty
-        assertEquals(0, queue.getSize());
-
-        final Mail mail = createMail();
-
-        long enqueueTime = System.currentTimeMillis();
-        queue.enQueue(mail, 30, TimeUnit.SECONDS);
-
-        Thread.sleep(200);
-
-        assertEquals(1, queue.getSize());
-
-        Thread flushThread = new Thread(new Runnable() {
-
-            @Override
-            public void run() {
-                try {
-                    // wait for 2 seconds then flush the queue
-                    Thread.sleep(4000);
-                    assertEquals(1, queue.flush());
-                } catch (Exception e) {
-                    throw new RuntimeException(e);
-                }
-
-            }
-        });
-        flushThread.start();
-
-        // this will block until flush is called
-        MailQueueItem item = queue.deQueue();
-        checkMail(mail, item.getMail());
-        item.done(true);
-
-        long dequeueTime = System.currentTimeMillis() - enqueueTime;
-
-        assertEquals(0, queue.getSize());
-
-        // check if the flush kicked in
-        assertTrue(dequeueTime < 30 * 1000);
-    }
-
-    @Test
-    public void testRemoveWithRecipient() throws MessagingException, InterruptedException,
MailAddressException {
-        assertEquals(0, queue.getSize());
-
-        Mail mail = createMail();
-        mail.setRecipients(Arrays.asList(new MailAddress("remove@me1")));
-
-        Mail mail2 = createMail();
-        mail2.setRecipients(Arrays.asList(new MailAddress("remove@me2")));
-
-        queue.enQueue(mail);
-        queue.enQueue(mail2);
-
-        Thread.sleep(200);
-
-        assertEquals(2, queue.getSize());
-        assertEquals(1, queue.remove(ManageableMailQueue.Type.Recipient, "remove@me1"));
-
-        Thread.sleep(200);
-        assertEquals(1, queue.getSize());
-
-        assertEquals(1, queue.remove(ManageableMailQueue.Type.Recipient, "remove@me2"));
-        assertEquals(0, queue.getSize());
-
-    }
-
-    @Test
-    public void testRemoveWithSender() throws MessagingException, InterruptedException, MailAddressException
{
-        assertEquals(0, queue.getSize());
-
-        MailImpl mail = createMail();
-        mail.setSender(new MailAddress("remove@me1"));
-
-        MailImpl mail2 = createMail();
-        mail2.setSender(new MailAddress("remove@me2"));
-
-        queue.enQueue(mail);
-        queue.enQueue(mail2);
-
-        Thread.sleep(200);
-
-        assertEquals(2, queue.getSize());
-        assertEquals(1, queue.remove(ManageableMailQueue.Type.Sender, "remove@me1"));
-
-        Thread.sleep(200);
-        assertEquals(1, queue.getSize());
-
-        assertEquals(1, queue.remove(ManageableMailQueue.Type.Sender, "remove@me2"));
-        assertEquals(0, queue.getSize());
-
-    }
-
-    @Test
-    public void testRemoveWithName() throws MessagingException, InterruptedException, MailAddressException
{
-        assertEquals(0, queue.getSize());
-
-        MailImpl mail = createMail();
-        mail.setName("remove@me1");
-
-        MailImpl mail2 = createMail();
-        mail2.setName("remove@me2");
-
-        queue.enQueue(mail);
-        queue.enQueue(mail2);
-
-        Thread.sleep(200);
-
-        assertEquals(2, queue.getSize());
-        assertEquals(1, queue.remove(ManageableMailQueue.Type.Name, "remove@me1"));
-
-        Thread.sleep(200);
-        assertEquals(1, queue.getSize());
-
-        assertEquals(1, queue.remove(ManageableMailQueue.Type.Name, "remove@me2"));
-        assertEquals(0, queue.getSize());
-
-    }
-
-    protected MailImpl createMail() throws MessagingException {
-        MailImpl mail = new MailImpl();
-        mail.setName("" + System.currentTimeMillis());
-        mail.setAttribute("test1", System.currentTimeMillis());
-        mail.setErrorMessage(UUID.randomUUID().toString());
-        mail.setLastUpdated(new Date());
-        mail.setRecipients(Arrays.asList(new MailAddress("test@test"), new MailAddress("test@test2")));
-        mail.setSender(new MailAddress("sender@senderdomain"));
-
-        MimeMessage message = new MimeMessage(Session.getInstance(new Properties()));
-        message.setText("test");
-        message.setHeader("testheader", "testvalie");
-        message.saveChanges();
-        mail.setMessage(message);
-        return mail;
-
-    }
-
-    @SuppressWarnings("unchecked")
-    protected void checkMail(Mail enqueuedMail, Mail dequeuedMail) throws MessagingException,
IOException {
-        assertEquals(enqueuedMail.getErrorMessage(), dequeuedMail.getErrorMessage());
-        assertEquals(enqueuedMail.getMessageSize(), dequeuedMail.getMessageSize());
-        assertEquals(enqueuedMail.getName(), dequeuedMail.getName());
-        assertEquals(enqueuedMail.getRemoteAddr(), dequeuedMail.getRemoteAddr());
-        assertEquals(enqueuedMail.getState(), dequeuedMail.getState());
-        assertEquals(enqueuedMail.getLastUpdated(), dequeuedMail.getLastUpdated());
-        assertEquals(enqueuedMail.getRemoteHost(), dequeuedMail.getRemoteHost());
-        assertEquals(enqueuedMail.getSender(), dequeuedMail.getSender());
-
-        assertEquals(enqueuedMail.getRecipients().size(), dequeuedMail.getRecipients().size());
-        Iterator<String> attributes = enqueuedMail.getAttributeNames();
-        while (attributes.hasNext()) {
-            String name = attributes.next();
-            assertNotNull(dequeuedMail.getAttribute(name));
-        }
-
-        MimeMessage enqueuedMsg = enqueuedMail.getMessage();
-        MimeMessage dequeuedMsg = dequeuedMail.getMessage();
-        Enumeration<String> enQueuedHeaders = enqueuedMsg.getAllHeaderLines();
-        Enumeration<String> deQueuedHeaders = dequeuedMsg.getAllHeaderLines();
-        while (enQueuedHeaders.hasMoreElements()) {
-            assertEquals(enQueuedHeaders.nextElement(), deQueuedHeaders.nextElement());
-
-        }
-        assertFalse(deQueuedHeaders.hasMoreElements());
-
-        assertEquals(enqueuedMsg.getContent(), dequeuedMsg.getContent());
-
-    }
-
-    @Test
-    public void testPrioritySupport() throws InterruptedException, MessagingException, IOException,
MailAddressException {
-        // should be empty
-        assertEquals(0, queue.getSize());
-
-        Mail mail = createMail();
-        Mail mail2 = createMail();
-        mail2.setAttribute(JMSMailQueue.MAIL_PRIORITY, JMSMailQueue.HIGH_PRIORITY);
-
-        queue.enQueue(mail);
-        queue.enQueue(mail2);
-
-        Thread.sleep(200);
-
-        assertEquals(2, queue.getSize());
-
-        // we should get mail2 first as it has a higher priority set
-        assertEquals(2, queue.getSize());
-        MailQueueItem item2 = queue.deQueue();
-        checkMail(mail2, item2.getMail());
-        item2.done(true);
-
-        Thread.sleep(200);
-
-        assertEquals(1, queue.getSize());
-        MailQueueItem item3 = queue.deQueue();
-        checkMail(mail, item3.getMail());
-        item3.done(true);
-
-        Thread.sleep(200);
-
-        // should be empty
-        assertEquals(0, queue.getSize());
+    @Override
+    public JMSMailQueue getQueue() {
+        return queue;
     }
 
-    @Test
-    public void testBrowse() throws MessagingException, InterruptedException, IOException,
MailAddressException {
-        // should be empty
-        assertEquals(0, queue.getSize());
-
-        Mail mail = createMail();
-        Mail mail2 = createMail();
-
-        queue.enQueue(mail);
-        queue.enQueue(mail2);
-
-        Thread.sleep(200);
-
-        assertEquals(2, queue.getSize());
-
-        MailQueueIterator it = queue.browse();
-        checkMail(mail, it.next().getMail());
-        checkMail(mail2, it.next().getMail());
-        assertFalse(it.hasNext());
-        it.close();
-
-        assertEquals(2, queue.getSize());
-        MailQueueItem item2 = queue.deQueue();
-        checkMail(mail, item2.getMail());
-        item2.done(true);
-        Thread.sleep(200);
-
-        assertEquals(1, queue.getSize());
-        it = queue.browse();
-        checkMail(mail2, it.next().getMail());
-        assertFalse(it.hasNext());
-        it.close();
+    @Override
+    public void setQueue(JMSMailQueue queue) {
+        this.queue = queue;
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message