d62fbdd6bfafbc702df836cfb7c21d43ed501161
[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 /**
44  * User administration tests with persistence based on Hibernate. This
45  * executes the same test cases as {@link
46  * org.wamblee.usermgt.UserAdministrationImplTest} with in addition, one test
47  * case that executes all Hibernate test cases separately with each test case
48  * in its own transaction.
49  *
50  * @author Erik Brakkee
51  */
52 public class HibernateUserAdministrationTest extends UserAdministrationImplTest {
53     /**
54      * DOCUMENT ME!
55      */
56     private static final Log LOG = LogFactory.getLog(HibernateUserAdministrationTest.class);
57
58     /**
59      * DOCUMENT ME!
60      */
61     private DefaultContainer container;
62
63     /**
64      * DOCUMENT ME!
65      */
66     private Scope scope;
67
68     /**
69      * DOCUMENT ME!
70      */
71     private DatabaseTesterComponent databaseTester;
72
73     /**
74      * DOCUMENT ME!
75      */
76     private EhCache<Serializable, Serializable> userCache;
77
78     /**
79      * DOCUMENT ME!
80      */
81     private UserAdministration userAdmin;
82
83     /* (non-Javadoc)
84      * @see org.wamblee.usermgt.UserAdministrationImplTest#setUp()
85      */
86     /**
87      * DOCUMENT ME!
88      *
89      * @throws Exception DOCUMENT ME!
90      */
91     @Override
92     protected void setUp() throws Exception {
93         container = new DefaultContainer("top");
94         DatabaseComponentFactory.addDatabaseConfig(container);
95         container.addComponent(new DatasourceComponent("datasource"));
96         container.addComponent(new UserAdministrationComponent("admin", true));
97
98         ClassConfiguration dbtesterConfig = new ClassConfiguration(DatabaseTesterComponent.class);
99         dbtesterConfig.getObjectConfig().getSetterConfig().initAllSetters();
100         container.addComponent("databaseTester", dbtesterConfig);
101
102         ObjectConfiguration config = new ObjectConfiguration(HibernateUserAdministrationTest.class);
103         config.getSetterConfig().clear().add("setUserCache")
104         .add("setDatabaseTester").add("setUserAdmin");
105         container.addComponent("testcase", this, config);
106
107         scope = container.start();
108
109         databaseTester.cleanDatabase();
110
111         super.setUp();
112         clearUserCache();
113     }
114
115     /**
116      * DOCUMENT ME!
117      *
118      * @param aUserCache DOCUMENT ME!
119      */
120     public void setUserCache(EhCache<Serializable, Serializable> aUserCache) {
121         userCache = aUserCache;
122     }
123
124     /**
125      * DOCUMENT ME!
126      *
127      * @param aDatabaseTester DOCUMENT ME!
128      */
129     public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
130         databaseTester = aDatabaseTester;
131     }
132
133     /**
134      * DOCUMENT ME!
135      *
136      * @param aUserAdmin DOCUMENT ME!
137      */
138     public void setUserAdmin(UserAdministration aUserAdmin) {
139         userAdmin = aUserAdmin;
140     }
141
142     /**
143      * DOCUMENT ME!
144      *
145      * @throws Exception DOCUMENT ME!
146      */
147     @Override
148     protected void tearDown() throws Exception {
149         container.stop(scope);
150         super.tearDown();
151     }
152
153     /*
154      * (non-Javadoc)
155      *
156      * @see org.wamblee.usermgt.UserAdministrationImplTest#createAdmin()
157      */
158     /**
159      * DOCUMENT ME!
160      *
161      * @return DOCUMENT ME!
162      */
163     @Override
164     protected UserAdministration createAdmin() {
165         return userAdmin;
166     }
167
168     /**
169      * DOCUMENT ME!
170      *
171      * @throws SQLException DOCUMENT ME!
172      * @throws RuntimeException DOCUMENT ME!
173      */
174     public void testAllTestsInASeparateTransaction() throws SQLException {
175         Method[] methods = UserAdministrationImplTest.class.getMethods();
176
177         for (final Method method : methods) {
178             if (method.getName().startsWith("test")) {
179                 databaseTester.cleanDatabase();
180                 clearUserCache();
181                 databaseTester.executeTransaction(new TestTransactionCallbackWithoutResult() {
182                         public void execute() throws Exception {
183                             LOG.info("Running test " + method.getName());
184
185                             try {
186                                 method.invoke(HibernateUserAdministrationTest.this);
187                             } catch (Throwable t) {
188                                 LOG.error("Test " + method.getName()
189                                     + " failed");
190                                 throw new RuntimeException(t.getMessage(), t);
191                             } finally {
192                                 LOG.info("Test " + method.getName()
193                                     + " finished");
194                             }
195                         }
196                     });
197             }
198         }
199     }
200
201     /**
202      * 
203      */
204     private void clearUserCache() {
205         userCache.clear();
206     }
207 }