KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > userrepository > MockUsersRepositoryTest


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.userrepository;
21
22 import org.apache.avalon.framework.container.ContainerUtil;
23 import org.apache.james.services.User;
24 import org.apache.james.services.UsersRepository;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 import junit.framework.TestCase;
30
31 /**
32  * Test basic behaviours of UsersFileRepository
33  */

34 public class MockUsersRepositoryTest extends TestCase {
35
36     /**
37      * Users repository
38      */

39     protected UsersRepository usersRepository;
40
41     /**
42      * Create the repository to be tested.
43      *
44      * @return the user repository
45      * @throws Exception
46      */

47     protected UsersRepository getUsersRepository() throws Exception JavaDoc {
48         return new MockUsersRepository();
49     }
50
51     public void testUsersRepositoryEmpty() {
52         assertEquals("users repository not empty", 0, usersRepository.countUsers());
53         assertFalse("users repository not empty", usersRepository.list().hasNext());
54     }
55     
56     public void testAddUserOnce() {
57         boolean res = usersRepository.addUser("username", "password");
58         assertTrue("User not added", res);
59         res = usersRepository.addUser("username", "password2");
60         assertFalse("User added twice!", res);
61         try {
62             usersRepository.addUser("username2", (Object JavaDoc) "password2");
63             assertTrue(usersRepository.contains("username2"));
64             User u = new DefaultJamesUser("username3","SHA","password3");
65             usersRepository.addUser(u);
66             assertTrue(usersRepository.contains("username3"));
67         } catch (UnsupportedOperationException JavaDoc e) {
68             
69         }
70         
71         try {
72             usersRepository.addUser("username2", new Object JavaDoc());
73             fail("adduser should throw an exception if a non string is passed");
74         } catch (Exception JavaDoc e) {
75             
76         }
77         
78     }
79     
80     public void testUserAddedIsFound() {
81         boolean res = usersRepository.addUser("username", "password");
82         assertTrue("User not added", res);
83         User user = usersRepository.getUserByName("username");
84         assertNotNull(user);
85         assertEquals("username does not match", user.getUserName(), "username");
86         assertTrue("user not contained in the repository", usersRepository.contains("username"));
87         try {
88             assertTrue("case insensitive user not found in the repository", usersRepository.containsCaseInsensitive("userName"));
89         } catch (UnsupportedOperationException JavaDoc e) {
90             // some implementation could not support deprecated methods
91
}
92         
93         User u = usersRepository.getUserByName("uSERNAMe");
94         assertNull("found the user searching for a different case!", u);
95         
96         String JavaDoc realname = usersRepository.getRealName("uSERNAMe");
97         assertNull("name is not null", realname);
98         // assertEquals("name is different", "username", realname);
99
}
100     
101     public void testUserListing() {
102         ArrayList JavaDoc keys = new ArrayList JavaDoc(3);
103         keys.add("username1");
104         keys.add("username2");
105         keys.add("username3");
106         for (Iterator JavaDoc i = keys.iterator(); i.hasNext(); ) {
107             String JavaDoc username = (String JavaDoc) i.next();
108             boolean res = usersRepository.addUser(username, username);
109             assertTrue("User "+username+" not added", res);
110         }
111         assertEquals("Wrong number of users found", keys.size(), usersRepository.countUsers());
112
113         // check list return all and only the expected users
114
ArrayList JavaDoc check = new ArrayList JavaDoc(keys);
115         for (Iterator JavaDoc i = usersRepository.list(); i.hasNext(); ) {
116             String JavaDoc username = (String JavaDoc) i.next();
117             if (getPasswordsEnabled()) {
118                 assertTrue(usersRepository.test(username, username));
119                 User u = usersRepository.getUserByName(username);
120                 u.setPassword("newpass");
121                 assertTrue(usersRepository.updateUser(u));
122             }
123             assertTrue(check.contains(username));
124             check.remove(username);
125         }
126         assertEquals("Some user has not be found", 0, check.size());
127     }
128     
129     public void testUserPassword() {
130         assertTrue("user not added", usersRepository.addUser("username","password"));
131         assertEquals("didn't accept the correct password ", usersRepository.test("username", "password"), getPasswordsEnabled());
132         assertFalse("accepted the wrong password #1", usersRepository.test("username", "password2"));
133         assertFalse("accepted the wrong password #2", usersRepository.test("username2", "password"));
134         assertFalse("accepted the wrong password #3", usersRepository.test("username", "Password"));
135         assertFalse("accepted the wrong password #4", usersRepository.test("username", "passwords"));
136         assertFalse("accepted the wrong password #5", usersRepository.test("userName", "password"));
137     }
138     
139     protected boolean getPasswordsEnabled() {
140         return true;
141     }
142
143     public void testUserAddRemoveCycle() {
144         assertFalse("accepted login when no user existed", usersRepository.test("username", "password"));
145         try {
146             usersRepository.removeUser("username");
147             // UsersFileRepository accept this call for every argument
148
// fail("removing an unknown user didn't fail!");
149
} catch (Exception JavaDoc e) {
150             
151         }
152         assertTrue("user not added", usersRepository.addUser("username","password"));
153         assertEquals("didn't accept the correct password", usersRepository.test("username", "password"),getPasswordsEnabled());
154         User user = usersRepository.getUserByName("username");
155         user.setPassword("newpass");
156         try {
157             assertTrue("user not updated", usersRepository.updateUser(user));
158             assertEquals("new password accepted", usersRepository.test("username", "newpass"), getPasswordsEnabled());
159             assertFalse("old password rejected", usersRepository.test("username", "password"));
160         } catch (UnsupportedOperationException JavaDoc e) {
161             // if updating users is not allowed check that this is a repository without password checking
162
assertFalse(getPasswordsEnabled());
163         }
164         try {
165             usersRepository.removeUser("username");
166         } catch (Exception JavaDoc e) {
167             fail("removing the user failed!");
168         }
169         assertFalse("user not existing", usersRepository.contains("username"));
170         assertFalse("new password rejected", usersRepository.test("username", "newpass"));
171         assertFalse("updated a non existing user: should fail!", usersRepository.updateUser(user));
172     }
173     
174     
175     public void testCaseInsensitivesMethods() {
176         assertTrue("User not added", usersRepository.addUser("userName", "password"));
177         try {
178             assertTrue(usersRepository.containsCaseInsensitive("usERname"));
179             assertNotNull(usersRepository.getUserByNameCaseInsensitive("userNAMe"));
180         } catch (UnsupportedOperationException JavaDoc e) {
181             // some implementations do not support it.
182
}
183         assertEquals(usersRepository.addUser("USERNAME", "password"), getAllowMultipleUsersWithDifferentCases());
184         
185     }
186     
187     
188     /**
189      * @return
190      */

191     protected boolean getCheckCase() {
192         return true;
193     }
194     
195     protected boolean getAllowMultipleUsersWithDifferentCases() {
196         return getCheckCase();
197     }
198
199
200     /**
201      * @see junit.framework.TestCase#setUp()
202      */

203     protected void setUp() throws Exception JavaDoc {
204         super.setUp();
205         this.usersRepository = getUsersRepository();
206     }
207
208     /**
209      * @see junit.framework.TestCase#tearDown()
210      */

211     protected void tearDown() throws Exception JavaDoc {
212         disposeUsersRepository();
213         super.tearDown();
214     }
215
216
217     /**
218      * Dispose the repository
219      */

220     protected void disposeUsersRepository() {
221         ContainerUtil.dispose(this.usersRepository);
222     }
223
224 }
225
Popular Tags