KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > services > MailServerTestAllImplementations


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.services;
23
24 import org.apache.avalon.framework.service.ServiceException;
25
26 import junit.framework.TestCase;
27
28 /**
29  * tests all implementations for interface MailServer
30  */

31 abstract public class MailServerTestAllImplementations extends TestCase {
32     
33     protected static final String JavaDoc EXISTING_USER_NAME = "testExistingUserName";
34
35     abstract public MailServer createMailServer() throws ServiceException;
36     abstract public boolean allowsPasswordlessUser();
37
38     /**
39      * while addUser() is part of MailServer interface, a user cannot be tested for afterwards
40      * at the same time, James allows to do exactly this via isLocalUser(), other implementations
41      * might vary.
42      */

43     abstract public boolean canTestUserExists();
44     abstract public boolean isUserExisting(MailServer mailServerImpl, String JavaDoc username);
45     
46     public void testId() throws ServiceException {
47         MailServer mailServer = createMailServer();
48         
49         String JavaDoc id = mailServer.getId();
50         assertNotNull("mail id not null", id);
51         assertFalse("mail id not empty", "".equals(id));
52     }
53     
54     public void testIdIncrement() throws ServiceException {
55         MailServer mailServer = createMailServer();
56         
57         String JavaDoc id1 = mailServer.getId();
58         String JavaDoc id2 = mailServer.getId();
59         assertFalse("next id is different", id1.equals(id2));
60     }
61     
62     public void testAddUser() throws ServiceException {
63         
64         // addUser acts on field localUsers for class org.apache.james.James
65
// thus, it is unrelated to getUserInbox() for the only known implementation of MailServer
66
// TODO clarify this
67

68         MailServer mailServer = createMailServer();
69
70         String JavaDoc userName = "testUserName";
71
72         if (canTestUserExists())
73         {
74             assertFalse("this is a fresh user", isUserExisting(mailServer, userName+"@localhost"));
75         }
76         
77         boolean allowsPasswordlessUser = allowsPasswordlessUser();
78         try {
79             boolean success = mailServer.addUser(userName, null);
80             if (!allowsPasswordlessUser) fail("null pwd was accepted unexpectedly");
81             if (!success) fail("null pwd was not accepted unexpectedly");
82         } catch (Exception JavaDoc e) {
83             if (allowsPasswordlessUser) fail("null pwd not accepted unexpectedly (with exception)");
84         }
85
86         userName = userName + "_next";
87         String JavaDoc password = "password";
88         
89         boolean success = mailServer.addUser(userName, password);
90         if (!success) fail("user has not been added");
91         
92         if (canTestUserExists())
93         {
94             assertTrue("user is present now", isUserExisting(mailServer, userName));
95         }
96         
97         boolean successAgain = mailServer.addUser(userName, password);
98         if (successAgain) fail("user has been added two times");
99         
100     }
101
102     public void testGetNonexistingUserInbox() throws ServiceException {
103
104         MailServer mailServer = createMailServer();
105
106         String JavaDoc userName = "testNonexisitingUserName";
107         MailRepository userInbox = null;
108         
109         userInbox = mailServer.getUserInbox(userName);
110         assertEquals("test user does not exist", null, userInbox);
111     }
112     
113     public void testGetExisitingUserInbox() throws ServiceException {
114         MailServer mailServer = createMailServer();
115
116         MailRepository userInbox = mailServer.getUserInbox(EXISTING_USER_NAME);
117         assertNotNull("existing user exists", userInbox);
118     }
119 }
120
Popular Tags