KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > matchers > AbstractHasMailAttributeTest


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