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.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;
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.
42 * @author Erik Brakkee
44 public class HibernateUserAdministrationTest extends UserAdministrationImplTest {
46 private static final Log LOG = LogFactory.getLog(HibernateUserAdministrationTest.class);
48 private DefaultContainer _container;
51 private DatabaseTesterComponent _databaseTester;
52 private EhCache<Serializable, Serializable> _userCache;
53 private UserAdministration _userAdmin;
57 * @see org.wamblee.usermgt.UserAdministrationImplTest#setUp()
60 protected void setUp() throws Exception {
62 _container = new DefaultContainer("top");
63 DatabaseComponentFactory.addDatabaseConfig(_container);
64 _container.addComponent(new DatasourceComponent("datasource"));
65 _container.addComponent(new UserAdministrationComponent("admin", true));
67 _container.addComponent("databaseTester", DatabaseTesterComponent.class);
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);
75 _scope = _container.start();
77 _databaseTester.cleanDatabase();
83 public void setUserCache(EhCache<Serializable, Serializable> aUserCache) {
84 _userCache = aUserCache;
87 public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
88 _databaseTester = aDatabaseTester;
91 public void setUserAdmin(UserAdministration aUserAdmin) {
92 _userAdmin = aUserAdmin;
98 * @see org.wamblee.usermgt.UserAdministrationImplTest#createAdmin()
101 protected UserAdministration createAdmin() {
105 public void testAllTestsInASeparateTransaction() throws SQLException {
107 Method[] methods = UserAdministrationImplTest.class.getMethods();
108 for (final Method method : methods) {
109 if (method.getName().startsWith("test")) {
110 _databaseTester.cleanDatabase();
112 _databaseTester.executeTransaction(new TestTransactionCallbackWithoutResult() {
113 public void execute() throws Exception {
114 LOG.info("Running test " + method.getName());
116 method.invoke(HibernateUserAdministrationTest.this);
117 } catch (Throwable t) {
118 LOG.error("Test " + method.getName() + " failed");
119 throw new RuntimeException(t.getMessage(), t);
122 LOG.info("Test " + method.getName() + " finished");
134 private void clearUserCache() {