2 * Copyright 2005 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.usermgt;
18 import junit.framework.TestCase;
20 import org.wamblee.security.encryption.Md5HexMessageDigester;
22 import org.wamblee.usermgt.UserMgtException.Reason;
24 import java.sql.SQLException;
30 * Tests the inmemory user set. Intended to be subclassed for other
31 * implementations of user set.
33 public class InMemoryUserSetTest extends TestCase {
37 protected static final String PASSWORD = "abc123";
42 private UserSet users;
47 private GroupSet groups;
55 * This method must be overriden in subclasses.
57 * @return New user set object.
59 protected UserSet createUserSet() {
60 return new InMemoryUserSet(new RegexpNameValidator(
61 RegexpNameValidator.PASSWORD_PATTERN, Reason.INVALID_PASSWORD,
62 "Password must contain at least 6 characters"),
63 new Md5HexMessageDigester());
67 * This method must be overriden in subclasses.
69 * @return New group set object.
71 protected GroupSet createGroupSet() {
72 return new InMemoryGroupSet();
76 * @see org.wamblee.test.SpringTestCase#setUp()
81 * @throws Exception DOCUMENT ME!
84 protected void setUp() throws Exception {
86 users = createUserSet();
87 groups = createGroupSet();
88 group = new Group("group0");
96 * @return DOCUMENT ME!
98 protected UserSet getUsers() {
105 * @return DOCUMENT ME!
107 protected GroupSet getGroups() {
114 * @param aName DOCUMENT ME!
116 * @return DOCUMENT ME!
118 protected Group createGroup(String aName) {
119 return new Group(aName);
125 * @param aName DOCUMENT ME!
126 * @param aPassword DOCUMENT ME!
127 * @param aGroup DOCUMENT ME!
129 * @return DOCUMENT ME!
131 * @throws UserMgtException DOCUMENT ME!
133 protected User createUser(String aName, String aPassword, Group aGroup)
134 throws UserMgtException {
135 return UsermgtTestUtils.createUser(aName, aPassword, aGroup);
141 * @param aUser DOCUMENT ME!
142 * @param aGroup DOCUMENT ME!
144 * @throws UserMgtException DOCUMENT ME!
146 protected void addUserToGroup(User aUser, Group aGroup)
147 throws UserMgtException {
148 aUser.addGroup(aGroup);
154 * @param aUser DOCUMENT ME!
155 * @param aGroup DOCUMENT ME!
157 * @throws UserMgtException DOCUMENT ME!
159 protected void removeUserFromGroup(User aUser, Group aGroup)
160 throws UserMgtException {
161 aUser.removeGroup(aGroup);
165 * Additional check to be implemented by a subclass.
167 * @param aUser User to check for existence.
169 * @throws SQLException DOCUMENT ME!
171 protected void checkUserExists(String aUser) throws SQLException {
176 * Additional check to be implemented by a subclass.
178 * @param aUser User to check for non-existence.
180 * @throws SQLException DOCUMENT ME!
182 protected void checkUserNotExists(String aUser) throws SQLException {
187 * Additional check to be implemented by a subclass.
189 * @param aSize Expected number of users.
191 * @throws SQLException DOCUMENT ME!
193 protected void checkUserCount(int aSize) throws SQLException {
194 assertEquals(aSize, users.size());
198 * Additional check to be implemented by a subclass.
200 * @param aUser User to check for existence.
202 * @throws SQLException DOCUMENT ME!
204 protected void checkGroupExists(String aUser) throws SQLException {
209 * Additional check to be implemented by a subclass.
211 * @param aUser User to check for non-existence.
213 * @throws SQLException DOCUMENT ME!
215 protected void checkGroupNotExists(String aUser) throws SQLException {
220 * Additional check to be implemented by a subclass.
222 * @param aSize Expected number of users.
224 * @throws SQLException DOCUMENT ME!
226 protected void checkGroupCount(int aSize) throws SQLException {
231 * Adds a user and verifies that the user is added using find(),
232 * list(), and contains().
234 * @throws SQLException DOCUMENT ME!
235 * @throws UserMgtException DOCUMENT ME!
237 public void testAdd() throws SQLException, UserMgtException {
238 User user = createUser("user1", PASSWORD, group);
239 assertTrue(users.add(user));
240 checkUserExists(user.getName());
243 User user2 = users.find("user1");
244 assertNotNull(user2);
245 assertEquals(user.getName(), user2.getName());
247 Set<User> set = users.list();
248 assertEquals(1, set.size());
249 assertTrue(set.contains(user));
253 * Tries to find a non-existing user. Verifies that null is
256 * @throws SQLException DOCUMENT ME!
257 * @throws UserMgtException DOCUMENT ME!
259 public void testFindUnknownUser() throws SQLException, UserMgtException {
260 User user1 = createUser("user1", PASSWORD, group);
261 User user2 = createUser("user2", PASSWORD, group);
264 checkUserExists(user1.getName());
265 checkUserExists(user2.getName());
267 assertNull(users.find("user3"));
268 checkUserNotExists("user3");
272 * Adds duplicate user. Verifies that the existing user is left
275 * @throws SQLException DOCUMENT ME!
276 * @throws UserMgtException DOCUMENT ME!
278 public void testAddDuplicateUser() throws SQLException, UserMgtException {
279 User user1 = createUser("user1", PASSWORD, group);
282 assertEquals(1, users.list().size());
283 assertTrue(users.contains(user1));
284 user1 = createUser("user1", PASSWORD, group);
285 assertFalse(users.add(user1));
286 assertEquals(1, users.list().size());
288 checkUserExists(user1.getName());
293 * Removes a user. Verifies that the user is removed and the
294 * return value is true.
296 * @throws SQLException DOCUMENT ME!
297 * @throws UserMgtException DOCUMENT ME!
299 public void testRemoveUser() throws SQLException, UserMgtException {
300 User user1 = createUser("user1", PASSWORD, group);
302 assertTrue(users.contains(user1));
305 assertTrue(users.remove(user1));
306 assertFalse(users.contains(user1));
307 assertNull(users.find(user1.getName()));
308 assertEquals(0, users.list().size());
313 * Removes a non-existing user. Verifies that no users are removed
314 * an that the return value is true.
316 * @throws SQLException DOCUMENT ME!
317 * @throws UserMgtException DOCUMENT ME!
319 public void testRemoveNonExistingUser()
320 throws SQLException, UserMgtException {
321 User user1 = createUser("user1", PASSWORD, group);
325 User nonExistingUser = createUser("user2", PASSWORD, group);
326 nonExistingUser.setPrimaryKey(new Long(1000));
327 nonExistingUser.setPersistedVersion(10);
328 assertFalse(users.remove(nonExistingUser));
329 assertTrue(users.contains(user1));
330 assertEquals(1, users.list().size());
335 * Adds a number of users to the set and verifies that list()
338 * @throws SQLException DOCUMENT ME!
339 * @throws UserMgtException DOCUMENT ME!
341 public void testList() throws SQLException, UserMgtException {
342 User user1 = createUser("user1", PASSWORD, group);
343 User user2 = createUser("user2", PASSWORD, group);
344 User user3 = createUser("user3", PASSWORD, group);
345 assertTrue(users.add(user1));
346 assertTrue(users.add(user2));
347 assertTrue(users.add(user3));
349 checkUserExists(user1.getName());
350 checkUserExists(user2.getName());
351 checkUserExists(user3.getName());
353 Set<User> set = users.list();
354 assertTrue(set.contains(user1));
355 assertTrue(set.contains(user2));
356 assertTrue(set.contains(user3));
362 * Adds several users to different groups and verifies that the
363 * correct users are returned when looking for users in different groups.
365 * @throws SQLException
366 * @throws UserMgtException DOCUMENT ME!
368 public void testListByGroup() throws SQLException, UserMgtException {
369 Group group1 = new Group("group1");
370 Group group2 = new Group("group2");
371 Group group3 = new Group("group3");
380 User user1 = createUser("user1", PASSWORD, group1);
381 user1.addGroup(group2);
382 user1.addGroup(group3);
384 User user2 = createUser("user2", PASSWORD, group2);
385 user2.addGroup(group3);
387 User user3 = createUser("user3", PASSWORD, group3);
392 checkUserExists(user1.getName());
393 checkUserExists(user2.getName());
394 checkUserExists(user3.getName());
396 checkGroupExists(group1.getName());
397 checkGroupExists(group2.getName());
398 checkGroupExists(group3.getName());
401 checkGroupCount(3 + 1); // also count the group that was created in the setUp().
403 Set<User> list = users.list(group1);
404 assertTrue(list.contains(user1));
405 assertEquals(1, list.size());
407 list = users.list(group2);
408 assertTrue(list.contains(user1));
409 assertTrue(list.contains(user2));
410 assertEquals(2, list.size());
412 list = users.list(group3);
413 assertTrue(list.contains(user1));
414 assertTrue(list.contains(user2));
415 assertTrue(list.contains(user3));
416 assertEquals(3, list.size());