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