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.mailet.MockMail;
021: import org.apache.james.test.mock.mailet.MockMailContext;
022: import org.apache.james.test.mock.mailet.MockMailetConfig;
023: import org.apache.mailet.Mail;
024: import org.apache.mailet.MailAddress;
025: import org.apache.mailet.Mailet;
026:
027: import javax.mail.MessagingException;
028: import javax.mail.internet.MimeMessage;
029: import javax.mail.internet.ParseException;
030:
031: import java.io.UnsupportedEncodingException;
032: import java.util.Arrays;
033:
034: import junit.framework.TestCase;
035:
036: public class ToProcessorTest extends TestCase {
037:
038: private MimeMessage mockedMimeMessage;
039:
040: private Mail mockedMail;
041:
042: private Mailet mailet;
043:
044: private String processor = null;
045:
046: private String notice = null;
047:
048: public ToProcessorTest(String arg0)
049: throws UnsupportedEncodingException {
050: super (arg0);
051: }
052:
053: private void setProcessor(String processor) {
054: this .processor = processor;
055: }
056:
057: private void setNotice(String notice) {
058: this .notice = notice;
059: }
060:
061: private void setupMockedMail(MimeMessage m) throws ParseException {
062: mockedMail = new MockMail();
063: mockedMail.setMessage(m);
064: mockedMail.setRecipients(Arrays.asList(new MailAddress[] {
065: new MailAddress("test@james.apache.org"),
066: new MailAddress("test2@james.apache.org") }));
067:
068: }
069:
070: private void setupMailet() throws MessagingException {
071: mailet = new ToProcessor();
072: MockMailetConfig mci = new MockMailetConfig("Test",
073: new MockMailContext());
074: if (processor != null) {
075: mci.setProperty("processor", processor);
076: }
077: if (notice != null) {
078: mci.setProperty("notice", notice);
079: }
080: mailet.init(mci);
081: }
082:
083: // test if ToProcessor works
084: public void testValidToProcessor() throws MessagingException {
085: setProcessor("error");
086: setNotice("error in message");
087: setupMockedMail(mockedMimeMessage);
088: setupMailet();
089:
090: mailet.service(mockedMail);
091:
092: assertEquals(processor, mockedMail.getState());
093: assertEquals(notice, mockedMail.getErrorMessage());
094:
095: }
096:
097: // test if exception was thrown
098: public void testExceptionThrown() throws MessagingException {
099: boolean exceptionThrown = false;
100: setProcessor(null);
101: setNotice("error in message");
102: setupMockedMail(mockedMimeMessage);
103:
104: try {
105: setupMailet();
106: mailet.service(mockedMail);
107: } catch (MessagingException m) {
108: exceptionThrown = true;
109: }
110: assertTrue(exceptionThrown);
111: }
112:
113: }
|