(no commit message)
[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 import java.util.logging.Level;
21 import java.util.logging.Logger;
22
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
30  * {@link org.wamblee.security.authentication.UserAdministrationImplTest} with
31  * in addition, one test case that executes all Hibernate test cases separately
32  * with each test case in its own transaction.
33  * 
34  * @author Erik Brakkee
35  */
36 public class JpaUserAdministrationTest extends UserAdministrationImplTest {
37     private static final Logger LOG = Logger
38         .getLogger(JpaUserAdministrationTest.class.getName());
39
40     private UserAdministrationTester userAdminTester;
41
42     /*
43      * (non-Javadoc)
44      * 
45      * @see org.wamblee.usermgt.UserAdministrationImplTest#setUp()
46      */
47     @Override
48     protected void setUp() throws Exception {
49         userAdminTester = new UserAdministrationTester();
50         userAdminTester.start();
51         super.setUp();
52         clearUserCache();
53     }
54
55     @Override
56     protected void tearDown() throws Exception {
57         userAdminTester.stop();
58         super.tearDown();
59     }
60
61     /*
62      * (non-Javadoc)
63      * 
64      * @see org.wamblee.usermgt.UserAdministrationImplTest#createAdmin()
65      */
66     @Override
67     protected UserAdministration createAdmin() {
68         return userAdminTester.getUserAdministration();
69     }
70
71     public void testAllTestsInASeparateTransaction() throws Exception {
72         Method[] methods = UserAdministrationImplTest.class.getMethods();
73
74         for (final Method method : methods) {
75             if (method.getName().startsWith("test")) {
76                 userAdminTester.getJpaTester().getDbUtils().cleanDatabase(
77                     new SecurityTables());
78                 clearUserCache();
79                 userAdminTester.getJpaTester().getDbUtils()
80                     .executeInTransaction(new JdbcUnitOfWork<Void>() {
81                         @Override
82                         public Void execute(Connection aConnection)
83                             throws Exception {
84                             LOG.info("Running test " + method.getName());
85
86                             try {
87                                 method.invoke(JpaUserAdministrationTest.this);
88                             } catch (Throwable t) {
89                                 LOG.log(Level.WARNING, "Test " +
90                                     method.getName() + " failed", t);
91                                 throw new RuntimeException(t.getMessage(), t);
92                             } finally {
93                                 LOG.info("Test " + method.getName() +
94                                     " finished");
95                             }
96                             return null;
97                         }
98                     });
99             }
100         }
101     }
102
103     private void clearUserCache() {
104         userAdminTester.getUserCache().clear();
105     }
106 }