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