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