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