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