KickJava   Java API By Example, From Geeks To Geeks.

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


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.MockMatcherConfig;
25
26 import org.apache.mailet.Mail;
27 import org.apache.mailet.MailAddress;
28 import org.apache.mailet.MailetContext;
29 import org.apache.mailet.Matcher;
30
31 import javax.mail.MessagingException JavaDoc;
32 import javax.mail.internet.MimeMessage JavaDoc;
33
34 import java.io.UnsupportedEncodingException JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Arrays JavaDoc;
37 import java.util.Collection JavaDoc;
38 import java.util.Iterator JavaDoc;
39
40 import junit.framework.TestCase;
41
42 public class HostIsLocalTest extends TestCase {
43
44     private MockMail mockedMail;
45
46     private Matcher matcher;
47
48     private final String JavaDoc[] LOCALSERVER = new String JavaDoc[] { "james.apache.org" };
49
50     private MailAddress[] recipients;
51
52     public HostIsLocalTest(String JavaDoc arg0) throws UnsupportedEncodingException JavaDoc {
53         super(arg0);
54     }
55
56     private void setRecipients(MailAddress[] recipients) {
57         this.recipients = recipients;
58     }
59
60     private void setupMockedMail() {
61         mockedMail = new MockMail();
62         mockedMail.setRecipients(Arrays.asList(recipients));
63
64     }
65
66     private void setupMatcher() throws MessagingException JavaDoc {
67
68         MailetContext mockMailContext = new MailetContext() {
69
70             Collection JavaDoc localServer = new ArrayList JavaDoc(Arrays.asList(LOCALSERVER));
71
72             public void bounce(Mail mail, String JavaDoc message)
73                     throws MessagingException JavaDoc {
74                 throw new UnsupportedOperationException JavaDoc(
75                         "Unimplemented mock service");
76
77             }
78
79             public void bounce(Mail mail, String JavaDoc message, MailAddress bouncer)
80                     throws MessagingException JavaDoc {
81                 throw new UnsupportedOperationException JavaDoc(
82                         "Unimplemented mock service");
83
84             }
85
86             public Collection JavaDoc getMailServers(String JavaDoc host) {
87                 throw new UnsupportedOperationException JavaDoc(
88                         "Unimplemented mock service");
89             }
90
91             public MailAddress getPostmaster() {
92                 throw new UnsupportedOperationException JavaDoc(
93                         "Unimplemented mock service");
94             }
95
96             public Object JavaDoc getAttribute(String JavaDoc name) {
97                 throw new UnsupportedOperationException JavaDoc(
98                         "Unimplemented mock service");
99             }
100
101             public Iterator JavaDoc getAttributeNames() {
102                 throw new UnsupportedOperationException JavaDoc(
103                         "Unimplemented mock service");
104             }
105
106             public int getMajorVersion() {
107                 throw new UnsupportedOperationException JavaDoc(
108                         "Unimplemented mock service");
109             }
110
111             public int getMinorVersion() {
112                 throw new UnsupportedOperationException JavaDoc(
113                         "Unimplemented mock service");
114             }
115
116             public String JavaDoc getServerInfo() {
117                 throw new UnsupportedOperationException JavaDoc(
118                         "Unimplemented mock service");
119             }
120
121             public boolean isLocalServer(String JavaDoc serverName) {
122                 return localServer.contains(serverName);
123             }
124
125             public boolean isLocalUser(String JavaDoc userAccount) {
126                 throw new UnsupportedOperationException JavaDoc(
127                         "Unimplemented mock service");
128             }
129
130             public boolean isLocalEmail(MailAddress mailAddress) {
131                 throw new UnsupportedOperationException JavaDoc(
132                         "Unimplemented mock service");
133             }
134
135             public void log(String JavaDoc message) {
136                 throw new UnsupportedOperationException JavaDoc(
137                         "Unimplemented mock service");
138             }
139
140             public void log(String JavaDoc message, Throwable JavaDoc t) {
141                 throw new UnsupportedOperationException JavaDoc(
142                         "Unimplemented mock service");
143             }
144
145             public void removeAttribute(String JavaDoc name) {
146                 throw new UnsupportedOperationException JavaDoc(
147                         "Unimplemented mock service");
148             }
149
150             public void sendMail(MimeMessage JavaDoc msg) throws MessagingException JavaDoc {
151                 throw new UnsupportedOperationException JavaDoc(
152                         "Unimplemented mock service");
153             }
154
155             public void sendMail(MailAddress sender, Collection JavaDoc recipients,
156                     MimeMessage JavaDoc msg) throws MessagingException JavaDoc {
157                 throw new UnsupportedOperationException JavaDoc(
158                         "Unimplemented mock service");
159             }
160
161             public void sendMail(MailAddress sender, Collection JavaDoc recipients,
162                     MimeMessage JavaDoc msg, String JavaDoc state) throws MessagingException JavaDoc {
163                 throw new UnsupportedOperationException JavaDoc(
164                         "Unimplemented mock service");
165             }
166
167             public void sendMail(Mail mail) throws MessagingException JavaDoc {
168                 throw new UnsupportedOperationException JavaDoc(
169                         "Unimplemented mock service");
170             }
171
172             public void setAttribute(String JavaDoc name, Object JavaDoc object) {
173                 throw new UnsupportedOperationException JavaDoc(
174                         "Unimplemented mock service");
175             }
176
177             public void storeMail(MailAddress sender, MailAddress recipient,
178                     MimeMessage JavaDoc msg) throws MessagingException JavaDoc {
179                 throw new UnsupportedOperationException JavaDoc(
180                         "Unimplemented mock service");
181             }
182
183             public Iterator JavaDoc getSMTPHostAddresses(String JavaDoc domainName) {
184                 throw new UnsupportedOperationException JavaDoc(
185                         "Unimplemented mock service");
186             }
187
188         };
189
190         matcher = new HostIsLocal();
191         MockMatcherConfig mci = new MockMatcherConfig("HostIsLocal",
192                 mockMailContext);
193         matcher.init(mci);
194     }
195
196     // test if all recipients get returned as matched
197
public void testHostIsMatchedAllRecipients() throws MessagingException JavaDoc {
198         setRecipients(new MailAddress[] {
199                 new MailAddress("test@james.apache.org"),
200                 new MailAddress("test2@james.apache.org") });
201
202         setupMockedMail();
203         setupMatcher();
204
205         Collection JavaDoc matchedRecipients = matcher.match(mockedMail);
206
207         assertNotNull(matchedRecipients);
208         assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
209                 .size());
210     }
211
212     // test if one recipients get returned as matched
213
public void testHostIsMatchedOneRecipient() throws MessagingException JavaDoc {
214         setRecipients(new MailAddress[] {
215                 new MailAddress("test@james2.apache.org"),
216                 new MailAddress("test2@james.apache.org") });
217
218         setupMockedMail();
219         setupMatcher();
220
221         Collection JavaDoc matchedRecipients = matcher.match(mockedMail);
222
223         assertNotNull(matchedRecipients);
224         assertEquals(matchedRecipients.size(), 1);
225     }
226
227     // test if no recipient get returned cause it not match
228
public void testHostIsNotMatch() throws MessagingException JavaDoc {
229         setRecipients(new MailAddress[] {
230                 new MailAddress("test@james2.apache.org"),
231                 new MailAddress("test2@james2.apache.org") });
232
233         setupMockedMail();
234         setupMatcher();
235
236         Collection JavaDoc matchedRecipients = matcher.match(mockedMail);
237
238         assertEquals(matchedRecipients.size(), 0);
239     }
240 }
241
Popular Tags