source code formatting.
[utils] / security / src / test / java / org / wamblee / usermgt / InMemoryUserSetTest.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 org.wamblee.security.encryption.Md5HexMessageDigester;
21
22 import org.wamblee.usermgt.UserMgtException.Reason;
23
24 import java.sql.SQLException;
25
26 import java.util.Set;
27
28
29 /**
30  * Tests the inmemory user set. Intended to be subclassed for other
31  * implementations of user set.
32  */
33 public class InMemoryUserSetTest extends TestCase {
34     /**
35      * DOCUMENT ME!
36      */
37     protected static final String PASSWORD = "abc123";
38
39     /**
40      * DOCUMENT ME!
41      */
42     private UserSet users;
43
44     /**
45      * DOCUMENT ME!
46      */
47     private GroupSet groups;
48
49     /**
50      * DOCUMENT ME!
51      */
52     private Group group;
53
54     /**
55      * This method must be overriden in subclasses.
56      *
57      * @return New user set object.
58      */
59     protected UserSet createUserSet() {
60         return new InMemoryUserSet(new RegexpNameValidator(
61                 RegexpNameValidator.PASSWORD_PATTERN, Reason.INVALID_PASSWORD,
62                 "Password must contain at least 6 characters"),
63             new Md5HexMessageDigester());
64     }
65
66     /**
67      * This method must be overriden in subclasses.
68      *
69      * @return New group set object.
70      */
71     protected GroupSet createGroupSet() {
72         return new InMemoryGroupSet();
73     }
74
75     /* (non-Javadoc)
76      * @see org.wamblee.test.SpringTestCase#setUp()
77      */
78     /**
79      * DOCUMENT ME!
80      *
81      * @throws Exception DOCUMENT ME!
82      */
83     @Override
84     protected void setUp() throws Exception {
85         super.setUp();
86         users      = createUserSet();
87         groups     = createGroupSet();
88         group      = new Group("group0");
89         groups.add(group);
90         checkUserCount(0);
91     }
92
93     /**
94      * DOCUMENT ME!
95      *
96      * @return DOCUMENT ME!
97      */
98     protected UserSet getUsers() {
99         return users;
100     }
101
102     /**
103      * DOCUMENT ME!
104      *
105      * @return DOCUMENT ME!
106      */
107     protected GroupSet getGroups() {
108         return groups;
109     }
110
111     /**
112      * DOCUMENT ME!
113      *
114      * @param aName DOCUMENT ME!
115      *
116      * @return DOCUMENT ME!
117      */
118     protected Group createGroup(String aName) {
119         return new Group(aName);
120     }
121
122     /**
123      * DOCUMENT ME!
124      *
125      * @param aName DOCUMENT ME!
126      * @param aPassword DOCUMENT ME!
127      * @param aGroup DOCUMENT ME!
128      *
129      * @return DOCUMENT ME!
130      *
131      * @throws UserMgtException DOCUMENT ME!
132      */
133     protected User createUser(String aName, String aPassword, Group aGroup)
134         throws UserMgtException {
135         return UsermgtTestUtils.createUser(aName, aPassword, aGroup);
136     }
137
138     /**
139      * DOCUMENT ME!
140      *
141      * @param aUser DOCUMENT ME!
142      * @param aGroup DOCUMENT ME!
143      *
144      * @throws UserMgtException DOCUMENT ME!
145      */
146     protected void addUserToGroup(User aUser, Group aGroup)
147         throws UserMgtException {
148         aUser.addGroup(aGroup);
149     }
150
151     /**
152      * DOCUMENT ME!
153      *
154      * @param aUser DOCUMENT ME!
155      * @param aGroup DOCUMENT ME!
156      *
157      * @throws UserMgtException DOCUMENT ME!
158      */
159     protected void removeUserFromGroup(User aUser, Group aGroup)
160         throws UserMgtException {
161         aUser.removeGroup(aGroup);
162     }
163
164     /**
165      * Additional check to be implemented by a subclass.
166      *
167      * @param aUser User to check for existence.
168      *
169      * @throws SQLException DOCUMENT ME!
170      */
171     protected void checkUserExists(String aUser) throws SQLException {
172         // Empty
173     }
174
175     /**
176      * Additional check to be implemented by a subclass.
177      *
178      * @param aUser User to check for non-existence.
179      *
180      * @throws SQLException DOCUMENT ME!
181      */
182     protected void checkUserNotExists(String aUser) throws SQLException {
183         // Empty
184     }
185
186     /**
187      * Additional check to be implemented by a subclass.
188      *
189      * @param aSize Expected number of users.
190      *
191      * @throws SQLException DOCUMENT ME!
192      */
193     protected void checkUserCount(int aSize) throws SQLException {
194         assertEquals(aSize, users.size());
195     }
196
197     /**
198      * Additional check to be implemented by a subclass.
199      *
200      * @param aUser User to check for existence.
201      *
202      * @throws SQLException DOCUMENT ME!
203      */
204     protected void checkGroupExists(String aUser) throws SQLException {
205         // Empty
206     }
207
208     /**
209      * Additional check to be implemented by a subclass.
210      *
211      * @param aUser User to check for non-existence.
212      *
213      * @throws SQLException DOCUMENT ME!
214      */
215     protected void checkGroupNotExists(String aUser) throws SQLException {
216         // Empty
217     }
218
219     /**
220      * Additional check to be implemented by a subclass.
221      *
222      * @param aSize Expected number of users.
223      *
224      * @throws SQLException DOCUMENT ME!
225      */
226     protected void checkGroupCount(int aSize) throws SQLException {
227         // Empty
228     }
229
230     /**
231      * Adds a user and verifies that the user is added using  find(),
232      * list(),  and contains().
233      *
234      * @throws SQLException DOCUMENT ME!
235      * @throws UserMgtException DOCUMENT ME!
236      */
237     public void testAdd() throws SQLException, UserMgtException {
238         User user = createUser("user1", PASSWORD, group);
239         assertTrue(users.add(user));
240         checkUserExists(user.getName());
241         checkUserCount(1);
242
243         User user2 = users.find("user1");
244         assertNotNull(user2);
245         assertEquals(user.getName(), user2.getName());
246
247         Set<User> set = users.list();
248         assertEquals(1, set.size());
249         assertTrue(set.contains(user));
250     }
251
252     /**
253      * Tries to find a non-existing user. Verifies that null is
254      * returned.
255      *
256      * @throws SQLException DOCUMENT ME!
257      * @throws UserMgtException DOCUMENT ME!
258      */
259     public void testFindUnknownUser() throws SQLException, UserMgtException {
260         User user1 = createUser("user1", PASSWORD, group);
261         User user2 = createUser("user2", PASSWORD, group);
262         users.add(user1);
263         users.add(user2);
264         checkUserExists(user1.getName());
265         checkUserExists(user2.getName());
266
267         assertNull(users.find("user3"));
268         checkUserNotExists("user3");
269     }
270
271     /**
272      * Adds duplicate user. Verifies that the existing user is left
273      * untouched.
274      *
275      * @throws SQLException DOCUMENT ME!
276      * @throws UserMgtException DOCUMENT ME!
277      */
278     public void testAddDuplicateUser() throws SQLException, UserMgtException {
279         User user1 = createUser("user1", PASSWORD, group);
280         users.add(user1);
281
282         assertEquals(1, users.list().size());
283         assertTrue(users.contains(user1));
284         user1 = createUser("user1", PASSWORD, group);
285         assertFalse(users.add(user1));
286         assertEquals(1, users.list().size());
287
288         checkUserExists(user1.getName());
289         checkUserCount(1);
290     }
291
292     /**
293      * Removes a user. Verifies that the user is  removed and the
294      * return value is true.
295      *
296      * @throws SQLException DOCUMENT ME!
297      * @throws UserMgtException DOCUMENT ME!
298      */
299     public void testRemoveUser() throws SQLException, UserMgtException {
300         User user1 = createUser("user1", PASSWORD, group);
301         users.add(user1);
302         assertTrue(users.contains(user1));
303         checkUserCount(1);
304
305         assertTrue(users.remove(user1));
306         assertFalse(users.contains(user1));
307         assertNull(users.find(user1.getName()));
308         assertEquals(0, users.list().size());
309         checkUserCount(0);
310     }
311
312     /**
313      * Removes a non-existing user. Verifies that no users are removed
314      * an that the return value is true.
315      *
316      * @throws SQLException DOCUMENT ME!
317      * @throws UserMgtException DOCUMENT ME!
318      */
319     public void testRemoveNonExistingUser()
320         throws SQLException, UserMgtException {
321         User user1 = createUser("user1", PASSWORD, group);
322         users.add(user1);
323         checkUserCount(1);
324
325         User nonExistingUser = createUser("user2", PASSWORD, group);
326         nonExistingUser.setPrimaryKey(new Long(1000));
327         nonExistingUser.setPersistedVersion(10);
328         assertFalse(users.remove(nonExistingUser));
329         assertTrue(users.contains(user1));
330         assertEquals(1, users.list().size());
331         checkUserCount(1);
332     }
333
334     /**
335      * Adds a number of users to the set and verifies that list()
336      * returns them all.
337      *
338      * @throws SQLException DOCUMENT ME!
339      * @throws UserMgtException DOCUMENT ME!
340      */
341     public void testList() throws SQLException, UserMgtException {
342         User user1 = createUser("user1", PASSWORD, group);
343         User user2 = createUser("user2", PASSWORD, group);
344         User user3 = createUser("user3", PASSWORD, group);
345         assertTrue(users.add(user1));
346         assertTrue(users.add(user2));
347         assertTrue(users.add(user3));
348
349         checkUserExists(user1.getName());
350         checkUserExists(user2.getName());
351         checkUserExists(user3.getName());
352
353         Set<User> set = users.list();
354         assertTrue(set.contains(user1));
355         assertTrue(set.contains(user2));
356         assertTrue(set.contains(user3));
357
358         checkUserCount(3);
359     }
360
361     /**
362      * Adds several users to different groups and verifies that  the
363      * correct users are returned when looking for users in  different groups.
364      *
365      * @throws SQLException
366      * @throws UserMgtException DOCUMENT ME!
367      */
368     public void testListByGroup() throws SQLException, UserMgtException {
369         Group group1 = new Group("group1");
370         Group group2 = new Group("group2");
371         Group group3 = new Group("group3");
372         groups.add(group1);
373         groups.add(group2);
374         groups.add(group3);
375
376         //           user1    user2    user3
377         // group1     y         
378         // group2     y       y           
379         // group3     y       y           y
380         User user1 = createUser("user1", PASSWORD, group1);
381         user1.addGroup(group2);
382         user1.addGroup(group3);
383
384         User user2 = createUser("user2", PASSWORD, group2);
385         user2.addGroup(group3);
386
387         User user3 = createUser("user3", PASSWORD, group3);
388         users.add(user1);
389         users.add(user2);
390         users.add(user3);
391
392         checkUserExists(user1.getName());
393         checkUserExists(user2.getName());
394         checkUserExists(user3.getName());
395
396         checkGroupExists(group1.getName());
397         checkGroupExists(group2.getName());
398         checkGroupExists(group3.getName());
399
400         checkUserCount(3);
401         checkGroupCount(3 + 1); // also count the group that was created in the setUp().
402
403         Set<User> list = users.list(group1);
404         assertTrue(list.contains(user1));
405         assertEquals(1, list.size());
406
407         list = users.list(group2);
408         assertTrue(list.contains(user1));
409         assertTrue(list.contains(user2));
410         assertEquals(2, list.size());
411
412         list = users.list(group3);
413         assertTrue(list.contains(user1));
414         assertTrue(list.contains(user2));
415         assertTrue(list.contains(user3));
416         assertEquals(3, list.size());
417     }
418 }