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;
44 * This method must be overriden in subclasses.
45 * @return New user set object.
47 protected UserSet createUserSet() {
48 return new InMemoryUserSet( new RegexpNameValidator(RegexpNameValidator.PASSWORD_PATTERN, Reason.INVALID_PASSWORD, "Password must contain at least 6 characters"),
49 new Md5HexMessageDigester());
53 * This method must be overriden in subclasses.
54 * @return New group set object.
56 protected GroupSet createGroupSet() {
57 return new InMemoryGroupSet();
61 * @see org.wamblee.test.SpringTestCase#setUp()
64 protected void setUp() throws Exception {
66 _users = createUserSet();
67 _groups = createGroupSet();
68 _group = new Group("group0");
74 protected UserSet getUsers() {
78 protected GroupSet getGroups() {
82 protected Group createGroup(String aName) {
83 return new Group(aName);
86 protected User createUser(String aName, String aPassword, Group aGroup) throws UserMgtException {
87 return UsermgtTestUtils.createUser(aName, aPassword, aGroup);
90 protected void addUserToGroup(User aUser, Group aGroup) throws UserMgtException {
91 aUser.addGroup(aGroup);
94 protected void removeUserFromGroup(User aUser, Group aGroup ) throws UserMgtException {
95 aUser.removeGroup(aGroup);
99 * Additional check to be implemented by a subclass.
100 * @param aUser User to check for existence.
102 protected void checkUserExists(String aUser) throws SQLException {
107 * Additional check to be implemented by a subclass.
108 * @param aUser User to check for non-existence.
110 protected void checkUserNotExists(String aUser) throws SQLException {
115 * Additional check to be implemented by a subclass.
116 * @param aSize Expected number of users.
118 protected void checkUserCount(int aSize) throws SQLException {
119 assertEquals(aSize, _users.size());
123 * Additional check to be implemented by a subclass.
124 * @param aUser User to check for existence.
126 protected void checkGroupExists(String aUser) throws SQLException {
131 * Additional check to be implemented by a subclass.
132 * @param aUser User to check for non-existence.
134 protected void checkGroupNotExists(String aUser) throws SQLException {
139 * Additional check to be implemented by a subclass.
140 * @param aSize Expected number of users.
142 protected void checkGroupCount(int aSize) throws SQLException {
148 * Adds a user and verifies that the user is added using
149 * find(), list(), and contains().
152 public void testAdd() throws SQLException, UserMgtException {
153 User user = createUser("user1", PASSWORD, _group);
154 assertTrue( _users.add(user) );
155 checkUserExists(user.getName());
157 User user2 = _users.find("user1");
158 assertNotNull(user2);
159 assertEquals(user.getName(), user2.getName());
160 Set<User> set = _users.list();
161 assertEquals(1, set.size());
162 assertTrue(set.contains(user));
166 * Tries to find a non-existing user. Verifies that null is
170 public void testFindUnknownUser() throws SQLException, UserMgtException {
171 User user1 = createUser("user1", PASSWORD, _group);
172 User user2 = createUser("user2", PASSWORD, _group);
175 checkUserExists(user1.getName());
176 checkUserExists(user2.getName());
178 assertNull( _users.find("user3") );
179 checkUserNotExists("user3");
183 * Adds duplicate user. Verifies that the existing user is left untouched.
185 public void testAddDuplicateUser() throws SQLException, UserMgtException {
186 User user1 = createUser("user1", PASSWORD, _group);
189 assertEquals(1, _users.list().size());
190 assertTrue(_users.contains(user1));
191 user1 = createUser("user1", PASSWORD, _group);
192 assertFalse(_users.add(user1));
193 assertEquals(1, _users.list().size());
195 checkUserExists(user1.getName());
200 * Removes a user. Verifies that the user is
201 * removed and the return value is true.
204 public void testRemoveUser() throws SQLException, UserMgtException {
205 User user1 = createUser("user1", PASSWORD, _group);
207 assertTrue(_users.contains(user1));
210 assertTrue(_users.remove(user1));
211 assertFalse(_users.contains(user1));
212 assertNull(_users.find(user1.getName()));
213 assertEquals(0, _users.list().size());
218 * Removes a non-existing user. Verifies that no users are
219 * removed an that the return value is true.
222 public void testRemoveNonExistingUser() throws SQLException, UserMgtException {
223 User user1 = createUser("user1", PASSWORD, _group);
226 User nonExistingUser = createUser("user2", PASSWORD, _group);
227 nonExistingUser.setPrimaryKey(new Long(1000));
228 nonExistingUser.setPersistedVersion(10);
229 assertFalse(_users.remove(nonExistingUser));
230 assertTrue(_users.contains(user1));
231 assertEquals(1, _users.list().size());
236 * Adds a number of users to the set and verifies that list()
240 public void testList() throws SQLException, UserMgtException {
241 User user1 = createUser("user1", PASSWORD, _group);
242 User user2 = createUser("user2", PASSWORD, _group);
243 User user3 = createUser("user3", PASSWORD, _group);
244 assertTrue(_users.add(user1));
245 assertTrue(_users.add(user2));
246 assertTrue(_users.add(user3));
248 checkUserExists(user1.getName());
249 checkUserExists(user2.getName());
250 checkUserExists(user3.getName());
252 Set<User> set = _users.list();
253 assertTrue(set.contains(user1));
254 assertTrue(set.contains(user2));
255 assertTrue(set.contains(user3));
261 * Adds several users to different groups and verifies that
262 * the correct users are returned when looking for users in
264 * @throws SQLException
266 public void testListByGroup() throws SQLException, UserMgtException {
267 Group group1 = new Group("group1");
268 Group group2 = new Group("group2");
269 Group group3 = new Group("group3");
279 User user1 = createUser("user1", PASSWORD, group1);
280 user1.addGroup(group2);
281 user1.addGroup(group3);
282 User user2 = createUser("user2", PASSWORD, group2);
283 user2.addGroup(group3);
284 User user3 = createUser("user3", PASSWORD, group3);
289 checkUserExists(user1.getName());
290 checkUserExists(user2.getName());
291 checkUserExists(user3.getName());
293 checkGroupExists(group1.getName());
294 checkGroupExists(group2.getName());
295 checkGroupExists(group3.getName());
298 checkGroupCount(3+1); // also count the group that was created in the setUp().
300 Set<User> list = _users.list(group1);
301 assertTrue(list.contains(user1));
302 assertEquals(1, list.size());
304 list = _users.list(group2);
305 assertTrue(list.contains(user1));
306 assertTrue(list.contains(user2));
307 assertEquals(2, list.size());
309 list = _users.list(group3);
310 assertTrue(list.contains(user1));
311 assertTrue(list.contains(user2));
312 assertTrue(list.contains(user3));
313 assertEquals(3, list.size());