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.apache.log4j.Logger;
22 import org.wamblee.security.encryption.Md5HexMessageDigester;
24 import org.wamblee.usermgt.UserMgtException.Reason;
30 * Test of user administration implementation.
32 * @author Erik Brakkee
34 public class UserAdministrationImplTest extends TestCase {
38 private static final Logger LOGGER = Logger.getLogger(UserAdministrationImplTest.class);
43 private static final String USER1 = "piet";
48 private static final String PASS1 = "passpiet";
53 private static final String USER2 = "kees";
58 private static final String PASS2 = "passkees";
63 private static final String GROUP1 = "cyclists";
68 private static final String GROUP2 = "runners";
73 private UserAdministration admin;
78 * @see junit.framework.TestCase#setUp()
83 * @throws Exception DOCUMENT ME!
86 protected void setUp() throws Exception {
88 admin = createAdmin();
94 * @return DOCUMENT ME!
96 protected UserAdministration createAdmin() {
97 UserSet users = new InMemoryUserSet(new RegexpNameValidator(
98 RegexpNameValidator.PASSWORD_PATTERN,
99 Reason.INVALID_PASSWORD,
100 "Password must contain at least 6 characters"),
101 new Md5HexMessageDigester());
102 GroupSet groups = new InMemoryGroupSet();
104 return new UserAdministrationImpl(users, groups,
105 new RegexpNameValidator(RegexpNameValidator.ID_PATTERN,
106 Reason.INVALID_USERNAME, "Invalid user"),
107 new RegexpNameValidator(RegexpNameValidator.ID_PATTERN,
108 Reason.INVALID_GROUPNAME, "Invalid group"));
114 * @param aName DOCUMENT ME!
115 * @param aPassword DOCUMENT ME!
116 * @param aGroup DOCUMENT ME!
118 * @return DOCUMENT ME!
120 * @throws UserMgtException DOCUMENT ME!
122 protected User createUser(String aName, String aPassword, Group aGroup)
123 throws UserMgtException {
124 return UsermgtTestUtils.createUser(aName, aPassword, aGroup);
128 * Constructs the admin, verify it contains no users and no groups.
130 public void testConstruct() {
131 assertEquals(0, admin.getUsers().size());
132 assertEquals(0, admin.getGroups().size());
133 assertEquals(0, admin.getUserCount());
134 assertEquals(0, admin.getGroupCount());
138 * Creates a new group. Verifies the group is created correctly and
139 * that the user is added.
141 * @throws UserMgtException DOCUMENT ME!
143 public void testCreateGroup() throws UserMgtException {
144 Group group = admin.createGroup(GROUP1);
145 assertNotNull(group);
146 assertEquals(GROUP1, group.getName());
148 Set<Group> groups = admin.getGroups();
149 assertEquals(1, groups.size());
150 assertEquals(1, admin.getGroupCount());
151 assertTrue(groups.contains(group));
157 * @param aUsername DOCUMENT ME!
159 private void createInvalidGroup(String aUsername) {
161 admin.createGroup(aUsername);
163 } catch (UserMgtException e) {
164 assertEquals(UserMgtException.Reason.INVALID_GROUPNAME,
166 assertEquals(0, admin.getGroupCount());
171 * Creates a new group with an invalid name. Verifies that the
172 * appropriate exception is thrown.
174 * @throws UserMgtException
176 public void testCreateInvalidGroupName() throws UserMgtException {
177 createInvalidGroup("");
178 createInvalidGroup("0abc"); // should not start with digits
179 createInvalidGroup("a b"); // should not contain spaces
180 createInvalidGroup(" aa");
181 createInvalidGroup("aa ");
185 * Creates a new group which conflicts with an existing one.
186 * Verifies that the UserMgtException is thrown and that no group is
189 * @throws UserMgtException DOCUMENT ME!
191 public void testCreateDuplicateGroup() throws UserMgtException {
192 admin.createGroup(GROUP1);
195 admin.createGroup(GROUP1);
196 } catch (UserMgtException e) {
197 assertEquals(UserMgtException.Reason.DUPLICATE_GROUP, e.getReason());
198 assertEquals(1, admin.getGroupCount());
207 * Creates a new user. Verifies the user is created correctly and
208 * that the user is added.
210 * @throws UserMgtException DOCUMENT ME!
212 public void testCreateUser() throws UserMgtException {
213 Group group = admin.createGroup(GROUP1);
214 User user = admin.createUser(USER1, PASS1, group);
216 assertEquals(USER1, user.getName());
217 user.checkPassword(PASS1);
219 Set<User> users = admin.getUsers();
220 assertEquals(1, users.size());
221 assertEquals(1, admin.getUserCount());
222 assertTrue(users.contains(user));
228 * @param aUsername DOCUMENT ME!
229 * @param aGroup DOCUMENT ME!
231 private void createInvalidUser(String aUsername, Group aGroup) {
233 admin.createUser(aUsername, "pass", aGroup);
235 } catch (UserMgtException e) {
236 assertEquals(UserMgtException.Reason.INVALID_USERNAME, e.getReason());
237 assertEquals(0, admin.getUserCount());
242 * Constructs users with invalid names. Verifies that the
243 * appropriate exception is thrown.
245 * @throws UserMgtException DOCUMENT ME!
247 public void testCreateInvalidUserName() throws UserMgtException {
248 Group group = admin.createGroup(GROUP1);
249 createInvalidUser("", group);
250 createInvalidUser("0abc", group); // should not start with digits
251 createInvalidUser("a b", group); // should not contain spaces
252 createInvalidUser(" aa", group);
253 createInvalidUser("aa ", group);
257 * Creates a new user which conflicts with an existing one.
258 * Verifies that the UserMgtException is thrown and that no user is added.
260 * @throws UserMgtException DOCUMENT ME!
262 public void testCreateDuplicateUser() throws UserMgtException {
263 Group group = admin.createGroup(GROUP1);
264 admin.createUser(USER1, PASS1, group);
267 admin.createUser(USER1, PASS2, group);
269 } catch (UserMgtException e) {
270 assertEquals(UserMgtException.Reason.DUPLICATE_USER, e.getReason());
271 assertEquals(1, admin.getUserCount());
276 * Gets a known user by name. Verifies the correct user is
277 * obtained. Verifies that null is returned when trying to obtain an
280 * @throws UserMgtException DOCUMENT ME!
282 public void testGetUser() throws UserMgtException {
283 Group group = admin.createGroup(GROUP1);
284 User user = admin.createUser(USER1, PASS1, group);
285 User user2 = admin.getUser(USER1);
286 assertTrue(user.equals(user2));
287 assertNull(admin.getUser(USER2));
291 * Gets a known group by name. Verifies the correct group is
292 * obtained. Verifies that null is returned when the group is not known.
294 * @throws UserMgtException DOCUMENT ME!
296 public void testGetGroup() throws UserMgtException {
297 Group group = admin.createGroup(GROUP1);
298 Group group2 = admin.getGroup(GROUP1);
299 assertTrue(group.equals(group2));
300 assertNull(admin.getGroup(GROUP2));
304 * Adds a user to a group. Verifies that the user is added using
305 * several API calls. Verifies that an exception occurs if the user is not
306 * already part of the group.
308 * @throws UserMgtException DOCUMENT ME!
310 public void testAddUserToGroup() throws UserMgtException {
311 Group group = admin.createGroup(GROUP1);
312 User user = admin.createUser(USER1, PASS1, group);
313 Group group2 = admin.createGroup(GROUP2);
314 assertTrue(user.isInGroup(group));
315 assertFalse(user.isInGroup(group2));
316 admin.addUserToGroup(user, group2);
317 assertTrue(user.isInGroup(group));
318 assertTrue(user.isInGroup(group2));
320 Set<User> users = admin.getUsers(group2);
321 assertNotNull(users);
322 assertEquals(1, users.size());
323 assertTrue(users.contains(user));
326 admin.addUserToGroup(user, group);
327 } catch (UserMgtException e) {
328 assertEquals(UserMgtException.Reason.USER_ALREADY_IN_GROUP,
338 * Adds a user to a group where the user does not exist. Verifies
339 * that an exception occurs.
341 * @throws UserMgtException DOCUMENT ME!
343 public void testAddUserToGroupUnknownUser() throws UserMgtException {
344 Group group = admin.createGroup(GROUP1);
345 User user = createUser(USER1, PASS1, group);
348 admin.addUserToGroup(user, group);
349 } catch (UserMgtException e) {
350 assertEquals(UserMgtException.Reason.UNKNOWN_USER, e.getReason());
359 * Adds a user to a group where the user does not exist. Verifies
360 * that an exception occurs.
362 * @throws UserMgtException DOCUMENT ME!
364 public void testAddUserToGroupUnknownGroup() throws UserMgtException {
365 Group group = admin.createGroup(GROUP1);
366 User user = admin.createUser(USER1, PASS1, group);
367 Group group2 = new Group(GROUP2);
370 admin.addUserToGroup(user, group2);
371 } catch (UserMgtException e) {
372 assertEquals(UserMgtException.Reason.UNKNOWN_GROUP, e.getReason());
381 * Removes a user from a group. Verifies that the user is removed
382 * from the group using several API calls. Verifies that an exception
383 * occurs if the user not part of the group or if the user is only part of
386 * @throws UserMgtException DOCUMENT ME!
388 public void testRemoveUserFromGroup() throws UserMgtException {
389 Group group = admin.createGroup(GROUP1);
391 User user = admin.createUser(USER1, PASS1, group);
392 Group group2 = admin.createGroup(GROUP2);
393 admin.addUserToGroup(user, group2);
395 Set<Group> groups = user.getGroups();
396 assertEquals(2, groups.size());
397 assertTrue(groups.contains(group));
398 assertTrue(groups.contains(group2));
400 admin.removeUserFromGroup(user, group);
401 groups = user.getGroups();
402 assertEquals(1, groups.size());
403 assertTrue(groups.contains(group2));
404 assertFalse(groups.contains(group));
408 * Removes a user from a group where the user is not known.
409 * Verifies that an exception is thrown.
411 * @throws UserMgtException DOCUMENT ME!
413 public void testRemoveUserFromGroupUnknownUser() throws UserMgtException {
414 Group group = admin.createGroup(GROUP1);
415 User user = createUser(USER1, GROUP1, group);
418 admin.removeUserFromGroup(user, group);
419 } catch (UserMgtException e) {
420 assertEquals(UserMgtException.Reason.UNKNOWN_USER, e.getReason());
425 * Removes a user from a group where the group is not known.
426 * Verifies that an exception is thrown.
428 * @throws UserMgtException DOCUMENT ME!
430 public void testRemoveUserFromGroupUnknownGroup() throws UserMgtException {
431 Group group = admin.createGroup(GROUP1);
432 User user = admin.createUser(USER1, PASS1, group);
433 Group group2 = new Group(GROUP2);
436 admin.removeUserFromGroup(user, group2);
437 } catch (UserMgtException e) {
438 assertEquals(UserMgtException.Reason.UNKNOWN_GROUP, e.getReason());
443 * Removes a user from a group where the user is only part of one
444 * group. Verifies that an exception is thrown.
446 * @throws UserMgtException DOCUMENT ME!
448 public void testRemoveUserFromGroupOnlyGroup() throws UserMgtException {
449 Group group = admin.createGroup(GROUP1);
450 User user = admin.createUser(USER1, PASS1, group);
453 admin.removeUserFromGroup(user, group);
454 } catch (UserMgtException e) {
455 assertEquals(UserMgtException.Reason.USER_MUST_BE_IN_A_GROUP,
461 * Gets the list of users and groups. Verifies that the correct
462 * suers and groups are returned. Verifies also that the relations from
463 * user to group are correct.
465 * @throws UserMgtException DOCUMENT ME!
467 public void testGetUsersAndGroups() throws UserMgtException {
468 Group group1 = admin.createGroup(GROUP1);
469 Group group2 = admin.createGroup(GROUP2);
471 User user1 = admin.createUser(USER1, PASS1, group1);
472 admin.addUserToGroup(user1, group2);
474 User user2 = admin.createUser(USER2, PASS2, group2);
476 Set<User> users = admin.getUsers();
477 assertEquals(2, users.size());
478 assertTrue(users.contains(user1));
479 assertTrue(users.contains(user2));
481 Set<Group> groups = admin.getGroups();
482 assertEquals(2, groups.size());
483 assertTrue(groups.contains(group1));
484 assertTrue(groups.contains(group2));
486 assertTrue(user1.isInGroup(group1));
487 assertTrue(user1.isInGroup(group2));
488 assertFalse(user2.isInGroup(group1));
489 assertTrue(user2.isInGroup(group2));
491 Set<Group> groups1 = user1.getGroups();
492 assertEquals(2, groups1.size());
494 Set<Group> groups2 = user2.getGroups();
495 assertEquals(1, groups2.size());
499 * Renames a user. Verifies that the user is renamed. Verifies that
500 * exceptions are thrown when an attempt is made to rename the user to
501 * itself or to another existing user, or when the group does not exist.
503 * @throws UserMgtException DOCUMENT ME!
505 public void testRenameUser() throws UserMgtException {
506 Group group = admin.createGroup(GROUP1);
507 User user1 = admin.createUser(USER1, PASS1, group);
508 admin.renameUser(user1, USER2);
509 assertEquals(USER2, user1.getName());
510 assertEquals(user1, admin.getUser(USER2));
512 admin.createUser(USER1, PASS1, group);
515 admin.renameUser(user1, USER1);
516 } catch (UserMgtException e) {
517 assertEquals(UserMgtException.Reason.DUPLICATE_USER, e.getReason());
519 // do a trivial reanem
521 admin.renameUser(user1, user1.getName());
522 } catch (UserMgtException e2) {
523 assertEquals(UserMgtException.Reason.TRIVIAL_RENAME,
536 * Renames a user to a user with an invalid username. Verifies that
537 * the appropriate exception is thrown.
539 * @throws UserMgtException DOCUMENT ME!
541 public void testRenameUserInvalidUsername() throws UserMgtException {
542 Group group = admin.createGroup(GROUP1);
543 User user1 = admin.createUser(USER1, PASS1, group);
546 admin.renameUser(user1, USER2);
547 } catch (UserMgtException e) {
548 assertEquals(e.getReason(), Reason.INVALID_USERNAME);
553 * Renames a group. Verifies that the group is renamed. Verifies
554 * that exceptions are thrown when an attempt is made to rename the group
555 * to itself or to another existing group or when the group does not
558 * @throws UserMgtException DOCUMENT ME!
560 public void testRenameGroup() throws UserMgtException {
561 Group group = admin.createGroup(GROUP1);
562 admin.renameGroup(group, GROUP2);
563 assertEquals(GROUP2, group.getName());
564 assertEquals(group, admin.getGroup(GROUP2));
566 admin.createGroup(GROUP1);
569 admin.renameGroup(group, GROUP1);
570 } catch (UserMgtException e) {
571 assertEquals(UserMgtException.Reason.DUPLICATE_GROUP, e.getReason());
573 // do a trivial reanem
575 admin.renameGroup(group, group.getName());
576 } catch (UserMgtException e2) {
577 assertEquals(UserMgtException.Reason.TRIVIAL_RENAME,
592 * Renames a group to a group with an invalid name. Verifies that
593 * the appropriate exception is thrown.
595 * @throws UserMgtException DOCUMENT ME!
597 public void testRenameGroupInvalidGroupname() throws UserMgtException {
598 Group group = admin.createGroup(GROUP1);
601 admin.renameGroup(group, "a b");
602 } catch (UserMgtException e) {
603 assertEquals(e.getReason(), Reason.INVALID_GROUPNAME);
608 * Removes a user. Verifies that the user is removed. Verifies that
609 * the an exception is thrown when the user does not exist.
611 * @throws UserMgtException DOCUMENT ME!
613 public void testRemoveUser() throws UserMgtException {
614 Group group = admin.createGroup(GROUP1);
615 User user = admin.createUser(USER1, PASS1, group);
617 assertEquals(1, admin.getUserCount());
618 admin.removeUser(user);
619 assertEquals(0, admin.getUserCount());
621 admin.createUser(USER1, PASS1, group);
622 assertEquals(1, admin.getUserCount());
624 User user2 = createUser(USER2, PASS2, group);
627 admin.removeUser(user2);
628 } catch (UserMgtException e) {
629 assertEquals(UserMgtException.Reason.UNKNOWN_USER, e.getReason());
634 * Removes a group. Verifies that the group is removed. Verifies
635 * that the an exception is thrown when the group does not exist or if
636 * there are still users in the group.
638 * @throws UserMgtException DOCUMENT ME!
640 public void testRemoveGroup() throws UserMgtException {
641 Group group1 = admin.createGroup(GROUP1);
642 assertEquals(1, admin.getGroupCount());
643 admin.removeGroup(group1);
644 assertEquals(0, admin.getGroupCount());
645 group1 = admin.createGroup(GROUP1);
647 admin.createUser(USER1, PASS1, group1);
650 admin.removeGroup(group1);
651 } catch (UserMgtException e) {
652 assertEquals(UserMgtException.Reason.GROUP_STILL_OCCUPIED,
662 * Tries to remove an unknown group. Verifies that an exception is
665 * @throws UserMgtException DOCUMENT ME!
667 public void testRemoveGroupUnknownGroup() throws UserMgtException {
668 Group group = admin.createGroup(GROUP1);
669 Group group2 = new Group(GROUP2);
672 admin.removeGroup(group2);
673 } catch (UserMgtException e) {
674 assertEquals(UserMgtException.Reason.UNKNOWN_GROUP, e.getReason());
679 * Changes the password, verifies that this succeeds.
681 * @throws UserMgtException
683 public void testChangePassword() throws UserMgtException {
684 Group group = admin.createGroup(GROUP1);
685 User user = admin.createUser(USER1, PASS1, group);
686 user.changePassword(PASS1, PASS2);
688 // retrieve the user and verifies the password hasn't changed.
689 User user2 = admin.getUser(USER1);
692 user2.checkPassword(PASS2);
693 fail(); // password should not have changed already.
694 } catch (UserMgtException e) {
698 // now notify the admin of the change in the user
699 admin.userModified(user);
701 user2 = admin.getUser(USER1);
702 user2.checkPassword(PASS2); // this time it should succeed.
706 * Performance test. Finds a user by name.
708 * @throws UserMgtException DOCUMENT ME!
710 public void testPerformanceFindUserByName() throws UserMgtException {
711 Group group = admin.createGroup(GROUP1);
712 admin.createUser(USER1, PASS1, group);
715 long time = System.currentTimeMillis();
717 for (int i = 0; i < n; i++) {
718 admin.getUser(USER1);
721 LOGGER.info("Looked up a user " + n + " times in "
722 + ((float) (System.currentTimeMillis() - time) / 1000.0));