KickJava   Java API By Example, From Geeks To Geeks.

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


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.james.test.mock.mailet.MockMail;
25 import org.apache.james.test.mock.mailet.MockMailContext;
26 import org.apache.james.test.mock.mailet.MockMailetConfig;
27 import org.apache.james.test.util.Util;
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
36 public class MailAttributesToMimeHeadersTest extends TestCase {
37
38     private Mailet mailet;
39
40     private final String JavaDoc HEADER_NAME1 = "JUNIT";
41
42     private final String JavaDoc HEADER_NAME2 = "JUNIT2";
43
44     private final String JavaDoc MAIL_ATTRIBUTE_VALUE1 = "test1";
45
46     private final String JavaDoc MAIL_ATTRIBUTE_VALUE2 = "test2";
47
48     private final String JavaDoc MAIL_ATTRIBUTE_NAME1 = "org.apache.james.test";
49
50     private final String JavaDoc MAIL_ATTRIBUTE_NAME2 = "org.apache.james.test2";
51
52     private String JavaDoc config1 = MAIL_ATTRIBUTE_NAME1 + "; " + HEADER_NAME1;
53
54     private String JavaDoc config2 = MAIL_ATTRIBUTE_NAME2 + "; " + HEADER_NAME2;
55
56     public MailAttributesToMimeHeadersTest(String JavaDoc arg0)
57             throws UnsupportedEncodingException JavaDoc {
58         super(arg0);
59     }
60
61     private void setConfig1(String JavaDoc config1) {
62         this.config1 = config1;
63     }
64
65     private String JavaDoc getConfig1() {
66         return config1;
67     }
68
69     private void setupMailet() throws MessagingException JavaDoc {
70         mailet = new MailAttributesToMimeHeaders();
71         MockMailetConfig mci = new MockMailetConfig("Test",
72                 new MockMailContext());
73         mci.setProperty("simplemapping", getConfig1());
74         mci.setProperty("simplemapping", config2);
75         mailet.init(mci);
76     }
77
78     private MockMail setupMail(MimeMessage JavaDoc m) throws ParseException JavaDoc {
79         MockMail mockedMail = Util.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 JavaDoc {
87         MockMail mockedMail = setupMail(Util.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 JavaDoc {
102         boolean exception = false;
103         MockMail mockedMail = setupMail(Util.createMimeMessage());
104         setConfig1("test");
105
106         try {
107             setupMailet();
108             mailet.service(mockedMail);
109         } catch (MessagingException JavaDoc e) {
110             exception = true;
111         }
112
113         assertTrue(exception);
114
115     }
116 }
117
Popular Tags