james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nor...@apache.org
Subject svn commit: r910050 - in /james/mailet/standard/trunk/src: main/java/org/apache/james/transport/matchers/ test/java/org/apache/james/transport/matchers/
Date Sun, 14 Feb 2010 17:26:13 GMT
Author: norman
Date: Sun Feb 14 17:26:13 2010
New Revision: 910050

URL: http://svn.apache.org/viewvc?rev=910050&view=rev
Log:
Add And,Not,Or and XOR Matcher. Part of JAMES-948. Thx to Ralph B Holland for the patch

Added:
    james/mailet/standard/trunk/src/main/java/org/apache/james/transport/matchers/And.java
    james/mailet/standard/trunk/src/main/java/org/apache/james/transport/matchers/Not.java
    james/mailet/standard/trunk/src/main/java/org/apache/james/transport/matchers/Or.java
    james/mailet/standard/trunk/src/main/java/org/apache/james/transport/matchers/Xor.java
    james/mailet/standard/trunk/src/test/java/org/apache/james/transport/matchers/AndTest.java
    james/mailet/standard/trunk/src/test/java/org/apache/james/transport/matchers/OrTest.java
    james/mailet/standard/trunk/src/test/java/org/apache/james/transport/matchers/XorTest.java

Added: james/mailet/standard/trunk/src/main/java/org/apache/james/transport/matchers/And.java
URL: http://svn.apache.org/viewvc/james/mailet/standard/trunk/src/main/java/org/apache/james/transport/matchers/And.java?rev=910050&view=auto
==============================================================================
--- james/mailet/standard/trunk/src/main/java/org/apache/james/transport/matchers/And.java
(added)
+++ james/mailet/standard/trunk/src/main/java/org/apache/james/transport/matchers/And.java
Sun Feb 14 17:26:13 2010
@@ -0,0 +1,109 @@
+package org.apache.james.transport.matchers;
+
+/****************************************************************
+ * 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.                                           *
+ ****************************************************************/
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.ArrayList;
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.Mail;
+import javax.mail.MessagingException;
+import org.apache.mailet.Matcher;
+import org.apache.mailet.base.CompositeMatcherBase;
+
+/**
+ * This matcher performs And conjunction between the two recipients
+ * 
+ */
+public class And extends CompositeMatcherBase {
+
+    /**
+     * This is the And CompositeMatcher - consider it to be an intersection of
+     * the results. If any match returns an empty recipient result the matching
+     * is short-circuited.
+     * 
+     * @return Collection of Recipient from the And composition results of the
+     *         child Matchers.
+     */
+    public Collection match(Mail mail) throws MessagingException {
+        Collection finalResult = null;
+        Matcher matcher = null;
+        boolean first = true;
+        for (Iterator matcherIter = iterator(); matcherIter.hasNext();) {
+            matcher = (Matcher) (matcherIter.next());
+            Collection result = matcher.match(mail);
+
+            if (result == null) {
+                // short-circuit
+                // log("Matching with " +
+                // matcher.getMatcherConfig().getMatcherName() +
+                // " result.size()=0");
+                return new ArrayList(0);
+            }
+            if (result.size() == 0) {
+                return result;
+            }
+
+            // log("Matching with " +
+            // matcher.getMatcherConfig().getMatcherName() +
+            // " result.size()="+result.size());
+
+            if (first) {
+                finalResult = result;
+                first = false;
+            } else {
+                // Check if we need to And ...
+                // if the finalResult and the subsequent result are the same
+                // collection, then it contains the same recipients
+                // so we can short-circuit building the AND of the two
+                if (finalResult != result) {
+                    if (result != null) {
+                        // the two results are different collections, so we AND
+                        // them
+                        // Ensure that the finalResult only contains recipients
+                        // in the result collection
+                        Collection newResult = new ArrayList();
+                        MailAddress recipient = null;
+                        for (Iterator i = finalResult.iterator(); i.hasNext();) {
+                            recipient = (MailAddress) i.next();
+                            // log("recipient="+recipient.toString());
+                            if (result.contains(recipient)) {
+                                newResult.add(recipient);
+                            }
+                        }
+                        recipient = null;
+                        // basically the finalResult gets replaced with a
+                        // smaller result
+                        // otherwise finalResult would have been equal to result
+                        // (in all cases)
+                        finalResult = newResult;
+                    } else {
+                        finalResult = result;
+                    }
+                }
+            }
+            result = null;
+        }
+        matcher = null;
+        // log("answer is "+finalResult.toString());
+        return finalResult;
+    }
+
+}

Added: james/mailet/standard/trunk/src/main/java/org/apache/james/transport/matchers/Not.java
URL: http://svn.apache.org/viewvc/james/mailet/standard/trunk/src/main/java/org/apache/james/transport/matchers/Not.java?rev=910050&view=auto
==============================================================================
--- james/mailet/standard/trunk/src/main/java/org/apache/james/transport/matchers/Not.java
(added)
+++ james/mailet/standard/trunk/src/main/java/org/apache/james/transport/matchers/Not.java
Sun Feb 14 17:26:13 2010
@@ -0,0 +1,60 @@
+package org.apache.james.transport.matchers;
+
+/****************************************************************
+ * 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.                                           *
+ ****************************************************************/
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import org.apache.mailet.Matcher;
+import org.apache.mailet.Mail;
+import org.apache.mailet.base.CompositeMatcherBase;
+
+import javax.mail.MessagingException;
+
+public class Not extends CompositeMatcherBase {
+
+    /**
+     * This is the Not CompositeMatcher - consider what wasn't in the result set
+     * of each child matcher. Of course it is easier to understand if it only
+     * includes one matcher in the composition, the normal recommended use. @See
+     * CompositeMatcher interface.
+     * 
+     * @return Collectiom of Recipient from the Negated composition of the child
+     *         Matcher(s).
+     */
+    public Collection match(Mail mail) throws MessagingException {
+        Collection finalResult = mail.getRecipients();
+        Matcher matcher = null;
+        for (Iterator matcherIter = iterator(); matcherIter.hasNext();) {
+            matcher = (Matcher) (matcherIter.next());
+            // log("Matching with " +
+            // matcher.getMatcherConfig().getMatcherName());
+            Collection result = matcher.match(mail);
+            if (result == finalResult) {
+                // Not is an empty list
+                finalResult = null;
+            } else if (result != null) {
+                finalResult = new ArrayList(finalResult);
+                finalResult.removeAll(result);
+            }
+        }
+        return finalResult;
+    }
+}

Added: james/mailet/standard/trunk/src/main/java/org/apache/james/transport/matchers/Or.java
URL: http://svn.apache.org/viewvc/james/mailet/standard/trunk/src/main/java/org/apache/james/transport/matchers/Or.java?rev=910050&view=auto
==============================================================================
--- james/mailet/standard/trunk/src/main/java/org/apache/james/transport/matchers/Or.java
(added)
+++ james/mailet/standard/trunk/src/main/java/org/apache/james/transport/matchers/Or.java
Sun Feb 14 17:26:13 2010
@@ -0,0 +1,105 @@
+/****************************************************************
+ * 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.transport.matchers;
+
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.Mail;
+import org.apache.mailet.Matcher;
+import org.apache.mailet.base.CompositeMatcherBase;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.ArrayList;
+import javax.mail.MessagingException;
+
+public class Or extends CompositeMatcherBase {
+
+    /**
+     * This is the Or CompositeMatcher - consider it to be a union of the
+     * results. If any match results in a full set of recipients the matching is
+     * short-circuited.
+     * 
+     * @return Collection of Recipient from the Or composition results of the
+     *         child matchers.
+     */
+    public Collection match(Mail mail) throws MessagingException {
+        Collection finalResult = null;
+        Matcher matcher = null;
+        boolean first = true;
+
+        // the size of the complete set of recipients
+        int size = mail.getRecipients().size();
+
+        // Loop through until the finalResult is full or all the child matchers
+        // have been executed
+        for (Iterator matcherIter = iterator(); matcherIter.hasNext();) {
+            matcher = (Matcher) matcherIter.next();
+            // log("Matching with "
+            // + matcher
+            // .getMatcherConfig()
+            // .getMatcherName()
+            // );
+            Collection result = matcher.match(mail);
+            if (first) {
+                if (result == null) {
+                    result = new ArrayList(0);
+                }
+                finalResult = result;
+                first = false;
+            } else {
+                // Check if we need to Or ...
+                // if the finalResult and the subsequent result are the same
+                // collection, then it contains the same recipients
+                // so we can short-circuit building the OR of the two
+                if (finalResult != result) {
+                    if (result != null) {
+                        if (finalResult == null) {
+                            finalResult = result;
+                        } else {
+                            // the two results are different collections, so we
+                            // must OR them
+                            // Ensure that the finalResult only contains one
+                            // copy of the recipients in the result collection
+                            MailAddress recipient = null;
+                            for (Iterator i = result.iterator(); i.hasNext();) {
+                                recipient = (MailAddress) i.next();
+                                if (!finalResult.contains(recipient)) {
+                                    finalResult.add(recipient);
+                                }
+                            }
+                            recipient = null;
+                        }
+                    }
+                }
+            }
+            if (finalResult.size() == size) {
+                // we have a complete set of recipients, no need to OR in
+                // anymore
+                // i.e. short-circuit the Or
+                break;
+            }
+            result = null;
+            matcher = null;
+        }
+        // log("OrMatch: end.");
+        return finalResult;
+    }
+
+}

Added: james/mailet/standard/trunk/src/main/java/org/apache/james/transport/matchers/Xor.java
URL: http://svn.apache.org/viewvc/james/mailet/standard/trunk/src/main/java/org/apache/james/transport/matchers/Xor.java?rev=910050&view=auto
==============================================================================
--- james/mailet/standard/trunk/src/main/java/org/apache/james/transport/matchers/Xor.java
(added)
+++ james/mailet/standard/trunk/src/main/java/org/apache/james/transport/matchers/Xor.java
Sun Feb 14 17:26:13 2010
@@ -0,0 +1,93 @@
+/****************************************************************
+ * 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.transport.matchers;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.ArrayList;
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.Mail;
+import javax.mail.MessagingException;
+import org.apache.mailet.Matcher;
+import org.apache.mailet.base.CompositeMatcherBase;
+
+public class Xor extends CompositeMatcherBase {
+
+    /**
+     * This is the Xor CompositeMatcher - consider it to be the inequality
+     * operator for recipients. If any recipients match other matcher results
+     * then the result does not include that recipient.
+     * 
+     * @return Collection of Recipients from the Xor composition of the child
+     *         matchers.
+     */
+    public Collection match(Mail mail) throws MessagingException {
+        Collection finalResult = null;
+        Matcher matcher = null;
+        boolean first = true;
+        for (Iterator matcherIter = iterator(); matcherIter.hasNext();) {
+            matcher = (Matcher) (matcherIter.next());
+            Collection result = matcher.match(mail);
+            if (result == null) {
+                result = new ArrayList(0);
+            }
+            // log("Matching with " +
+            // matcher.getMatcherConfig().getMatcherName() +
+            // " result="+result.toString() );
+
+            if (first) {
+                finalResult = result;
+                first = false;
+            } else {
+                // Check if we need to Xor ...
+                // if the finalResult and the subsequent result are the same
+                // collection, then it contains the same recipients
+                // so we can short-circuit building the XOR and return an empty
+                // set
+                if (finalResult == result) {
+                    // the XOR of the same collection is empty
+                    finalResult.clear();
+                    // log("same collection - so clear");
+                } else {
+                    // the two results are different collections, so we XOR them
+                    // Ensure that the finalResult does not contain recipients
+                    // in the result collection
+                    MailAddress recipient = null;
+                    for (Iterator i = result.iterator(); i.hasNext();) {
+                        recipient = (MailAddress) (i.next());
+                        if (!finalResult.contains(recipient)) {
+                            finalResult.add(recipient);
+                        } else {
+                            finalResult.remove(recipient);
+                        }
+                    }
+                    recipient = null;
+                    // log("xor recipients into new finalResult="+finalResult);
+                }
+                // basically the finalResult gets replaced with a smaller result
+                // otherwise finalResult would have been equal to result (in all
+                // cases)
+            }
+            result = null;
+        }
+        return finalResult;
+    }
+
+}

Added: james/mailet/standard/trunk/src/test/java/org/apache/james/transport/matchers/AndTest.java
URL: http://svn.apache.org/viewvc/james/mailet/standard/trunk/src/test/java/org/apache/james/transport/matchers/AndTest.java?rev=910050&view=auto
==============================================================================
--- james/mailet/standard/trunk/src/test/java/org/apache/james/transport/matchers/AndTest.java
(added)
+++ james/mailet/standard/trunk/src/test/java/org/apache/james/transport/matchers/AndTest.java
Sun Feb 14 17:26:13 2010
@@ -0,0 +1,114 @@
+/****************************************************************
+ * 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.transport.matchers;
+
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.Matcher;
+import org.apache.mailet.CompositeMatcher;
+import org.apache.mailet.base.test.FakeMail;
+import org.apache.mailet.base.test.FakeMailContext;
+import org.apache.mailet.base.test.FakeMatcherConfig;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.ParseException;
+
+import java.io.UnsupportedEncodingException;
+import java.util.Arrays;
+import java.util.Collection;
+
+import junit.framework.TestCase;
+
+public class AndTest extends TestCase {
+
+    private FakeMailContext context;
+    private FakeMail mockedMail;
+
+    private CompositeMatcher matcher;
+
+    public AndTest(String arg0) throws UnsupportedEncodingException {
+        super(arg0);
+    }
+
+    private void setupMockedMail() throws ParseException {
+        mockedMail = new FakeMail();
+        mockedMail.setRecipients(Arrays.asList(new MailAddress[] {
+        new MailAddress("test@james.apache.org"),
+        new MailAddress("test2@james.apache.org") }));
+
+    }
+
+    /**
+     * Setup a composite Or matcher and test it
+     * @throws MessagingException
+     */
+    private void setupMatcher() throws MessagingException {
+        context = new FakeMailContext();
+        matcher = new And();
+        FakeMatcherConfig mci = new FakeMatcherConfig("And",context);
+        matcher.init(mci);
+    }
+    
+    private void setupChild(String match) throws MessagingException
+    {
+        Matcher child = null;
+        if (match.equals("All"))
+        {
+            child = new All();
+        }
+        else
+        {
+            child = new RecipientIs();
+        }
+        FakeMatcherConfig sub = new FakeMatcherConfig(match,context);
+        child.init(sub);
+        matcher.add(child);
+        
+    }
+
+    // test if all recipients was returned
+    public void testAndIntersectSameTwice() throws MessagingException {
+        setupMockedMail();
+        setupMatcher();
+        setupChild("RecipientIs=test@james.apache.org");
+        setupChild("RecipientIs=test@james.apache.org");
+        setupChild("All");
+            
+        Collection matchedRecipients = matcher.match(mockedMail);
+
+        assertNotNull(matchedRecipients);
+        assertEquals(1,matchedRecipients.size());
+        MailAddress address = (MailAddress)matchedRecipients.iterator().next();
+        assertEquals(address,"test@james.apache.org");
+    }
+    
+    public void testAndNoIntersect() throws MessagingException {
+        setupMockedMail();
+        setupMatcher();
+        setupChild("RecipientIs=test@james.apache.org");
+        setupChild("RecipientIs=test2@james.apache.org");
+            
+        Collection matchedRecipients = matcher.match(mockedMail);
+
+        assertNotNull(matchedRecipients);
+        assertEquals(0,matchedRecipients.size());
+    }
+    
+
+}

Added: james/mailet/standard/trunk/src/test/java/org/apache/james/transport/matchers/OrTest.java
URL: http://svn.apache.org/viewvc/james/mailet/standard/trunk/src/test/java/org/apache/james/transport/matchers/OrTest.java?rev=910050&view=auto
==============================================================================
--- james/mailet/standard/trunk/src/test/java/org/apache/james/transport/matchers/OrTest.java
(added)
+++ james/mailet/standard/trunk/src/test/java/org/apache/james/transport/matchers/OrTest.java
Sun Feb 14 17:26:13 2010
@@ -0,0 +1,96 @@
+/****************************************************************
+ * 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.transport.matchers;
+
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.Matcher;
+import org.apache.mailet.CompositeMatcher;
+import org.apache.mailet.base.test.FakeMail;
+import org.apache.mailet.base.test.FakeMailContext;
+import org.apache.mailet.base.test.FakeMatcherConfig;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.ParseException;
+
+import java.io.UnsupportedEncodingException;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Iterator;
+
+import junit.framework.TestCase;
+
+public class OrTest extends TestCase {
+
+    private FakeMail mockedMail;
+
+    private CompositeMatcher matcher;
+
+    public OrTest(String arg0) throws UnsupportedEncodingException {
+        super(arg0);
+    }
+
+    private void setupMockedMail() throws ParseException {
+        mockedMail = new FakeMail();
+        mockedMail.setRecipients(Arrays.asList(new MailAddress[] {
+        new MailAddress("test@james.apache.org"),
+        new MailAddress("test2@james.apache.org") }));
+
+    }
+
+    /**
+     * Setup a composite Or matcher and test it
+     * @throws MessagingException
+     */
+    private void setupMatcher() throws MessagingException {
+        FakeMailContext context = new FakeMailContext();
+        matcher = new Or();
+        FakeMatcherConfig mci = new FakeMatcherConfig("Or",context);
+        matcher.init(mci);
+        Matcher child = null;
+        FakeMatcherConfig sub = null;
+        child = new RecipientIs();
+        sub = new FakeMatcherConfig("RecipientIsRegex=test@james.apache.org",context);
+        child.init(sub);
+        matcher.add(child);
+        child = new RecipientIs();
+        sub = new FakeMatcherConfig("RecipientIsRegex=test2@james.apache.org",context);
+        child.init(sub);
+        matcher.add(child);
+    }
+
+    // test if all recipients was returned
+    public void testAllRecipientsReturned() throws MessagingException {
+        setupMockedMail();
+        setupMatcher();
+
+        Collection matchedRecipients = matcher.match(mockedMail);
+
+        assertNotNull(matchedRecipients);
+        assertEquals(matchedRecipients.size(), mockedMail.getRecipients().size());
+        
+        // Now ensure they match the actual recipients
+        Iterator iterator = matchedRecipients.iterator(); 
+        MailAddress address = (MailAddress)iterator.next();
+        assertEquals(address,"test@james.apache.org");
+        address = (MailAddress)iterator.next();
+        assertEquals(address,"test2@james.apache.org");
+    }
+
+}

Added: james/mailet/standard/trunk/src/test/java/org/apache/james/transport/matchers/XorTest.java
URL: http://svn.apache.org/viewvc/james/mailet/standard/trunk/src/test/java/org/apache/james/transport/matchers/XorTest.java?rev=910050&view=auto
==============================================================================
--- james/mailet/standard/trunk/src/test/java/org/apache/james/transport/matchers/XorTest.java
(added)
+++ james/mailet/standard/trunk/src/test/java/org/apache/james/transport/matchers/XorTest.java
Sun Feb 14 17:26:13 2010
@@ -0,0 +1,118 @@
+/****************************************************************
+ * 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.transport.matchers;
+
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.Matcher;
+import org.apache.mailet.CompositeMatcher;
+import org.apache.mailet.base.test.FakeMail;
+import org.apache.mailet.base.test.FakeMailContext;
+import org.apache.mailet.base.test.FakeMatcherConfig;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.ParseException;
+
+import java.io.UnsupportedEncodingException;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Iterator;
+
+import junit.framework.TestCase;
+
+public class XorTest extends TestCase {
+
+    private FakeMailContext context;
+    private FakeMail mockedMail;
+
+    private CompositeMatcher matcher;
+
+    public XorTest(String arg0) throws UnsupportedEncodingException {
+        super(arg0);
+    }
+
+    private void setupMockedMail() throws ParseException {
+        mockedMail = new FakeMail();
+        mockedMail.setRecipients(Arrays.asList(new MailAddress[] {
+        new MailAddress("test@james.apache.org"),
+        new MailAddress("test2@james.apache.org") }));
+
+    }
+
+    /**
+     * Setup a composite Or matcher and test it
+     * @throws MessagingException
+     */
+    private void setupMatcher() throws MessagingException {
+        context = new FakeMailContext();
+        matcher = new Xor();
+        FakeMatcherConfig mci = new FakeMatcherConfig("Xor",context);
+        matcher.init(mci);
+    }
+    
+    private void setupChild(String match) throws MessagingException
+    {
+        Matcher child = null;
+        if (match.equals("All"))
+        {
+            child = new All();
+        }
+        else
+        {
+            child = new RecipientIs();
+        }
+        FakeMatcherConfig sub = new FakeMatcherConfig(match,context);
+        child.init(sub);
+        matcher.add(child);
+        
+    }
+
+    // test if all recipients was returned
+    public void testIntersectSame() throws MessagingException {
+        setupMockedMail();
+        setupMatcher();
+        setupChild("RecipientIsRegex=test@james.apache.org");
+        setupChild("RecipientIsRegex=test@james.apache.org");
+            
+        Collection matchedRecipients = matcher.match(mockedMail);
+
+        assertNotNull(matchedRecipients);
+        assertEquals(0,matchedRecipients.size());
+    }
+    
+    public void testNoIntersect() throws MessagingException {
+        setupMockedMail();
+        setupMatcher();
+        setupChild("RecipientIsRegex=test@james.apache.org");
+        setupChild("RecipientIsRegex=test2@james.apache.org");
+            
+        Collection matchedRecipients = matcher.match(mockedMail);
+
+        assertNotNull(matchedRecipients);
+        assertEquals(2,matchedRecipients.size());
+        
+        Iterator iterator = matchedRecipients.iterator();
+        MailAddress address = (MailAddress)iterator.next();
+        assertEquals(address,"test@james.apache.org");
+        address = (MailAddress)iterator.next();
+        assertEquals(address,"test2@james.apache.org");
+    }
+    
+
+}



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