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.UnsupportedEncodingException;
24 import java.util.Collection;
25
26 import javax.mail.MessagingException;
27 import javax.mail.internet.ParseException;
28
29 import junit.framework.TestCase;
30
31 import org.apache.mailet.base.test.MailUtil;
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.Matcher;
36
37 public class SMTPIsAuthNetworkTest extends TestCase {
38
39 private FakeMail mockedMail;
40
41 private Matcher matcher;
42
43 private boolean isAuthorized = false;
44
45 private final String MAIL_ATTRIBUTE_NAME = "org.apache.james.SMTPIsAuthNetwork";
46
47 public SMTPIsAuthNetworkTest(String arg0)
48 throws UnsupportedEncodingException {
49 super(arg0);
50 }
51
52 private void setIsAuthorized(boolean isAuthorized) {
53 this.isAuthorized = isAuthorized;
54 }
55
56 private void setupMockedMail() throws ParseException {
57 mockedMail = MailUtil.createMockMail2Recipients(null);
58 if (isAuthorized) {
59 mockedMail.setAttribute(MAIL_ATTRIBUTE_NAME, "true");
60 }
61 }
62
63 private void setupMatcher() throws MessagingException {
64 matcher = new SMTPIsAuthNetwork();
65 FakeMatcherConfig mci = new FakeMatcherConfig("SMTPIsAuthNetwork",
66 new FakeMailContext());
67 matcher.init(mci);
68 }
69
70 public void testIsAuthNetwork() throws MessagingException {
71 setIsAuthorized(true);
72 setupMockedMail();
73 setupMatcher();
74
75 Collection matchedRecipients = matcher.match(mockedMail);
76
77 assertNotNull(matchedRecipients);
78 assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
79 .size());
80 }
81
82 public void testIsNotAuthNetwork() throws MessagingException {
83 setIsAuthorized(false);
84 setupMockedMail();
85 setupMatcher();
86
87 Collection matchedRecipients = matcher.match(mockedMail);
88
89 assertNull(matchedRecipients);
90 }
91 }