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  
21  package org.apache.james.transport.matchers;
22  
23  import junit.framework.TestCase;
24  import org.apache.mailet.base.test.FakeMail;
25  import org.apache.mailet.base.test.FakeMailContext;
26  import org.apache.mailet.base.test.FakeMatcherConfig;
27  import org.apache.mailet.base.test.MailUtil;
28  import org.apache.mailet.Matcher;
29  
30  import javax.mail.MessagingException;
31  import javax.mail.internet.MimeMessage;
32  import javax.mail.internet.ParseException;
33  import java.io.UnsupportedEncodingException;
34  import java.util.Collection;
35  
36  public class FetchedFromTest extends TestCase {
37  
38      private MimeMessage mockedMimeMessage;
39  
40      private FakeMail mockedMail;
41  
42      private Matcher matcher;
43  
44      private final String HEADER_NAME = "X-fetched-from";
45  
46      private final String HEADER_VALUE = "james-user";
47  
48      private String headerName = "defaultHeaderName";
49  
50      private String headerValue = "defaultHeaderValue";
51  
52      public FetchedFromTest(String arg0) throws UnsupportedEncodingException {
53          super(arg0);
54      }
55  
56      private void setHeaderName(String headerName) {
57          this.headerName = headerName;
58      }
59  
60      private void setHeaderValue(String headerValue) {
61          this.headerValue = headerValue;
62      }
63  
64      private void setupMockedMimeMessage() throws MessagingException {
65          mockedMimeMessage = MailUtil.createMimeMessage(headerName, headerValue);
66      }
67  
68      private void setupMockedMail(MimeMessage m) throws ParseException {
69          mockedMail = MailUtil.createMockMail2Recipients(m);
70      }
71  
72      private void setupMatcher() throws MessagingException {
73          setupMockedMimeMessage();
74          matcher = new FetchedFrom();
75          FakeMatcherConfig mci = new FakeMatcherConfig("FetchedFrom="
76                  + HEADER_VALUE, new FakeMailContext());
77          matcher.init(mci);
78      }
79  
80      // test if the Header was matched
81      public void testHeaderIsMatched() throws MessagingException {
82          setHeaderName(HEADER_NAME);
83          setHeaderValue(HEADER_VALUE);
84  
85          setupMockedMimeMessage();
86          setupMockedMail(mockedMimeMessage);
87          setupMatcher();
88  
89          Collection matchedRecipients = matcher.match(mockedMail);
90          assertNotNull(matchedRecipients);
91          assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
92                  .size());
93      }
94  
95      // test if the Header was not matched
96      public void testHeaderIsNotMatched() throws MessagingException {
97          setHeaderName(HEADER_NAME);
98          setHeaderValue(headerValue);
99  
100         setupMockedMimeMessage();
101         setupMockedMail(mockedMimeMessage);
102         setupMatcher();
103 
104         Collection matchedRecipients = matcher.match(mockedMail);
105         assertNull(matchedRecipients);
106     }
107 
108     // test if the Header was removed after matched
109     public void testHeaderWasRemovedAfterMatched() throws MessagingException {
110         setHeaderName(HEADER_NAME);
111         setHeaderValue(HEADER_VALUE);
112 
113         setupMockedMimeMessage();
114         setupMockedMail(mockedMimeMessage);
115         setupMatcher();
116 
117         Collection matchedRecipients = matcher.match(mockedMail);
118         Collection matchedRecipients2 = matcher.match(mockedMail);
119 
120         assertNotNull(matchedRecipients);
121         assertNull(matchedRecipients2);
122     }
123 }