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.ParseException;
032: import javax.mail.internet.MimeMessage.RecipientType;
033:
034: import java.io.Serializable;
035: import java.io.UnsupportedEncodingException;
036: import java.util.Arrays;
037: import java.util.Collection;
038:
039: import junit.framework.TestCase;
040:
041: public class HasMailAttributeWithValueRegexTest extends TestCase {
042:
043: private MimeMessage mockedMimeMessage;
044:
045: private MockMail mockedMail;
046:
047: private Matcher matcher;
048:
049: private final String MAIL_ATTRIBUTE_NAME = "org.apache.james.test.junit";
050:
051: private final String MAIL_ATTRIBUTE_VALUE = "true";
052:
053: private String mailAttributeName = "org.apache.james";
054:
055: private String mailAttributeValue = "false";
056:
057: private String regex = ".*";
058:
059: public HasMailAttributeWithValueRegexTest(String arg0)
060: throws UnsupportedEncodingException {
061: super (arg0);
062: }
063:
064: private void setMailAttributeName(String mailAttributeName) {
065: this .mailAttributeName = mailAttributeName;
066: }
067:
068: private void setMailAttributeValue(String mailAttributeValue) {
069: this .mailAttributeValue = mailAttributeValue;
070: }
071:
072: private void setRegex(String regex) {
073: this .regex = regex;
074: }
075:
076: private void setupMockedMimeMessage() throws MessagingException {
077: String sender = "test@james.apache.org";
078: String rcpt = "test2@james.apache.org";
079:
080: mockedMimeMessage = new MockMimeMessage();
081: mockedMimeMessage.setFrom(new InternetAddress(sender));
082: mockedMimeMessage.setRecipients(RecipientType.TO, rcpt);
083: mockedMimeMessage.setSubject("testmail");
084: mockedMimeMessage.setText("testtext");
085: mockedMimeMessage.saveChanges();
086:
087: }
088:
089: private void setupMockedMail(MimeMessage m) throws ParseException {
090: mockedMail = new MockMail();
091: mockedMail.setMessage(m);
092: mockedMail.setRecipients(Arrays.asList(new MailAddress[] {
093: new MailAddress("test@james.apache.org"),
094: new MailAddress("test2@james.apache.org") }));
095: mockedMail.setAttribute(mailAttributeName,
096: (Serializable) mailAttributeValue);
097:
098: }
099:
100: private void setupMatcher() throws MessagingException {
101: setupMockedMimeMessage();
102: matcher = new HasMailAttributeWithValueRegex();
103: MockMatcherConfig mci = new MockMatcherConfig(
104: "HasMailAttribute=" + MAIL_ATTRIBUTE_NAME + ", "
105: + regex, new MockMailContext());
106: matcher.init(mci);
107: }
108:
109: // test if the mail attribute was matched
110: public void testAttributeIsMatched() throws MessagingException {
111: setMailAttributeName(MAIL_ATTRIBUTE_NAME);
112: setMailAttributeValue(MAIL_ATTRIBUTE_VALUE);
113: setRegex(".*");
114:
115: setupMockedMimeMessage();
116: setupMockedMail(mockedMimeMessage);
117: setupMatcher();
118:
119: Collection matchedRecipients = matcher.match(mockedMail);
120:
121: assertNotNull(matchedRecipients);
122: assertEquals(matchedRecipients.size(), mockedMail
123: .getRecipients().size());
124: }
125:
126: // test if the mail attribute was not matched
127: public void testHeaderIsNotMatched() throws MessagingException {
128: setRegex("\\d");
129: setupMockedMimeMessage();
130: setupMockedMail(mockedMimeMessage);
131: setupMatcher();
132:
133: Collection matchedRecipients = matcher.match(mockedMail);
134:
135: assertNull(matchedRecipients);
136: }
137:
138: // test if an exception was thrown cause the regex was invalid
139: public void testHeaderIsNotMatchedCauseValue()
140: throws MessagingException {
141:
142: String invalidRegex = "(!(";
143: String regexException = null;
144: String exception = "Malformed pattern: " + invalidRegex;
145:
146: setRegex(invalidRegex);
147: setupMockedMimeMessage();
148: setupMockedMail(mockedMimeMessage);
149:
150: try {
151: setupMatcher();
152: } catch (MessagingException m) {
153: regexException = m.getMessage();
154: }
155:
156: Collection matchedRecipients = matcher.match(mockedMail);
157:
158: assertNull(matchedRecipients);
159: assertEquals(regexException, exception);
160:
161: }
162: }
|