View Javadoc

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.core;
22  
23  import junit.framework.TestCase;
24  
25  import javax.mail.MessagingException;
26  
27  import org.apache.mailet.Mail;
28  
29  /***
30   * testing common behavior of Mail implementors. subclasses automatically get their Mail-behavior tested.
31   */
32  public abstract class MailTestAllImplementations extends TestCase {
33  
34      /*** provide the concrete implementation to test */
35      protected abstract Mail createMailImplementation();
36  
37      protected void helperTestInitialState(Mail mail) throws MessagingException {
38          assertFalse("no initial attributes", mail.hasAttributes());
39          assertNull("no initial error", mail.getErrorMessage());
40          assertNotNull("initial last update set", mail.getLastUpdated());
41          try {
42              assertTrue("no initial recipient", mail.getRecipients().isEmpty());
43          } catch (NullPointerException e) {
44              // current behavior. *BUT*, shouldn't this method better return with an empty list?!
45          }
46          assertEquals("initial remote address is localhost ip", "127.0.0.1", mail.getRemoteAddr());
47          assertEquals("initial remote host is localhost", "localhost", mail.getRemoteHost());
48          assertEquals("default initial state", Mail.DEFAULT, mail.getState());
49      }
50  
51      protected void helperTestMessageSize(Mail mail, int expectedMsgSize) throws MessagingException {
52          try {
53              assertEquals("initial message size == " + expectedMsgSize, expectedMsgSize, mail.getMessageSize());
54          } catch (NullPointerException e) {
55              // current behavior. *BUT*, shouldn't this method return more gracefully?!
56          }
57      }
58  
59      public void testAttributes() {
60          Mail mail = createMailImplementation();
61          assertFalse("no initial attributes", mail.hasAttributes());
62          assertFalse("attributes initially empty", mail.getAttributeNames().hasNext());
63          assertNull("not found on emtpy list", mail.getAttribute("test"));
64          assertNull("no previous item with key", mail.setAttribute("testKey", "testValue"));
65          assertEquals("item found", "testValue", mail.getAttribute("testKey"));
66          assertTrue("has attribute", mail.hasAttributes());
67          assertEquals("item removed", "testValue", mail.removeAttribute("testKey"));
68          assertNull("item no longer found", mail.getAttribute("testKey"));
69      }
70  
71  }