1   /*****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.james.transport.matchers;
21  
22  import org.apache.james.test.mock.javaxmail.MockMimeMessage;
23  import org.apache.james.test.mock.mailet.MockMail;
24  import org.apache.james.test.mock.mailet.MockMailContext;
25  import org.apache.james.test.mock.mailet.MockMatcherConfig;
26  
27  import org.apache.mailet.MailAddress;
28  import org.apache.mailet.Matcher;
29  
30  import javax.mail.MessagingException;
31  import javax.mail.internet.InternetAddress;
32  import javax.mail.internet.MimeMessage;
33  import javax.mail.internet.MimeMessage.RecipientType;
34  
35  import java.io.UnsupportedEncodingException;
36  import java.util.Arrays;
37  import java.util.Collection;
38  
39  import junit.framework.TestCase;
40  
41  public class RecipientIsRegexTest extends TestCase {
42  
43      private MimeMessage mockedMimeMessage;
44  
45      private MockMail mockedMail;
46  
47      private Matcher matcher;
48  
49      private MailAddress[] recipients;
50  
51      private String regex = ".*";
52  
53      public RecipientIsRegexTest(String arg0)
54              throws UnsupportedEncodingException {
55          super(arg0);
56      }
57  
58      private void setRecipients(MailAddress[] recipients) {
59          this.recipients = recipients;
60      }
61  
62      private void setRegex(String regex) {
63          this.regex = regex;
64      }
65  
66      private void setupMockedMimeMessage() throws MessagingException {
67          String sender = "test@james.apache.org";
68          String rcpt = "test2@james.apache.org";
69  
70          mockedMimeMessage = new MockMimeMessage();
71          mockedMimeMessage.setFrom(new InternetAddress(sender));
72          mockedMimeMessage.setRecipients(RecipientType.TO, rcpt);
73          mockedMimeMessage.setSubject("testmail");
74          mockedMimeMessage.setText("testtext");
75          mockedMimeMessage.saveChanges();
76  
77      }
78  
79      private void setupMockedMail(MimeMessage m) {
80          mockedMail = new MockMail();
81          mockedMail.setMessage(m);
82          mockedMail.setRecipients(Arrays.asList(recipients));
83  
84      }
85  
86      private void setupMatcher() throws MessagingException {
87          setupMockedMimeMessage();
88          matcher = new RecipientIsRegex();
89          MockMatcherConfig mci = new MockMatcherConfig("RecipientIsRegex="
90                  + regex, new MockMailContext());
91          matcher.init(mci);
92      }
93  
94      
95      public void testRegexIsMatchedAllRecipients() throws MessagingException {
96          setRecipients(new MailAddress[] { new MailAddress(
97                  "test@james.apache.org") });
98          setRegex(".*@.*");
99          setupMockedMimeMessage();
100         setupMockedMail(mockedMimeMessage);
101         setupMatcher();
102 
103         Collection matchedRecipients = matcher.match(mockedMail);
104 
105         assertNotNull(matchedRecipients);
106         assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
107                 .size());
108     }
109 
110     
111     public void testRegexIsMatchedOneRecipient() throws MessagingException {
112         setRecipients(new MailAddress[] {
113                 new MailAddress("test@james.apache.org"),
114                 new MailAddress("test2@james.apache.org") });
115         setRegex("^test@.*");
116         setupMockedMimeMessage();
117         setupMockedMail(mockedMimeMessage);
118         setupMatcher();
119 
120         Collection matchedRecipients = matcher.match(mockedMail);
121 
122         assertNotNull(matchedRecipients);
123         assertEquals(matchedRecipients.size(), 1);
124     }
125 
126     
127     public void testRegexIsNotMatch() throws MessagingException {
128         setRecipients(new MailAddress[] {
129                 new MailAddress("test@james2.apache.org"),
130                 new MailAddress("test2@james2.apache.org") });
131         setRegex(".*//+");
132         setupMockedMimeMessage();
133         setupMockedMail(mockedMimeMessage);
134         setupMatcher();
135 
136         Collection matchedRecipients = matcher.match(mockedMail);
137 
138         assertEquals(matchedRecipients.size(), 0);
139     }
140 
141     
142     public void testRegexIsNotMatchedCauseError() throws MessagingException {
143         Collection matchedRecipients = null;
144         String invalidRegex = "(!(";
145         String regexException = null;
146         String exception = "Malformed pattern: " + invalidRegex;
147 
148         setRecipients(new MailAddress[] {
149                 new MailAddress("test@james2.apache.org"),
150                 new MailAddress("test2@james2.apache.org") });
151 
152         setRegex(invalidRegex);
153         setupMockedMimeMessage();
154         setupMockedMail(mockedMimeMessage);
155 
156         try {
157             setupMatcher();
158             matchedRecipients = matcher.match(mockedMail);
159         } catch (MessagingException m) {
160             m.printStackTrace();
161             regexException = m.getMessage();
162         }
163         
164         assertNull(matchedRecipients);
165         assertEquals(regexException, exception);
166 
167     }
168 }