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.mailet.base.test.FakeMail;
25  import org.apache.mailet.base.test.FakeMailContext;
26  import org.apache.mailet.base.test.FakeMailetConfig;
27  import org.apache.mailet.base.test.MailUtil;
28  import org.apache.mailet.Mailet;
29  
30  import javax.mail.MessagingException;
31  import javax.mail.internet.MimeMessage;
32  import java.io.UnsupportedEncodingException;
33  
34  public class SetMimeHeaderTest extends TestCase {
35  
36      private Mailet mailet;
37  
38      private final String HEADER_NAME = "JUNIT";
39  
40      private final String HEADER_VALUE = "test-value";
41  
42      private String headerName = "defaultHeaderName";
43  
44      private String headerValue = "defaultHeaderValue";
45  
46      public SetMimeHeaderTest(String arg0) throws UnsupportedEncodingException {
47          super(arg0);
48      }
49  
50      private void setHeaderName(String headerName) {
51          this.headerName = headerName;
52      }
53  
54      private void setHeaderValue(String headerValue) {
55          this.headerValue = headerValue;
56      }
57  
58      private void setupMailet() throws MessagingException {
59          mailet = new SetMimeHeader();
60          FakeMailetConfig mci = new FakeMailetConfig("Test",
61                  new FakeMailContext());
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 {
70          MimeMessage mockedMimeMessage = MailUtil.createMimeMessage(headerName, headerValue);
71          FakeMail mockedMail = MailUtil.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 {
83          setHeaderName(HEADER_NAME);
84          setHeaderValue(headerValue);
85  
86          MimeMessage mockedMimeMessage = MailUtil.createMimeMessage(headerName, headerValue);
87          FakeMail mockedMail = MailUtil.createMockMail2Recipients(mockedMimeMessage);
88          setupMailet();
89  
90          mailet.service(mockedMail);
91  
92          assertEquals(HEADER_VALUE, mockedMail.getMessage().getHeader(
93                  HEADER_NAME)[0]);
94  
95      }
96  }