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.jpa;
18 import java.lang.reflect.Method;
19 import java.sql.Connection;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.wamblee.cache.EhCache;
24 import org.wamblee.io.ClassPathResource;
25 import org.wamblee.security.encryption.Md5HexMessageDigester;
26 import org.wamblee.security.encryption.MessageDigester;
27 import org.wamblee.support.persistence.JpaTester;
28 import org.wamblee.support.persistence.TransactionProxyFactory;
29 import org.wamblee.support.persistence.DatabaseUtils.JdbcUnitOfWork;
30 import org.wamblee.usermgt.GroupSet;
31 import org.wamblee.usermgt.NameValidator;
32 import org.wamblee.usermgt.RegexpNameValidator;
33 import org.wamblee.usermgt.User;
34 import org.wamblee.usermgt.UserAdministration;
35 import org.wamblee.usermgt.UserAdministrationImpl;
36 import org.wamblee.usermgt.UserAdministrationImplTest;
37 import org.wamblee.usermgt.UserSet;
40 * User administration tests with persistence based on Hibernate. This executes
41 * the same test cases as {@link org.wamblee.usermgt.UserAdministrationImplTest}
42 * with in addition, one test case that executes all Hibernate test cases
43 * separately with each test case in its own transaction.
45 * @author Erik Brakkee
47 public class JpaUserAdministrationTest extends UserAdministrationImplTest {
48 private static final Log LOG = LogFactory
49 .getLog(JpaUserAdministrationTest.class);
51 private JpaTester jpaTester;
53 private EhCache<String, User> userCache;
55 private UserAdministration userAdmin;
60 * @see org.wamblee.usermgt.UserAdministrationImplTest#setUp()
63 protected void setUp() throws Exception {
64 jpaTester = new JpaTester(new SecurityPersistenceUnit());
67 userCache = new EhCache<String, User>(new ClassPathResource(
68 "properties/org.wamblee.security.ehcache.xml"), "users");
70 TransactionProxyFactory<UserAdministration> factory = new TransactionProxyFactory<UserAdministration>(
71 jpaTester.getJpaBuilder(), UserAdministration.class);
73 NameValidator passwordValidator = new RegexpNameValidator(".{5,}",
74 "INVALID_PASSWORD", "Password must have at least 5 characters");
75 MessageDigester passwordDigester = new Md5HexMessageDigester();
76 UserSet userset = new JpaUserSet(userCache, passwordValidator,
77 passwordDigester, factory.getTransactionScopedEntityManager());
78 GroupSet groupset = new JpaGroupSet(factory
79 .getTransactionScopedEntityManager());
81 NameValidator userValidator = new RegexpNameValidator(
82 "[a-zA-Z]+[a-zA-Z0-9]*", "INVALID_USERNAME", "");
83 NameValidator groupValidator = new RegexpNameValidator(
84 "[a-zA-Z]+[a-zA-Z0-9]*", "INVALID_GROUPNAME", "");
85 UserAdministration userAdminImpl = new UserAdministrationImpl(userset,
86 groupset, userValidator, groupValidator);
87 userAdmin = factory.getProxy(userAdminImpl);
94 protected void tearDown() throws Exception {
102 * @see org.wamblee.usermgt.UserAdministrationImplTest#createAdmin()
105 protected UserAdministration createAdmin() {
109 public void testAllTestsInASeparateTransaction() throws Exception {
110 Method[] methods = UserAdministrationImplTest.class.getMethods();
112 for (final Method method : methods) {
113 if (method.getName().startsWith("test")) {
114 jpaTester.getDbUtils().cleanDatabase();
116 jpaTester.getDbUtils().executeInTransaction(
117 new JdbcUnitOfWork<Void>() {
119 public Void execute(Connection aConnection)
121 LOG.info("Running test " + method.getName());
124 method.invoke(JpaUserAdministrationTest.this);
125 } catch (Throwable t) {
126 LOG.error("Test " + method.getName() +
128 throw new RuntimeException(t.getMessage(), t);
130 LOG.info("Test " + method.getName() +
140 private void clearUserCache() {