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 RecipientIsRegexTest extends TestCase {
040:
041: private MimeMessage mockedMimeMessage;
042:
043: private MockMail mockedMail;
044:
045: private Matcher matcher;
046:
047: private MailAddress[] recipients;
048:
049: private String regex = ".*";
050:
051: public RecipientIsRegexTest(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 setRegex(String regex) {
061: this .regex = regex;
062: }
063:
064: private void setupMockedMimeMessage() throws MessagingException {
065: String sender = "test@james.apache.org";
066: String rcpt = "test2@james.apache.org";
067:
068: mockedMimeMessage = new MockMimeMessage();
069: mockedMimeMessage.setFrom(new InternetAddress(sender));
070: mockedMimeMessage.setRecipients(RecipientType.TO, rcpt);
071: mockedMimeMessage.setSubject("testmail");
072: mockedMimeMessage.setText("testtext");
073: mockedMimeMessage.saveChanges();
074:
075: }
076:
077: private void setupMockedMail(MimeMessage m) {
078: mockedMail = new MockMail();
079: mockedMail.setMessage(m);
080: mockedMail.setRecipients(Arrays.asList(recipients));
081:
082: }
083:
084: private void setupMatcher() throws MessagingException {
085: setupMockedMimeMessage();
086: matcher = new RecipientIsRegex();
087: MockMatcherConfig mci = new MockMatcherConfig(
088: "RecipientIsRegex=" + regex, new MockMailContext());
089: matcher.init(mci);
090: }
091:
092: // test if the recipients get returned as matched
093: public void testRegexIsMatchedAllRecipients()
094: throws MessagingException {
095: setRecipients(new MailAddress[] { new MailAddress(
096: "test@james.apache.org") });
097: setRegex(".*@.*");
098: setupMockedMimeMessage();
099: setupMockedMail(mockedMimeMessage);
100: setupMatcher();
101:
102: Collection matchedRecipients = matcher.match(mockedMail);
103:
104: assertNotNull(matchedRecipients);
105: assertEquals(matchedRecipients.size(), mockedMail
106: .getRecipients().size());
107: }
108:
109: // test if one recipients get returned as matched
110: public void testRegexIsMatchedOneRecipient()
111: throws MessagingException {
112: setRecipients(new MailAddress[] {
113: new MailAddress("test@james.apache.org"),
114: new MailAddress("test2@james.apache.org") });
115: setRegex("^test@.*");
116: setupMockedMimeMessage();
117: setupMockedMail(mockedMimeMessage);
118: setupMatcher();
119:
120: Collection matchedRecipients = matcher.match(mockedMail);
121:
122: assertNotNull(matchedRecipients);
123: assertEquals(matchedRecipients.size(), 1);
124: }
125:
126: // test if no recipient get returned cause it not match
127: public void testRegexIsNotMatch() throws MessagingException {
128: setRecipients(new MailAddress[] {
129: new MailAddress("test@james2.apache.org"),
130: new MailAddress("test2@james2.apache.org") });
131: setRegex(".*\\+");
132: setupMockedMimeMessage();
133: setupMockedMail(mockedMimeMessage);
134: setupMatcher();
135:
136: Collection matchedRecipients = matcher.match(mockedMail);
137:
138: assertEquals(matchedRecipients.size(), 0);
139: }
140:
141: // test if an exception was thrown cause the regex was invalid
142: public void testRegexIsNotMatchedCauseError()
143: throws MessagingException {
144: Collection matchedRecipients = null;
145: String invalidRegex = "(!(";
146: String regexException = null;
147: String exception = "Malformed pattern: " + invalidRegex;
148:
149: setRecipients(new MailAddress[] {
150: new MailAddress("test@james2.apache.org"),
151: new MailAddress("test2@james2.apache.org") });
152:
153: setRegex(invalidRegex);
154: setupMockedMimeMessage();
155: setupMockedMail(mockedMimeMessage);
156:
157: try {
158: setupMatcher();
159: matchedRecipients = matcher.match(mockedMail);
160: } catch (MessagingException m) {
161: m.printStackTrace();
162: regexException = m.getMessage();
163: }
164:
165: assertNull(matchedRecipients);
166: assertEquals(regexException, exception);
167:
168: }
169: }
|