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