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;
18 import junit.framework.TestCase;
20 import java.sql.SQLException;
25 * Tests the inmemory group set. Intended to be subclassed for other
26 * implementations of group set.
28 public class InMemoryGroupSetTest extends TestCase {
29 private GroupSet groups;
32 * This method must be overriden in subclasses.
34 * @return New group set object.
36 protected GroupSet createGroupSet() {
37 return new InMemoryGroupSet();
43 * @see org.wamblee.test.SpringTestCase#setUp()
46 protected void setUp() throws Exception {
48 groups = createGroupSet();
53 * Additional check to be implemented by a subclass.
56 * Group to check for existence.
59 protected void checkGroupExists(String aGroup) throws SQLException {
64 * Additional check to be implemented by a subclass.
67 * Group to check for non-existence.
70 protected void checkGroupNotExists(String aGroup) throws SQLException {
75 * Additional check to be implemented by a subclass.
78 * Expected number of groups.
81 protected void checkGroupCount(int aSize) throws SQLException {
82 assertEquals(aSize, groups.size());
86 * Adds a group and verifies that the group is added using find(), list(),
90 public void testAdd() throws SQLException {
91 Group group = new Group("group1");
92 assertTrue(groups.add(group));
93 checkGroupExists(group.getName());
96 Group group2 = groups.find("group1");
97 assertNotNull(group2);
98 assertEquals(group.getName(), group2.getName());
100 Set<Group> set = groups.list();
101 assertEquals(1, set.size());
102 assertTrue(set.contains(group));
106 * Tries to find a non-existing group. Verifies that null is returned.
109 public void testFindUnknownGroup() throws SQLException {
110 Group group1 = new Group("group1");
111 Group group2 = new Group("group2");
114 checkGroupExists(group1.getName());
115 checkGroupExists(group2.getName());
117 assertNull(groups.find("group3"));
118 checkGroupNotExists("group3");
122 * Adds duplicate group. Verifies that the existing group is left untouched.
125 public void testAddDuplicateGroup() throws SQLException {
126 Group group1 = new Group("group1");
129 assertEquals(1, groups.list().size());
130 assertTrue(groups.contains(group1));
131 group1 = new Group("group1");
132 assertFalse(groups.add(group1));
133 assertEquals(1, groups.list().size());
135 checkGroupExists(group1.getName());
140 * Removes a group. Verifies that the group is removed and the return value
144 public void testRemoveGroup() throws SQLException {
145 Group group1 = new Group("group1");
147 assertTrue(groups.contains(group1));
150 assertTrue(groups.remove(group1));
151 assertFalse(groups.contains(group1));
152 assertNull(groups.find(group1.getName()));
153 assertEquals(0, groups.list().size());
158 * Removes a non-existing group. Verifies that no groups are removed an that
159 * the return value is true.
162 public void testRemoveNonExistingGroup() throws SQLException {
163 Group group1 = new Group("group1");
167 Group nonExistingGroup = new Group("group2");
168 nonExistingGroup.setPrimaryKey(new Long(1000));
169 nonExistingGroup.setPersistedVersion(1000);
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 SQLException {
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));