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 java.io.Serializable;
24  import java.util.Collection;
25  
26  import javax.mail.MessagingException;
27  import javax.mail.internet.MimeMessage;
28  import javax.mail.internet.ParseException;
29  
30  import junit.framework.TestCase;
31  
32  import org.apache.mailet.base.test.FakeMail;
33  import org.apache.mailet.base.test.FakeMailContext;
34  import org.apache.mailet.base.test.FakeMatcherConfig;
35  import org.apache.mailet.base.test.MailUtil;
36  import org.apache.mailet.base.GenericMatcher;
37  import org.apache.mailet.Matcher;
38  
39  public abstract class AbstractHasMailAttributeTest extends TestCase {
40      protected MimeMessage mockedMimeMessage;
41  
42      protected FakeMail mockedMail;
43  
44      protected Matcher matcher;
45  
46      protected final String MAIL_ATTRIBUTE_NAME = "org.apache.james.test.junit";
47  
48      protected final String MAIL_ATTRIBUTE_VALUE = "true";
49  
50      protected String mailAttributeName = "org.apache.james";
51  
52      protected String mailAttributeValue = "false";
53  
54      public AbstractHasMailAttributeTest() {
55          super(null);
56      }
57  
58      protected void setMailAttributeName(String mailAttributeName) {
59          this.mailAttributeName = mailAttributeName;
60      }
61  
62      protected void setMailAttributeValue(String mailAttributeValue) {
63          this.mailAttributeValue = mailAttributeValue;
64      }
65  
66      protected void setupMockedMail(MimeMessage m) throws ParseException {
67          mockedMail = MailUtil.createMockMail2Recipients(m);
68          mockedMail.setAttribute(mailAttributeName,
69                  (Serializable) mailAttributeValue);
70      }
71  
72      protected void setupMatcher() throws MessagingException {
73          matcher = createMatcher();
74          FakeMatcherConfig mci = new FakeMatcherConfig(getConfigOption()
75                  + getHasMailAttribute(), new FakeMailContext());
76          matcher.init(mci);
77      }
78  
79      // test if the mail attribute was matched
80      public void testAttributeIsMatched() throws MessagingException {
81          init();
82  
83          setupAll();
84  
85          Collection matchedRecipients = matcher.match(mockedMail);
86  
87          assertNotNull(matchedRecipients);
88          assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
89                  .size());
90      }
91  
92      protected void init() {
93          setMailAttributeName(MAIL_ATTRIBUTE_NAME);
94          setMailAttributeValue(MAIL_ATTRIBUTE_VALUE);
95      }
96  
97      protected void setupAll() throws MessagingException {
98          setupMockedMail(mockedMimeMessage);
99          setupMatcher();
100     }
101 
102     // test if the mail attribute was not matched
103     public void testAttributeIsNotMatched() throws MessagingException {
104         setupAll();
105 
106         Collection matchedRecipients = matcher.match(mockedMail);
107 
108         assertNull(matchedRecipients);
109     }
110 
111     protected abstract String getHasMailAttribute();
112 
113     protected abstract GenericMatcher createMatcher();
114 
115     protected abstract String getConfigOption();
116 }