2 * Copyright 2005-2010 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.
16 package org.wamblee.usermgt.hibernate;
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
21 import org.wamblee.cache.EhCache;
23 import org.wamblee.system.adapters.ClassConfiguration;
24 import org.wamblee.system.adapters.DefaultContainer;
25 import org.wamblee.system.adapters.ObjectConfiguration;
26 import org.wamblee.system.components.DatabaseComponentFactory;
27 import org.wamblee.system.core.Scope;
28 import org.wamblee.system.spring.component.DatabaseTesterComponent;
29 import org.wamblee.system.spring.component.DatasourceComponent;
31 import org.wamblee.test.spring.TestTransactionCallbackWithoutResult;
33 import org.wamblee.usermgt.UserAdministration;
34 import org.wamblee.usermgt.UserAdministrationImplTest;
36 import java.io.Serializable;
38 import java.lang.reflect.Method;
40 import java.sql.SQLException;
43 * User administration tests with persistence based on Hibernate. This executes
44 * the same test cases as {@link org.wamblee.usermgt.UserAdministrationImplTest}
45 * with in addition, one test case that executes all Hibernate test cases
46 * separately with each test case in its own transaction.
48 * @author Erik Brakkee
50 public class HibernateUserAdministrationTest extends UserAdministrationImplTest {
51 private static final Log LOG = LogFactory
52 .getLog(HibernateUserAdministrationTest.class);
54 private DefaultContainer container;
58 private DatabaseTesterComponent databaseTester;
60 private EhCache<Serializable, Serializable> userCache;
62 private UserAdministration userAdmin;
67 * @see org.wamblee.usermgt.UserAdministrationImplTest#setUp()
70 protected void setUp() throws Exception {
71 container = new DefaultContainer("top");
72 DatabaseComponentFactory.addDatabaseConfig(container);
73 container.addComponent(new DatasourceComponent("datasource"));
74 container.addComponent(new UserAdministrationComponent("admin", true));
76 ClassConfiguration dbtesterConfig = new ClassConfiguration(
77 DatabaseTesterComponent.class);
78 dbtesterConfig.getObjectConfig().getSetterConfig().initAllSetters();
79 container.addComponent("databaseTester", dbtesterConfig);
81 ObjectConfiguration config = new ObjectConfiguration(
82 HibernateUserAdministrationTest.class);
83 config.getSetterConfig().clear().add("setUserCache").add(
84 "setDatabaseTester").add("setUserAdmin");
85 container.addComponent("testcase", this, config);
87 scope = container.start();
89 databaseTester.cleanDatabase();
95 public void setUserCache(EhCache<Serializable, Serializable> aUserCache) {
96 userCache = aUserCache;
99 public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
100 databaseTester = aDatabaseTester;
103 public void setUserAdmin(UserAdministration aUserAdmin) {
104 userAdmin = aUserAdmin;
108 protected void tearDown() throws Exception {
109 container.stop(scope);
116 * @see org.wamblee.usermgt.UserAdministrationImplTest#createAdmin()
119 protected UserAdministration createAdmin() {
123 public void testAllTestsInASeparateTransaction() throws SQLException {
124 Method[] methods = UserAdministrationImplTest.class.getMethods();
126 for (final Method method : methods) {
127 if (method.getName().startsWith("test")) {
128 databaseTester.cleanDatabase();
131 .executeTransaction(new TestTransactionCallbackWithoutResult() {
132 public void execute() throws Exception {
133 LOG.info("Running test " + method.getName());
137 .invoke(HibernateUserAdministrationTest.this);
138 } catch (Throwable t) {
139 LOG.error("Test " + method.getName() +
141 throw new RuntimeException(t.getMessage(), t);
143 LOG.info("Test " + method.getName() +
152 private void clearUserCache() {