2 * Copyright 2008-2010 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.security.authentication.jpa;
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.Md5HexMessageDigester;
23 import org.wamblee.security.authentication.MessageDigester;
24 import org.wamblee.security.authentication.NameValidator;
25 import org.wamblee.security.authentication.RegexpNameValidator;
26 import org.wamblee.security.authentication.User;
27 import org.wamblee.security.authentication.UserAdministration;
28 import org.wamblee.security.authentication.UserAdministrationImpl;
29 import org.wamblee.security.authentication.UserSet;
30 import org.wamblee.test.persistence.JpaTester;
31 import org.wamblee.test.persistence.PersistenceUnitDescription;
32 import org.wamblee.test.persistence.TransactionProxyFactory;
35 * Setup of a security repository for unit test. This provides all the necessary wiring
38 * @author Erik Brakkee
40 public class UserAdministrationTester {
42 private PersistenceUnitDescription persistenceUnit;
43 private JpaTester jpaTester;
44 private Cache<String, User> userCache;
45 private UserAdministration userAdmin;
46 private MessageDigester passwordDigester;
48 public UserAdministrationTester() {
49 persistenceUnit = new SecurityPersistenceUnit();
50 jpaTester = new JpaTester(persistenceUnit);
53 public void start() throws Exception {
55 userCache = new EhCache<String, User>(
56 new ClassPathResource("properties/org.wamblee.security.ehcache.xml"),
59 TransactionProxyFactory<UserAdministration> factory = new TransactionProxyFactory<UserAdministration>(
60 jpaTester.getJpaBuilder(), UserAdministration.class);
62 NameValidator passwordValidator = new RegexpNameValidator(".{5,}",
63 "INVALID_PASSWORD", "Password must have at least 5 characters");
64 passwordDigester = new Md5HexMessageDigester();
65 UserSet userset = new JpaUserSet(userCache, passwordValidator,
66 passwordDigester, factory.getTransactionScopedEntityManager());
67 GroupSet groupset = new JpaGroupSet(factory
68 .getTransactionScopedEntityManager());
70 NameValidator userValidator = new RegexpNameValidator(
71 "[a-zA-Z]+[a-zA-Z0-9]*", "INVALID_USERNAME", "");
72 NameValidator groupValidator = new RegexpNameValidator(
73 "[a-zA-Z]+[a-zA-Z0-9]*", "INVALID_GROUPNAME", "");
74 UserAdministration userAdminImpl = new UserAdministrationImpl(userset,
75 groupset, userValidator, groupValidator);
76 userAdmin = factory.getProxy(userAdminImpl);
79 public PersistenceUnitDescription getPersistenceUnit() {
80 return persistenceUnit;
83 public JpaTester getJpaTester() {
87 public Cache<String, User> getUserCache() {
91 public UserAdministration getUserAdministration() {
95 public MessageDigester getPasswordEncoder() {
96 return passwordDigester;
99 public void stop() throws Exception {