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  package org.apache.james.transport.mailets;
21  
22  import junit.framework.TestCase;
23  
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.Mail;
28  import org.apache.mailet.Mailet;
29  
30  import javax.mail.MessagingException;
31  import javax.mail.internet.ParseException;
32  
33  public class RemoveMailAttributeTest extends TestCase {
34  
35      public static final String MAIL_ATTRIBUTE_NAME1 = "org.apache.james.test.junit";
36  
37      public static final String MAIL_ATTRIBUTE_NAME2 = "org.apache.james.test.junit2";
38  
39      private Mail setupMockedMail() throws ParseException {
40          Mail mockedMail = new FakeMail();
41          mockedMail.setAttribute(MAIL_ATTRIBUTE_NAME1, "true");
42          mockedMail.setAttribute(MAIL_ATTRIBUTE_NAME2, "true");
43          return mockedMail;
44      }
45  
46      private Mailet setupMailet(String attribute) throws MessagingException {
47          Mailet mailet = new RemoveMailAttribute();
48          FakeMailetConfig mci = new FakeMailetConfig("Test",
49                  new FakeMailContext());
50          if (attribute != null) {
51              mci.setProperty("name", attribute);
52          }
53  
54          mailet.init(mci);
55          return mailet;
56      }
57  
58  
59      public void testRemoveMailAttribute() throws MessagingException {
60          Mail m = setupMockedMail();
61          Mailet mailet = setupMailet(MAIL_ATTRIBUTE_NAME1);
62  
63          // check if the mail has a attribute
64          assertNotNull("Attribute exists",m.getAttribute(MAIL_ATTRIBUTE_NAME1));
65          assertNotNull("Attribute exists",m.getAttribute(MAIL_ATTRIBUTE_NAME2));
66  
67          mailet.service(m);
68  
69          // Check if the attribute was removed
70          assertNull("Attribute exists",m.getAttribute(MAIL_ATTRIBUTE_NAME1));
71          assertNotNull("Attribute deleted",m.getAttribute(MAIL_ATTRIBUTE_NAME2));
72      }
73  
74  
75      public void testInvalidConfig() throws MessagingException {
76          boolean exception = false;
77          try {
78              setupMailet(null);
79          } catch (MessagingException e) {
80              exception = true;
81          }
82  
83          assertTrue("invalid Config", exception);
84      }
85  
86  }