KickJava   Java API By Example, From Geeks To Geeks.

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


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.MockMailContext;
25 import org.apache.james.test.mock.mailet.MockMailetConfig;
26 import org.apache.james.test.util.Util;
27 import org.apache.mailet.Mail;
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 import java.io.UnsupportedEncodingException JavaDoc;
34
35 public class AddSubjectPrefixTest extends TestCase {
36
37     private MimeMessage JavaDoc mockedMimeMessage;
38
39     private Mail mockedMail;
40
41     private Mailet mailet;
42
43     private final String JavaDoc SUBJECT_PREFIX = "JUNIT";
44
45     private String JavaDoc subject = null;
46
47     public AddSubjectPrefixTest(String JavaDoc arg0)
48             throws UnsupportedEncodingException JavaDoc {
49         super(arg0);
50     }
51
52     private void setSubject(String JavaDoc subject) {
53         this.subject = subject;
54     }
55
56     private void setupMockedMimeMessage() throws MessagingException JavaDoc {
57         mockedMimeMessage = Util.createMimeMessageWithSubject(subject);
58     }
59
60     private void setupMockedMail(MimeMessage JavaDoc m) throws ParseException JavaDoc {
61         mockedMail = Util.createMockMail2Recipients(m);
62     }
63
64     private void setupMailet() throws MessagingException JavaDoc {
65         setupMockedMimeMessage();
66         mailet = new AddSubjectPrefix();
67         MockMailetConfig mci = new MockMailetConfig("Test", new MockMailContext());
68         mci.setProperty("subjectPrefix", SUBJECT_PREFIX);
69
70         mailet.init(mci);
71     }
72
73     private void setupInvalidMailet() throws MessagingException JavaDoc {
74         setupMockedMimeMessage();
75         mailet = new AddSubjectPrefix();
76         MockMailetConfig mci = new MockMailetConfig("Test", new MockMailContext());
77         mci.setProperty("subjectPrefix", "");
78
79         mailet.init(mci);
80     }
81
82     // test if prefix was added
83
public void testSubjectPrefixWasAdded() throws MessagingException JavaDoc {
84         setSubject("test");
85         setupMockedMimeMessage();
86         setupMockedMail(mockedMimeMessage);
87         setupMailet();
88
89         mailet.service(mockedMail);
90
91         assertEquals(SUBJECT_PREFIX + " " + subject, mockedMail.getMessage().getSubject());
92
93     }
94
95     // test if prefix was added to message without subject
96
public void testSubjectPrefixWasAddedWithoutSubject()
97             throws MessagingException JavaDoc {
98         setupMockedMimeMessage();
99         setupMockedMail(mockedMimeMessage);
100         setupMailet();
101
102         mailet.service(mockedMail);
103
104         assertEquals(SUBJECT_PREFIX, mockedMail.getMessage().getSubject());
105
106     }
107
108     // test if exception was thrown cause missing configure value
109
public void testThrowException() throws MessagingException JavaDoc {
110         boolean exceptionThrown = false;
111         setupMockedMimeMessage();
112         setupMockedMail(mockedMimeMessage);
113
114         try {
115             setupInvalidMailet();
116         } catch (MessagingException JavaDoc m) {
117             exceptionThrown = true;
118         }
119         assertTrue(exceptionThrown);
120
121     }
122 }
123
Popular Tags