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 HasHeaderTest extends TestCase {
041:
042: private MimeMessage mockedMimeMessage;
043:
044: private MockMail mockedMail;
045:
046: private Matcher matcher;
047:
048: private final String HEADER_NAME = "JUNIT";
049:
050: private final String HEADER_VALUE = "test-value";
051:
052: private String headerName = "defaultHeaderName";
053:
054: private String headerValue = "defaultHeaderValue";
055:
056: public HasHeaderTest(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 HasHeader();
095: MockMatcherConfig mci = new MockMatcherConfig("HasHeader="
096: + HEADER_NAME, 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:
111: assertNotNull(matchedRecipients);
112: assertEquals(matchedRecipients.size(), mockedMail
113: .getRecipients().size());
114: }
115:
116: // test if the Header was not matched
117: public void testHeaderIsNotMatched() throws MessagingException {
118: setupMockedMimeMessage();
119: setupMockedMail(mockedMimeMessage);
120: setupMatcher();
121:
122: Collection matchedRecipients = matcher.match(mockedMail);
123:
124: assertNull(matchedRecipients);
125: }
126: }
|