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