X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=security%2Fimpl%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fsecurity%2Fauthentication%2FInMemoryUserSetTest.java;fp=security%2Fimpl%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fsecurity%2Fauthentication%2FInMemoryUserSetTest.java;h=0000000000000000000000000000000000000000;hb=9449ea0f360f6e9c14057db57f3ee0bfba947ab4;hp=d7d26f22f6fb1cb5ae86bb5c52c2266ce7b5aba5;hpb=e8b988e92306a4aea2f047af1b48588147288831;p=utils diff --git a/security/impl/src/test/java/org/wamblee/security/authentication/InMemoryUserSetTest.java b/security/impl/src/test/java/org/wamblee/security/authentication/InMemoryUserSetTest.java deleted file mode 100644 index d7d26f22..00000000 --- a/security/impl/src/test/java/org/wamblee/security/authentication/InMemoryUserSetTest.java +++ /dev/null @@ -1,350 +0,0 @@ -/* - * Copyright 2005-2010 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.security.authentication; - -import junit.framework.TestCase; - -import org.wamblee.security.authentication.Group; -import org.wamblee.security.authentication.GroupSet; -import org.wamblee.security.authentication.InMemoryGroupSet; -import org.wamblee.security.authentication.InMemoryUserSet; -import org.wamblee.security.authentication.RegexpNameValidator; -import org.wamblee.security.authentication.User; -import org.wamblee.security.authentication.UserMgtException; -import org.wamblee.security.authentication.UserSet; -import org.wamblee.security.authentication.UserMgtException.Reason; -import org.wamblee.security.encryption.Md5HexMessageDigester; - - -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 { - protected static final String PASSWORD = "abc123"; - - private UserSet users; - - private GroupSet groups; - - 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() - */ - @Override - protected void setUp() throws Exception { - super.setUp(); - users = createUserSet(); - groups = createGroupSet(); - group = new Group("group0"); - groups.add(group); - checkUserCount(0); - } - - protected UserSet getUsers() { - return users; - } - - protected GroupSet getGroups() { - return groups; - } - - protected Group createGroup(String aName) { - return new Group(aName); - } - - protected User createUser(String aName, String aPassword, Group aGroup) - throws UserMgtException { - return UsermgtTestUtils.createUser(aName, aPassword, aGroup); - } - - protected void addUserToGroup(User aUser, Group aGroup) - throws UserMgtException { - aUser.addGroup(aGroup); - } - - 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. - * - */ - protected void checkUserExists(String aUser) throws Exception { - // Empty - } - - /** - * Additional check to be implemented by a subclass. - * - * @param aUser - * User to check for non-existence. - * - */ - protected void checkUserNotExists(String aUser) throws Exception { - // Empty - } - - /** - * Additional check to be implemented by a subclass. - * - * @param aSize - * Expected number of users. - * - */ - protected void checkUserCount(int aSize) throws Exception { - assertEquals(aSize, users.size()); - } - - /** - * Additional check to be implemented by a subclass. - * - * @param aUser - * User to check for existence. - * - */ - protected void checkGroupExists(String aUser) throws Exception { - // Empty - } - - /** - * Additional check to be implemented by a subclass. - * - * @param aUser - * User to check for non-existence. - * - */ - protected void checkGroupNotExists(String aUser) throws Exception { - // Empty - } - - /** - * Additional check to be implemented by a subclass. - * - * @param aSize - * Expected number of users. - * - */ - protected void checkGroupCount(int aSize) throws Exception { - // Empty - } - - /** - * Adds a user and verifies that the user is added using find(), list(), and - * contains(). - * - */ - public void testAdd() throws Exception { - 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. - * - */ - public void testFindUnknownUser() throws Exception { - 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. - * - */ - public void testAddDuplicateUser() throws Exception { - 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. - * - */ - public void testRemoveUser() throws Exception { - 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. - * - */ - public void testRemoveNonExistingUser() throws Exception, - UserMgtException { - User user1 = createUser("user1", PASSWORD, group); - users.add(user1); - checkUserCount(1); - - User nonExistingUser = createUser("user2", PASSWORD, group); - 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. - * - */ - public void testList() throws Exception { - 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 - */ - public void testListByGroup() throws Exception { - 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()); - } -}