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.mailet.base.test.FakeMail;
24  import org.apache.mailet.base.test.FakeMailContext;
25  import org.apache.mailet.base.test.FakeMailetConfig;
26  import org.apache.mailet.Mail;
27  import org.apache.mailet.MailAddress;
28  import org.apache.mailet.Mailet;
29  
30  import javax.mail.MessagingException;
31  import javax.mail.internet.MimeMessage;
32  import javax.mail.internet.ParseException;
33  
34  import java.io.UnsupportedEncodingException;
35  import java.util.Arrays;
36  
37  import junit.framework.TestCase;
38  
39  public class ToProcessorTest extends TestCase {
40  
41      private MimeMessage mockedMimeMessage;
42  
43      private Mail mockedMail;
44  
45      private Mailet mailet;
46  
47      private String processor = null;
48  
49      private String notice = null;
50  
51      public ToProcessorTest(String arg0) throws UnsupportedEncodingException {
52          super(arg0);
53      }
54  
55      private void setProcessor(String processor) {
56          this.processor = processor;
57      }
58  
59      private void setNotice(String notice) {
60          this.notice = notice;
61      }
62  
63      private void setupMockedMail(MimeMessage m) throws ParseException {
64          mockedMail = new FakeMail();
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 {
73          mailet = new ToProcessor();
74          FakeMailetConfig mci = new FakeMailetConfig("Test",
75                  new FakeMailContext());
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 {
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 {
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 m) {
110             exceptionThrown = true;
111         }
112         assertTrue(exceptionThrown);
113     }
114 
115 }