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 HasMailAttributeTest 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: public HasMailAttributeTest(String arg0)
058: throws UnsupportedEncodingException {
059: super (arg0);
060: }
061:
062: private void setMailAttributeName(String mailAttributeName) {
063: this .mailAttributeName = mailAttributeName;
064: }
065:
066: private void setMailAttributeValue(String mailAttributeValue) {
067: this .mailAttributeValue = mailAttributeValue;
068: }
069:
070: private void setupMockedMimeMessage() throws MessagingException {
071: String sender = "test@james.apache.org";
072: String rcpt = "test2@james.apache.org";
073:
074: mockedMimeMessage = new MockMimeMessage();
075: mockedMimeMessage.setFrom(new InternetAddress(sender));
076: mockedMimeMessage.setRecipients(RecipientType.TO, rcpt);
077: mockedMimeMessage.setSubject("testmail");
078: mockedMimeMessage.setText("testtext");
079: mockedMimeMessage.saveChanges();
080:
081: }
082:
083: private void setupMockedMail(MimeMessage m) throws ParseException {
084: mockedMail = new MockMail();
085: mockedMail.setMessage(m);
086: mockedMail.setRecipients(Arrays.asList(new MailAddress[] {
087: new MailAddress("test@james.apache.org"),
088: new MailAddress("test2@james.apache.org") }));
089: mockedMail.setAttribute(mailAttributeName,
090: (Serializable) mailAttributeValue);
091:
092: }
093:
094: private void setupMatcher() throws MessagingException {
095: setupMockedMimeMessage();
096: matcher = new HasMailAttribute();
097: MockMatcherConfig mci = new MockMatcherConfig(
098: "HasMailAttribute=" + MAIL_ATTRIBUTE_NAME,
099: new MockMailContext());
100: matcher.init(mci);
101: }
102:
103: // test if the mail attribute was matched
104: public void testAttributeIsMatched() throws MessagingException {
105: setMailAttributeName(MAIL_ATTRIBUTE_NAME);
106: setMailAttributeValue(MAIL_ATTRIBUTE_VALUE);
107:
108: setupMockedMimeMessage();
109: setupMockedMail(mockedMimeMessage);
110: setupMatcher();
111:
112: Collection matchedRecipients = matcher.match(mockedMail);
113:
114: assertNotNull(matchedRecipients);
115: assertEquals(matchedRecipients.size(), mockedMail
116: .getRecipients().size());
117: }
118:
119: // test if the mail attribute was not matched
120: public void testAttributeIsNotMatched() throws MessagingException {
121: setupMockedMimeMessage();
122: setupMockedMail(mockedMimeMessage);
123: setupMatcher();
124:
125: Collection matchedRecipients = matcher.match(mockedMail);
126:
127: assertNull(matchedRecipients);
128: }
129: }
|