001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.transport.matchers;
019:
020: import org.apache.james.test.mock.javaxmail.MockMimeMessage;
021: import org.apache.james.test.mock.mailet.MockMail;
022: import org.apache.james.test.mock.mailet.MockMatcherConfig;
023:
024: import org.apache.mailet.Mail;
025: import org.apache.mailet.MailAddress;
026: import org.apache.mailet.MailetContext;
027: import org.apache.mailet.Matcher;
028:
029: import javax.mail.MessagingException;
030: import javax.mail.internet.InternetAddress;
031: import javax.mail.internet.MimeMessage;
032: import javax.mail.internet.MimeMessage.RecipientType;
033:
034: import java.io.UnsupportedEncodingException;
035: import java.util.ArrayList;
036: import java.util.Arrays;
037: import java.util.Collection;
038: import java.util.Iterator;
039:
040: import junit.framework.TestCase;
041:
042: public class HostIsLocalTest extends TestCase {
043:
044: private MimeMessage mockedMimeMessage;
045:
046: private MockMail mockedMail;
047:
048: private Matcher matcher;
049:
050: private final String[] LOCALSERVER = new String[] { "james.apache.org" };
051:
052: private MailAddress[] recipients;
053:
054: public HostIsLocalTest(String arg0)
055: throws UnsupportedEncodingException {
056: super (arg0);
057: }
058:
059: private void setRecipients(MailAddress[] recipients) {
060: this .recipients = recipients;
061: }
062:
063: private void setupMockedMimeMessage() throws MessagingException {
064: String sender = "test@james.apache.org";
065: String rcpt = "test2@james.apache.org";
066:
067: mockedMimeMessage = new MockMimeMessage();
068: mockedMimeMessage.setFrom(new InternetAddress(sender));
069: mockedMimeMessage.setRecipients(RecipientType.TO, rcpt);
070: mockedMimeMessage.setSubject("testmail");
071: mockedMimeMessage.setText("testtext");
072: mockedMimeMessage.saveChanges();
073:
074: }
075:
076: private void setupMockedMail(MimeMessage m) {
077: mockedMail = new MockMail();
078: mockedMail.setMessage(m);
079: mockedMail.setRecipients(Arrays.asList(recipients));
080:
081: }
082:
083: private void setupMatcher() throws MessagingException {
084:
085: MailetContext mockMailContext = new MailetContext() {
086:
087: Collection localServer = new ArrayList(Arrays
088: .asList(LOCALSERVER));
089:
090: public void bounce(Mail mail, String message)
091: throws MessagingException {
092: throw new UnsupportedOperationException(
093: "Unimplemented mock service");
094:
095: }
096:
097: public void bounce(Mail mail, String message,
098: MailAddress bouncer) throws MessagingException {
099: 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)
169: throws MessagingException {
170: throw new UnsupportedOperationException(
171: "Unimplemented mock service");
172: }
173:
174: public void sendMail(MailAddress sender,
175: Collection recipients, MimeMessage msg)
176: throws MessagingException {
177: throw new UnsupportedOperationException(
178: "Unimplemented mock service");
179: }
180:
181: public void sendMail(MailAddress sender,
182: Collection recipients, MimeMessage msg, String state)
183: throws MessagingException {
184: throw new UnsupportedOperationException(
185: "Unimplemented mock service");
186: }
187:
188: public void sendMail(Mail mail) throws MessagingException {
189: throw new UnsupportedOperationException(
190: "Unimplemented mock service");
191: }
192:
193: public void setAttribute(String name, Object object) {
194: throw new UnsupportedOperationException(
195: "Unimplemented mock service");
196: }
197:
198: public void storeMail(MailAddress sender,
199: MailAddress recipient, MimeMessage msg)
200: throws MessagingException {
201: throw new UnsupportedOperationException(
202: "Unimplemented mock service");
203: }
204:
205: public Iterator getSMTPHostAddresses(String domainName) {
206: throw new UnsupportedOperationException(
207: "Unimplemented mock service");
208: }
209:
210: };
211:
212: setupMockedMimeMessage();
213: matcher = new HostIsLocal();
214: MockMatcherConfig mci = new MockMatcherConfig("HostIsLocal",
215: mockMailContext);
216: matcher.init(mci);
217: }
218:
219: // test if all recipients get returned as matched
220: public void testHostIsMatchedAllRecipients()
221: throws MessagingException {
222: setRecipients(new MailAddress[] {
223: new MailAddress("test@james.apache.org"),
224: new MailAddress("test2@james.apache.org") });
225:
226: setupMockedMimeMessage();
227: setupMockedMail(mockedMimeMessage);
228: setupMatcher();
229:
230: Collection matchedRecipients = matcher.match(mockedMail);
231:
232: assertNotNull(matchedRecipients);
233: assertEquals(matchedRecipients.size(), mockedMail
234: .getRecipients().size());
235: }
236:
237: // test if one recipients get returned as matched
238: public void testHostIsMatchedOneRecipient()
239: throws MessagingException {
240: setRecipients(new MailAddress[] {
241: new MailAddress("test@james2.apache.org"),
242: new MailAddress("test2@james.apache.org") });
243:
244: setupMockedMimeMessage();
245: setupMockedMail(mockedMimeMessage);
246: setupMatcher();
247:
248: Collection matchedRecipients = matcher.match(mockedMail);
249:
250: assertNotNull(matchedRecipients);
251: assertEquals(matchedRecipients.size(), 1);
252: }
253:
254: // test if no recipient get returned cause it not match
255: public void testHostIsNotMatch() throws MessagingException {
256: setRecipients(new MailAddress[] {
257: new MailAddress("test@james2.apache.org"),
258: new MailAddress("test2@james2.apache.org") });
259:
260: setupMockedMimeMessage();
261: setupMockedMail(mockedMimeMessage);
262: setupMatcher();
263:
264: Collection matchedRecipients = matcher.match(mockedMail);
265:
266: assertEquals(matchedRecipients.size(), 0);
267: }
268: }
|