Author: norman
Date: Thu May  6 13:23:56 2010
New Revision: 941717

URL: http://svn.apache.org/viewvc?rev=941717&view=rev
Log:
Fix NPE in MatcherInverter and add a TestCase (JAMES-996)

Added:
    
james/mailet/base/trunk/src/test/java/org/apache/mailet/base/MatcherInverterTest.java
Modified:
    
james/mailet/base/trunk/src/main/java/org/apache/mailet/base/MatcherInverter.java

Modified: 
james/mailet/base/trunk/src/main/java/org/apache/mailet/base/MatcherInverter.java
URL: 
http://svn.apache.org/viewvc/james/mailet/base/trunk/src/main/java/org/apache/mailet/base/MatcherInverter.java?rev=941717&r1=941716&r2=941717&view=diff
==============================================================================
--- 
james/mailet/base/trunk/src/main/java/org/apache/mailet/base/MatcherInverter.java
 (original)
+++ 
james/mailet/base/trunk/src/main/java/org/apache/mailet/base/MatcherInverter.java
 Thu May  6 13:23:56 2010
@@ -77,8 +77,16 @@ public class MatcherInverter implements 
         // Create a new recipient Collection cause mail.getRecipients() give a 
reference to the internal 
         // list of recipients. If we make changes there the original 
collection whould be corrupted
         Collection recipients = new ArrayList(mail.getRecipients());
+        Collection matchedRcpts = wrappedMatcher.match(mail);
+        
+        // check if a only a part of the recipients matched
+        if (matchedRcpts != null) {
+            recipients.removeAll(matchedRcpts);
+            if (recipients.isEmpty()) {
+                return null;
+            }
+        }
        
-        recipients.removeAll(wrappedMatcher.match(mail));
         return recipients;
     }
 

Added: 
james/mailet/base/trunk/src/test/java/org/apache/mailet/base/MatcherInverterTest.java
URL: 
http://svn.apache.org/viewvc/james/mailet/base/trunk/src/test/java/org/apache/mailet/base/MatcherInverterTest.java?rev=941717&view=auto
==============================================================================
--- 
james/mailet/base/trunk/src/test/java/org/apache/mailet/base/MatcherInverterTest.java
 (added)
+++ 
james/mailet/base/trunk/src/test/java/org/apache/mailet/base/MatcherInverterTest.java
 Thu May  6 13:23:56 2010
@@ -0,0 +1,79 @@
+/****************************************************************
+ * 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.mailet.base;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+import javax.mail.MessagingException;
+
+import junit.framework.TestCase;
+
+import org.apache.mailet.Mail;
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.base.test.FakeMail;
+
+public class MatcherInverterTest extends TestCase {
+
+    public void testAllMatch() throws MessagingException {
+        MatcherInverter inverter = new MatcherInverter(new GenericMatcher() {
+
+            public Collection match(Mail mail) throws MessagingException {
+                return null;
+            }
+        });
+        FakeMail mail = new FakeMail();
+        mail.setRecipients(Arrays.asList(new MailAddress[] { new 
MailAddress("user", "domain") }));
+
+        assertNotNull("Should match all recipients", inverter.match(mail));
+    }
+    
+    public void testNonMatch() throws MessagingException {
+        final MailAddress address1 = new MailAddress("user", "domain");
+        final MailAddress address2 = new MailAddress("user", "domain2");
+        
+        MatcherInverter inverter = new MatcherInverter(new GenericMatcher() {
+
+            public Collection match(Mail mail) throws MessagingException {
+                return mail.getRecipients();
+            }
+        });
+        FakeMail mail = new FakeMail();
+        mail.setRecipients(Arrays.asList(new MailAddress[] { address1, 
address2 }));
+
+        assertNull("Should match all recipients", inverter.match(mail));
+    }
+    
+    public void testOneMatch() throws MessagingException {
+        final MailAddress address1 = new MailAddress("user", "domain");
+        final MailAddress address2 = new MailAddress("user", "domain2");
+        
+        MatcherInverter inverter = new MatcherInverter(new GenericMatcher() {
+
+            public Collection match(Mail mail) throws MessagingException {
+                return Arrays.asList(new MailAddress[] {address1});
+            }
+        });
+        FakeMail mail = new FakeMail();
+        mail.setRecipients(Arrays.asList(new MailAddress[] { address1, 
address2 }));
+
+        assertEquals("Should match one recipient", address2.toString(), 
inverter.match(mail).iterator().next().toString());
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to