1 /************************************************************************
2 * Copyright (c) 2006 The Apache Software Foundation. *
3 * All rights reserved. *
4 * ------------------------------------------------------------------- *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you *
6 * may not use this file except in compliance with the License. You *
7 * may obtain a copy of the License at: *
8 * *
9 * http://www.apache.org/licenses/LICENSE-2.0 *
10 * *
11 * Unless required by applicable law or agreed to in writing, software *
12 * distributed under the License is distributed on an "AS IS" BASIS, *
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14 * implied. See the License for the specific language governing *
15 * permissions and limitations under the License. *
16 ***********************************************************************/
17
18 package org.apache.james.transport.matchers;
19
20 import org.apache.james.test.mock.javaxmail.MockMimeMessage;
21 import org.apache.james.test.mock.mailet.MockMail;
22 import org.apache.james.test.mock.mailet.MockMatcherConfig;
23
24 import org.apache.mailet.Mail;
25 import org.apache.mailet.MailAddress;
26 import org.apache.mailet.MailetContext;
27 import org.apache.mailet.Matcher;
28
29 import javax.mail.MessagingException;
30 import javax.mail.internet.InternetAddress;
31 import javax.mail.internet.MimeMessage;
32 import javax.mail.internet.MimeMessage.RecipientType;
33
34 import java.io.UnsupportedEncodingException;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.Collection;
38 import java.util.Iterator;
39
40 import junit.framework.TestCase;
41
42 public class HostIsLocalTest extends TestCase {
43
44 private MimeMessage mockedMimeMessage;
45
46 private MockMail mockedMail;
47
48 private Matcher matcher;
49
50 private final String[] LOCALSERVER = new String[] { "james.apache.org" };
51
52 private MailAddress[] recipients;
53
54 public HostIsLocalTest(String arg0) throws UnsupportedEncodingException {
55 super(arg0);
56 }
57
58 private void setRecipients(MailAddress[] recipients) {
59 this.recipients = recipients;
60 }
61
62 private void setupMockedMimeMessage() throws MessagingException {
63 String sender = "test@james.apache.org";
64 String rcpt = "test2@james.apache.org";
65
66 mockedMimeMessage = new MockMimeMessage();
67 mockedMimeMessage.setFrom(new InternetAddress(sender));
68 mockedMimeMessage.setRecipients(RecipientType.TO, rcpt);
69 mockedMimeMessage.setSubject("testmail");
70 mockedMimeMessage.setText("testtext");
71 mockedMimeMessage.saveChanges();
72
73 }
74
75 private void setupMockedMail(MimeMessage m) {
76 mockedMail = new MockMail();
77 mockedMail.setMessage(m);
78 mockedMail.setRecipients(Arrays.asList(recipients));
79
80 }
81
82 private void setupMatcher() throws MessagingException {
83
84 MailetContext mockMailContext = new MailetContext() {
85
86 Collection localServer = new ArrayList(Arrays.asList(LOCALSERVER));
87
88 public void bounce(Mail mail, String message)
89 throws MessagingException {
90 throw new UnsupportedOperationException(
91 "Unimplemented mock service");
92
93 }
94
95 public void bounce(Mail mail, String message, MailAddress bouncer)
96 throws MessagingException {
97 throw new UnsupportedOperationException(
98 "Unimplemented mock service");
99
100 }
101
102 public Collection getMailServers(String host) {
103 throw new UnsupportedOperationException(
104 "Unimplemented mock service");
105 }
106
107 public MailAddress getPostmaster() {
108 throw new UnsupportedOperationException(
109 "Unimplemented mock service");
110 }
111
112 public Object getAttribute(String name) {
113 throw new UnsupportedOperationException(
114 "Unimplemented mock service");
115 }
116
117 public Iterator getAttributeNames() {
118 throw new UnsupportedOperationException(
119 "Unimplemented mock service");
120 }
121
122 public int getMajorVersion() {
123 throw new UnsupportedOperationException(
124 "Unimplemented mock service");
125 }
126
127 public int getMinorVersion() {
128 throw new UnsupportedOperationException(
129 "Unimplemented mock service");
130 }
131
132 public String getServerInfo() {
133 throw new UnsupportedOperationException(
134 "Unimplemented mock service");
135 }
136
137 public boolean isLocalServer(String serverName) {
138 return localServer.contains(serverName);
139 }
140
141 public boolean isLocalUser(String userAccount) {
142 throw new UnsupportedOperationException(
143 "Unimplemented mock service");
144 }
145
146 public boolean isLocalEmail(MailAddress mailAddress) {
147 throw new UnsupportedOperationException(
148 "Unimplemented mock service");
149 }
150
151 public void log(String message) {
152 throw new UnsupportedOperationException(
153 "Unimplemented mock service");
154 }
155
156 public void log(String message, Throwable t) {
157 throw new UnsupportedOperationException(
158 "Unimplemented mock service");
159 }
160
161 public void removeAttribute(String name) {
162 throw new UnsupportedOperationException(
163 "Unimplemented mock service");
164 }
165
166 public void sendMail(MimeMessage msg) throws MessagingException {
167 throw new UnsupportedOperationException(
168 "Unimplemented mock service");
169 }
170
171 public void sendMail(MailAddress sender, Collection recipients,
172 MimeMessage msg) throws MessagingException {
173 throw new UnsupportedOperationException(
174 "Unimplemented mock service");
175 }
176
177 public void sendMail(MailAddress sender, Collection recipients,
178 MimeMessage msg, String state) throws MessagingException {
179 throw new UnsupportedOperationException(
180 "Unimplemented mock service");
181 }
182
183 public void sendMail(Mail mail) throws MessagingException {
184 throw new UnsupportedOperationException(
185 "Unimplemented mock service");
186 }
187
188 public void setAttribute(String name, Object object) {
189 throw new UnsupportedOperationException(
190 "Unimplemented mock service");
191 }
192
193 public void storeMail(MailAddress sender, MailAddress recipient,
194 MimeMessage msg) throws MessagingException {
195 throw new UnsupportedOperationException(
196 "Unimplemented mock service");
197 }
198
199 public Iterator getSMTPHostAddresses(String domainName) {
200 throw new UnsupportedOperationException(
201 "Unimplemented mock service");
202 }
203
204 };
205
206 setupMockedMimeMessage();
207 matcher = new HostIsLocal();
208 MockMatcherConfig mci = new MockMatcherConfig("HostIsLocal",
209 mockMailContext);
210 matcher.init(mci);
211 }
212
213
214 public void testHostIsMatchedAllRecipients() throws MessagingException {
215 setRecipients(new MailAddress[] {
216 new MailAddress("test@james.apache.org"),
217 new MailAddress("test2@james.apache.org") });
218
219 setupMockedMimeMessage();
220 setupMockedMail(mockedMimeMessage);
221 setupMatcher();
222
223 Collection matchedRecipients = matcher.match(mockedMail);
224
225 assertNotNull(matchedRecipients);
226 assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
227 .size());
228 }
229
230
231 public void testHostIsMatchedOneRecipient() throws MessagingException {
232 setRecipients(new MailAddress[] {
233 new MailAddress("test@james2.apache.org"),
234 new MailAddress("test2@james.apache.org") });
235
236 setupMockedMimeMessage();
237 setupMockedMail(mockedMimeMessage);
238 setupMatcher();
239
240 Collection matchedRecipients = matcher.match(mockedMail);
241
242 assertNotNull(matchedRecipients);
243 assertEquals(matchedRecipients.size(), 1);
244 }
245
246
247 public void testHostIsNotMatch() throws MessagingException {
248 setRecipients(new MailAddress[] {
249 new MailAddress("test@james2.apache.org"),
250 new MailAddress("test2@james2.apache.org") });
251
252 setupMockedMimeMessage();
253 setupMockedMail(mockedMimeMessage);
254 setupMatcher();
255
256 Collection matchedRecipients = matcher.match(mockedMail);
257
258 assertEquals(matchedRecipients.size(), 0);
259 }
260 }