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