KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 package org.apache.james.transport.mailets;
23
24 import javax.mail.MessagingException JavaDoc;
25 import javax.mail.internet.MimeMessage JavaDoc;
26
27 import org.apache.james.test.mock.mailet.MockMail;
28 import org.apache.james.test.mock.mailet.MockMailContext;
29 import org.apache.james.test.mock.mailet.MockMailetConfig;
30 import org.apache.james.test.util.Util;
31 import org.apache.mailet.GenericMailet;
32 import org.apache.mailet.Mail;
33
34 import junit.framework.TestCase;
35
36 public class RemoveMimeHeaderTest extends TestCase {
37
38     private final static String JavaDoc HEADER1 = "header1";
39     
40     private final static String JavaDoc HEADER2 = "header2";
41     
42     private GenericMailet setupMockedMailet(String JavaDoc name1,String JavaDoc name2) throws MessagingException JavaDoc {
43         GenericMailet mailet = new RemoveMimeHeader();
44         MockMailetConfig mci = new MockMailetConfig("Test", new MockMailContext());
45         if (name1 != null) mci.setProperty("name", name1);
46         if (name2 != null) mci.setProperty("name", name2);
47
48         mailet.init(mci);
49         return mailet;
50     }
51     
52     private MimeMessage JavaDoc getMockedMimeMessage() throws MessagingException JavaDoc {
53         MimeMessage JavaDoc mockedMimeMessage = Util.createMimeMessage();
54         mockedMimeMessage.setHeader(HEADER1, "true");
55         mockedMimeMessage.setHeader(HEADER2, "true");
56         mockedMimeMessage.saveChanges();
57         return mockedMimeMessage;
58     }
59     
60     private Mail getMockedMail(MimeMessage JavaDoc message) {
61         Mail m = new MockMail();
62         m.setMessage(message);
63         return m;
64     }
65     
66     public void testOneHeaderRemoved() throws MessagingException JavaDoc {
67         GenericMailet mailet = setupMockedMailet(HEADER1,null);
68         Mail mail = getMockedMail(getMockedMimeMessage());
69     
70         // Get sure both headers are present
71
assertNotNull("Header present",mail.getMessage().getHeader(HEADER1));
72         assertNotNull("Header present",mail.getMessage().getHeader(HEADER2));
73     
74         mailet.service(mail);
75     
76         // The first header should be removed
77
assertNull("Header removed",mail.getMessage().getHeader(HEADER1));
78         assertNotNull("Header present",mail.getMessage().getHeader(HEADER2));
79     }
80     
81     public void testTwoHeaderRemoved() throws MessagingException JavaDoc {
82         GenericMailet mailet = setupMockedMailet(HEADER1,HEADER2);
83         Mail mail = getMockedMail(getMockedMimeMessage());
84     
85         // Get sure both headers are present
86
assertNotNull("Header present",mail.getMessage().getHeader(HEADER1));
87         assertNotNull("Header present",mail.getMessage().getHeader(HEADER2));
88     
89         mailet.service(mail);
90     
91         // Both header should be removed
92
assertNull("Header removed",mail.getMessage().getHeader(HEADER1));
93         assertNull("Header removed",mail.getMessage().getHeader(HEADER2));
94     }
95     
96     public void testNoHeaderRemoved() throws MessagingException JavaDoc {
97         GenericMailet mailet = setupMockedMailet("h1","h2");
98         Mail mail = getMockedMail(getMockedMimeMessage());
99     
100         // Get sure both headers are present
101
assertNotNull("Header present",mail.getMessage().getHeader(HEADER1));
102         assertNotNull("Header present",mail.getMessage().getHeader(HEADER2));
103     
104         mailet.service(mail);
105     
106         // Both header should be removed
107
assertNotNull("Header present",mail.getMessage().getHeader(HEADER1));
108         assertNotNull("header present", mail.getMessage().getHeader(HEADER2));
109     }
110     
111     public void testInvalidConfig() throws MessagingException JavaDoc {
112         boolean exception = false;
113         try {
114             setupMockedMailet(null,null);
115         } catch (MessagingException JavaDoc e) {
116             exception = true;
117         }
118         assertTrue("Exception thrown",exception);
119     }
120     
121     
122 }
123
Popular Tags