Removed DOCUMENT ME comments that were generated and applied source code
[utils] / security / src / test / java / org / wamblee / usermgt / InMemoryGroupSetTest.java
1 /*
2  * Copyright 2005 the original author or authors.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.wamblee.usermgt;
17
18 import junit.framework.TestCase;
19
20 import java.sql.SQLException;
21
22 import java.util.Set;
23
24 /**
25  * Tests the inmemory group set. Intended to be subclassed for other
26  * implementations of group set.
27  */
28 public class InMemoryGroupSetTest extends TestCase {
29     protected GroupSet groups;
30
31     /**
32      * This method must be overriden in subclasses.
33      * 
34      * @return New group set object.
35      */
36     protected GroupSet createGroupSet() {
37         return new InMemoryGroupSet();
38     }
39
40     /*
41      * (non-Javadoc)
42      * 
43      * @see org.wamblee.test.SpringTestCase#setUp()
44      */
45     @Override
46     protected void setUp() throws Exception {
47         super.setUp();
48         groups = createGroupSet();
49         checkGroupCount(0);
50     }
51
52     /**
53      * Additional check to be implemented by a subclass.
54      * 
55      * @param aGroup
56      *            Group to check for existence.
57      * 
58      */
59     protected void checkGroupExists(String aGroup) throws SQLException {
60         // Empty
61     }
62
63     /**
64      * Additional check to be implemented by a subclass.
65      * 
66      * @param aGroup
67      *            Group to check for non-existence.
68      * 
69      */
70     protected void checkGroupNotExists(String aGroup) throws SQLException {
71         // Empty
72     }
73
74     /**
75      * Additional check to be implemented by a subclass.
76      * 
77      * @param aSize
78      *            Expected number of groups.
79      * 
80      */
81     protected void checkGroupCount(int aSize) throws SQLException {
82         assertEquals(aSize, groups.size());
83     }
84
85     /**
86      * Adds a group and verifies that the group is added using find(), list(),
87      * and contains().
88      * 
89      */
90     public void testAdd() throws SQLException {
91         Group group = new Group("group1");
92         assertTrue(groups.add(group));
93         checkGroupExists(group.getName());
94         checkGroupCount(1);
95
96         Group group2 = groups.find("group1");
97         assertNotNull(group2);
98         assertEquals(group.getName(), group2.getName());
99
100         Set<Group> set = groups.list();
101         assertEquals(1, set.size());
102         assertTrue(set.contains(group));
103     }
104
105     /**
106      * Tries to find a non-existing group. Verifies that null is returned.
107      * 
108      */
109     public void testFindUnknownGroup() throws SQLException {
110         Group group1 = new Group("group1");
111         Group group2 = new Group("group2");
112         groups.add(group1);
113         groups.add(group2);
114         checkGroupExists(group1.getName());
115         checkGroupExists(group2.getName());
116
117         assertNull(groups.find("group3"));
118         checkGroupNotExists("group3");
119     }
120
121     /**
122      * Adds duplicate group. Verifies that the existing group is left untouched.
123      * 
124      */
125     public void testAddDuplicateGroup() throws SQLException {
126         Group group1 = new Group("group1");
127         groups.add(group1);
128
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());
134
135         checkGroupExists(group1.getName());
136         checkGroupCount(1);
137     }
138
139     /**
140      * Removes a group. Verifies that the group is removed and the return value
141      * is true.
142      * 
143      */
144     public void testRemoveGroup() throws SQLException {
145         Group group1 = new Group("group1");
146         groups.add(group1);
147         assertTrue(groups.contains(group1));
148         checkGroupCount(1);
149
150         assertTrue(groups.remove(group1));
151         assertFalse(groups.contains(group1));
152         assertNull(groups.find(group1.getName()));
153         assertEquals(0, groups.list().size());
154         checkGroupCount(0);
155     }
156
157     /**
158      * Removes a non-existing group. Verifies that no groups are removed an that
159      * the return value is true.
160      * 
161      */
162     public void testRemoveNonExistingGroup() throws SQLException {
163         Group group1 = new Group("group1");
164         groups.add(group1);
165         checkGroupCount(1);
166
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());
173         checkGroupCount(1);
174     }
175
176     /**
177      * Adds a number of groups to the set and verifies that list() returns them
178      * all.
179      * 
180      */
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));
188
189         checkGroupExists(group1.getName());
190         checkGroupExists(group2.getName());
191         checkGroupExists(group3.getName());
192
193         Set<Group> set = groups.list();
194         assertTrue(set.contains(group1));
195         assertTrue(set.contains(group2));
196         assertTrue(set.contains(group3));
197
198         checkGroupCount(3);
199     }
200 }