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