KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > mailets > ToProcessorTest


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
21 package org.apache.james.transport.mailets;
22
23 import org.apache.james.test.mock.mailet.MockMail;
24 import org.apache.james.test.mock.mailet.MockMailContext;
25 import org.apache.james.test.mock.mailet.MockMailetConfig;
26 import org.apache.mailet.Mail;
27 import org.apache.mailet.MailAddress;
28 import org.apache.mailet.Mailet;
29
30 import javax.mail.MessagingException JavaDoc;
31 import javax.mail.internet.MimeMessage JavaDoc;
32 import javax.mail.internet.ParseException JavaDoc;
33
34 import java.io.UnsupportedEncodingException JavaDoc;
35 import java.util.Arrays JavaDoc;
36
37 import junit.framework.TestCase;
38
39 public class ToProcessorTest extends TestCase {
40
41     private MimeMessage JavaDoc mockedMimeMessage;
42
43     private Mail mockedMail;
44
45     private Mailet mailet;
46
47     private String JavaDoc processor = null;
48
49     private String JavaDoc notice = null;
50
51     public ToProcessorTest(String JavaDoc arg0) throws UnsupportedEncodingException JavaDoc {
52         super(arg0);
53     }
54
55     private void setProcessor(String JavaDoc processor) {
56         this.processor = processor;
57     }
58
59     private void setNotice(String JavaDoc notice) {
60         this.notice = notice;
61     }
62
63     private void setupMockedMail(MimeMessage JavaDoc m) throws ParseException JavaDoc {
64         mockedMail = new MockMail();
65         mockedMail.setMessage(m);
66         mockedMail.setRecipients(Arrays.asList(new MailAddress[] {
67                 new MailAddress("test@james.apache.org"),
68                 new MailAddress("test2@james.apache.org") }));
69
70     }
71
72     private void setupMailet() throws MessagingException JavaDoc {
73         mailet = new ToProcessor();
74         MockMailetConfig mci = new MockMailetConfig("Test",
75                 new MockMailContext());
76         if (processor != null) {
77             mci.setProperty("processor", processor);
78         }
79         if (notice != null) {
80             mci.setProperty("notice", notice);
81         }
82         mailet.init(mci);
83     }
84
85     // test if ToProcessor works
86
public void testValidToProcessor() throws MessagingException JavaDoc {
87         setProcessor("error");
88         setNotice("error in message");
89         setupMockedMail(mockedMimeMessage);
90         setupMailet();
91
92         mailet.service(mockedMail);
93
94         assertEquals(processor, mockedMail.getState());
95         assertEquals(notice, mockedMail.getErrorMessage());
96
97     }
98
99     // test if exception was thrown
100
public void testExceptionThrown() throws MessagingException JavaDoc {
101         boolean exceptionThrown = false;
102         setProcessor(null);
103         setNotice("error in message");
104         setupMockedMail(mockedMimeMessage);
105
106         try {
107             setupMailet();
108             mailet.service(mockedMail);
109         } catch (MessagingException JavaDoc m) {
110             exceptionThrown = true;
111         }
112         assertTrue(exceptionThrown);
113     }
114
115 }
116
Popular Tags