2 * Copyright 2005 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.wamblee.usermgt;
19 import java.sql.SQLException;
22 import junit.framework.TestCase;
24 import org.wamblee.persistence.hibernate.HibernateMappingFiles;
25 import org.wamblee.security.encryption.Md5HexMessageDigester;
26 import org.wamblee.test.spring.SpringTestCase;
27 import org.wamblee.test.spring.SpringConfigFiles;
28 import org.wamblee.usermgt.UserMgtException.Reason;
31 * Tests the inmemory user set. Intended to be subclassed for other
32 * implementations of user set.
34 public class InMemoryUserSetTest extends TestCase {
36 protected static final String PASSWORD = "abc123";
38 private UserSet _users;
39 private GroupSet _groups;
43 public InMemoryUserSetTest() {
47 protected InMemoryUserSetTest(Class<? extends SpringConfigFiles> aSprings,
48 Class<? extends HibernateMappingFiles> aMappings) {
53 * This method must be overriden in subclasses.
54 * @return New user set object.
56 protected UserSet createUserSet() {
57 return new InMemoryUserSet( new RegexpNameValidator(RegexpNameValidator.PASSWORD_PATTERN, Reason.INVALID_PASSWORD, "Password must contain at least 6 characters"),
58 new Md5HexMessageDigester());
62 * This method must be overriden in subclasses.
63 * @return New group set object.
65 protected GroupSet createGroupSet() {
66 return new InMemoryGroupSet();
70 * @see org.wamblee.test.SpringTestCase#setUp()
73 protected void setUp() throws Exception {
75 _users = createUserSet();
76 _groups = createGroupSet();
77 _group = new Group("group0");
83 protected UserSet getUsers() {
87 protected GroupSet getGroups() {
91 protected Group createGroup(String aName) {
92 return new Group(aName);
95 protected User createUser(String aName, String aPassword, Group aGroup) throws UserMgtException {
96 return UsermgtTestUtils.createUser(aName, aPassword, aGroup);
99 protected void addUserToGroup(User aUser, Group aGroup) throws UserMgtException {
100 aUser.addGroup(aGroup);
103 protected void removeUserFromGroup(User aUser, Group aGroup ) throws UserMgtException {
104 aUser.removeGroup(aGroup);
108 * Additional check to be implemented by a subclass.
109 * @param aUser User to check for existence.
111 protected void checkUserExists(String aUser) throws SQLException {
116 * Additional check to be implemented by a subclass.
117 * @param aUser User to check for non-existence.
119 protected void checkUserNotExists(String aUser) throws SQLException {
124 * Additional check to be implemented by a subclass.
125 * @param aSize Expected number of users.
127 protected void checkUserCount(int aSize) throws SQLException {
128 assertEquals(aSize, _users.size());
132 * Additional check to be implemented by a subclass.
133 * @param aUser User to check for existence.
135 protected void checkGroupExists(String aUser) throws SQLException {
140 * Additional check to be implemented by a subclass.
141 * @param aUser User to check for non-existence.
143 protected void checkGroupNotExists(String aUser) throws SQLException {
148 * Additional check to be implemented by a subclass.
149 * @param aSize Expected number of users.
151 protected void checkGroupCount(int aSize) throws SQLException {
157 * Adds a user and verifies that the user is added using
158 * find(), list(), and contains().
161 public void testAdd() throws SQLException, UserMgtException {
162 User user = createUser("user1", PASSWORD, _group);
163 assertTrue( _users.add(user) );
164 checkUserExists(user.getName());
166 User user2 = _users.find("user1");
167 assertNotNull(user2);
168 assertEquals(user.getName(), user2.getName());
169 Set<User> set = _users.list();
170 assertEquals(1, set.size());
171 assertTrue(set.contains(user));
175 * Tries to find a non-existing user. Verifies that null is
179 public void testFindUnknownUser() throws SQLException, UserMgtException {
180 User user1 = createUser("user1", PASSWORD, _group);
181 User user2 = createUser("user2", PASSWORD, _group);
184 checkUserExists(user1.getName());
185 checkUserExists(user2.getName());
187 assertNull( _users.find("user3") );
188 checkUserNotExists("user3");
192 * Adds duplicate user. Verifies that the existing user is left untouched.
194 public void testAddDuplicateUser() throws SQLException, UserMgtException {
195 User user1 = createUser("user1", PASSWORD, _group);
198 assertEquals(1, _users.list().size());
199 assertTrue(_users.contains(user1));
200 user1 = createUser("user1", PASSWORD, _group);
201 assertFalse(_users.add(user1));
202 assertEquals(1, _users.list().size());
204 checkUserExists(user1.getName());
209 * Removes a user. Verifies that the user is
210 * removed and the return value is true.
213 public void testRemoveUser() throws SQLException, UserMgtException {
214 User user1 = createUser("user1", PASSWORD, _group);
216 assertTrue(_users.contains(user1));
219 assertTrue(_users.remove(user1));
220 assertFalse(_users.contains(user1));
221 assertNull(_users.find(user1.getName()));
222 assertEquals(0, _users.list().size());
227 * Removes a non-existing user. Verifies that no users are
228 * removed an that the return value is true.
231 public void testRemoveNonExistingUser() throws SQLException, UserMgtException {
232 User user1 = createUser("user1", PASSWORD, _group);
235 User nonExistingUser = createUser("user2", PASSWORD, _group);
236 nonExistingUser.setPrimaryKey(new Long(1000));
237 nonExistingUser.setPersistedVersion(10);
238 assertFalse(_users.remove(nonExistingUser));
239 assertTrue(_users.contains(user1));
240 assertEquals(1, _users.list().size());
245 * Adds a number of users to the set and verifies that list()
249 public void testList() throws SQLException, UserMgtException {
250 User user1 = createUser("user1", PASSWORD, _group);
251 User user2 = createUser("user2", PASSWORD, _group);
252 User user3 = createUser("user3", PASSWORD, _group);
253 assertTrue(_users.add(user1));
254 assertTrue(_users.add(user2));
255 assertTrue(_users.add(user3));
257 checkUserExists(user1.getName());
258 checkUserExists(user2.getName());
259 checkUserExists(user3.getName());
261 Set<User> set = _users.list();
262 assertTrue(set.contains(user1));
263 assertTrue(set.contains(user2));
264 assertTrue(set.contains(user3));
270 * Adds several users to different groups and verifies that
271 * the correct users are returned when looking for users in
273 * @throws SQLException
275 public void testListByGroup() throws SQLException, UserMgtException {
276 Group group1 = new Group("group1");
277 Group group2 = new Group("group2");
278 Group group3 = new Group("group3");
288 User user1 = createUser("user1", PASSWORD, group1);
289 user1.addGroup(group2);
290 user1.addGroup(group3);
291 User user2 = createUser("user2", PASSWORD, group2);
292 user2.addGroup(group3);
293 User user3 = createUser("user3", PASSWORD, group3);
298 checkUserExists(user1.getName());
299 checkUserExists(user2.getName());
300 checkUserExists(user3.getName());
302 checkGroupExists(group1.getName());
303 checkGroupExists(group2.getName());
304 checkGroupExists(group3.getName());
307 checkGroupCount(3+1); // also count the group that was created in the setUp().
309 Set<User> list = _users.list(group1);
310 assertTrue(list.contains(user1));
311 assertEquals(1, list.size());
313 list = _users.list(group2);
314 assertTrue(list.contains(user1));
315 assertTrue(list.contains(user2));
316 assertEquals(2, list.size());
318 list = _users.list(group3);
319 assertTrue(list.contains(user1));
320 assertTrue(list.contains(user2));
321 assertTrue(list.contains(user3));
322 assertEquals(3, list.size());