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.
16 package org.wamblee.usermgt;
18 import junit.framework.TestCase;
20 import java.sql.SQLException;
26 * Tests the inmemory group set. Intended to be subclassed for other
27 * implementations of group set.
29 public class InMemoryGroupSetTest extends TestCase {
33 protected GroupSet groups;
36 * This method must be overriden in subclasses.
38 * @return New group set object.
40 protected GroupSet createGroupSet() {
41 return new InMemoryGroupSet();
45 * @see org.wamblee.test.SpringTestCase#setUp()
50 * @throws Exception DOCUMENT ME!
53 protected void setUp() throws Exception {
55 groups = createGroupSet();
60 * Additional check to be implemented by a subclass.
62 * @param aGroup Group to check for existence.
64 * @throws SQLException DOCUMENT ME!
66 protected void checkGroupExists(String aGroup) throws SQLException {
71 * Additional check to be implemented by a subclass.
73 * @param aGroup Group to check for non-existence.
75 * @throws SQLException DOCUMENT ME!
77 protected void checkGroupNotExists(String aGroup) throws SQLException {
82 * Additional check to be implemented by a subclass.
84 * @param aSize Expected number of groups.
86 * @throws SQLException DOCUMENT ME!
88 protected void checkGroupCount(int aSize) throws SQLException {
89 assertEquals(aSize, groups.size());
93 * Adds a group and verifies that the group is added using find(),
94 * list(), and contains().
96 * @throws SQLException DOCUMENT ME!
98 public void testAdd() throws SQLException {
99 Group group = new Group("group1");
100 assertTrue(groups.add(group));
101 checkGroupExists(group.getName());
104 Group group2 = groups.find("group1");
105 assertNotNull(group2);
106 assertEquals(group.getName(), group2.getName());
108 Set<Group> set = groups.list();
109 assertEquals(1, set.size());
110 assertTrue(set.contains(group));
114 * Tries to find a non-existing group. Verifies that null is
117 * @throws SQLException DOCUMENT ME!
119 public void testFindUnknownGroup() throws SQLException {
120 Group group1 = new Group("group1");
121 Group group2 = new Group("group2");
124 checkGroupExists(group1.getName());
125 checkGroupExists(group2.getName());
127 assertNull(groups.find("group3"));
128 checkGroupNotExists("group3");
132 * Adds duplicate group. Verifies that the existing group is left
135 * @throws SQLException DOCUMENT ME!
137 public void testAddDuplicateGroup() throws SQLException {
138 Group group1 = new Group("group1");
141 assertEquals(1, groups.list().size());
142 assertTrue(groups.contains(group1));
143 group1 = new Group("group1");
144 assertFalse(groups.add(group1));
145 assertEquals(1, groups.list().size());
147 checkGroupExists(group1.getName());
152 * Removes a group. Verifies that the group is removed and the
153 * return value is true.
155 * @throws SQLException DOCUMENT ME!
157 public void testRemoveGroup() throws SQLException {
158 Group group1 = new Group("group1");
160 assertTrue(groups.contains(group1));
163 assertTrue(groups.remove(group1));
164 assertFalse(groups.contains(group1));
165 assertNull(groups.find(group1.getName()));
166 assertEquals(0, groups.list().size());
171 * Removes a non-existing group. Verifies that no groups are
172 * removed an that the return value is true.
174 * @throws SQLException DOCUMENT ME!
176 public void testRemoveNonExistingGroup() throws SQLException {
177 Group group1 = new Group("group1");
181 Group nonExistingGroup = new Group("group2");
182 nonExistingGroup.setPrimaryKey(new Long(1000));
183 nonExistingGroup.setPersistedVersion(1000);
184 assertFalse(groups.remove(nonExistingGroup));
185 assertTrue(groups.contains(group1));
186 assertEquals(1, groups.list().size());
191 * Adds a number of groups to the set and verifies that list()
194 * @throws SQLException DOCUMENT ME!
196 public void testList() throws SQLException {
197 Group group1 = new Group("group1");
198 Group group2 = new Group("group2");
199 Group group3 = new Group("group3");
200 assertTrue(groups.add(group1));
201 assertTrue(groups.add(group2));
202 assertTrue(groups.add(group3));
204 checkGroupExists(group1.getName());
205 checkGroupExists(group2.getName());
206 checkGroupExists(group3.getName());
208 Set<Group> set = groups.list();
209 assertTrue(set.contains(group1));
210 assertTrue(set.contains(group2));
211 assertTrue(set.contains(group3));