KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > core > MailTestAllImplementations


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
22 package org.apache.james.core;
23
24 import junit.framework.TestCase;
25
26 import javax.mail.MessagingException JavaDoc;
27
28 import org.apache.mailet.Mail;
29
30 /**
31  * testing common behavior of Mail implementors. subclasses automatically get their Mail-behavior tested.
32  */

33 public abstract class MailTestAllImplementations extends TestCase {
34
35     /** provide the concrete implementation to test */
36     protected abstract Mail createMailImplementation();
37
38     protected void helperTestInitialState(Mail mail) throws MessagingException JavaDoc {
39         assertFalse("no initial attributes", mail.hasAttributes());
40         assertNull("no initial error", mail.getErrorMessage());
41         assertNotNull("initial last update set", mail.getLastUpdated());
42         try {
43             assertTrue("no initial recipient", mail.getRecipients().isEmpty());
44         } catch (NullPointerException JavaDoc e) {
45             // current behavior. *BUT*, shouldn't this method better return with an empty list?!
46
}
47         assertEquals("initial remote address is localhost ip", "127.0.0.1", mail.getRemoteAddr());
48         assertEquals("initial remote host is localhost", "localhost", mail.getRemoteHost());
49         assertEquals("default initial state", Mail.DEFAULT, mail.getState());
50     }
51
52     protected void helperTestMessageSize(Mail mail, int expectedMsgSize) throws MessagingException JavaDoc {
53         try {
54             assertEquals("initial message size == " + expectedMsgSize, expectedMsgSize, mail.getMessageSize());
55         } catch (NullPointerException JavaDoc e) {
56             // current behavior. *BUT*, shouldn't this method return more gracefully?!
57
}
58     }
59
60     public void testAttributes() {
61         Mail mail = createMailImplementation();
62         assertFalse("no initial attributes", mail.hasAttributes());
63         assertFalse("attributes initially empty", mail.getAttributeNames().hasNext());
64         assertNull("not found on emtpy list", mail.getAttribute("test"));
65         assertNull("no previous item with key", mail.setAttribute("testKey", "testValue"));
66         assertEquals("item found", "testValue", mail.getAttribute("testKey"));
67         assertTrue("has attribute", mail.hasAttributes());
68         assertEquals("item removed", "testValue", mail.removeAttribute("testKey"));
69         assertNull("item no longer found", mail.getAttribute("testKey"));
70     }
71
72 }
73
Popular Tags