You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by no...@apache.org on 2010/09/22 20:35:26 UTC

svn commit: r1000129 - in /james/server/trunk/queue-api/src/test: ./ java/ java/org/ java/org/apache/ java/org/apache/james/ java/org/apache/james/queue/ java/org/apache/james/queue/MockMailQueue.java java/org/apache/james/queue/MockMailQueueFactory.java

Author: norman
Date: Wed Sep 22 18:35:26 2010
New Revision: 1000129

URL: http://svn.apache.org/viewvc?rev=1000129&view=rev
Log:
Add mock implementation for MailQueue and MailQeueuFactory (to be used later in junit tests)

Added:
    james/server/trunk/queue-api/src/test/
    james/server/trunk/queue-api/src/test/java/
    james/server/trunk/queue-api/src/test/java/org/
    james/server/trunk/queue-api/src/test/java/org/apache/
    james/server/trunk/queue-api/src/test/java/org/apache/james/
    james/server/trunk/queue-api/src/test/java/org/apache/james/queue/
    james/server/trunk/queue-api/src/test/java/org/apache/james/queue/MockMailQueue.java
    james/server/trunk/queue-api/src/test/java/org/apache/james/queue/MockMailQueueFactory.java

Added: james/server/trunk/queue-api/src/test/java/org/apache/james/queue/MockMailQueue.java
URL: http://svn.apache.org/viewvc/james/server/trunk/queue-api/src/test/java/org/apache/james/queue/MockMailQueue.java?rev=1000129&view=auto
==============================================================================
--- james/server/trunk/queue-api/src/test/java/org/apache/james/queue/MockMailQueue.java (added)
+++ james/server/trunk/queue-api/src/test/java/org/apache/james/queue/MockMailQueue.java Wed Sep 22 18:35:26 2010
@@ -0,0 +1,91 @@
+/****************************************************************
+ * 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;
+
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import javax.mail.MessagingException;
+
+import org.apache.mailet.Mail;
+
+public class MockMailQueue implements MailQueue{
+
+    private final LinkedBlockingQueue<Mail> queue = new LinkedBlockingQueue<Mail>();
+    private boolean throwException;
+    private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
+    
+    /**
+     * Throw an {@link MailQueueException} on next operation
+     */
+    public void throwExceptionOnNextOperation() {
+        this.throwException = true;
+    }
+    
+    public void deQueue(DequeueOperation operation) throws MailQueueException, MessagingException {
+        if (throwException) {
+            throwException = false;
+            throw new MailQueueException("Mock");
+        }
+        try {
+            operation.process(queue.take());
+        } catch (InterruptedException e) {
+            throw new MailQueueException("Mock",e);
+        }
+    }
+
+    public void enQueue(final Mail mail, long delay, TimeUnit unit) throws MailQueueException, MessagingException {
+        if (throwException) {
+            throwException = false;
+            throw new MailQueueException("Mock");
+        }        
+        scheduler.schedule(new Runnable() {
+
+            public void run() {
+                try {
+                    queue.put(mail);
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                }                
+            }
+            
+        }, delay, unit);
+    }
+
+    public void enQueue(Mail mail) throws MailQueueException, MessagingException {
+        if (throwException) {
+            throwException = false;
+            throw new MailQueueException("Mock");
+        }
+        try {
+            queue.put(mail);
+        } catch (InterruptedException e) {
+            throw new MailQueueException("Mock",e);
+        }
+    }
+
+    
+    public void clear() {
+        queue.clear();
+    }
+}

Added: james/server/trunk/queue-api/src/test/java/org/apache/james/queue/MockMailQueueFactory.java
URL: http://svn.apache.org/viewvc/james/server/trunk/queue-api/src/test/java/org/apache/james/queue/MockMailQueueFactory.java?rev=1000129&view=auto
==============================================================================
--- james/server/trunk/queue-api/src/test/java/org/apache/james/queue/MockMailQueueFactory.java (added)
+++ james/server/trunk/queue-api/src/test/java/org/apache/james/queue/MockMailQueueFactory.java Wed Sep 22 18:35:26 2010
@@ -0,0 +1,42 @@
+/****************************************************************
+ * 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;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class MockMailQueueFactory implements MailQueueFactory{
+    private final Map<String, MailQueue> queues = new HashMap<String, MailQueue>();
+
+    public synchronized MailQueue getQueue(String name) {
+        MailQueue queue = queues.get(name);
+        if (queue == null) {
+            queue = new MockMailQueue();
+            queues.put(name, queue);
+        }
+
+        return queue;
+    }
+
+    public void clear() {
+        queues.clear();
+    }
+}



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