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 RecipientIsTest extends TestCase {
040:
041: private MimeMessage mockedMimeMessage;
042:
043: private MockMail mockedMail;
044:
045: private Matcher matcher;
046:
047: private final String RECIPIENT_NAME = "test@james.apache.org";
048:
049: private MailAddress[] recipients;
050:
051: public RecipientIsTest(String arg0)
052: throws UnsupportedEncodingException {
053: super (arg0);
054: }
055:
056: private void setRecipients(MailAddress[] recipients) {
057: this .recipients = recipients;
058: }
059:
060: private void setupMockedMimeMessage() throws MessagingException {
061: String sender = "test@james.apache.org";
062: String rcpt = "test2@james.apache.org";
063:
064: mockedMimeMessage = new MockMimeMessage();
065: mockedMimeMessage.setFrom(new InternetAddress(sender));
066: mockedMimeMessage.setRecipients(RecipientType.TO, rcpt);
067: mockedMimeMessage.setSubject("testmail");
068: mockedMimeMessage.setText("testtext");
069: mockedMimeMessage.saveChanges();
070:
071: }
072:
073: private void setupMockedMail(MimeMessage m) {
074: mockedMail = new MockMail();
075: mockedMail.setMessage(m);
076: mockedMail.setRecipients(Arrays.asList(recipients));
077:
078: }
079:
080: private void setupMatcher() throws MessagingException {
081: setupMockedMimeMessage();
082: matcher = new RecipientIs();
083: MockMatcherConfig mci = new MockMatcherConfig("RecipientIs="
084: + RECIPIENT_NAME, new MockMailContext());
085: matcher.init(mci);
086: }
087:
088: // test if the recipients get returned as matched
089: public void testHostIsMatchedAllRecipients()
090: throws MessagingException {
091: setRecipients(new MailAddress[] { new MailAddress(
092: "test@james.apache.org") });
093:
094: setupMockedMimeMessage();
095: setupMockedMail(mockedMimeMessage);
096: setupMatcher();
097:
098: Collection matchedRecipients = matcher.match(mockedMail);
099:
100: assertNotNull(matchedRecipients);
101: assertEquals(matchedRecipients.size(), mockedMail
102: .getRecipients().size());
103: }
104:
105: // test if one recipients get returned as matched
106: public void testHostIsMatchedOneRecipient()
107: throws MessagingException {
108: setRecipients(new MailAddress[] {
109: new MailAddress("test@james.apache.org"),
110: new MailAddress("test2@james.apache.org") });
111:
112: setupMockedMimeMessage();
113: setupMockedMail(mockedMimeMessage);
114: setupMatcher();
115:
116: Collection matchedRecipients = matcher.match(mockedMail);
117:
118: assertNotNull(matchedRecipients);
119: assertEquals(matchedRecipients.size(), 1);
120: }
121:
122: // test if no recipient get returned cause it not match
123: public void testHostIsNotMatch() throws MessagingException {
124: setRecipients(new MailAddress[] {
125: new MailAddress("test@james2.apache.org"),
126: new MailAddress("test2@james2.apache.org") });
127:
128: setupMockedMimeMessage();
129: setupMockedMail(mockedMimeMessage);
130: setupMatcher();
131:
132: Collection matchedRecipients = matcher.match(mockedMail);
133:
134: assertEquals(matchedRecipients.size(), 0);
135: }
136: }
|