8eaa8becfe14909f48c75307b64c63cb7c1ee55d
[utils] /
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 UserAdministrationTester userAdminTester; 
55
56     /*
57      * (non-Javadoc)
58      * 
59      * @see org.wamblee.usermgt.UserAdministrationImplTest#setUp()
60      */
61     @Override
62     protected void setUp() throws Exception {
63         userAdminTester = new UserAdministrationTester(); 
64         userAdminTester.start();
65         super.setUp();
66         clearUserCache();
67     }
68
69     @Override
70     protected void tearDown() throws Exception {
71         userAdminTester.stop();
72         super.tearDown();
73     }
74
75     /*
76      * (non-Javadoc)
77      * 
78      * @see org.wamblee.usermgt.UserAdministrationImplTest#createAdmin()
79      */
80     @Override
81     protected UserAdministration createAdmin() {
82         return userAdminTester.getUserAdministration();
83     }
84     
85     public void testAllTestsInASeparateTransaction() throws Exception {
86         Method[] methods = UserAdministrationImplTest.class.getMethods();
87
88         for (final Method method : methods) {
89             if (method.getName().startsWith("test")) {
90                 userAdminTester.getJpaTester().getDbUtils().cleanDatabase();
91                 clearUserCache();
92                 userAdminTester.getJpaTester().getDbUtils().executeInTransaction(
93                     new JdbcUnitOfWork<Void>() {
94                         @Override
95                         public Void execute(Connection aConnection)
96                             throws Exception {
97                             LOG.info("Running test " + method.getName());
98
99                             try {
100                                 method.invoke(JpaUserAdministrationTest.this);
101                             } catch (Throwable t) {
102                                 LOG.error("Test " + method.getName() +
103                                     " failed");
104                                 throw new RuntimeException(t.getMessage(), t);
105                             } finally {
106                                 LOG.info("Test " + method.getName() +
107                                     " finished");
108                             }
109                             return null;
110                         }
111                     });
112             }
113         }
114     }
115
116     private void clearUserCache() {
117         userAdminTester.getUserCache().clear();
118     }
119 }