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.security.encryption.Md5HexMessageDigester;
25 import org.wamblee.usermgt.UserMgtException.Reason;
28 * Tests the inmemory user set. Intended to be subclassed for other
29 * implementations of user set.
31 public class InMemoryUserSetTest extends TestCase {
33 protected static final String PASSWORD = "abc123";
35 private UserSet _users;
36 private GroupSet _groups;
41 * This method must be overriden in subclasses.
42 * @return New user set object.
44 protected UserSet createUserSet() {
45 return new InMemoryUserSet( new RegexpNameValidator(RegexpNameValidator.PASSWORD_PATTERN, Reason.INVALID_PASSWORD, "Password must contain at least 6 characters"),
46 new Md5HexMessageDigester());
50 * This method must be overriden in subclasses.
51 * @return New group set object.
53 protected GroupSet createGroupSet() {
54 return new InMemoryGroupSet();
58 * @see org.wamblee.test.SpringTestCase#setUp()
61 protected void setUp() throws Exception {
63 _users = createUserSet();
64 _groups = createGroupSet();
65 _group = new Group("group0");
71 protected UserSet getUsers() {
75 protected GroupSet getGroups() {
79 protected Group createGroup(String aName) {
80 return new Group(aName);
83 protected User createUser(String aName, String aPassword, Group aGroup) throws UserMgtException {
84 return UsermgtTestUtils.createUser(aName, aPassword, aGroup);
87 protected void addUserToGroup(User aUser, Group aGroup) throws UserMgtException {
88 aUser.addGroup(aGroup);
91 protected void removeUserFromGroup(User aUser, Group aGroup ) throws UserMgtException {
92 aUser.removeGroup(aGroup);
96 * Additional check to be implemented by a subclass.
97 * @param aUser User to check for existence.
99 protected void checkUserExists(String aUser) throws SQLException {
104 * Additional check to be implemented by a subclass.
105 * @param aUser User to check for non-existence.
107 protected void checkUserNotExists(String aUser) throws SQLException {
112 * Additional check to be implemented by a subclass.
113 * @param aSize Expected number of users.
115 protected void checkUserCount(int aSize) throws SQLException {
116 assertEquals(aSize, _users.size());
120 * Additional check to be implemented by a subclass.
121 * @param aUser User to check for existence.
123 protected void checkGroupExists(String aUser) throws SQLException {
128 * Additional check to be implemented by a subclass.
129 * @param aUser User to check for non-existence.
131 protected void checkGroupNotExists(String aUser) throws SQLException {
136 * Additional check to be implemented by a subclass.
137 * @param aSize Expected number of users.
139 protected void checkGroupCount(int aSize) throws SQLException {
145 * Adds a user and verifies that the user is added using
146 * find(), list(), and contains().
149 public void testAdd() throws SQLException, UserMgtException {
150 User user = createUser("user1", PASSWORD, _group);
151 assertTrue( _users.add(user) );
152 checkUserExists(user.getName());
154 User user2 = _users.find("user1");
155 assertNotNull(user2);
156 assertEquals(user.getName(), user2.getName());
157 Set<User> set = _users.list();
158 assertEquals(1, set.size());
159 assertTrue(set.contains(user));
163 * Tries to find a non-existing user. Verifies that null is
167 public void testFindUnknownUser() throws SQLException, UserMgtException {
168 User user1 = createUser("user1", PASSWORD, _group);
169 User user2 = createUser("user2", PASSWORD, _group);
172 checkUserExists(user1.getName());
173 checkUserExists(user2.getName());
175 assertNull( _users.find("user3") );
176 checkUserNotExists("user3");
180 * Adds duplicate user. Verifies that the existing user is left untouched.
182 public void testAddDuplicateUser() throws SQLException, UserMgtException {
183 User user1 = createUser("user1", PASSWORD, _group);
186 assertEquals(1, _users.list().size());
187 assertTrue(_users.contains(user1));
188 user1 = createUser("user1", PASSWORD, _group);
189 assertFalse(_users.add(user1));
190 assertEquals(1, _users.list().size());
192 checkUserExists(user1.getName());
197 * Removes a user. Verifies that the user is
198 * removed and the return value is true.
201 public void testRemoveUser() throws SQLException, UserMgtException {
202 User user1 = createUser("user1", PASSWORD, _group);
204 assertTrue(_users.contains(user1));
207 assertTrue(_users.remove(user1));
208 assertFalse(_users.contains(user1));
209 assertNull(_users.find(user1.getName()));
210 assertEquals(0, _users.list().size());
215 * Removes a non-existing user. Verifies that no users are
216 * removed an that the return value is true.
219 public void testRemoveNonExistingUser() throws SQLException, UserMgtException {
220 User user1 = createUser("user1", PASSWORD, _group);
223 User nonExistingUser = createUser("user2", PASSWORD, _group);
224 nonExistingUser.setPrimaryKey(new Long(1000));
225 nonExistingUser.setPersistedVersion(10);
226 assertFalse(_users.remove(nonExistingUser));
227 assertTrue(_users.contains(user1));
228 assertEquals(1, _users.list().size());
233 * Adds a number of users to the set and verifies that list()
237 public void testList() throws SQLException, UserMgtException {
238 User user1 = createUser("user1", PASSWORD, _group);
239 User user2 = createUser("user2", PASSWORD, _group);
240 User user3 = createUser("user3", PASSWORD, _group);
241 assertTrue(_users.add(user1));
242 assertTrue(_users.add(user2));
243 assertTrue(_users.add(user3));
245 checkUserExists(user1.getName());
246 checkUserExists(user2.getName());
247 checkUserExists(user3.getName());
249 Set<User> set = _users.list();
250 assertTrue(set.contains(user1));
251 assertTrue(set.contains(user2));
252 assertTrue(set.contains(user3));
258 * Adds several users to different groups and verifies that
259 * the correct users are returned when looking for users in
261 * @throws SQLException
263 public void testListByGroup() throws SQLException, UserMgtException {
264 Group group1 = new Group("group1");
265 Group group2 = new Group("group2");
266 Group group3 = new Group("group3");
276 User user1 = createUser("user1", PASSWORD, group1);
277 user1.addGroup(group2);
278 user1.addGroup(group3);
279 User user2 = createUser("user2", PASSWORD, group2);
280 user2.addGroup(group3);
281 User user3 = createUser("user3", PASSWORD, group3);
286 checkUserExists(user1.getName());
287 checkUserExists(user2.getName());
288 checkUserExists(user3.getName());
290 checkGroupExists(group1.getName());
291 checkGroupExists(group2.getName());
292 checkGroupExists(group3.getName());
295 checkGroupCount(3+1); // also count the group that was created in the setUp().
297 Set<User> list = _users.list(group1);
298 assertTrue(list.contains(user1));
299 assertEquals(1, list.size());
301 list = _users.list(group2);
302 assertTrue(list.contains(user1));
303 assertTrue(list.contains(user2));
304 assertEquals(2, list.size());
306 list = _users.list(group3);
307 assertTrue(list.contains(user1));
308 assertTrue(list.contains(user2));
309 assertTrue(list.contains(user3));
310 assertEquals(3, list.size());