KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.UnsupportedEncodingException JavaDoc;
33
34 public class SetMimeHeaderTest extends TestCase {
35
36     private Mailet mailet;
37
38     private final String JavaDoc HEADER_NAME = "JUNIT";
39
40     private final String JavaDoc HEADER_VALUE = "test-value";
41
42     private String JavaDoc headerName = "defaultHeaderName";
43
44     private String JavaDoc headerValue = "defaultHeaderValue";
45
46     public SetMimeHeaderTest(String JavaDoc arg0) throws UnsupportedEncodingException JavaDoc {
47         super(arg0);
48     }
49
50     private void setHeaderName(String JavaDoc headerName) {
51         this.headerName = headerName;
52     }
53
54     private void setHeaderValue(String JavaDoc headerValue) {
55         this.headerValue = headerValue;
56     }
57
58     private void setupMailet() throws MessagingException JavaDoc {
59         mailet = new SetMimeHeader();
60         MockMailetConfig mci = new MockMailetConfig("Test",
61                 new MockMailContext());
62         mci.setProperty("name", HEADER_NAME);
63         mci.setProperty("value", HEADER_VALUE);
64
65         mailet.init(mci);
66     }
67
68     // test if the Header was add
69
public void testHeaderIsPresent() throws MessagingException JavaDoc {
70         MimeMessage JavaDoc mockedMimeMessage = Util.createMimeMessage(headerName, headerValue);
71         MockMail mockedMail = Util.createMockMail2Recipients(mockedMimeMessage);
72         setupMailet();
73
74         mailet.service(mockedMail);
75
76         assertEquals(HEADER_VALUE, mockedMail.getMessage().getHeader(
77                 HEADER_NAME)[0]);
78
79     }
80
81     // test if the Header was replaced
82
public void testHeaderIsReplaced() throws MessagingException JavaDoc {
83         setHeaderName(HEADER_NAME);
84         setHeaderValue(headerValue);
85
86         MimeMessage JavaDoc mockedMimeMessage = Util.createMimeMessage(headerName, headerValue);
87         MockMail mockedMail = Util.createMockMail2Recipients(mockedMimeMessage);
88         setupMailet();
89
90         mailet.service(mockedMail);
91
92         assertEquals(HEADER_VALUE, mockedMail.getMessage().getHeader(
93                 HEADER_NAME)[0]);
94
95     }
96 }
97
Popular Tags