08ec4ae99bce41072161cd533a07b940018f80ee
[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.io.Serializable;
20 import java.lang.reflect.Method;
21 import java.sql.SQLException;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.wamblee.cache.EhCache;
26 import org.wamblee.system.adapters.ClassConfiguration;
27 import org.wamblee.system.adapters.DefaultContainer;
28 import org.wamblee.system.adapters.ObjectConfiguration;
29 import org.wamblee.system.components.DatabaseComponentFactory;
30 import org.wamblee.system.core.Scope;
31 import org.wamblee.system.spring.component.DatabaseTesterComponent;
32 import org.wamblee.system.spring.component.DatasourceComponent;
33 import org.wamblee.test.spring.TestTransactionCallbackWithoutResult;
34 import org.wamblee.usermgt.UserAdministration;
35 import org.wamblee.usermgt.UserAdministrationImplTest;
36
37 /**
38  * User administration tests with persistence based on Hibernate. This executes
39  * the same test cases as {@link org.wamblee.usermgt.UserAdministrationImplTest}
40  * with in addition, one test case that executes all Hibernate test cases
41  * separately with each test case in its own transaction.
42  *
43  * @author Erik Brakkee
44  */
45 public class HibernateUserAdministrationTest extends UserAdministrationImplTest {
46     
47     private static final Log LOG = LogFactory.getLog(HibernateUserAdministrationTest.class);
48
49     private DefaultContainer container;
50     private Scope scope;
51     
52     private DatabaseTesterComponent databaseTester;
53     private EhCache<Serializable, Serializable> userCache;
54     private UserAdministration userAdmin; 
55     
56     
57     /* (non-Javadoc)
58      * @see org.wamblee.usermgt.UserAdministrationImplTest#setUp()
59      */
60     @Override
61     protected void setUp() throws Exception {
62         
63         container = new DefaultContainer("top");
64         DatabaseComponentFactory.addDatabaseConfig(container);
65         container.addComponent(new DatasourceComponent("datasource"));
66         container.addComponent(new UserAdministrationComponent("admin", true));
67         
68         ClassConfiguration dbtesterConfig = new ClassConfiguration(DatabaseTesterComponent.class); 
69         dbtesterConfig.getObjectConfig().getSetterConfig().initAllSetters();
70         container.addComponent("databaseTester", dbtesterConfig);
71         
72         ObjectConfiguration config = new ObjectConfiguration(
73                 HibernateUserAdministrationTest.class);
74         config.getSetterConfig().clear().add(
75                 "setUserCache").add("setDatabaseTester").add("setUserAdmin");
76         container.addComponent("testcase", this, config);
77
78         scope = container.start();
79
80         databaseTester.cleanDatabase();
81        
82         super.setUp();
83         clearUserCache();
84     }
85     
86     public void setUserCache(EhCache<Serializable, Serializable> aUserCache) {
87         userCache = aUserCache;
88     }
89     
90     public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
91         databaseTester = aDatabaseTester;
92     }
93     
94     public void setUserAdmin(UserAdministration aUserAdmin) {
95         userAdmin = aUserAdmin;
96     }
97
98     @Override
99     protected void tearDown()  throws Exception { 
100         container.stop(scope);
101         super.tearDown();
102     }
103
104     /*
105      * (non-Javadoc)
106      * 
107      * @see org.wamblee.usermgt.UserAdministrationImplTest#createAdmin()
108      */
109     @Override
110     protected UserAdministration createAdmin() {
111         return userAdmin;
112     }
113
114     public void testAllTestsInASeparateTransaction() throws SQLException {
115
116         Method[] methods = UserAdministrationImplTest.class.getMethods();
117         for (final Method method : methods) {
118             if (method.getName().startsWith("test")) {
119                 databaseTester.cleanDatabase();
120                 clearUserCache();
121                 databaseTester.executeTransaction(new TestTransactionCallbackWithoutResult() {
122                     public void execute() throws Exception {
123                         LOG.info("Running test " + method.getName());
124                         try {
125                             method.invoke(HibernateUserAdministrationTest.this);
126                         } catch (Throwable t) {
127                             LOG.error("Test " + method.getName() + " failed");
128                             throw new RuntimeException(t.getMessage(), t); 
129                         }
130                         finally {
131                             LOG.info("Test " + method.getName() + " finished");
132                         }
133
134                     }
135                 });
136             }
137         }
138     }
139
140     /**
141      * 
142      */
143     private void clearUserCache() {
144         userCache.clear();
145     }
146 }