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.security.authentication;
20 import org.wamblee.security.authentication.Group;
21 import org.wamblee.security.authentication.GroupSet;
22 import org.wamblee.security.authentication.InMemoryGroupSet;
24 import junit.framework.TestCase;
27 * Tests the inmemory group set. Intended to be subclassed for other
28 * implementations of group set.
30 public class InMemoryGroupSetTest extends TestCase {
31 private GroupSet groups;
34 * This method must be overriden in subclasses.
36 * @return New group set object.
38 protected GroupSet createGroupSet() {
39 return new InMemoryGroupSet();
45 * @see org.wamblee.test.SpringTestCase#setUp()
48 protected void setUp() throws Exception {
50 groups = createGroupSet();
55 * Additional check to be implemented by a subclass.
58 * Group to check for existence.
61 protected void checkGroupExists(String aGroup) throws Exception {
66 * Additional check to be implemented by a subclass.
69 * Group to check for non-existence.
72 protected void checkGroupNotExists(String aGroup) throws Exception {
77 * Additional check to be implemented by a subclass.
80 * Expected number of groups.
83 protected void checkGroupCount(int aSize) throws Exception {
84 assertEquals(aSize, groups.size());
88 * Adds a group and verifies that the group is added using find(), list(),
92 public void testAdd() throws Exception {
93 Group group = new Group("group1");
94 assertTrue(groups.add(group));
95 checkGroupExists(group.getName());
98 Group group2 = groups.find("group1");
99 assertNotNull(group2);
100 assertEquals(group.getName(), group2.getName());
102 Set<Group> set = groups.list();
103 assertEquals(1, set.size());
104 assertTrue(set.contains(group));
108 * Tries to find a non-existing group. Verifies that null is returned.
111 public void testFindUnknownGroup() throws Exception {
112 Group group1 = new Group("group1");
113 Group group2 = new Group("group2");
116 checkGroupExists(group1.getName());
117 checkGroupExists(group2.getName());
119 assertNull(groups.find("group3"));
120 checkGroupNotExists("group3");
124 * Adds duplicate group. Verifies that the existing group is left untouched.
127 public void testAddDuplicateGroup() throws Exception {
128 Group group1 = new Group("group1");
131 assertEquals(1, groups.list().size());
132 assertTrue(groups.contains(group1));
133 group1 = new Group("group1");
134 assertFalse(groups.add(group1));
135 assertEquals(1, groups.list().size());
137 checkGroupExists(group1.getName());
142 * Removes a group. Verifies that the group is removed and the return value
146 public void testRemoveGroup() throws Exception {
147 Group group1 = new Group("group1");
149 assertTrue(groups.contains(group1));
152 assertTrue(groups.remove(group1));
153 assertFalse(groups.contains(group1));
154 assertNull(groups.find(group1.getName()));
155 assertEquals(0, groups.list().size());
160 * Removes a non-existing group. Verifies that no groups are removed an that
161 * the return value is true.
164 public void testRemoveNonExistingGroup() throws Exception {
165 Group group1 = new Group("group1");
169 Group nonExistingGroup = new Group("group2");
170 assertFalse(groups.remove(nonExistingGroup));
171 assertTrue(groups.contains(group1));
172 assertEquals(1, groups.list().size());
177 * Adds a number of groups to the set and verifies that list() returns them
181 public void testList() throws Exception {
182 Group group1 = new Group("group1");
183 Group group2 = new Group("group2");
184 Group group3 = new Group("group3");
185 assertTrue(groups.add(group1));
186 assertTrue(groups.add(group2));
187 assertTrue(groups.add(group3));
189 checkGroupExists(group1.getName());
190 checkGroupExists(group2.getName());
191 checkGroupExists(group3.getName());
193 Set<Group> set = groups.list();
194 assertTrue(set.contains(group1));
195 assertTrue(set.contains(group2));
196 assertTrue(set.contains(group3));
201 public void testRenameGroupTwice() {
202 Group group = new Group("x");
204 groups.groupModified(group);
206 groups.groupModified(group);
207 Group g = groups.find("y");
209 groups.groupModified(group);