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 org.wamblee.persistence.hibernate.HibernateMappingFiles;
23 import org.wamblee.test.spring.SpringTestCase;
24 import org.wamblee.test.spring.SpringConfigFiles;
27 * Tests the inmemory group set. Intended to be subclassed for other
28 * implementations of group set.
30 public class InMemoryGroupSetTest extends SpringTestCase {
32 protected GroupSet _groups;
34 public InMemoryGroupSetTest() {
35 super(SpringConfigFiles.class, HibernateMappingFiles.class);
38 protected InMemoryGroupSetTest(Class<? extends SpringConfigFiles> aSprings,
39 Class<? extends HibernateMappingFiles> aMappings) {
40 super(aSprings, aMappings);
44 * This method must be overriden in subclasses.
45 * @return New group set object.
47 protected GroupSet createGroupSet() {
48 return new InMemoryGroupSet();
52 * @see org.wamblee.test.SpringTestCase#setUp()
55 protected void setUp() throws Exception {
57 _groups = createGroupSet();
64 * Additional check to be implemented by a subclass.
65 * @param aGroup Group to check for existence.
67 protected void checkGroupExists(String aGroup) throws SQLException {
72 * Additional check to be implemented by a subclass.
73 * @param aGroup Group to check for non-existence.
75 protected void checkGroupNotExists(String aGroup) throws SQLException {
80 * Additional check to be implemented by a subclass.
81 * @param aSize Expected number of groups.
83 protected void checkGroupCount(int aSize) throws SQLException {
84 assertEquals(aSize, _groups.size());
88 * Adds a group and verifies that the group is added using
89 * find(), list(), and contains().
92 public void testAdd() throws SQLException {
93 Group group = new Group("group1");
94 assertTrue( _groups.add(group) );
95 checkGroupExists(group.getName());
97 Group group2 = _groups.find("group1");
98 assertNotNull(group2);
99 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
110 public void testFindUnknownGroup() throws SQLException {
111 Group group1 = new Group("group1");
112 Group group2 = new Group("group2");
115 checkGroupExists(group1.getName());
116 checkGroupExists(group2.getName());
118 assertNull( _groups.find("group3") );
119 checkGroupNotExists("group3");
123 * 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
141 * removed and the return value is true.
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
159 * removed an that the return value is true.
162 public void testRemoveNonExistingGroup() throws SQLException {
163 Group group1 = new Group("group1");
166 Group nonExistingGroup = new Group("group2");
167 nonExistingGroup.setPrimaryKey(new Long(1000));
168 nonExistingGroup.setPersistedVersion(1000);
169 assertFalse(_groups.remove(nonExistingGroup));
170 assertTrue(_groups.contains(group1));
171 assertEquals(1, _groups.list().size());
176 * Adds a number of groups to the set and verifies that list()
180 public void testList() throws SQLException {
181 Group group1 = new Group("group1");
182 Group group2 = new Group("group2");
183 Group group3 = new Group("group3");
184 assertTrue(_groups.add(group1));
185 assertTrue(_groups.add(group2));
186 assertTrue(_groups.add(group3));
188 checkGroupExists(group1.getName());
189 checkGroupExists(group2.getName());
190 checkGroupExists(group3.getName());
192 Set<Group> set = _groups.list();
193 assertTrue(set.contains(group1));
194 assertTrue(set.contains(group2));
195 assertTrue(set.contains(group3));