/* * Copyright 2005 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.wamblee.usermgt; import junit.framework.TestCase; import org.wamblee.security.encryption.Md5HexMessageDigester; import org.wamblee.usermgt.UserMgtException.Reason; import java.sql.SQLException; import java.util.Set; /** * Tests the inmemory user set. Intended to be subclassed for other * implementations of user set. */ public class InMemoryUserSetTest extends TestCase { /** * DOCUMENT ME! */ protected static final String PASSWORD = "abc123"; /** * DOCUMENT ME! */ private UserSet users; /** * DOCUMENT ME! */ private GroupSet groups; /** * DOCUMENT ME! */ private Group group; /** * This method must be overriden in subclasses. * * @return New user set object. */ protected UserSet createUserSet() { return new InMemoryUserSet(new RegexpNameValidator( RegexpNameValidator.PASSWORD_PATTERN, Reason.INVALID_PASSWORD, "Password must contain at least 6 characters"), new Md5HexMessageDigester()); } /** * This method must be overriden in subclasses. * * @return New group set object. */ protected GroupSet createGroupSet() { return new InMemoryGroupSet(); } /* (non-Javadoc) * @see org.wamblee.test.SpringTestCase#setUp() */ /** * DOCUMENT ME! * * @throws Exception DOCUMENT ME! */ @Override protected void setUp() throws Exception { super.setUp(); users = createUserSet(); groups = createGroupSet(); group = new Group("group0"); groups.add(group); checkUserCount(0); } /** * DOCUMENT ME! * * @return DOCUMENT ME! */ protected UserSet getUsers() { return users; } /** * DOCUMENT ME! * * @return DOCUMENT ME! */ protected GroupSet getGroups() { return groups; } /** * DOCUMENT ME! * * @param aName DOCUMENT ME! * * @return DOCUMENT ME! */ protected Group createGroup(String aName) { return new Group(aName); } /** * DOCUMENT ME! * * @param aName DOCUMENT ME! * @param aPassword DOCUMENT ME! * @param aGroup DOCUMENT ME! * * @return DOCUMENT ME! * * @throws UserMgtException DOCUMENT ME! */ protected User createUser(String aName, String aPassword, Group aGroup) throws UserMgtException { return UsermgtTestUtils.createUser(aName, aPassword, aGroup); } /** * DOCUMENT ME! * * @param aUser DOCUMENT ME! * @param aGroup DOCUMENT ME! * * @throws UserMgtException DOCUMENT ME! */ protected void addUserToGroup(User aUser, Group aGroup) throws UserMgtException { aUser.addGroup(aGroup); } /** * DOCUMENT ME! * * @param aUser DOCUMENT ME! * @param aGroup DOCUMENT ME! * * @throws UserMgtException DOCUMENT ME! */ protected void removeUserFromGroup(User aUser, Group aGroup) throws UserMgtException { aUser.removeGroup(aGroup); } /** * Additional check to be implemented by a subclass. * * @param aUser User to check for existence. * * @throws SQLException DOCUMENT ME! */ protected void checkUserExists(String aUser) throws SQLException { // Empty } /** * Additional check to be implemented by a subclass. * * @param aUser User to check for non-existence. * * @throws SQLException DOCUMENT ME! */ protected void checkUserNotExists(String aUser) throws SQLException { // Empty } /** * Additional check to be implemented by a subclass. * * @param aSize Expected number of users. * * @throws SQLException DOCUMENT ME! */ protected void checkUserCount(int aSize) throws SQLException { assertEquals(aSize, users.size()); } /** * Additional check to be implemented by a subclass. * * @param aUser User to check for existence. * * @throws SQLException DOCUMENT ME! */ protected void checkGroupExists(String aUser) throws SQLException { // Empty } /** * Additional check to be implemented by a subclass. * * @param aUser User to check for non-existence. * * @throws SQLException DOCUMENT ME! */ protected void checkGroupNotExists(String aUser) throws SQLException { // Empty } /** * Additional check to be implemented by a subclass. * * @param aSize Expected number of users. * * @throws SQLException DOCUMENT ME! */ protected void checkGroupCount(int aSize) throws SQLException { // Empty } /** * Adds a user and verifies that the user is added using find(), * list(), and contains(). * * @throws SQLException DOCUMENT ME! * @throws UserMgtException DOCUMENT ME! */ public void testAdd() throws SQLException, UserMgtException { User user = createUser("user1", PASSWORD, group); assertTrue(users.add(user)); checkUserExists(user.getName()); checkUserCount(1); User user2 = users.find("user1"); assertNotNull(user2); assertEquals(user.getName(), user2.getName()); Set set = users.list(); assertEquals(1, set.size()); assertTrue(set.contains(user)); } /** * Tries to find a non-existing user. Verifies that null is * returned. * * @throws SQLException DOCUMENT ME! * @throws UserMgtException DOCUMENT ME! */ public void testFindUnknownUser() throws SQLException, UserMgtException { User user1 = createUser("user1", PASSWORD, group); User user2 = createUser("user2", PASSWORD, group); users.add(user1); users.add(user2); checkUserExists(user1.getName()); checkUserExists(user2.getName()); assertNull(users.find("user3")); checkUserNotExists("user3"); } /** * Adds duplicate user. Verifies that the existing user is left * untouched. * * @throws SQLException DOCUMENT ME! * @throws UserMgtException DOCUMENT ME! */ public void testAddDuplicateUser() throws SQLException, UserMgtException { User user1 = createUser("user1", PASSWORD, group); users.add(user1); assertEquals(1, users.list().size()); assertTrue(users.contains(user1)); user1 = createUser("user1", PASSWORD, group); assertFalse(users.add(user1)); assertEquals(1, users.list().size()); checkUserExists(user1.getName()); checkUserCount(1); } /** * Removes a user. Verifies that the user is removed and the * return value is true. * * @throws SQLException DOCUMENT ME! * @throws UserMgtException DOCUMENT ME! */ public void testRemoveUser() throws SQLException, UserMgtException { User user1 = createUser("user1", PASSWORD, group); users.add(user1); assertTrue(users.contains(user1)); checkUserCount(1); assertTrue(users.remove(user1)); assertFalse(users.contains(user1)); assertNull(users.find(user1.getName())); assertEquals(0, users.list().size()); checkUserCount(0); } /** * Removes a non-existing user. Verifies that no users are removed * an that the return value is true. * * @throws SQLException DOCUMENT ME! * @throws UserMgtException DOCUMENT ME! */ public void testRemoveNonExistingUser() throws SQLException, UserMgtException { User user1 = createUser("user1", PASSWORD, group); users.add(user1); checkUserCount(1); User nonExistingUser = createUser("user2", PASSWORD, group); nonExistingUser.setPrimaryKey(new Long(1000)); nonExistingUser.setPersistedVersion(10); assertFalse(users.remove(nonExistingUser)); assertTrue(users.contains(user1)); assertEquals(1, users.list().size()); checkUserCount(1); } /** * Adds a number of users to the set and verifies that list() * returns them all. * * @throws SQLException DOCUMENT ME! * @throws UserMgtException DOCUMENT ME! */ public void testList() throws SQLException, UserMgtException { User user1 = createUser("user1", PASSWORD, group); User user2 = createUser("user2", PASSWORD, group); User user3 = createUser("user3", PASSWORD, group); assertTrue(users.add(user1)); assertTrue(users.add(user2)); assertTrue(users.add(user3)); checkUserExists(user1.getName()); checkUserExists(user2.getName()); checkUserExists(user3.getName()); Set set = users.list(); assertTrue(set.contains(user1)); assertTrue(set.contains(user2)); assertTrue(set.contains(user3)); checkUserCount(3); } /** * Adds several users to different groups and verifies that the * correct users are returned when looking for users in different groups. * * @throws SQLException * @throws UserMgtException DOCUMENT ME! */ public void testListByGroup() throws SQLException, UserMgtException { Group group1 = new Group("group1"); Group group2 = new Group("group2"); Group group3 = new Group("group3"); groups.add(group1); groups.add(group2); groups.add(group3); // user1 user2 user3 // group1 y // group2 y y // group3 y y y User user1 = createUser("user1", PASSWORD, group1); user1.addGroup(group2); user1.addGroup(group3); User user2 = createUser("user2", PASSWORD, group2); user2.addGroup(group3); User user3 = createUser("user3", PASSWORD, group3); users.add(user1); users.add(user2); users.add(user3); checkUserExists(user1.getName()); checkUserExists(user2.getName()); checkUserExists(user3.getName()); checkGroupExists(group1.getName()); checkGroupExists(group2.getName()); checkGroupExists(group3.getName()); checkUserCount(3); checkGroupCount(3 + 1); // also count the group that was created in the setUp(). Set list = users.list(group1); assertTrue(list.contains(user1)); assertEquals(1, list.size()); list = users.list(group2); assertTrue(list.contains(user1)); assertTrue(list.contains(user2)); assertEquals(2, list.size()); list = users.list(group3); assertTrue(list.contains(user1)); assertTrue(list.contains(user2)); assertTrue(list.contains(user3)); assertEquals(3, list.size()); } }