2 * Copyright 2005 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.
17 package org.wamblee.usermgt;
19 import java.sql.SQLException;
22 import junit.framework.TestCase;
25 * Tests the inmemory group set. Intended to be subclassed for other
26 * implementations of group set.
28 public class InMemoryGroupSetTest extends TestCase {
30 protected GroupSet _groups;
33 * This method must be overriden in subclasses.
34 * @return New group set object.
36 protected GroupSet createGroupSet() {
37 return new InMemoryGroupSet();
41 * @see org.wamblee.test.SpringTestCase#setUp()
44 protected void setUp() throws Exception {
46 _groups = createGroupSet();
53 * Additional check to be implemented by a subclass.
54 * @param aGroup Group to check for existence.
56 protected void checkGroupExists(String aGroup) throws SQLException {
61 * Additional check to be implemented by a subclass.
62 * @param aGroup Group to check for non-existence.
64 protected void checkGroupNotExists(String aGroup) throws SQLException {
69 * Additional check to be implemented by a subclass.
70 * @param aSize Expected number of groups.
72 protected void checkGroupCount(int aSize) throws SQLException {
73 assertEquals(aSize, _groups.size());
77 * Adds a group and verifies that the group is added using
78 * find(), list(), and contains().
81 public void testAdd() throws SQLException {
82 Group group = new Group("group1");
83 assertTrue( _groups.add(group) );
84 checkGroupExists(group.getName());
86 Group group2 = _groups.find("group1");
87 assertNotNull(group2);
88 assertEquals(group.getName(), group2.getName());
89 Set<Group> set = _groups.list();
90 assertEquals(1, set.size());
91 assertTrue(set.contains(group));
95 * Tries to find a non-existing group. Verifies that null is
99 public void testFindUnknownGroup() throws SQLException {
100 Group group1 = new Group("group1");
101 Group group2 = new Group("group2");
104 checkGroupExists(group1.getName());
105 checkGroupExists(group2.getName());
107 assertNull( _groups.find("group3") );
108 checkGroupNotExists("group3");
112 * Adds duplicate group. Verifies that the existing group is left untouched.
114 public void testAddDuplicateGroup() throws SQLException {
115 Group group1 = new Group("group1");
118 assertEquals(1, _groups.list().size());
119 assertTrue(_groups.contains(group1));
120 group1 = new Group("group1");
121 assertFalse(_groups.add(group1));
122 assertEquals(1, _groups.list().size());
124 checkGroupExists(group1.getName());
129 * Removes a group. Verifies that the group is
130 * removed and the return value is true.
133 public void testRemoveGroup() throws SQLException {
134 Group group1 = new Group("group1");
136 assertTrue(_groups.contains(group1));
139 assertTrue(_groups.remove(group1));
140 assertFalse(_groups.contains(group1));
141 assertNull(_groups.find(group1.getName()));
142 assertEquals(0, _groups.list().size());
147 * Removes a non-existing group. Verifies that no groups are
148 * removed an that the return value is true.
151 public void testRemoveNonExistingGroup() throws SQLException {
152 Group group1 = new Group("group1");
155 Group nonExistingGroup = new Group("group2");
156 nonExistingGroup.setPrimaryKey(new Long(1000));
157 nonExistingGroup.setPersistedVersion(1000);
158 assertFalse(_groups.remove(nonExistingGroup));
159 assertTrue(_groups.contains(group1));
160 assertEquals(1, _groups.list().size());
165 * Adds a number of groups to the set and verifies that list()
169 public void testList() throws SQLException {
170 Group group1 = new Group("group1");
171 Group group2 = new Group("group2");
172 Group group3 = new Group("group3");
173 assertTrue(_groups.add(group1));
174 assertTrue(_groups.add(group2));
175 assertTrue(_groups.add(group3));
177 checkGroupExists(group1.getName());
178 checkGroupExists(group2.getName());
179 checkGroupExists(group3.getName());
181 Set<Group> set = _groups.list();
182 assertTrue(set.contains(group1));
183 assertTrue(set.contains(group2));
184 assertTrue(set.contains(group3));