Removed DOCUMENT ME comments that were generated and applied source code
[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 package org.wamblee.usermgt.hibernate;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20
21 import org.wamblee.cache.EhCache;
22
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;
30
31 import org.wamblee.test.spring.TestTransactionCallbackWithoutResult;
32
33 import org.wamblee.usermgt.UserAdministration;
34 import org.wamblee.usermgt.UserAdministrationImplTest;
35
36 import java.io.Serializable;
37
38 import java.lang.reflect.Method;
39
40 import java.sql.SQLException;
41
42 /**
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.
47  * 
48  * @author Erik Brakkee
49  */
50 public class HibernateUserAdministrationTest extends UserAdministrationImplTest {
51     private static final Log LOG = LogFactory
52         .getLog(HibernateUserAdministrationTest.class);
53
54     private DefaultContainer container;
55
56     private Scope scope;
57
58     private DatabaseTesterComponent databaseTester;
59
60     private EhCache<Serializable, Serializable> userCache;
61
62     private UserAdministration userAdmin;
63
64     /*
65      * (non-Javadoc)
66      * 
67      * @see org.wamblee.usermgt.UserAdministrationImplTest#setUp()
68      */
69     @Override
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));
75
76         ClassConfiguration dbtesterConfig = new ClassConfiguration(
77             DatabaseTesterComponent.class);
78         dbtesterConfig.getObjectConfig().getSetterConfig().initAllSetters();
79         container.addComponent("databaseTester", dbtesterConfig);
80
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);
86
87         scope = container.start();
88
89         databaseTester.cleanDatabase();
90
91         super.setUp();
92         clearUserCache();
93     }
94
95     public void setUserCache(EhCache<Serializable, Serializable> aUserCache) {
96         userCache = aUserCache;
97     }
98
99     public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
100         databaseTester = aDatabaseTester;
101     }
102
103     public void setUserAdmin(UserAdministration aUserAdmin) {
104         userAdmin = aUserAdmin;
105     }
106
107     @Override
108     protected void tearDown() throws Exception {
109         container.stop(scope);
110         super.tearDown();
111     }
112
113     /*
114      * (non-Javadoc)
115      * 
116      * @see org.wamblee.usermgt.UserAdministrationImplTest#createAdmin()
117      */
118     @Override
119     protected UserAdministration createAdmin() {
120         return userAdmin;
121     }
122
123     public void testAllTestsInASeparateTransaction() throws SQLException {
124         Method[] methods = UserAdministrationImplTest.class.getMethods();
125
126         for (final Method method : methods) {
127             if (method.getName().startsWith("test")) {
128                 databaseTester.cleanDatabase();
129                 clearUserCache();
130                 databaseTester
131                     .executeTransaction(new TestTransactionCallbackWithoutResult() {
132                         public void execute() throws Exception {
133                             LOG.info("Running test " + method.getName());
134
135                             try {
136                                 method
137                                     .invoke(HibernateUserAdministrationTest.this);
138                             } catch (Throwable t) {
139                                 LOG.error("Test " + method.getName() +
140                                     " failed");
141                                 throw new RuntimeException(t.getMessage(), t);
142                             } finally {
143                                 LOG.info("Test " + method.getName() +
144                                     " finished");
145                             }
146                         }
147                     });
148             }
149         }
150     }
151
152     private void clearUserCache() {
153         userCache.clear();
154     }
155 }