/* * 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 java.sql.SQLException; import java.util.Set; import junit.framework.TestCase; /** * Tests the inmemory group set. Intended to be subclassed for other * implementations of group set. */ public class InMemoryGroupSetTest extends TestCase { protected GroupSet _groups; /** * 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(); _groups = createGroupSet(); checkGroupCount(0); } /** * Additional check to be implemented by a subclass. * @param aGroup Group to check for existence. */ protected void checkGroupExists(String aGroup) throws SQLException { // Empty } /** * Additional check to be implemented by a subclass. * @param aGroup Group to check for non-existence. */ protected void checkGroupNotExists(String aGroup) throws SQLException { // Empty } /** * Additional check to be implemented by a subclass. * @param aSize Expected number of groups. */ protected void checkGroupCount(int aSize) throws SQLException { assertEquals(aSize, _groups.size()); } /** * Adds a group and verifies that the group is added using * find(), list(), and contains(). * */ public void testAdd() throws SQLException { Group group = new Group("group1"); assertTrue( _groups.add(group) ); checkGroupExists(group.getName()); checkGroupCount(1); Group group2 = _groups.find("group1"); assertNotNull(group2); assertEquals(group.getName(), group2.getName()); Set set = _groups.list(); assertEquals(1, set.size()); assertTrue(set.contains(group)); } /** * Tries to find a non-existing group. Verifies that null is * returned. * */ public void testFindUnknownGroup() throws SQLException { Group group1 = new Group("group1"); Group group2 = new Group("group2"); _groups.add(group1); _groups.add(group2); checkGroupExists(group1.getName()); checkGroupExists(group2.getName()); assertNull( _groups.find("group3") ); checkGroupNotExists("group3"); } /** * Adds duplicate group. Verifies that the existing group is left untouched. */ public void testAddDuplicateGroup() throws SQLException { Group group1 = new Group("group1"); _groups.add(group1); assertEquals(1, _groups.list().size()); assertTrue(_groups.contains(group1)); group1 = new Group("group1"); assertFalse(_groups.add(group1)); assertEquals(1, _groups.list().size()); checkGroupExists(group1.getName()); checkGroupCount(1); } /** * Removes a group. Verifies that the group is * removed and the return value is true. * */ public void testRemoveGroup() throws SQLException { Group group1 = new Group("group1"); _groups.add(group1); assertTrue(_groups.contains(group1)); checkGroupCount(1); assertTrue(_groups.remove(group1)); assertFalse(_groups.contains(group1)); assertNull(_groups.find(group1.getName())); assertEquals(0, _groups.list().size()); checkGroupCount(0); } /** * Removes a non-existing group. Verifies that no groups are * removed an that the return value is true. * */ public void testRemoveNonExistingGroup() throws SQLException { Group group1 = new Group("group1"); _groups.add(group1); checkGroupCount(1); Group nonExistingGroup = new Group("group2"); nonExistingGroup.setPrimaryKey(new Long(1000)); nonExistingGroup.setPersistedVersion(1000); assertFalse(_groups.remove(nonExistingGroup)); assertTrue(_groups.contains(group1)); assertEquals(1, _groups.list().size()); checkGroupCount(1); } /** * Adds a number of groups to the set and verifies that list() * returns them all. * */ public void testList() throws SQLException { Group group1 = new Group("group1"); Group group2 = new Group("group2"); Group group3 = new Group("group3"); assertTrue(_groups.add(group1)); assertTrue(_groups.add(group2)); assertTrue(_groups.add(group3)); checkGroupExists(group1.getName()); checkGroupExists(group2.getName()); checkGroupExists(group3.getName()); Set set = _groups.list(); assertTrue(set.contains(group1)); assertTrue(set.contains(group2)); assertTrue(set.contains(group3)); checkGroupCount(3); } }