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