align package names.
[utils] / security / jpatest / src / test / java / org / wamblee / security / authentication / jpa / JpaUserAdministrationTest.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.jpa;
17
18 import java.lang.reflect.Method;
19 import java.sql.Connection;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.junit.Ignore;
24 import org.wamblee.cache.EhCache;
25 import org.wamblee.io.ClassPathResource;
26 import org.wamblee.security.authentication.GroupSet;
27 import org.wamblee.security.authentication.NameValidator;
28 import org.wamblee.security.authentication.RegexpNameValidator;
29 import org.wamblee.security.authentication.User;
30 import org.wamblee.security.authentication.UserAdministration;
31 import org.wamblee.security.authentication.UserAdministrationImpl;
32 import org.wamblee.security.authentication.UserAdministrationImplTest;
33 import org.wamblee.security.authentication.UserSet;
34 import org.wamblee.security.authentication.jpa.JpaGroupSet;
35 import org.wamblee.security.authentication.jpa.JpaUserSet;
36 import org.wamblee.security.encryption.Md5HexMessageDigester;
37 import org.wamblee.security.encryption.MessageDigester;
38 import org.wamblee.support.persistence.JpaTester;
39 import org.wamblee.support.persistence.TransactionProxyFactory;
40 import org.wamblee.support.persistence.DatabaseUtils.JdbcUnitOfWork;
41
42 /**
43  * User administration tests with persistence based on Hibernate. This executes
44  * the same test cases as {@link org.wamblee.security.authentication.UserAdministrationImplTest}
45  * with in addition, one test case that executes all Hibernate test cases
46  * separately with each test case in its own transaction.
47  * 
48  * @author Erik Brakkee
49  */
50 public class JpaUserAdministrationTest extends UserAdministrationImplTest {
51     private static final Log LOG = LogFactory
52         .getLog(JpaUserAdministrationTest.class);
53
54     private JpaTester jpaTester;
55
56     private EhCache<String, User> userCache;
57
58     private UserAdministration userAdmin;
59
60     /*
61      * (non-Javadoc)
62      * 
63      * @see org.wamblee.usermgt.UserAdministrationImplTest#setUp()
64      */
65     @Override
66     protected void setUp() throws Exception {
67         jpaTester = new JpaTester(new SecurityPersistenceUnit());
68         jpaTester.start();
69
70         userCache = new EhCache<String, User>(new ClassPathResource(
71             "properties/org.wamblee.security.ehcache.xml"), "users");
72
73         TransactionProxyFactory<UserAdministration> factory = new TransactionProxyFactory<UserAdministration>(
74             jpaTester.getJpaBuilder(), UserAdministration.class);
75
76         NameValidator passwordValidator = new RegexpNameValidator(".{5,}",
77             "INVALID_PASSWORD", "Password must have at least 5 characters");
78         MessageDigester passwordDigester = new Md5HexMessageDigester();
79         UserSet userset = new JpaUserSet(userCache, passwordValidator,
80             passwordDigester, factory.getTransactionScopedEntityManager());
81         GroupSet groupset = new JpaGroupSet(factory
82             .getTransactionScopedEntityManager());
83
84         NameValidator userValidator = new RegexpNameValidator(
85             "[a-zA-Z]+[a-zA-Z0-9]*", "INVALID_USERNAME", "");
86         NameValidator groupValidator = new RegexpNameValidator(
87             "[a-zA-Z]+[a-zA-Z0-9]*", "INVALID_GROUPNAME", "");
88         UserAdministration userAdminImpl = new UserAdministrationImpl(userset,
89             groupset, userValidator, groupValidator);
90         userAdmin = factory.getProxy(userAdminImpl);
91
92         super.setUp();
93         clearUserCache();
94     }
95
96     @Override
97     protected void tearDown() throws Exception {
98         jpaTester.stop();
99         super.tearDown();
100     }
101
102     /*
103      * (non-Javadoc)
104      * 
105      * @see org.wamblee.usermgt.UserAdministrationImplTest#createAdmin()
106      */
107     @Override
108     protected UserAdministration createAdmin() {
109         return userAdmin;
110     }
111     
112     public void testAllTestsInASeparateTransaction() throws Exception {
113         Method[] methods = UserAdministrationImplTest.class.getMethods();
114
115         for (final Method method : methods) {
116             if (method.getName().startsWith("test")) {
117                 jpaTester.getDbUtils().cleanDatabase();
118                 clearUserCache();
119                 jpaTester.getDbUtils().executeInTransaction(
120                     new JdbcUnitOfWork<Void>() {
121                         @Override
122                         public Void execute(Connection aConnection)
123                             throws Exception {
124                             LOG.info("Running test " + method.getName());
125
126                             try {
127                                 method.invoke(JpaUserAdministrationTest.this);
128                             } catch (Throwable t) {
129                                 LOG.error("Test " + method.getName() +
130                                     " failed");
131                                 throw new RuntimeException(t.getMessage(), t);
132                             } finally {
133                                 LOG.info("Test " + method.getName() +
134                                     " finished");
135                             }
136                             return null;
137                         }
138                     });
139             }
140         }
141     }
142
143     private void clearUserCache() {
144         userCache.clear();
145     }
146 }