234ed2664b13731a170f3f11cc385a8676ce272c
[utils] / security / usermgt / src / test / java / org / wamblee / security / authentication / UsermgtTestUtils.java
1 /*
2  * Copyright 2005-2010 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.security.authentication;
17
18 import org.wamblee.security.authentication.Group;
19 import org.wamblee.security.authentication.RegexpNameValidator;
20 import org.wamblee.security.authentication.User;
21 import org.wamblee.security.authentication.UserMgtException;
22 import org.wamblee.security.authentication.UserMgtException.Reason;
23
24
25 /**
26  * User management test utilities.
27  * 
28  * @author Erik Brakkee
29  */
30 public class UsermgtTestUtils {
31     private static final String DUMMY_GROUP = "dummygroup";
32
33     private static final String DUMMY_PASSWD = "dummypasswd";
34
35     public static Group createGroup(String aName) {
36         return new Group(aName);
37     }
38
39     public static User createUser(String aUsername) throws UserMgtException {
40         return createUser(aUsername, DUMMY_GROUP);
41     }
42
43     public static User createUser(String aUsername, String aGroup)
44         throws UserMgtException {
45         return createUser(aUsername, createGroup(aGroup));
46     }
47
48     public static User createUser(String aUsername, Group aGroup)
49         throws UserMgtException {
50         return createUser(aUsername, DUMMY_PASSWD, aGroup);
51     }
52
53     public static User createUser(String aName, String aPassword, Group aGroup)
54         throws UserMgtException {
55         User user = new User(aName, aPassword, new RegexpNameValidator(
56             RegexpNameValidator.PASSWORD_PATTERN, Reason.INVALID_PASSWORD,
57             "Password must be at least 6 chars"), new Md5HexMessageDigester());
58         user.addGroup(aGroup);
59         return user; 
60     }
61
62     public static void addUserToGroup(User aUser, Group aGroup)
63         throws UserMgtException {
64         aUser.addGroup(aGroup);
65     }
66
67     public static void removeUserFromGroup(User aUser, Group aGroup)
68         throws UserMgtException {
69         aUser.removeGroup(aGroup);
70     }
71 }