View Javadoc

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 HostIsTest extends TestCase {
42  
43      private MimeMessage mockedMimeMessage;
44  
45      private MockMail mockedMail;
46  
47      private Matcher matcher;
48  
49      private final String HOST_NAME = "james.apache.org";
50  
51      private MailAddress[] recipients;
52  
53      public HostIsTest(String arg0) throws UnsupportedEncodingException {
54          super(arg0);
55      }
56  
57      private void setRecipients(MailAddress[] recipients) {
58          this.recipients = recipients;
59      }
60  
61      private void setupMockedMimeMessage() throws MessagingException {
62          String sender = "test@james.apache.org";
63          String rcpt = "test2@james.apache.org";
64  
65          mockedMimeMessage = new MockMimeMessage();
66          mockedMimeMessage.setFrom(new InternetAddress(sender));
67          mockedMimeMessage.setRecipients(RecipientType.TO, rcpt);
68          mockedMimeMessage.setSubject("testmail");
69          mockedMimeMessage.setText("testtext");
70          mockedMimeMessage.saveChanges();
71  
72      }
73  
74      private void setupMockedMail(MimeMessage m) {
75          mockedMail = new MockMail();
76          mockedMail.setMessage(m);
77          mockedMail.setRecipients(Arrays.asList(recipients));
78  
79      }
80  
81      private void setupMatcher() throws MessagingException {
82          setupMockedMimeMessage();
83          matcher = new HostIs();
84          MockMatcherConfig mci = new MockMatcherConfig("HostIs=" + HOST_NAME,
85                  new MockMailContext());
86          matcher.init(mci);
87      }
88  
89      // test if all recipients get returned as matched
90      public void testHostIsMatchedAllRecipients() throws MessagingException {
91          setRecipients(new MailAddress[] {
92                  new MailAddress("test@james.apache.org"),
93                  new MailAddress("test2@james.apache.org") });
94  
95          setupMockedMimeMessage();
96          setupMockedMail(mockedMimeMessage);
97          setupMatcher();
98  
99          Collection matchedRecipients = matcher.match(mockedMail);
100 
101         assertNotNull(matchedRecipients);
102         assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
103                 .size());
104     }
105 
106     // test if one recipients get returned as matched
107     public void testHostIsMatchedOneRecipient() throws MessagingException {
108         setRecipients(new MailAddress[] {
109                 new MailAddress("test@james2.apache.org"),
110                 new MailAddress("test2@james.apache.org") });
111 
112         setupMockedMimeMessage();
113         setupMockedMail(mockedMimeMessage);
114         setupMatcher();
115 
116         Collection matchedRecipients = matcher.match(mockedMail);
117 
118         assertNotNull(matchedRecipients);
119         assertEquals(matchedRecipients.size(), 1);
120     }
121 
122     // test if no recipient get returned cause it not match
123     public void testHostIsNotMatch() throws MessagingException {
124         setRecipients(new MailAddress[] {
125                 new MailAddress("test@james2.apache.org"),
126                 new MailAddress("test2@james2.apache.org") });
127 
128         setupMockedMimeMessage();
129         setupMockedMail(mockedMimeMessage);
130         setupMatcher();
131 
132         Collection matchedRecipients = matcher.match(mockedMail);
133 
134         assertEquals(matchedRecipients.size(), 0);
135     }
136 }