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;
24 import org.wamblee.persistence.hibernate.HibernateMappingFiles;
25 import org.wamblee.test.spring.SpringTestCase;
26 import org.wamblee.test.spring.SpringConfigFiles;
29 * Tests the inmemory group set. Intended to be subclassed for other
30 * implementations of group set.
32 public class InMemoryGroupSetTest extends TestCase {
34 protected GroupSet _groups;
37 * 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()
48 protected void setUp() throws Exception {
50 _groups = createGroupSet();
57 * Additional check to be implemented by a subclass.
58 * @param aGroup Group to check for existence.
60 protected void checkGroupExists(String aGroup) throws SQLException {
65 * Additional check to be implemented by a subclass.
66 * @param aGroup Group to check for non-existence.
68 protected void checkGroupNotExists(String aGroup) throws SQLException {
73 * Additional check to be implemented by a subclass.
74 * @param aSize Expected number of groups.
76 protected void checkGroupCount(int aSize) throws SQLException {
77 assertEquals(aSize, _groups.size());
81 * Adds a group and verifies that the group is added using
82 * find(), list(), and contains().
85 public void testAdd() throws SQLException {
86 Group group = new Group("group1");
87 assertTrue( _groups.add(group) );
88 checkGroupExists(group.getName());
90 Group group2 = _groups.find("group1");
91 assertNotNull(group2);
92 assertEquals(group.getName(), group2.getName());
93 Set<Group> set = _groups.list();
94 assertEquals(1, set.size());
95 assertTrue(set.contains(group));
99 * Tries to find a non-existing group. Verifies that null is
103 public void testFindUnknownGroup() throws SQLException {
104 Group group1 = new Group("group1");
105 Group group2 = new Group("group2");
108 checkGroupExists(group1.getName());
109 checkGroupExists(group2.getName());
111 assertNull( _groups.find("group3") );
112 checkGroupNotExists("group3");
116 * Adds duplicate group. Verifies that the existing group is left untouched.
118 public void testAddDuplicateGroup() throws SQLException {
119 Group group1 = new Group("group1");
122 assertEquals(1, _groups.list().size());
123 assertTrue(_groups.contains(group1));
124 group1 = new Group("group1");
125 assertFalse(_groups.add(group1));
126 assertEquals(1, _groups.list().size());
128 checkGroupExists(group1.getName());
133 * Removes a group. Verifies that the group is
134 * removed and the return value is true.
137 public void testRemoveGroup() throws SQLException {
138 Group group1 = new Group("group1");
140 assertTrue(_groups.contains(group1));
143 assertTrue(_groups.remove(group1));
144 assertFalse(_groups.contains(group1));
145 assertNull(_groups.find(group1.getName()));
146 assertEquals(0, _groups.list().size());
151 * Removes a non-existing group. Verifies that no groups are
152 * removed an that the return value is true.
155 public void testRemoveNonExistingGroup() throws SQLException {
156 Group group1 = new Group("group1");
159 Group nonExistingGroup = new Group("group2");
160 nonExistingGroup.setPrimaryKey(new Long(1000));
161 nonExistingGroup.setPersistedVersion(1000);
162 assertFalse(_groups.remove(nonExistingGroup));
163 assertTrue(_groups.contains(group1));
164 assertEquals(1, _groups.list().size());
169 * Adds a number of groups to the set and verifies that list()
173 public void testList() throws SQLException {
174 Group group1 = new Group("group1");
175 Group group2 = new Group("group2");
176 Group group3 = new Group("group3");
177 assertTrue(_groups.add(group1));
178 assertTrue(_groups.add(group2));
179 assertTrue(_groups.add(group3));
181 checkGroupExists(group1.getName());
182 checkGroupExists(group2.getName());
183 checkGroupExists(group3.getName());
185 Set<Group> set = _groups.list();
186 assertTrue(set.contains(group1));
187 assertTrue(set.contains(group2));
188 assertTrue(set.contains(group3));