939b5153d6a4df38334e642a34abfb9ee1bfb8e1
[utils] / security / src / test / java / org / wamblee / usermgt / hibernate / HibernateUserAdministrationTest.java
1 /*
2  * Copyright 2005 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
17 package org.wamblee.usermgt.hibernate;
18
19 import java.lang.reflect.Method;
20 import java.sql.SQLException;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.wamblee.cache.Cache;
25 import org.wamblee.general.BeanKernel;
26 import org.wamblee.test.TestTransactionCallbackWithoutResult;
27 import org.wamblee.usermgt.UserAdministration;
28 import org.wamblee.usermgt.UserAdministrationImplTest;
29 import org.wamblee.usermgt.UsermgtHibernateMappingFiles;
30 import org.wamblee.usermgt.UsermgtSpringConfigFiles;
31
32 /**
33  * User administration tests with persistence based on Hibernate. This executes
34  * the same test cases as {@link org.wamblee.usermgt.UserAdministrationImplTest}
35  * with in addition, one test case that executes all Hibernate test cases
36  * separately with each test case in its own transaction.
37  *
38  * @author Erik Brakkee
39  */
40 public class HibernateUserAdministrationTest extends UserAdministrationImplTest {
41     
42     private static final Log LOG = LogFactory.getLog(HibernateUserAdministrationTest.class);
43
44     public HibernateUserAdministrationTest() {
45         super(UsermgtSpringConfigFiles.class,
46                 UsermgtHibernateMappingFiles.class);
47     }
48     
49     /* (non-Javadoc)
50      * @see org.wamblee.usermgt.UserAdministrationImplTest#setUp()
51      */
52     @Override
53     protected void setUp() throws Exception {
54         super.setUp();
55         clearUserCache();
56     }
57
58     /*
59      * (non-Javadoc)
60      * 
61      * @see org.wamblee.usermgt.UserAdministrationImplTest#createAdmin()
62      */
63     @Override
64     protected UserAdministration createAdmin() {
65         return BeanKernel.getBeanFactory().find(UserAdministration.class);
66     }
67
68     public void testAllTestsInASeparateTransaction() throws SQLException {
69
70         Method[] methods = UserAdministrationImplTest.class.getMethods();
71         for (final Method method : methods) {
72             if (method.getName().startsWith("test")) {
73                 cleanDatabase();
74                 clearUserCache();
75                 executeTransaction(new TestTransactionCallbackWithoutResult() {
76                     public void execute() throws Exception {
77                         LOG.info("Running test " + method.getName());
78                         try {
79                             method.invoke(HibernateUserAdministrationTest.this);
80                         } catch (Throwable t) {
81                             LOG.error("Test " + method.getName() + " failed");
82                             throw new RuntimeException(t.getMessage(), t); 
83                         }
84                         finally {
85                             LOG.info("Test " + method.getName() + " finished");
86                         }
87
88                     }
89                 });
90             }
91         }
92     }
93
94     /**
95      * 
96      */
97     private void clearUserCache() {
98         BeanKernel.getBeanFactory().find("userCache", Cache.class).clear();
99     }
100 }