KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.james.test.mock.mailet.MockMail;
24 import org.apache.james.test.mock.mailet.MockMailContext;
25 import org.apache.james.test.mock.mailet.MockMatcherConfig;
26
27 import org.apache.mailet.MailAddress;
28 import org.apache.mailet.Matcher;
29
30 import javax.mail.MessagingException JavaDoc;
31
32 import java.io.UnsupportedEncodingException JavaDoc;
33 import java.util.Arrays JavaDoc;
34 import java.util.Collection JavaDoc;
35
36 import junit.framework.TestCase;
37
38 public class HostIsTest extends TestCase {
39
40     private MockMail mockedMail;
41
42     private Matcher matcher;
43
44     private final String JavaDoc HOST_NAME = "james.apache.org";
45
46     private MailAddress[] recipients;
47
48     public HostIsTest(String JavaDoc arg0) throws UnsupportedEncodingException JavaDoc {
49         super(arg0);
50     }
51
52     private void setRecipients(MailAddress[] recipients) {
53         this.recipients = recipients;
54     }
55
56     private void setupMockedMail() {
57         mockedMail = new MockMail();
58         mockedMail.setRecipients(Arrays.asList(recipients));
59
60     }
61
62     private void setupMatcher() throws MessagingException JavaDoc {
63         matcher = new HostIs();
64         MockMatcherConfig mci = new MockMatcherConfig("HostIs=" + HOST_NAME,
65                 new MockMailContext());
66         matcher.init(mci);
67     }
68
69     // test if all recipients get returned as matched
70
public void testHostIsMatchedAllRecipients() throws MessagingException JavaDoc {
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 JavaDoc matchedRecipients = matcher.match(mockedMail);
79
80         assertNotNull(matchedRecipients);
81         assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
82                 .size());
83     }
84
85     // test if one recipients get returned as matched
86
public void testHostIsMatchedOneRecipient() throws MessagingException JavaDoc {
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 JavaDoc matchedRecipients = matcher.match(mockedMail);
95
96         assertNotNull(matchedRecipients);
97         assertEquals(matchedRecipients.size(), 1);
98     }
99
100     // test if no recipient get returned cause it not match
101
public void testHostIsNotMatch() throws MessagingException JavaDoc {
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 JavaDoc matchedRecipients = matcher.match(mockedMail);
110
111         assertEquals(matchedRecipients.size(), 0);
112     }
113 }
114
Popular Tags