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.services;
22  
23  import junit.framework.TestCase;
24  
25  /***
26   * tests all implementations for interface MailServer
27   */
28  abstract public class MailServerTestAllImplementations extends TestCase {
29      
30      protected static final String EXISTING_USER_NAME = "testExistingUserName";
31  
32      abstract public MailServer createMailServer();
33      abstract public boolean allowsPasswordlessUser();
34  
35      /***
36       * while addUser() is part of MailServer interface, a user cannot be tested for afterwards
37       * at the same time, James allows to do exactly this via isLocalUser(), other implementations
38       * might vary. 
39       */
40      abstract public boolean canTestUserExists();
41      abstract public boolean isUserExisting(MailServer mailServerImpl, String username);
42      
43      public void testId() {
44          MailServer mailServer = createMailServer();
45          
46          String id = mailServer.getId();
47          assertNotNull("mail id not null", id);
48          assertFalse("mail id not empty", "".equals(id));
49      }
50      
51      public void testIdIncrement() {
52          MailServer mailServer = createMailServer();
53          
54          String id1 = mailServer.getId();
55          String id2 = mailServer.getId();
56          assertFalse("next id is different", id1.equals(id2));
57      }
58      
59      public void testAddUser() {
60          
61          // addUser acts on field localUsers for class org.apache.james.James 
62          // thus, it is unrelated to getUserInbox() for the only known implementation of MailServer
63          // TODO clarify this 
64          
65          MailServer mailServer = createMailServer();
66  
67          String userName = "testUserName";
68  
69          if (canTestUserExists())
70          {
71              assertFalse("this is a fresh user", isUserExisting(mailServer, userName));
72          }
73          
74          boolean allowsPasswordlessUser = allowsPasswordlessUser();
75          try {
76              boolean success = mailServer.addUser(userName, null);
77              if (!allowsPasswordlessUser) fail("null pwd was accepted unexpectedly");
78              if (!success) fail("null pwd was not accepted unexpectedly"); 
79          } catch (Exception e) {
80              if (allowsPasswordlessUser) fail("null pwd not accepted unexpectedly (with exception)");
81          }
82  
83          userName = userName + "_next"; 
84          String password = "password";
85          
86          boolean success = mailServer.addUser(userName, password);
87          if (!success) fail("user has not been added"); 
88          
89          if (canTestUserExists())
90          {
91              assertTrue("user is present now", isUserExisting(mailServer, userName));
92          }
93          
94          boolean successAgain = mailServer.addUser(userName, password);
95          if (successAgain) fail("user has been added two times"); 
96          
97      }
98  
99      public void testGetNonexistingUserInbox() {
100 
101         MailServer mailServer = createMailServer();
102 
103         String userName = "testNonexisitingUserName";
104         MailRepository userInbox = null;
105         
106         userInbox = mailServer.getUserInbox(userName);
107         assertEquals("test user does not exist", null, userInbox);
108     }
109     
110     public void testGetExisitingUserInbox() {
111         MailServer mailServer = createMailServer();
112 
113         MailRepository userInbox = mailServer.getUserInbox(EXISTING_USER_NAME);
114         assertNotNull("existing user exists", userInbox);
115     }
116 }