1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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 }