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.FakeMimeMessage;
25  import org.apache.mailet.base.test.FakeMail;
26  import org.apache.mailet.base.test.FakeMailContext;
27  import org.apache.mailet.base.test.FakeMatcherConfig;
28  import org.apache.mailet.base.test.MailUtil;
29  import org.apache.mailet.Matcher;
30  
31  import javax.mail.MessagingException;
32  import javax.mail.internet.MimeMessage;
33  import java.io.UnsupportedEncodingException;
34  
35  public abstract class AbstractSubjectIsTest extends TestCase {
36  
37      protected FakeMail mockedMail;
38  
39      protected Matcher matcher;
40  
41      private String subject = null;
42  
43      private FakeMimeMessage mockedMimeMessage;
44  
45      public AbstractSubjectIsTest(String arg0)
46              throws UnsupportedEncodingException {
47          super(arg0);
48      }
49  
50      protected void setSubject(String subject) {
51          this.subject = subject;
52      }
53  
54      protected void setupMockedMail(MimeMessage m) {
55          mockedMail = new FakeMail();
56          mockedMail.setMessage(m);
57  
58      }
59  
60      protected void setupMockedMimeMessage() throws MessagingException {
61          mockedMimeMessage = MailUtil.createMimeMessage("test", "test");
62          mockedMimeMessage.setSubject(subject);
63      }
64  
65      protected void setupMatcher() throws MessagingException {
66          matcher = createMatcher();
67          FakeMatcherConfig mci = new FakeMatcherConfig(getConfigOption()
68                  + getSubjectName(), new FakeMailContext());
69          matcher.init(mci);
70      }
71  
72      protected void setupAll() throws MessagingException {
73          setupMockedMimeMessage();
74          setupMockedMail(mockedMimeMessage);
75          setupMatcher();
76      }
77  
78      protected abstract String getConfigOption();
79  
80      protected abstract String getSubjectName();
81  
82      protected abstract Matcher createMatcher();
83  }