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.
16 package org.wamblee.security.authorization;
18 import junit.framework.TestCase;
20 import org.wamblee.security.encryption.Md5HexMessageDigester;
22 import org.wamblee.usermgt.Group;
23 import org.wamblee.usermgt.InMemoryGroupSet;
24 import org.wamblee.usermgt.InMemoryUserSet;
25 import org.wamblee.usermgt.RegexpNameValidator;
26 import org.wamblee.usermgt.User;
27 import org.wamblee.usermgt.UserAccessor;
28 import org.wamblee.usermgt.UserAdministration;
29 import org.wamblee.usermgt.UserAdministrationImpl;
30 import org.wamblee.usermgt.UserMgtException;
31 import org.wamblee.usermgt.UserMgtException.Reason;
35 * User access that always returns a user that belongs to a fixed group.
37 * @author Erik Brakkee
39 public class TestUserAccessor implements UserAccessor {
43 private static final String USER = "erik";
48 private static final String PASSWORD = "abc123";
53 private static final String GROUP = "users";
58 * @see org.wamblee.usermgt.UserAccessor#getCurrentUser()
63 * @return DOCUMENT ME!
65 * @throws RuntimeException DOCUMENT ME!
67 public User getCurrentUser() {
68 UserAdministration admin = new UserAdministrationImpl(new InMemoryUserSet(
69 new RegexpNameValidator(
70 RegexpNameValidator.PASSWORD_PATTERN,
71 Reason.INVALID_PASSWORD,
72 "Password must contain at least 6 characters"),
73 new Md5HexMessageDigester()), new InMemoryGroupSet(),
74 new RegexpNameValidator(RegexpNameValidator.ID_PATTERN,
75 Reason.INVALID_USERNAME, "Invalid user"),
76 new RegexpNameValidator(RegexpNameValidator.ID_PATTERN,
77 Reason.INVALID_GROUPNAME, "Invalid group"));
80 Group group = admin.createGroup(GROUP);
82 return admin.createUser(USER, PASSWORD, group);
83 } catch (UserMgtException e) {
84 TestCase.fail(e.getMessage());
85 throw new RuntimeException(e);