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 junit.framework.TestCase;
24  import org.apache.mailet.base.test.FakeMail;
25  import org.apache.mailet.base.test.FakeMailContext;
26  import org.apache.mailet.base.test.FakeMailetConfig;
27  import org.apache.mailet.base.test.MailUtil;
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  
36  public class MailAttributesToMimeHeadersTest extends TestCase {
37  
38      private Mailet mailet;
39  
40      private final String HEADER_NAME1 = "JUNIT";
41  
42      private final String HEADER_NAME2 = "JUNIT2";
43  
44      private final String MAIL_ATTRIBUTE_VALUE1 = "test1";
45  
46      private final String MAIL_ATTRIBUTE_VALUE2 = "test2";
47  
48      private final String MAIL_ATTRIBUTE_NAME1 = "org.apache.james.test";
49  
50      private final String MAIL_ATTRIBUTE_NAME2 = "org.apache.james.test2";
51  
52      private String config1 = MAIL_ATTRIBUTE_NAME1 + "; " + HEADER_NAME1;
53  
54      private String config2 = MAIL_ATTRIBUTE_NAME2 + "; " + HEADER_NAME2;
55  
56      public MailAttributesToMimeHeadersTest(String arg0)
57              throws UnsupportedEncodingException {
58          super(arg0);
59      }
60  
61      private void setConfig1(String config1) {
62          this.config1 = config1;
63      }
64  
65      private String getConfig1() {
66          return config1;
67      }
68  
69      private void setupMailet() throws MessagingException {
70          mailet = new MailAttributesToMimeHeaders();
71          FakeMailetConfig mci = new FakeMailetConfig("Test",
72                  new FakeMailContext());
73          mci.setProperty("simplemapping", getConfig1());
74          mci.setProperty("simplemapping", config2);
75          mailet.init(mci);
76      }
77  
78      private FakeMail setupMail(MimeMessage m) throws ParseException {
79          FakeMail mockedMail = MailUtil.createMockMail2Recipients(m);
80          mockedMail.setAttribute(MAIL_ATTRIBUTE_NAME1, MAIL_ATTRIBUTE_VALUE1);
81          mockedMail.setAttribute(MAIL_ATTRIBUTE_NAME2, MAIL_ATTRIBUTE_VALUE2);
82          return mockedMail;
83      }
84  
85      // test if the Headers were added
86      public void testHeadersArePresent() throws MessagingException {
87          FakeMail mockedMail = setupMail(MailUtil.createMimeMessage());
88          setupMailet();
89  
90          mailet.service(mockedMail);
91  
92          assertEquals(MAIL_ATTRIBUTE_VALUE1, mockedMail.getMessage().getHeader(
93                  HEADER_NAME1)[0]);
94  
95          assertEquals(MAIL_ATTRIBUTE_VALUE2, mockedMail.getMessage().getHeader(
96                  HEADER_NAME2)[0]);
97  
98      }
99  
100     // test if exception was thrown
101     public void testInvalidConfig() throws MessagingException {
102         boolean exception = false;
103         FakeMail mockedMail = setupMail(MailUtil.createMimeMessage());
104         setConfig1("test");
105 
106         try {
107             setupMailet();
108             mailet.service(mockedMail);
109         } catch (MessagingException e) {
110             exception = true;
111         }
112 
113         assertTrue(exception);
114 
115     }
116 }