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 java.io.UnsupportedEncodingException;
33  import java.util.Collection;
34  
35  public class HasHeaderTest extends TestCase {
36  
37      private MimeMessage mockedMimeMessage;
38  
39      private FakeMail mockedMail;
40  
41      private Matcher matcher;
42  
43      private final String HEADER_NAME = "JUNIT";
44  
45      private final String HEADER_VALUE = "test-value";
46  
47      private String headerName = "defaultHeaderName";
48  
49      private String headerValue = "defaultHeaderValue";
50  
51      public HasHeaderTest(String arg0) throws UnsupportedEncodingException {
52          super(arg0);
53      }
54  
55      private void setHeaderName(String headerName) {
56          this.headerName = headerName;
57      }
58  
59      private void setHeaderValue(String headerValue) {
60          this.headerValue = headerValue;
61      }
62  
63      private void setupMockedMimeMessage() throws MessagingException {
64          mockedMimeMessage = MailUtil.createMimeMessage(headerName, headerValue);
65      }
66  
67      private void setupMatcher() throws MessagingException {
68          setupMockedMimeMessage();
69          matcher = new HasHeader();
70          FakeMatcherConfig mci = new FakeMatcherConfig("HasHeader="
71                  + HEADER_NAME, new FakeMailContext());
72          matcher.init(mci);
73      }
74  
75      // test if the Header was matched
76      public void testHeaderIsMatched() throws MessagingException {
77          setHeaderName(HEADER_NAME);
78          setHeaderValue(HEADER_VALUE);
79  
80          setupMockedMimeMessage();
81          mockedMail = MailUtil.createMockMail2Recipients(mockedMimeMessage);
82          setupMatcher();
83  
84          Collection matchedRecipients = matcher.match(mockedMail);
85  
86          assertNotNull(matchedRecipients);
87          assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
88                  .size());
89      }
90  
91      // test if the Header was not matched
92      public void testHeaderIsNotMatched() throws MessagingException {
93          setupMockedMimeMessage();
94          mockedMail = MailUtil.createMockMail2Recipients(mockedMimeMessage);
95          setupMatcher();
96  
97          Collection matchedRecipients = matcher.match(mockedMail);
98  
99          assertNull(matchedRecipients);
100     }
101 }