(no commit message)
[utils] / security / jpatest / src / test / java / org / wamblee / security / authentication / jpa / UserAdministrationTester.java
1 /*
2  * Copyright 2008-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.jpa;
17
18 import org.wamblee.cache.Cache;
19 import org.wamblee.cache.EhCache;
20 import org.wamblee.io.ClassPathResource;
21 import org.wamblee.security.authentication.GroupSet;
22 import org.wamblee.security.authentication.NameValidator;
23 import org.wamblee.security.authentication.RegexpNameValidator;
24 import org.wamblee.security.authentication.User;
25 import org.wamblee.security.authentication.UserAdministration;
26 import org.wamblee.security.authentication.UserAdministrationImpl;
27 import org.wamblee.security.authentication.UserSet;
28 import org.wamblee.security.encryption.Md5HexMessageDigester;
29 import org.wamblee.security.encryption.MessageDigester;
30 import org.wamblee.support.persistence.JpaTester;
31 import org.wamblee.support.persistence.TransactionProxyFactory;
32
33 /**
34  * Setup of a security repository for unit test. This provides all the necessary wiring
35  * and JPA setup.
36  * 
37  * @author Erik Brakkee
38  */
39 public class UserAdministrationTester {
40
41     private JpaTester jpaTester;
42     private Cache<String, User> userCache;
43     private UserAdministration userAdmin; 
44
45     public UserAdministrationTester() {
46         jpaTester = new JpaTester(new SecurityPersistenceUnit());
47     }
48
49     public void start() throws Exception {
50         jpaTester.start();
51         userCache = new EhCache<String, User>(
52             new ClassPathResource("properties/org.wamblee.security.ehcache.xml"),
53             "users");
54
55         TransactionProxyFactory<UserAdministration> factory = new TransactionProxyFactory<UserAdministration>(
56             jpaTester.getJpaBuilder(), UserAdministration.class);
57
58         NameValidator passwordValidator = new RegexpNameValidator(".{5,}",
59             "INVALID_PASSWORD", "Password must have at least 5 characters");
60         MessageDigester passwordDigester = new Md5HexMessageDigester();
61         UserSet userset = new JpaUserSet(userCache, passwordValidator,
62             passwordDigester, factory.getTransactionScopedEntityManager());
63         GroupSet groupset = new JpaGroupSet(factory
64             .getTransactionScopedEntityManager());
65
66         NameValidator userValidator = new RegexpNameValidator(
67             "[a-zA-Z]+[a-zA-Z0-9]*", "INVALID_USERNAME", "");
68         NameValidator groupValidator = new RegexpNameValidator(
69             "[a-zA-Z]+[a-zA-Z0-9]*", "INVALID_GROUPNAME", "");
70         UserAdministration userAdminImpl = new UserAdministrationImpl(userset,
71             groupset, userValidator, groupValidator);
72         userAdmin = factory.getProxy(userAdminImpl);
73     }
74    
75     
76     public JpaTester getJpaTester() {
77         return jpaTester;
78     }
79     
80     public Cache<String, User> getUserCache() {
81         return userCache;
82     }
83
84     public UserAdministration getUserAdministration() {
85         return userAdmin;
86     }
87
88     public void stop() throws Exception {
89         jpaTester.stop();
90     }
91
92 }