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