KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > smtpserver > SetMimeHeaderHandlerTest


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.smtpserver;
22
23 import junit.framework.TestCase;
24 import org.apache.avalon.framework.container.ContainerUtil;
25 import org.apache.james.smtpserver.core.SetMimeHeaderHandler;
26 import org.apache.james.test.mock.avalon.MockLogger;
27 import org.apache.james.test.util.Util;
28 import org.apache.mailet.Mail;
29
30 import javax.mail.MessagingException JavaDoc;
31 import javax.mail.internet.MimeMessage JavaDoc;
32
33 public class SetMimeHeaderHandlerTest extends TestCase {
34
35     private SMTPSession mockedSMTPSession;
36
37     private MimeMessage JavaDoc mockedMimeMessage;
38
39     private Mail mockedMail;
40
41     private final String JavaDoc HEADER_NAME = "JUNIT";
42
43     private final String JavaDoc HEADER_VALUE = "test-value";
44
45     private String JavaDoc headerName = "defaultHeaderName";
46
47     private String JavaDoc headerValue = "defaultHeaderValue";
48
49     protected void setUp() throws Exception JavaDoc {
50         super.setUp();
51         setupMockedSMTPSession();
52     }
53
54     private void setHeaderName(String JavaDoc headerName) {
55         this.headerName = headerName;
56     }
57
58     private void setHeaderValue(String JavaDoc headerValue) {
59         this.headerValue = headerValue;
60     }
61
62     private void setupMockedMimeMessage() throws MessagingException JavaDoc {
63         mockedMimeMessage = Util.createMimeMessage(headerName, headerValue);
64     }
65
66     private void setupMockedSMTPSession() {
67         mockedSMTPSession = new AbstractSMTPSession() {
68
69             public Mail getMail() {
70                 return mockedMail;
71             }
72
73             public int getRcptCount() {
74                 return 0;
75             }
76
77         };
78     }
79
80     // test if the Header was add
81
public void testHeaderIsPresent() throws MessagingException JavaDoc {
82         setHeaderName(HEADER_NAME);
83         setHeaderValue(HEADER_VALUE);
84
85         setupMockedMimeMessage();
86         mockedMail = Util.createMockMail2Recipients(mockedMimeMessage);
87
88         SetMimeHeaderHandler header = new SetMimeHeaderHandler();
89
90         ContainerUtil.enableLogging(header, new MockLogger());
91
92         header.setHeaderName(HEADER_NAME);
93         header.setHeaderValue(HEADER_VALUE);
94         header.onMessage(mockedSMTPSession);
95
96         assertEquals(HEADER_VALUE, mockedSMTPSession.getMail().getMessage()
97                 .getHeader(HEADER_NAME)[0]);
98     }
99
100     // test if the Header was replaced
101
public void testHeaderIsReplaced() throws MessagingException JavaDoc {
102         setHeaderName(HEADER_NAME);
103         setHeaderValue(headerValue);
104
105         setupMockedMimeMessage();
106         mockedMail = Util.createMockMail2Recipients(mockedMimeMessage);
107
108         SetMimeHeaderHandler header = new SetMimeHeaderHandler();
109
110         ContainerUtil.enableLogging(header, new MockLogger());
111
112         header.setHeaderName(HEADER_NAME);
113         header.setHeaderValue(HEADER_VALUE);
114         header.onMessage(mockedSMTPSession);
115
116         assertEquals(HEADER_VALUE, mockedSMTPSession.getMail().getMessage()
117                 .getHeader(HEADER_NAME)[0]);
118     }
119
120 }
121
Popular Tags