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.MockMailContext;
023: import org.apache.james.test.mock.mailet.MockMatcherConfig;
024:
025: import org.apache.mailet.MailAddress;
026: import org.apache.mailet.Matcher;
027:
028: import javax.mail.MessagingException;
029: import javax.mail.internet.InternetAddress;
030: import javax.mail.internet.MimeMessage;
031: import javax.mail.internet.MimeMessage.RecipientType;
032:
033: import java.io.UnsupportedEncodingException;
034: import java.util.Arrays;
035: import java.util.Collection;
036:
037: import junit.framework.TestCase;
038:
039: public class IsSingleRecipientTest extends TestCase {
040:
041: private MimeMessage mockedMimeMessage;
042:
043: private MockMail mockedMail;
044:
045: private Matcher matcher;
046:
047: private MailAddress[] recipients;
048:
049: public IsSingleRecipientTest(String arg0)
050: throws UnsupportedEncodingException {
051: super (arg0);
052: }
053:
054: private void setRecipients(MailAddress[] recipients) {
055: this .recipients = recipients;
056: }
057:
058: private void setupMockedMimeMessage() throws MessagingException {
059: String sender = "test@james.apache.org";
060: String rcpt = "test2@james.apache.org";
061:
062: mockedMimeMessage = new MockMimeMessage();
063: mockedMimeMessage.setFrom(new InternetAddress(sender));
064: mockedMimeMessage.setRecipients(RecipientType.TO, rcpt);
065: mockedMimeMessage.setSubject("testmail");
066: mockedMimeMessage.setText("testtext");
067: mockedMimeMessage.saveChanges();
068:
069: }
070:
071: private void setupMockedMail(MimeMessage m) {
072: mockedMail = new MockMail();
073: mockedMail.setMessage(m);
074: mockedMail.setRecipients(Arrays.asList(recipients));
075:
076: }
077:
078: private void setupMatcher() throws MessagingException {
079:
080: matcher = new IsSingleRecipient();
081: MockMatcherConfig mci = new MockMatcherConfig(
082: "IsSingleRecipient", new MockMailContext());
083: matcher.init(mci);
084: }
085:
086: // test if matched
087: public void testHostIsMatchedAllRecipients()
088: throws MessagingException {
089: setRecipients(new MailAddress[] { new MailAddress(
090: "test@james.apache.org") });
091:
092: setupMockedMimeMessage();
093: setupMockedMail(mockedMimeMessage);
094: setupMatcher();
095:
096: Collection matchedRecipients = matcher.match(mockedMail);
097:
098: assertNotNull(matchedRecipients);
099: }
100:
101: // test if not matched
102: public void testHostIsMatchedOneRecipient()
103: throws MessagingException {
104: setRecipients(new MailAddress[] {
105: new MailAddress("test@james2.apache.org"),
106: new MailAddress("test2@james.apache.org") });
107:
108: setupMockedMimeMessage();
109: setupMockedMail(mockedMimeMessage);
110: setupMatcher();
111:
112: Collection matchedRecipients = matcher.match(mockedMail);
113:
114: assertNull(matchedRecipients);
115: }
116:
117: }
|