--- /dev/null
+/*
+ * 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 java.util.Set;
+
+import org.wamblee.security.authentication.Group;
+import org.wamblee.security.authentication.GroupSet;
+import org.wamblee.security.authentication.InMemoryGroupSet;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests the inmemory group set. Intended to be subclassed for other
+ * implementations of group set.
+ */
+public class InMemoryGroupSetTest extends TestCase {
+ private 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 Exception {
+ // Empty
+ }
+
+ /**
+ * Additional check to be implemented by a subclass.
+ *
+ * @param aGroup
+ * Group to check for non-existence.
+ *
+ */
+ protected void checkGroupNotExists(String aGroup) throws Exception {
+ // Empty
+ }
+
+ /**
+ * Additional check to be implemented by a subclass.
+ *
+ * @param aSize
+ * Expected number of groups.
+ *
+ */
+ protected void checkGroupCount(int aSize) throws Exception {
+ assertEquals(aSize, groups.size());
+ }
+
+ /**
+ * Adds a group and verifies that the group is added using find(), list(),
+ * and contains().
+ *
+ */
+ public void testAdd() throws Exception {
+ 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<Group> 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 Exception {
+ 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 Exception {
+ 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 Exception {
+ 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 Exception {
+ Group group1 = new Group("group1");
+ groups.add(group1);
+ checkGroupCount(1);
+
+ Group nonExistingGroup = new Group("group2");
+ 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 Exception {
+ 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<Group> set = groups.list();
+ assertTrue(set.contains(group1));
+ assertTrue(set.contains(group2));
+ assertTrue(set.contains(group3));
+
+ checkGroupCount(3);
+ }
+
+ public void testRenameGroupTwice() {
+ Group group = new Group("x");
+ groups.add(group);
+ groups.groupModified(group);
+ group.setName("y");
+ groups.groupModified(group);
+ Group g = groups.find("y");
+ assertNotNull(g);
+ groups.groupModified(group);
+ }
+}