1 /*****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20 package org.apache.james.transport.mailets;
21
22 import org.apache.james.test.mock.mailet.MockMail;
23 import org.apache.james.test.mock.mailet.MockMailContext;
24 import org.apache.james.test.mock.mailet.MockMailetConfig;
25 import org.apache.mailet.Mail;
26 import org.apache.mailet.MailAddress;
27 import org.apache.mailet.Mailet;
28
29 import javax.mail.MessagingException;
30 import javax.mail.internet.MimeMessage;
31 import javax.mail.internet.ParseException;
32
33 import java.io.UnsupportedEncodingException;
34 import java.util.Arrays;
35
36 import junit.framework.TestCase;
37
38 public class ToProcessorTest extends TestCase {
39
40 private MimeMessage mockedMimeMessage;
41
42 private Mail mockedMail;
43
44 private Mailet mailet;
45
46 private String processor = null;
47
48 private String notice = null;
49
50 public ToProcessorTest(String arg0) throws UnsupportedEncodingException {
51 super(arg0);
52 }
53
54 private void setProcessor(String processor) {
55 this.processor = processor;
56 }
57
58 private void setNotice(String notice) {
59 this.notice = notice;
60 }
61
62 private void setupMockedMail(MimeMessage m) throws ParseException {
63 mockedMail = new MockMail();
64 mockedMail.setMessage(m);
65 mockedMail.setRecipients(Arrays.asList(new MailAddress[] {
66 new MailAddress("test@james.apache.org"),
67 new MailAddress("test2@james.apache.org") }));
68
69 }
70
71 private void setupMailet() throws MessagingException {
72 mailet = new ToProcessor();
73 MockMailetConfig mci = new MockMailetConfig("Test",
74 new MockMailContext());
75 if (processor != null) {
76 mci.setProperty("processor", processor);
77 }
78 if (notice != null) {
79 mci.setProperty("notice", notice);
80 }
81 mailet.init(mci);
82 }
83
84
85 public void testValidToProcessor() throws MessagingException {
86 setProcessor("error");
87 setNotice("error in message");
88 setupMockedMail(mockedMimeMessage);
89 setupMailet();
90
91 mailet.service(mockedMail);
92
93 assertEquals(processor, mockedMail.getState());
94 assertEquals(notice, mockedMail.getErrorMessage());
95
96 }
97
98
99 public void testExceptionThrown() throws MessagingException {
100 boolean exceptionThrown = false;
101 setProcessor(null);
102 setNotice("error in message");
103 setupMockedMail(mockedMimeMessage);
104
105 try {
106 setupMailet();
107 mailet.service(mockedMail);
108 } catch (MessagingException m) {
109 exceptionThrown = true;
110 }
111 assertTrue(exceptionThrown);
112 }
113
114 }