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.UnsupportedEncodingException;
035: import java.util.Arrays;
036: import java.util.Collection;
037:
038: import junit.framework.TestCase;
039:
040: public class FetchedFromTest extends TestCase {
041:
042: private MimeMessage mockedMimeMessage;
043:
044: private MockMail mockedMail;
045:
046: private Matcher matcher;
047:
048: private final String HEADER_NAME = "X-fetched-from";
049:
050: private final String HEADER_VALUE = "james-user";
051:
052: private String headerName = "defaultHeaderName";
053:
054: private String headerValue = "defaultHeaderValue";
055:
056: public FetchedFromTest(String arg0)
057: throws UnsupportedEncodingException {
058: super (arg0);
059: }
060:
061: private void setHeaderName(String headerName) {
062: this .headerName = headerName;
063: }
064:
065: private void setHeaderValue(String headerValue) {
066: this .headerValue = headerValue;
067: }
068:
069: private void setupMockedMimeMessage() throws MessagingException {
070: String sender = "test@james.apache.org";
071: String rcpt = "test2@james.apache.org";
072:
073: mockedMimeMessage = new MockMimeMessage();
074: mockedMimeMessage.setFrom(new InternetAddress(sender));
075: mockedMimeMessage.setRecipients(RecipientType.TO, rcpt);
076: mockedMimeMessage.setHeader(headerName, headerValue);
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:
090: }
091:
092: private void setupMatcher() throws MessagingException {
093: setupMockedMimeMessage();
094: matcher = new FetchedFrom();
095: MockMatcherConfig mci = new MockMatcherConfig("FetchedFrom="
096: + HEADER_VALUE, new MockMailContext());
097: matcher.init(mci);
098: }
099:
100: // test if the Header was matched
101: public void testHeaderIsMatched() throws MessagingException {
102: setHeaderName(HEADER_NAME);
103: setHeaderValue(HEADER_VALUE);
104:
105: setupMockedMimeMessage();
106: setupMockedMail(mockedMimeMessage);
107: setupMatcher();
108:
109: Collection matchedRecipients = matcher.match(mockedMail);
110: assertNotNull(matchedRecipients);
111: assertEquals(matchedRecipients.size(), mockedMail
112: .getRecipients().size());
113: }
114:
115: // test if the Header was not matched
116: public void testHeaderIsNotMatched() throws MessagingException {
117: setHeaderName(HEADER_NAME);
118: setHeaderValue(headerValue);
119:
120: setupMockedMimeMessage();
121: setupMockedMail(mockedMimeMessage);
122: setupMatcher();
123:
124: Collection matchedRecipients = matcher.match(mockedMail);
125: assertNull(matchedRecipients);
126: }
127:
128: // test if the Header was removed after matched
129: public void testHeaderWasRemovedAfterMatched()
130: throws MessagingException {
131: setHeaderName(HEADER_NAME);
132: setHeaderValue(HEADER_VALUE);
133:
134: setupMockedMimeMessage();
135: setupMockedMail(mockedMimeMessage);
136: setupMatcher();
137:
138: Collection matchedRecipients = matcher.match(mockedMail);
139: Collection matchedRecipients2 = matcher.match(mockedMail);
140:
141: assertNotNull(matchedRecipients);
142: assertNull(matchedRecipients2);
143: }
144: }
|