james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ber...@apache.org
Subject svn commit: r422895 - in /james/server/trunk/src: java/org/apache/james/management/ java/org/apache/james/remotemanager/ java/org/apache/james/services/ test/org/apache/james/management/ test/org/apache/james/test/mock/javaxmail/
Date Mon, 17 Jul 2006 22:15:59 GMT
Author: berndf
Date: Mon Jul 17 15:15:57 2006
New Revision: 422895

URL: http://svn.apache.org/viewvc?rev=422895&view=rev
Log:
- add filtering options to spool management + unit tests (following proposal by Norman taken
from JAMES-530)
- fix MockMimeMessage.getRecipients(Message.RecipientType)

Added:
    james/server/trunk/src/java/org/apache/james/management/SpoolFilter.java
Modified:
    james/server/trunk/src/java/org/apache/james/management/SpoolManagement.java
    james/server/trunk/src/java/org/apache/james/management/SpoolManagementMBean.java
    james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandler.java
    james/server/trunk/src/java/org/apache/james/services/SpoolManagementService.java
    james/server/trunk/src/test/org/apache/james/management/SpoolManagementTest.java
    james/server/trunk/src/test/org/apache/james/test/mock/javaxmail/MockMimeMessage.java

Added: james/server/trunk/src/java/org/apache/james/management/SpoolFilter.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/management/SpoolFilter.java?rev=422895&view=auto
==============================================================================
--- james/server/trunk/src/java/org/apache/james/management/SpoolFilter.java (added)
+++ james/server/trunk/src/java/org/apache/james/management/SpoolFilter.java Mon Jul 17 15:15:57
2006
@@ -0,0 +1,114 @@
+/***********************************************************************
+ * Copyright (c) 2006 The Apache Software Foundation.                  *
+ * All rights reserved.                                                *
+ * ------------------------------------------------------------------- *
+ * Licensed 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.management;
+
+import org.apache.oro.text.regex.MalformedPatternException;
+import org.apache.oro.text.regex.Pattern;
+import org.apache.oro.text.regex.Perl5Compiler;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * immutual collection of filers used to specify which mail should be processed by SpoolManagement
+ * criterias are:
+ * exact state match
+ * all headers match all related headerValue regular expressions
+ */
+public class SpoolFilter {
+
+    public static final SpoolFilter ERRORMAIL_FILTER = new SpoolFilter("error");
+
+    private String state = null;
+
+    /**
+     * Map<String headerName, String headerValueRegex>
+     */
+    private final Map headerFilters = new HashMap();
+
+    /**
+     * Map<String headerName, Pattern>
+     */
+    private final Map headerFiltersCompiled = new HashMap();
+
+    public SpoolFilter(String state, String header, String headerValueRegex) {
+        this.state = state;
+        if (header != null) headerFilters.put(header, headerValueRegex);
+    }
+
+    public SpoolFilter(String state) {
+        this.state = state;
+    }
+
+    public SpoolFilter(String header, String headerValueRegex) {
+        this.state = null;
+        if (header != null) headerFilters.put(header, headerValueRegex);
+    }
+
+    /**
+     * @param state
+     * @param headerFilters Map<String headerName, String headerValueRegex>
+     */
+    public SpoolFilter(String state, Map headerFilters) {
+        this.state = state;
+        this.headerFilters.putAll(headerFilters);
+        this.headerFilters.remove(null); // NULL is not acceptable here
+    }
+
+    public boolean doFilter() {
+        return doFilterHeader() || doFilterState();
+    }
+
+    public boolean doFilterState() {
+        return state != null;
+    }
+
+    public boolean doFilterHeader() {
+        return headerFilters.size() > 0;
+    }
+
+    public String getState() {
+        return state;
+    }
+
+    public Iterator getHeaders() {
+        return headerFilters.keySet().iterator();
+    }
+
+    public String getHeaderValueRegex(String header) {
+        return (String) headerFilters.get(header);
+    }
+
+    public Pattern getHeaderValueRegexCompiled(String header) throws SpoolManagementException
{
+        Perl5Compiler compiler = new Perl5Compiler();
+
+        // try to reuse cached pattern
+        if (headerFiltersCompiled.get(header) != null) return (Pattern)headerFiltersCompiled.get(header);

+
+        String headerValueRegex = getHeaderValueRegex(header);
+        if (headerValueRegex == null) return null;
+        try {
+            return compiler.compile(headerValueRegex, Perl5Compiler.READ_ONLY_MASK);
+        } catch (MalformedPatternException e) {
+            throw new SpoolManagementException("failed to compile regular expression", e);
+        }
+    }
+    
+}

Modified: james/server/trunk/src/java/org/apache/james/management/SpoolManagement.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/management/SpoolManagement.java?rev=422895&r1=422894&r2=422895&view=diff
==============================================================================
--- james/server/trunk/src/java/org/apache/james/management/SpoolManagement.java (original)
+++ james/server/trunk/src/java/org/apache/james/management/SpoolManagement.java Mon Jul 17
15:15:57 2006
@@ -27,14 +27,21 @@
 import org.apache.james.services.SpoolRepository;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
+import org.apache.oro.text.regex.Pattern;
+import org.apache.oro.text.regex.Perl5Matcher;
 
 import javax.mail.MessagingException;
+import javax.mail.Address;
+import javax.mail.internet.MimeMessage;
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
-import java.util.ArrayList;
-import java.util.Collection;
 
+/**
+ * high-level management of spool contents like list, remove, resend
+ */
 public class SpoolManagement implements Serviceable, SpoolManagementService, SpoolManagementMBean
{
 
     private Store mailStore;
@@ -48,17 +55,88 @@
         setStore(mailStore);
     }
 
-    public String[] listSpoolItems(String spoolRepositoryURL) throws SpoolManagementException
{
+    public String[] listSpoolItems(String spoolRepositoryURL, String state, String header,
String headerValueRegex) 
+            throws SpoolManagementException {
+        return listSpoolItems(spoolRepositoryURL, new SpoolFilter(state, header, headerValueRegex));
+    }
+
+    public String[] listSpoolItems(String spoolRepositoryURL, SpoolFilter filter) throws
SpoolManagementException {
         List spoolItems;
         try {
-            spoolItems = getSpoolItems(spoolRepositoryURL);
+            spoolItems = getSpoolItems(spoolRepositoryURL, filter);
         } catch (Exception e) {
              throw new SpoolManagementException(e);
         }
         return (String[]) spoolItems.toArray(new String[]{});
     }
 
-    public List getSpoolItems(String spoolRepositoryURL) throws ServiceException, MessagingException
{
+
+    protected boolean filterMatches(Mail mail, SpoolFilter filter) throws SpoolManagementException
{
+        if (filter == null || !filter.doFilter()) return true;
+
+        if (filter.doFilterState() && !mail.getState().equalsIgnoreCase(filter.getState()))
return false;
+        
+        if (filter.doFilterHeader()) {
+
+            Perl5Matcher matcher = new Perl5Matcher();
+            
+            // check, if there is a match for every header/regex pair
+            Iterator headers = filter.getHeaders();
+            while (headers.hasNext()) {
+                String header = (String) headers.next();
+                
+                String[] headerValues;
+                try {
+                    headerValues = mail.getMessage().getHeader(header);
+                    if (headerValues == null) {
+                        if (header.equalsIgnoreCase("to")) {
+                            headerValues = addressesToStrings(mail.getMessage().getRecipients(MimeMessage.RecipientType.TO));
+                        }
+                        else if (header.equalsIgnoreCase("cc")) { 
+                            headerValues = addressesToStrings(mail.getMessage().getRecipients(MimeMessage.RecipientType.CC));
+                        }
+                        else if (header.equalsIgnoreCase("bcc")) { 
+                            headerValues = addressesToStrings(mail.getMessage().getRecipients(MimeMessage.RecipientType.BCC));
+                        }
+                        else if (header.equalsIgnoreCase("from")) { 
+                            headerValues = new String[]{mail.getMessage().getSender().toString()};
+                        }
+                    }
+                } catch (MessagingException e) {
+                    throw new SpoolManagementException("could not filter mail by headers",
e);
+                }
+                if (headerValues == null) return false; // no header for this criteria
+
+                Pattern pattern = filter.getHeaderValueRegexCompiled(header);
+
+                // the regex must match at least one entry for the header
+                boolean matched = false;
+                for (int i = 0; i < headerValues.length; i++) {
+                    String headerValue = headerValues[i];
+                    if (matcher.matches(headerValue, pattern)) {
+                        matched = true;
+                        break;
+                    }
+                }
+                if (!matched) return false;
+            }
+        }
+            
+        return true;
+    }
+
+    private String[] addressesToStrings(Address[] addresses) {
+        if (addresses == null) return null;
+        if (addresses.length == 0) return new String[]{};
+        String[] addressStrings = new String[addresses.length];
+        for (int i = 0; i < addresses.length; i++) {
+            addressStrings[i] = addresses[i].toString();
+        }
+        return addressStrings;
+    }
+
+    public List getSpoolItems(String spoolRepositoryURL, SpoolFilter filter)
+            throws ServiceException, MessagingException, SpoolManagementException {
         SpoolRepository spoolRepository = getSpoolRepository(spoolRepositoryURL);
 
         List items = new ArrayList();
@@ -69,8 +147,7 @@
             String key = spoolR.next().toString();
             Mail m = spoolRepository.retrieve(key);
 
-            // Only show email if its in error state.
-            if (m.getState().equals(Mail.ERROR)) {
+            if (filterMatches(m, filter)) {
                 StringBuffer itemInfo = new StringBuffer();
                 itemInfo.append("key: ").append(key).append(" sender: ").append(m.getSender()).append("
recipient:");
                 Collection recipients = m.getRecipients();
@@ -85,34 +162,39 @@
         return items;
     }
 
-    public int removeSpoolItems(String spoolRepositoryURL, String key) throws SpoolManagementException
{
+    public int removeSpoolItems(String spoolRepositoryURL, String key, String state, String
header, String headerValueRegex) 
+            throws SpoolManagementException {
+        return removeSpoolItems(spoolRepositoryURL, key, new SpoolFilter(state, header, headerValueRegex));
+    }
+
+    public int removeSpoolItems(String spoolRepositoryURL, String key, SpoolFilter filter)
throws SpoolManagementException {
         try {
-            return removeSpoolItems(spoolRepositoryURL, key, null);
+            return removeSpoolItems(spoolRepositoryURL, key, null, filter);
         } catch (Exception e) {
             throw new SpoolManagementException(e);
         }
     }
 
-    public int removeSpoolItems(String spoolRepositoryURL, String key, List lockingFailures)
throws ServiceException, MessagingException {
+    public int removeSpoolItems(String spoolRepositoryURL, String key, List lockingFailures,
SpoolFilter filter) throws ServiceException, MessagingException {
         int count = 0;
         SpoolRepository spoolRepository = getSpoolRepository(spoolRepositoryURL);
 
         if (key != null) {
-            count = removeMail(spoolRepository, key, count, lockingFailures);
+            count = removeMail(spoolRepository, key, count, lockingFailures, filter);
         } else {
             Iterator spoolR = spoolRepository.list();
 
             while (spoolR.hasNext()) {
                 key = (String)spoolR.next();
-                count = removeMail(spoolRepository, key, count, lockingFailures);
+                count = removeMail(spoolRepository, key, count, lockingFailures, filter);
             }
         }
         return count;
     }
 
-    private int removeMail(SpoolRepository spoolRepository, String key, int count, List lockingFailures)
throws MessagingException {
+    private int removeMail(SpoolRepository spoolRepository, String key, int count, List lockingFailures,
SpoolFilter filter) throws MessagingException {
         try {
-            if (removeMail(spoolRepository, key)) count++;
+            if (removeMail(spoolRepository, key, filter)) count++;
         } catch (IllegalStateException e) {
             lockingFailures.add(key);
         } catch (SpoolManagementException e) {
@@ -121,22 +203,27 @@
         return count;
     }
 
-    public int resendSpoolItems(String spoolRepositoryURL, String key) throws SpoolManagementException
{
+    public int resendSpoolItems(String spoolRepositoryURL, String key, SpoolFilter filter)
throws SpoolManagementException {
         try {
-            return resendSpoolItems(spoolRepositoryURL, key, null);
+            return resendSpoolItems(spoolRepositoryURL, key, null, filter);
         } catch (Exception e) {
             throw new SpoolManagementException(e);
         }
     }
 
-    public int resendSpoolItems(String spoolRepositoryURL, String key, List lockingFailures)
throws ServiceException, MessagingException {
+    public int resendSpoolItems(String spoolRepositoryURL, String key, String state, String
header, String headerValueRegex) throws SpoolManagementException {
+        return resendSpoolItems(spoolRepositoryURL, key, new SpoolFilter(state, header, headerValueRegex));
+    }
+
+    public int resendSpoolItems(String spoolRepositoryURL, String key, List lockingFailures,
SpoolFilter filter)
+            throws ServiceException, MessagingException, SpoolManagementException {
         int count = 0;
         SpoolRepository spoolRepository = getSpoolRepository(spoolRepositoryURL);
 
         // check if an key was given as argument
         if (key != null) {
             try {
-                if (resendErrorMail(spoolRepository, key)) count++;
+                if (resendMail(spoolRepository, key, filter)) count++;
             } catch (IllegalStateException e) {
                 if (lockingFailures != null) lockingFailures.add(key);
             }
@@ -147,7 +234,7 @@
             while (spoolR.hasNext()) {
                 key = spoolR.next().toString();
                 try {
-                    if (resendErrorMail(spoolRepository, key)) count++;
+                    if (resendMail(spoolRepository, key, filter)) count++;
                 } catch (IllegalStateException e) {
                     if (lockingFailures != null) lockingFailures.add(key);
                 }
@@ -161,17 +248,18 @@
      * 
      * @param spoolRepository The spoolRepository
      * @param key The message key
-     * @return true orf false
+     * @param filter
+     * @return true or false
      * @throws MessagingException Get thrown if there happen an error on modify the mail
      */
-    private boolean resendErrorMail(SpoolRepository spoolRepository, String key)
-            throws MessagingException, IllegalStateException {
+    private boolean resendMail(SpoolRepository spoolRepository, String key, SpoolFilter filter)
+            throws MessagingException, IllegalStateException, SpoolManagementException {
         if (!spoolRepository.lock(key)) throw new IllegalStateException("locking failure");
 
         // get the mail and set the error_message to "0" that will force the spoolmanager
to try to deliver it now!
         Mail m = spoolRepository.retrieve(key);
 
-        if (m.getState().equals(Mail.ERROR)) {
+        if (filterMatches(m, filter)) {
 
             // this will force Remotedelivery to try deliver the mail now!
             m.setLastUpdated(new Date(0));
@@ -194,15 +282,17 @@
      * Remove the mail that belongs to the given key and spoolRepository 
      * @param spoolRepository The spoolRepository
      * @param key The message key
+     * @param filter
      * @return true or false
      * @throws MessagingException Get thrown if there happen an error on modify the mail
      */
-    private boolean removeMail(SpoolRepository spoolRepository, String key) throws MessagingException,
SpoolManagementException {
+    private boolean removeMail(SpoolRepository spoolRepository, String key, SpoolFilter filter)

+            throws MessagingException, SpoolManagementException {
         if (!spoolRepository.lock(key)) throw new IllegalStateException("locking failure");
 
         Mail m = spoolRepository.retrieve(key);
         if (m == null) throw new SpoolManagementException("mail not available having key
" + key);
-        if (!m.getState().equals(Mail.ERROR)) return false;
+        if (!filterMatches(m, filter)) return false;
 
         spoolRepository.remove(key);
         return true;

Modified: james/server/trunk/src/java/org/apache/james/management/SpoolManagementMBean.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/management/SpoolManagementMBean.java?rev=422895&r1=422894&r2=422895&view=diff
==============================================================================
--- james/server/trunk/src/java/org/apache/james/management/SpoolManagementMBean.java (original)
+++ james/server/trunk/src/java/org/apache/james/management/SpoolManagementMBean.java Mon
Jul 17 15:15:57 2006
@@ -26,38 +26,54 @@
 public interface SpoolManagementMBean {
 
     /**
-     * List mails on the spool whose state is ERROR
+     * List mails on the spool matching the given criteria
      *
      * @phoenix:mx-operation
-     * @phoenix:mx-description List mails on the spool whose state is ERROR
+     * @phoenix:mx-description List mails on the spool matching the given criteria
      *
      * @param spoolRepositoryURL specifies the spool
+     * @param state only mails in the given state are processed, or ALL if NULL
+     * @param header the header whose value should be checked
+     * @param headerValueRegex regular expression matched against header value. only matching
mails are processed
      * @return number of removed items
+     * 
+     * @throws SpoolManagementException
      */
-    String[] listSpoolItems(String spoolRepositoryURL) throws SpoolManagementException;
+    String[] listSpoolItems(String spoolRepositoryURL, String state, String header, String
headerValueRegex) 
+            throws SpoolManagementException;
     
     /**
-     * Removes one specific or all mails from the given spool repository
+     * Removes one specific or all mails from the given spool repository matching the given
criteria
      *
      * @phoenix:mx-operation
-     * @phoenix:mx-description Removes one specific or all mails from the given spool repository
+     * @phoenix:mx-description Removes one specific or all mails from the given spool repository
matching 
+     * the given criteria
      *
      * @param spoolRepositoryURL specifies the spool
      * @param key identifies the item to be removed. if NULL, all items are removed
+     * @param state only mails in the given state are processed, or ALL if NULL
+     * @param header the header whose value should be checked
+     * @param headerValueRegex regular expression matched against header value. only matching
mails are processed
      * @return number of removed items
      */
-    int removeSpoolItems(String spoolRepositoryURL, String key) throws SpoolManagementException;
+    int removeSpoolItems(String spoolRepositoryURL, String key, String state, String header,
String headerValueRegex) 
+            throws SpoolManagementException;
 
     /**
-     * (Re-)tries to send one specific or all mails with status ERROR in the given spool
repository
+     * (Re-)tries to send one specific or all mails in the given spool repository matching
the given criteria
      *
      * @phoenix:mx-operation
-     * @phoenix:mx-description (Re-)tries to send one specific or all mails with status ERROR
in the given spool repository
+     * @phoenix:mx-description (Re-)tries to send one specific or all mails in the given
spool repository
+     * matching the given criteria
      *
      * @param spoolRepositoryURL specifies the spool
      * @param key identifies the item to be sent. if NULL, all items with status ERROR are
sent
+     * @param state only mails in the given state are processed, or ALL if NULL
+     * @param header the header whose value should be checked
+     * @param headerValueRegex regular expression matched against header value. only matching
mails are processed
      * @return number of processed items
      */
-    int resendSpoolItems(String spoolRepositoryURL, String key) throws SpoolManagementException;
+    int resendSpoolItems(String spoolRepositoryURL, String key, String state, String header,
String headerValueRegex) 
+            throws SpoolManagementException;
 
 }

Modified: james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandler.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandler.java?rev=422895&r1=422894&r2=422895&view=diff
==============================================================================
--- james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandler.java (original)
+++ james/server/trunk/src/java/org/apache/james/remotemanager/RemoteManagerHandler.java Mon
Jul 17 15:15:57 2006
@@ -18,6 +18,7 @@
 package org.apache.james.remotemanager;
 
 import org.apache.james.Constants;
+import org.apache.james.management.SpoolFilter;
 import org.apache.james.core.AbstractJamesHandler;
 import org.apache.james.services.JamesUser;
 import org.apache.james.services.User;
@@ -878,7 +879,7 @@
         String url = argument;
 
         try {
-            List spoolItems = theConfigData.getSpoolManagement().getSpoolItems(url);
+            List spoolItems = theConfigData.getSpoolManagement().getSpoolItems(url, SpoolFilter.ERRORMAIL_FILTER);
             count = spoolItems.size();
             if (count > 0) out.println("Messages in spool:");
             for (Iterator iterator = spoolItems.iterator(); iterator.hasNext();) {
@@ -920,7 +921,7 @@
         String url = args[0];
         String key = args.length == 2 ? args[1] : null;
         try {
-            count = theConfigData.getSpoolManagement().resendSpoolItems(url, key, null);
+            count = theConfigData.getSpoolManagement().resendSpoolItems(url, key, null, SpoolFilter.ERRORMAIL_FILTER);
             out.println("Number of flushed mails: " + count);
             out.flush();
 
@@ -960,7 +961,7 @@
 
         try {
             ArrayList lockingFailures = new ArrayList();
-            int count =  theConfigData.getSpoolManagement().removeSpoolItems(url, key, lockingFailures);
+            int count =  theConfigData.getSpoolManagement().removeSpoolItems(url, key, lockingFailures,
SpoolFilter.ERRORMAIL_FILTER);
 
             for (Iterator iterator = lockingFailures.iterator(); iterator.hasNext();) {
                 String lockFailureKey = (String) iterator.next();

Modified: james/server/trunk/src/java/org/apache/james/services/SpoolManagementService.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/services/SpoolManagementService.java?rev=422895&r1=422894&r2=422895&view=diff
==============================================================================
--- james/server/trunk/src/java/org/apache/james/services/SpoolManagementService.java (original)
+++ james/server/trunk/src/java/org/apache/james/services/SpoolManagementService.java Mon
Jul 17 15:15:57 2006
@@ -19,6 +19,8 @@
 package org.apache.james.services;
 
 import org.apache.avalon.framework.service.ServiceException;
+import org.apache.james.management.SpoolManagementException;
+import org.apache.james.management.SpoolFilter;
 
 import javax.mail.MessagingException;
 import java.util.List;
@@ -26,9 +28,9 @@
 public interface SpoolManagementService {
     String ROLE = "org.apache.james.services.SpoolManagementService";
 
-    public int removeSpoolItems(String spoolRepositoryURL, String key, List lockingFailures)
throws ServiceException, MessagingException;
+    public int removeSpoolItems(String spoolRepositoryURL, String key, List lockingFailures,
SpoolFilter filter) throws ServiceException, MessagingException;
     
-    public int resendSpoolItems(String spoolRepositoryURL, String key, List lockingFailures)
throws ServiceException, MessagingException;
+    public int resendSpoolItems(String spoolRepositoryURL, String key, List lockingFailures,
SpoolFilter filter) throws ServiceException, MessagingException, SpoolManagementException;
 
-    public List getSpoolItems(String spoolRepositoryURL) throws ServiceException, MessagingException;
+    public List getSpoolItems(String spoolRepositoryURL, SpoolFilter filter) throws ServiceException,
MessagingException, SpoolManagementException;
 }

Modified: james/server/trunk/src/test/org/apache/james/management/SpoolManagementTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/management/SpoolManagementTest.java?rev=422895&r1=422894&r2=422895&view=diff
==============================================================================
--- james/server/trunk/src/test/org/apache/james/management/SpoolManagementTest.java (original)
+++ james/server/trunk/src/test/org/apache/james/management/SpoolManagementTest.java Mon Jul
17 15:15:57 2006
@@ -31,6 +31,7 @@
 import org.apache.mailet.Mail;
 
 import javax.mail.MessagingException;
+import java.util.HashMap;
 
 /**
  * Tests the SpoolManagement
@@ -58,22 +59,75 @@
         return serviceManager;
     }
 
-    public void testListSpoolItems() throws SpoolManagementException, MessagingException
{
-        String[] mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL);
+    public void testListSpoolItemsDontFilter() throws SpoolManagementException, MessagingException
{
+        String[] mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL, SpoolFilter.ERRORMAIL_FILTER);
         assertEquals("emtpy spool", 0, mailList.length); // no mail in spool
 
         MockMail mockMail1 = createSpoolMail(Mail.DEFAULT, "subj1", 1);
         String messageID1 = mockMail1.getMessage().getMessageID();
 
-        mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL);
-        assertEquals("no error mail in spool", 0, mailList.length); 
+        MockMail mockMail2 = createSpoolMail(Mail.ERROR, "subj2", 2);
+        String messageID2 = mockMail2.getMessage().getMessageID();
+
+        mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL, new SpoolFilter(null));
+        assertEquals("finds all mails", 2, mailList.length);
+    }
+
+    public void testListSpoolItemsFilterByState() throws SpoolManagementException, MessagingException
{
+        MockMail mockMail1 = createSpoolMail(Mail.DEFAULT, "subj1", 1);
+        String messageID1 = mockMail1.getMessage().getMessageID();
+
+        String[] mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL, SpoolFilter.ERRORMAIL_FILTER);
+        assertEquals("no error mail in spool", 0, mailList.length);
 
         MockMail mockMail2 = createSpoolMail(Mail.ERROR, "subj2", 2);
         String messageID2 = mockMail2.getMessage().getMessageID();
 
-        mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL);
+        mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL, SpoolFilter.ERRORMAIL_FILTER);
         assertEquals("finds only 1 error mail", 1, mailList.length);
         assertTrue("finds only error mail", mailList[0].indexOf(messageID2) >= 0);
+
+        mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL, new SpoolFilter(Mail.DEFAULT));
+        assertEquals("finds only 1 default mail", 1, mailList.length);
+        assertTrue("finds only default mail", mailList[0].indexOf(messageID1) >= 0);
+
+        mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL, new SpoolFilter(Mail.DEFAULT.toUpperCase()));
+        assertEquals("finds default mail uppercase state", 1, mailList.length);
+    }
+
+    public void testListSpoolItemsFilterByHeader() throws SpoolManagementException, MessagingException
{
+
+        MockMail mockMail1 = createSpoolMail(Mail.DEFAULT, "TestHeader", "value1", 1);
+        String messageID1 = mockMail1.getMessage().getMessageID();
+
+        MockMail mockMail2 = createSpoolMail(Mail.ERROR, "TestHeader", "value2", 2);
+        String messageID2 = mockMail2.getMessage().getMessageID();
+
+        MockMail mockMail3 = createSpoolMail(Mail.ERROR, "TestHeader", "another", 3);
+        String messageID3 = mockMail3.getMessage().getMessageID();
+
+        String[] mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL, new SpoolFilter("TestHeader",
"value[12]"));
+        assertEquals("find all mails", 2, mailList.length);
+
+        mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL, new SpoolFilter("TestHeader",
"(.*)1"));
+        assertEquals("find first mail", 1, mailList.length);
+        assertTrue("finds only default mail", mailList[0].indexOf(messageID1) >= 0);
+
+        HashMap headerMap = new HashMap();
+        headerMap.put("TestHeader", "(.*)1");
+        headerMap.put("to", "test2@james.apache.org");
+        SpoolFilter matchableFilterWith2Headers = new SpoolFilter(null, headerMap);
+        mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL, matchableFilterWith2Headers);
+        assertEquals("find one mail", 1, mailList.length);
+        assertTrue("finds only default mail", mailList[0].indexOf(messageID1) >= 0);
+
+        headerMap = new HashMap();
+        headerMap.put("TestHeader", "(.*)1");
+        headerMap.put("UNmatchABLE", "test2@james.apache.org");
+        SpoolFilter unmatchableFilterWith2Headers = new SpoolFilter(null, headerMap);
+        mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL, unmatchableFilterWith2Headers);
+        assertEquals("find no mail", 0, mailList.length);
+
     }
 
     public void testRemoveErrorMails() throws SpoolManagementException, MessagingException
{
@@ -86,8 +140,8 @@
         MockMail mockMail3 = createSpoolMail(Mail.ERROR, "subj3", 3);
         String messageID3 = mockMail3.getMessage().getMessageID();
 
-        assertEquals("one mail removed", 2, m_spoolManagement.removeSpoolItems(OUTGOING_SPOOL,
null));
-        String[] mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL);
+        assertEquals("one mail removed", 2, m_spoolManagement.removeSpoolItems(OUTGOING_SPOOL,
null, SpoolFilter.ERRORMAIL_FILTER));
+        String[] mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL, SpoolFilter.ERRORMAIL_FILTER);
         assertEquals("finds only left non-error mail", 0, mailList.length);
     }
 
@@ -101,14 +155,22 @@
         MockMail mockMail3 = createSpoolMail(Mail.ERROR, "subj3", 3);
         String messageID3 = mockMail3.getMessage().getMessageID();
 
-        assertEquals("one mail removed", 1, m_spoolManagement.removeSpoolItems(OUTGOING_SPOOL,
messageID3));
-        String[] mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL);
+        assertEquals("one mail removed", 1, m_spoolManagement.removeSpoolItems(OUTGOING_SPOOL,
messageID3, SpoolFilter.ERRORMAIL_FILTER));
+        String[] mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL, SpoolFilter.ERRORMAIL_FILTER);
         assertEquals("still finds 1 error mail", 1, mailList.length);
         assertTrue("still finds error mail", mailList[0].indexOf(messageID2) >= 0);
     }
 
     private MockMail createSpoolMail(String state, String subject, int number) throws MessagingException
{
         MockMimeMessage mimeMessage = Util.createMimeMessage(subject, number);
+        MockMail mockMail = Util.createMockMail2Recipients(mimeMessage);
+        mockMail.setState(state);
+        m_mockSpoolRepository.store(mockMail);
+        return mockMail;
+    }
+
+    private MockMail createSpoolMail(String state, String headerName, String headerRegex,
int number) throws MessagingException {
+        MockMimeMessage mimeMessage = Util.createMimeMessage(headerName, headerRegex, "test",
number);
         MockMail mockMail = Util.createMockMail2Recipients(mimeMessage);
         mockMail.setState(state);
         m_mockSpoolRepository.store(mockMail);

Modified: james/server/trunk/src/test/org/apache/james/test/mock/javaxmail/MockMimeMessage.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/test/mock/javaxmail/MockMimeMessage.java?rev=422895&r1=422894&r2=422895&view=diff
==============================================================================
--- james/server/trunk/src/test/org/apache/james/test/mock/javaxmail/MockMimeMessage.java
(original)
+++ james/server/trunk/src/test/org/apache/james/test/mock/javaxmail/MockMimeMessage.java
Mon Jul 17 15:15:57 2006
@@ -90,7 +90,13 @@
     }
 
     public Address[] getRecipients(Message.RecipientType recipientType) throws MessagingException
{
-        return (Address[]) getRecipientsList(recipientType).toArray();
+        List recipientsList = getRecipientsList(recipientType);
+        List recipientAddresses = new ArrayList();
+        for (Iterator iterator = recipientsList.iterator(); iterator.hasNext();) {
+            String recipient = (String) iterator.next();
+            recipientAddresses.add(new InternetAddress(recipient));
+        }
+        return (Address[]) (recipientAddresses.toArray(new Address[]{}));
     }
 
     private List getRecipientsList(Message.RecipientType recipientType) {



---------------------------------------------------------------------
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