2 * Copyright 2005-2010 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;
20 import junit.framework.TestCase;
23 * Tests the inmemory group set. Intended to be subclassed for other
24 * implementations of group set.
26 public class InMemoryGroupSetTest extends TestCase {
27 private GroupSet groups;
30 * This method must be overriden in subclasses.
32 * @return New group set object.
34 protected GroupSet createGroupSet() {
35 return new InMemoryGroupSet();
41 * @see org.wamblee.test.SpringTestCase#setUp()
44 protected void setUp() throws Exception {
46 groups = createGroupSet();
51 * Additional check to be implemented by a subclass.
54 * Group to check for existence.
57 protected void checkGroupExists(String aGroup) throws Exception {
62 * Additional check to be implemented by a subclass.
65 * Group to check for non-existence.
68 protected void checkGroupNotExists(String aGroup) throws Exception {
73 * Additional check to be implemented by a subclass.
76 * Expected number of groups.
79 protected void checkGroupCount(int aSize) throws Exception {
80 assertEquals(aSize, groups.size());
84 * Adds a group and verifies that the group is added using find(), list(),
88 public void testAdd() throws Exception {
89 Group group = new Group("group1");
90 assertTrue(groups.add(group));
91 checkGroupExists(group.getName());
94 Group group2 = groups.find("group1");
95 assertNotNull(group2);
96 assertEquals(group.getName(), group2.getName());
98 Set<Group> set = groups.list();
99 assertEquals(1, set.size());
100 assertTrue(set.contains(group));
104 * Tries to find a non-existing group. Verifies that null is returned.
107 public void testFindUnknownGroup() throws Exception {
108 Group group1 = new Group("group1");
109 Group group2 = new Group("group2");
112 checkGroupExists(group1.getName());
113 checkGroupExists(group2.getName());
115 assertNull(groups.find("group3"));
116 checkGroupNotExists("group3");
120 * Adds duplicate group. Verifies that the existing group is left untouched.
123 public void testAddDuplicateGroup() throws Exception {
124 Group group1 = new Group("group1");
127 assertEquals(1, groups.list().size());
128 assertTrue(groups.contains(group1));
129 group1 = new Group("group1");
130 assertFalse(groups.add(group1));
131 assertEquals(1, groups.list().size());
133 checkGroupExists(group1.getName());
138 * Removes a group. Verifies that the group is removed and the return value
142 public void testRemoveGroup() throws Exception {
143 Group group1 = new Group("group1");
145 assertTrue(groups.contains(group1));
148 assertTrue(groups.remove(group1));
149 assertFalse(groups.contains(group1));
150 assertNull(groups.find(group1.getName()));
151 assertEquals(0, groups.list().size());
156 * Removes a non-existing group. Verifies that no groups are removed an that
157 * the return value is true.
160 public void testRemoveNonExistingGroup() throws Exception {
161 Group group1 = new Group("group1");
165 Group nonExistingGroup = new Group("group2");
166 assertFalse(groups.remove(nonExistingGroup));
167 assertTrue(groups.contains(group1));
168 assertEquals(1, groups.list().size());
173 * Adds a number of groups to the set and verifies that list() returns them
177 public void testList() throws Exception {
178 Group group1 = new Group("group1");
179 Group group2 = new Group("group2");
180 Group group3 = new Group("group3");
181 assertTrue(groups.add(group1));
182 assertTrue(groups.add(group2));
183 assertTrue(groups.add(group3));
185 checkGroupExists(group1.getName());
186 checkGroupExists(group2.getName());
187 checkGroupExists(group3.getName());
189 Set<Group> set = groups.list();
190 assertTrue(set.contains(group1));
191 assertTrue(set.contains(group2));
192 assertTrue(set.contains(group3));
197 public void testRenameGroupTwice() {
198 Group group = new Group("x");
200 groups.groupModified(group);
202 groups.groupModified(group);
203 Group g = groups.find("y");
205 groups.groupModified(group);