/* * Copyright 2005-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.wamblee.security.authentication.jpa; import java.lang.reflect.Method; import java.sql.Connection; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.Ignore; import org.wamblee.cache.EhCache; import org.wamblee.io.ClassPathResource; import org.wamblee.security.authentication.GroupSet; import org.wamblee.security.authentication.NameValidator; import org.wamblee.security.authentication.RegexpNameValidator; import org.wamblee.security.authentication.User; import org.wamblee.security.authentication.UserAdministration; import org.wamblee.security.authentication.UserAdministrationImpl; import org.wamblee.security.authentication.UserAdministrationImplTest; import org.wamblee.security.authentication.UserSet; import org.wamblee.security.authentication.jpa.JpaGroupSet; import org.wamblee.security.authentication.jpa.JpaUserSet; import org.wamblee.security.encryption.Md5HexMessageDigester; import org.wamblee.security.encryption.MessageDigester; import org.wamblee.support.persistence.JpaTester; import org.wamblee.support.persistence.TransactionProxyFactory; import org.wamblee.support.persistence.DatabaseUtils.JdbcUnitOfWork; /** * User administration tests with persistence based on Hibernate. This executes * the same test cases as {@link org.wamblee.security.authentication.UserAdministrationImplTest} * with in addition, one test case that executes all Hibernate test cases * separately with each test case in its own transaction. * * @author Erik Brakkee */ public class JpaUserAdministrationTest extends UserAdministrationImplTest { private static final Log LOG = LogFactory .getLog(JpaUserAdministrationTest.class); private UserAdministrationTester userAdminTester; /* * (non-Javadoc) * * @see org.wamblee.usermgt.UserAdministrationImplTest#setUp() */ @Override protected void setUp() throws Exception { userAdminTester = new UserAdministrationTester(); userAdminTester.start(); super.setUp(); clearUserCache(); } @Override protected void tearDown() throws Exception { userAdminTester.stop(); super.tearDown(); } /* * (non-Javadoc) * * @see org.wamblee.usermgt.UserAdministrationImplTest#createAdmin() */ @Override protected UserAdministration createAdmin() { return userAdminTester.getUserAdministration(); } public void testAllTestsInASeparateTransaction() throws Exception { Method[] methods = UserAdministrationImplTest.class.getMethods(); for (final Method method : methods) { if (method.getName().startsWith("test")) { userAdminTester.getJpaTester().getDbUtils().cleanDatabase(); clearUserCache(); userAdminTester.getJpaTester().getDbUtils().executeInTransaction( new JdbcUnitOfWork() { @Override public Void execute(Connection aConnection) throws Exception { LOG.info("Running test " + method.getName()); try { method.invoke(JpaUserAdministrationTest.this); } catch (Throwable t) { LOG.error("Test " + method.getName() + " failed"); throw new RuntimeException(t.getMessage(), t); } finally { LOG.info("Test " + method.getName() + " finished"); } return null; } }); } } } private void clearUserCache() { userAdminTester.getUserCache().clear(); } }