2 * Copyright 2005 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.wamblee.usermgt.hibernate;
19 import java.io.Serializable;
20 import java.lang.reflect.Method;
21 import java.sql.SQLException;
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;
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.
43 * @author Erik Brakkee
45 public class HibernateUserAdministrationTest extends UserAdministrationImplTest {
47 private static final Log LOG = LogFactory.getLog(HibernateUserAdministrationTest.class);
49 private DefaultContainer _container;
52 private DatabaseTesterComponent _databaseTester;
53 private EhCache<Serializable, Serializable> _userCache;
54 private UserAdministration _userAdmin;
58 * @see org.wamblee.usermgt.UserAdministrationImplTest#setUp()
61 protected void setUp() throws Exception {
63 _container = new DefaultContainer("top");
64 DatabaseComponentFactory.addDatabaseConfig(_container);
65 _container.addComponent(new DatasourceComponent("datasource"));
66 _container.addComponent(new UserAdministrationComponent("admin", true));
68 ClassConfiguration dbtesterConfig = new ClassConfiguration(DatabaseTesterComponent.class);
69 dbtesterConfig.getObjectConfig().getSetterConfig().initAllSetters();
70 _container.addComponent("databaseTester", dbtesterConfig);
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);
78 _scope = _container.start();
80 _databaseTester.cleanDatabase();
86 public void setUserCache(EhCache<Serializable, Serializable> aUserCache) {
87 _userCache = aUserCache;
90 public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
91 _databaseTester = aDatabaseTester;
94 public void setUserAdmin(UserAdministration aUserAdmin) {
95 _userAdmin = aUserAdmin;
99 protected void tearDown() throws Exception {
100 _container.stop(_scope);
107 * @see org.wamblee.usermgt.UserAdministrationImplTest#createAdmin()
110 protected UserAdministration createAdmin() {
114 public void testAllTestsInASeparateTransaction() throws SQLException {
116 Method[] methods = UserAdministrationImplTest.class.getMethods();
117 for (final Method method : methods) {
118 if (method.getName().startsWith("test")) {
119 _databaseTester.cleanDatabase();
121 _databaseTester.executeTransaction(new TestTransactionCallbackWithoutResult() {
122 public void execute() throws Exception {
123 LOG.info("Running test " + method.getName());
125 method.invoke(HibernateUserAdministrationTest.this);
126 } catch (Throwable t) {
127 LOG.error("Test " + method.getName() + " failed");
128 throw new RuntimeException(t.getMessage(), t);
131 LOG.info("Test " + method.getName() + " finished");
143 private void clearUserCache() {