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.junit.Ignore;
24 import org.wamblee.cache.EhCache;
25 import org.wamblee.io.ClassPathResource;
26 import org.wamblee.security.encryption.Md5HexMessageDigester;
27 import org.wamblee.security.encryption.MessageDigester;
28 import org.wamblee.support.persistence.JpaTester;
29 import org.wamblee.support.persistence.TransactionProxyFactory;
30 import org.wamblee.support.persistence.DatabaseUtils.JdbcUnitOfWork;
31 import org.wamblee.usermgt.GroupSet;
32 import org.wamblee.usermgt.NameValidator;
33 import org.wamblee.usermgt.RegexpNameValidator;
34 import org.wamblee.usermgt.User;
35 import org.wamblee.usermgt.UserAdministration;
36 import org.wamblee.usermgt.UserAdministrationImpl;
37 import org.wamblee.usermgt.UserAdministrationImplTest;
38 import org.wamblee.usermgt.UserSet;
41 * User administration tests with persistence based on Hibernate. This executes
42 * the same test cases as {@link org.wamblee.usermgt.UserAdministrationImplTest}
43 * with in addition, one test case that executes all Hibernate test cases
44 * separately with each test case in its own transaction.
46 * @author Erik Brakkee
48 public class JpaUserAdministrationTest extends UserAdministrationImplTest {
49 private static final Log LOG = LogFactory
50 .getLog(JpaUserAdministrationTest.class);
52 private JpaTester jpaTester;
54 private EhCache<String, User> userCache;
56 private UserAdministration userAdmin;
61 * @see org.wamblee.usermgt.UserAdministrationImplTest#setUp()
64 protected void setUp() throws Exception {
65 jpaTester = new JpaTester(new SecurityPersistenceUnit());
68 userCache = new EhCache<String, User>(new ClassPathResource(
69 "properties/org.wamblee.security.ehcache.xml"), "users");
71 TransactionProxyFactory<UserAdministration> factory = new TransactionProxyFactory<UserAdministration>(
72 jpaTester.getJpaBuilder(), UserAdministration.class);
74 NameValidator passwordValidator = new RegexpNameValidator(".{5,}",
75 "INVALID_PASSWORD", "Password must have at least 5 characters");
76 MessageDigester passwordDigester = new Md5HexMessageDigester();
77 UserSet userset = new JpaUserSet(userCache, passwordValidator,
78 passwordDigester, factory.getTransactionScopedEntityManager());
79 GroupSet groupset = new JpaGroupSet(factory
80 .getTransactionScopedEntityManager());
82 NameValidator userValidator = new RegexpNameValidator(
83 "[a-zA-Z]+[a-zA-Z0-9]*", "INVALID_USERNAME", "");
84 NameValidator groupValidator = new RegexpNameValidator(
85 "[a-zA-Z]+[a-zA-Z0-9]*", "INVALID_GROUPNAME", "");
86 UserAdministration userAdminImpl = new UserAdministrationImpl(userset,
87 groupset, userValidator, groupValidator);
88 userAdmin = factory.getProxy(userAdminImpl);
95 protected void tearDown() throws Exception {
103 * @see org.wamblee.usermgt.UserAdministrationImplTest#createAdmin()
106 protected UserAdministration createAdmin() {
110 public void testAllTestsInASeparateTransaction() throws Exception {
111 Method[] methods = UserAdministrationImplTest.class.getMethods();
113 for (final Method method : methods) {
114 if (method.getName().startsWith("test")) {
115 jpaTester.getDbUtils().cleanDatabase();
117 jpaTester.getDbUtils().executeInTransaction(
118 new JdbcUnitOfWork<Void>() {
120 public Void execute(Connection aConnection)
122 LOG.info("Running test " + method.getName());
125 method.invoke(JpaUserAdministrationTest.this);
126 } catch (Throwable t) {
127 LOG.error("Test " + method.getName() +
129 throw new RuntimeException(t.getMessage(), t);
131 LOG.info("Test " + method.getName() +
141 private void clearUserCache() {