package rename for test libraries.
[utils] / security / jpatest / src / test / java / org / wamblee / security / authentication / jpa / JpaUserAdministrationTest.java
1 /*
2  * Copyright 2005-2010 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.security.authentication.jpa;
17
18 import java.lang.reflect.Method;
19 import java.sql.Connection;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.wamblee.security.authentication.UserAdministration;
24 import org.wamblee.security.authentication.UserAdministrationImplTest;
25 import org.wamblee.test.persistence.DatabaseUtils.JdbcUnitOfWork;
26
27 /**
28  * User administration tests with persistence based on Hibernate. This executes
29  * the same test cases as {@link org.wamblee.security.authentication.UserAdministrationImplTest}
30  * with in addition, one test case that executes all Hibernate test cases
31  * separately with each test case in its own transaction.
32  * 
33  * @author Erik Brakkee
34  */
35 public class JpaUserAdministrationTest extends UserAdministrationImplTest {
36     private static final Log LOG = LogFactory
37         .getLog(JpaUserAdministrationTest.class);
38
39     private UserAdministrationTester userAdminTester; 
40
41     /*
42      * (non-Javadoc)
43      * 
44      * @see org.wamblee.usermgt.UserAdministrationImplTest#setUp()
45      */
46     @Override
47     protected void setUp() throws Exception {
48         userAdminTester = new UserAdministrationTester(); 
49         userAdminTester.start();
50         super.setUp();
51         clearUserCache();
52     }
53
54     @Override
55     protected void tearDown() throws Exception {
56         userAdminTester.stop();
57         super.tearDown();
58     }
59
60     /*
61      * (non-Javadoc)
62      * 
63      * @see org.wamblee.usermgt.UserAdministrationImplTest#createAdmin()
64      */
65     @Override
66     protected UserAdministration createAdmin() {
67         return userAdminTester.getUserAdministration();
68     }
69     
70     public void testAllTestsInASeparateTransaction() throws Exception {
71         Method[] methods = UserAdministrationImplTest.class.getMethods();
72
73         for (final Method method : methods) {
74             if (method.getName().startsWith("test")) {
75                 userAdminTester.getJpaTester().getDbUtils().cleanDatabase(new SecurityTables());
76                 clearUserCache();
77                 userAdminTester.getJpaTester().getDbUtils().executeInTransaction(
78                     new JdbcUnitOfWork<Void>() {
79                         @Override
80                         public Void execute(Connection aConnection)
81                             throws Exception {
82                             LOG.info("Running test " + method.getName());
83
84                             try {
85                                 method.invoke(JpaUserAdministrationTest.this);
86                             } catch (Throwable t) {
87                                 LOG.error("Test " + method.getName() +
88                                     " failed");
89                                 throw new RuntimeException(t.getMessage(), t);
90                             } finally {
91                                 LOG.info("Test " + method.getName() +
92                                     " finished");
93                             }
94                             return null;
95                         }
96                     });
97             }
98         }
99     }
100
101     private void clearUserCache() {
102         userAdminTester.getUserCache().clear();
103     }
104 }