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