2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.security.authentication;
18 import junit.framework.TestCase;
20 import org.wamblee.security.authentication.Group;
21 import org.wamblee.security.authentication.GroupSet;
22 import org.wamblee.security.authentication.InMemoryGroupSet;
23 import org.wamblee.security.authentication.InMemoryUserSet;
24 import org.wamblee.security.authentication.RegexpNameValidator;
25 import org.wamblee.security.authentication.User;
26 import org.wamblee.security.authentication.UserMgtException;
27 import org.wamblee.security.authentication.UserSet;
28 import org.wamblee.security.authentication.UserMgtException.Reason;
31 import java.sql.SQLException;
36 * Tests the inmemory user set. Intended to be subclassed for other
37 * implementations of user set.
39 public class InMemoryUserSetTest extends TestCase {
40 protected static final String PASSWORD = "abc123";
42 private UserSet users;
44 private GroupSet groups;
49 * This method must be overriden in subclasses.
51 * @return New user set object.
53 protected UserSet createUserSet() {
54 return new InMemoryUserSet(new RegexpNameValidator(
55 RegexpNameValidator.PASSWORD_PATTERN, Reason.INVALID_PASSWORD,
56 "Password must contain at least 6 characters"),
57 new Md5HexMessageDigester());
61 * This method must be overriden in subclasses.
63 * @return New group set object.
65 protected GroupSet createGroupSet() {
66 return new InMemoryGroupSet();
72 * @see org.wamblee.test.SpringTestCase#setUp()
75 protected void setUp() throws Exception {
77 users = createUserSet();
78 groups = createGroupSet();
79 group = new Group("group0");
84 protected UserSet getUsers() {
88 protected GroupSet getGroups() {
92 protected Group createGroup(String aName) {
93 return new Group(aName);
96 protected User createUser(String aName, String aPassword, Group aGroup)
97 throws UserMgtException {
98 return UsermgtTestUtils.createUser(aName, aPassword, aGroup);
101 protected void addUserToGroup(User aUser, Group aGroup)
102 throws UserMgtException {
103 aUser.addGroup(aGroup);
106 protected void removeUserFromGroup(User aUser, Group aGroup)
107 throws UserMgtException {
108 aUser.removeGroup(aGroup);
112 * Additional check to be implemented by a subclass.
115 * User to check for existence.
118 protected void checkUserExists(String aUser) throws Exception {
123 * Additional check to be implemented by a subclass.
126 * User to check for non-existence.
129 protected void checkUserNotExists(String aUser) throws Exception {
134 * Additional check to be implemented by a subclass.
137 * Expected number of users.
140 protected void checkUserCount(int aSize) throws Exception {
141 assertEquals(aSize, users.size());
145 * Additional check to be implemented by a subclass.
148 * User to check for existence.
151 protected void checkGroupExists(String aUser) throws Exception {
156 * Additional check to be implemented by a subclass.
159 * User to check for non-existence.
162 protected void checkGroupNotExists(String aUser) throws Exception {
167 * Additional check to be implemented by a subclass.
170 * Expected number of users.
173 protected void checkGroupCount(int aSize) throws Exception {
178 * Adds a user and verifies that the user is added using find(), list(), and
182 public void testAdd() throws Exception {
183 User user = createUser("user1", PASSWORD, group);
184 assertTrue(users.add(user));
185 checkUserExists(user.getName());
188 User user2 = users.find("user1");
189 assertNotNull(user2);
190 assertEquals(user.getName(), user2.getName());
192 Set<User> set = users.list();
193 assertEquals(1, set.size());
194 assertTrue(set.contains(user));
198 * Tries to find a non-existing user. Verifies that null is returned.
201 public void testFindUnknownUser() throws Exception {
202 User user1 = createUser("user1", PASSWORD, group);
203 User user2 = createUser("user2", PASSWORD, group);
206 checkUserExists(user1.getName());
207 checkUserExists(user2.getName());
209 assertNull(users.find("user3"));
210 checkUserNotExists("user3");
214 * Adds duplicate user. Verifies that the existing user is left untouched.
217 public void testAddDuplicateUser() throws Exception {
218 User user1 = createUser("user1", PASSWORD, group);
221 assertEquals(1, users.list().size());
222 assertTrue(users.contains(user1));
223 user1 = createUser("user1", PASSWORD, group);
224 assertFalse(users.add(user1));
225 assertEquals(1, users.list().size());
227 checkUserExists(user1.getName());
232 * Removes a user. Verifies that the user is removed and the return value is
236 public void testRemoveUser() throws Exception {
237 User user1 = createUser("user1", PASSWORD, group);
239 assertTrue(users.contains(user1));
242 assertTrue(users.remove(user1));
243 assertFalse(users.contains(user1));
244 assertNull(users.find(user1.getName()));
245 assertEquals(0, users.list().size());
250 * Removes a non-existing user. Verifies that no users are removed an that
251 * the return value is true.
254 public void testRemoveNonExistingUser() throws Exception,
256 User user1 = createUser("user1", PASSWORD, group);
260 User nonExistingUser = createUser("user2", PASSWORD, group);
261 assertFalse(users.remove(nonExistingUser));
262 assertTrue(users.contains(user1));
263 assertEquals(1, users.list().size());
268 * Adds a number of users to the set and verifies that list() returns them
272 public void testList() throws Exception {
273 User user1 = createUser("user1", PASSWORD, group);
274 User user2 = createUser("user2", PASSWORD, group);
275 User user3 = createUser("user3", PASSWORD, group);
276 assertTrue(users.add(user1));
277 assertTrue(users.add(user2));
278 assertTrue(users.add(user3));
280 checkUserExists(user1.getName());
281 checkUserExists(user2.getName());
282 checkUserExists(user3.getName());
284 Set<User> set = users.list();
285 assertTrue(set.contains(user1));
286 assertTrue(set.contains(user2));
287 assertTrue(set.contains(user3));
293 * Adds several users to different groups and verifies that the correct
294 * users are returned when looking for users in different groups.
296 * @throws SQLException
298 public void testListByGroup() throws Exception {
299 Group group1 = new Group("group1");
300 Group group2 = new Group("group2");
301 Group group3 = new Group("group3");
310 User user1 = createUser("user1", PASSWORD, group1);
311 user1.addGroup(group2);
312 user1.addGroup(group3);
314 User user2 = createUser("user2", PASSWORD, group2);
315 user2.addGroup(group3);
317 User user3 = createUser("user3", PASSWORD, group3);
322 checkUserExists(user1.getName());
323 checkUserExists(user2.getName());
324 checkUserExists(user3.getName());
326 checkGroupExists(group1.getName());
327 checkGroupExists(group2.getName());
328 checkGroupExists(group3.getName());
331 checkGroupCount(3 + 1); // also count the group that was created in the
334 Set<User> list = users.list(group1);
335 assertTrue(list.contains(user1));
336 assertEquals(1, list.size());
338 list = users.list(group2);
339 assertTrue(list.contains(user1));
340 assertTrue(list.contains(user2));
341 assertEquals(2, list.size());
343 list = users.list(group3);
344 assertTrue(list.contains(user1));
345 assertTrue(list.contains(user2));
346 assertTrue(list.contains(user3));
347 assertEquals(3, list.size());