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