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 org.apache.mailet.base.test.FakeMail;
24 import org.apache.mailet.base.test.FakeMailContext;
25 import org.apache.mailet.base.test.FakeMatcherConfig;
26
27 import org.apache.mailet.MailAddress;
28 import org.apache.mailet.Matcher;
29
30 import javax.mail.MessagingException;
31
32 import java.io.UnsupportedEncodingException;
33 import java.util.Arrays;
34 import java.util.Collection;
35
36 import junit.framework.TestCase;
37
38 public class HostIsTest extends TestCase {
39
40 private FakeMail mockedMail;
41
42 private Matcher matcher;
43
44 private final String HOST_NAME = "james.apache.org";
45
46 private MailAddress[] recipients;
47
48 public HostIsTest(String arg0) throws UnsupportedEncodingException {
49 super(arg0);
50 }
51
52 private void setRecipients(MailAddress[] recipients) {
53 this.recipients = recipients;
54 }
55
56 private void setupMockedMail() {
57 mockedMail = new FakeMail();
58 mockedMail.setRecipients(Arrays.asList(recipients));
59
60 }
61
62 private void setupMatcher() throws MessagingException {
63 matcher = new HostIs();
64 FakeMatcherConfig mci = new FakeMatcherConfig("HostIs=" + HOST_NAME,
65 new FakeMailContext());
66 matcher.init(mci);
67 }
68
69
70 public void testHostIsMatchedAllRecipients() throws MessagingException {
71 setRecipients(new MailAddress[] {
72 new MailAddress("test@james.apache.org"),
73 new MailAddress("test2@james.apache.org") });
74
75 setupMockedMail();
76 setupMatcher();
77
78 Collection matchedRecipients = matcher.match(mockedMail);
79
80 assertNotNull(matchedRecipients);
81 assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
82 .size());
83 }
84
85
86 public void testHostIsMatchedOneRecipient() throws MessagingException {
87 setRecipients(new MailAddress[] {
88 new MailAddress("test@james2.apache.org"),
89 new MailAddress("test2@james.apache.org") });
90
91 setupMockedMail();
92 setupMatcher();
93
94 Collection matchedRecipients = matcher.match(mockedMail);
95
96 assertNotNull(matchedRecipients);
97 assertEquals(matchedRecipients.size(), 1);
98 }
99
100
101 public void testHostIsNotMatch() throws MessagingException {
102 setRecipients(new MailAddress[] {
103 new MailAddress("test@james2.apache.org"),
104 new MailAddress("test2@james2.apache.org") });
105
106 setupMockedMail();
107 setupMatcher();
108
109 Collection matchedRecipients = matcher.match(mockedMail);
110
111 assertEquals(matchedRecipients.size(), 0);
112 }
113 }