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.mailets;
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.MockMailetConfig;
024: import org.apache.mailet.Mail;
025: import org.apache.mailet.MailAddress;
026: import org.apache.mailet.Mailet;
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:
037: import junit.framework.TestCase;
038:
039: public class SetMimeHeaderTest extends TestCase {
040:
041: private MimeMessage mockedMimeMessage;
042:
043: private Mail mockedMail;
044:
045: private Mailet mailet;
046:
047: private final String HEADER_NAME = "JUNIT";
048:
049: private final String HEADER_VALUE = "test-value";
050:
051: private String headerName = "defaultHeaderName";
052:
053: private String headerValue = "defaultHeaderValue";
054:
055: public SetMimeHeaderTest(String arg0)
056: throws UnsupportedEncodingException {
057: super (arg0);
058: }
059:
060: private void setHeaderName(String headerName) {
061: this .headerName = headerName;
062: }
063:
064: private void setHeaderValue(String headerValue) {
065: this .headerValue = headerValue;
066: }
067:
068: private void setupMockedMimeMessage() throws MessagingException {
069: String sender = "test@james.apache.org";
070: String rcpt = "test2@james.apache.org";
071:
072: mockedMimeMessage = new MockMimeMessage();
073: mockedMimeMessage.setFrom(new InternetAddress(sender));
074: mockedMimeMessage.setRecipients(RecipientType.TO, rcpt);
075: mockedMimeMessage.setHeader(headerName, headerValue);
076: mockedMimeMessage.setSubject("testmail");
077: mockedMimeMessage.setText("testtext");
078: mockedMimeMessage.saveChanges();
079:
080: }
081:
082: private void setupMockedMail(MimeMessage m) throws ParseException {
083: mockedMail = new MockMail();
084: mockedMail.setMessage(m);
085: mockedMail.setRecipients(Arrays.asList(new MailAddress[] {
086: new MailAddress("test@james.apache.org"),
087: new MailAddress("test2@james.apache.org") }));
088:
089: }
090:
091: private void setupMailet() throws MessagingException {
092: setupMockedMimeMessage();
093: mailet = new SetMimeHeader();
094: MockMailetConfig mci = new MockMailetConfig("Test",
095: new MockMailContext());
096: mci.setProperty("name", HEADER_NAME);
097: mci.setProperty("value", HEADER_VALUE);
098:
099: mailet.init(mci);
100: }
101:
102: // test if the Header was add
103: public void testHeaderIsPresent() throws MessagingException {
104: setupMockedMimeMessage();
105: setupMockedMail(mockedMimeMessage);
106: setupMailet();
107:
108: mailet.service(mockedMail);
109:
110: assertEquals(HEADER_VALUE, mockedMail.getMessage().getHeader(
111: HEADER_NAME)[0]);
112:
113: }
114:
115: // test if the Header was replaced
116: public void testHeaderIsReplaced() throws MessagingException {
117: setHeaderName(HEADER_NAME);
118: setHeaderValue(headerValue);
119:
120: setupMockedMimeMessage();
121: setupMockedMail(mockedMimeMessage);
122: setupMailet();
123:
124: mailet.service(mockedMail);
125:
126: assertEquals(HEADER_VALUE, mockedMail.getMessage().getHeader(
127: HEADER_NAME)[0]);
128:
129: }
130: }
|