KickJava   Java API By Example, From Geeks To Geeks.

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


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