From 3617674e6a5458435d060e5b80506f16d70f83d2 Mon Sep 17 00:00:00 2001 From: erik Date: Fri, 30 Apr 2010 22:05:51 +0000 Subject: [PATCH] --- .../jpa/JpaUserAdministrationTest.java | 143 ++++++++++ .../wamblee/usermgt/jpa/JpaUserSetTest.java | 269 ++++++++++++++++++ 2 files changed, 412 insertions(+) create mode 100644 security/jpatest/src/test/java/org/wamblee/usermgt/jpa/JpaUserAdministrationTest.java create mode 100644 security/jpatest/src/test/java/org/wamblee/usermgt/jpa/JpaUserSetTest.java diff --git a/security/jpatest/src/test/java/org/wamblee/usermgt/jpa/JpaUserAdministrationTest.java b/security/jpatest/src/test/java/org/wamblee/usermgt/jpa/JpaUserAdministrationTest.java new file mode 100644 index 00000000..a0c0eb1a --- /dev/null +++ b/security/jpatest/src/test/java/org/wamblee/usermgt/jpa/JpaUserAdministrationTest.java @@ -0,0 +1,143 @@ +/* + * 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.usermgt.jpa; + +import java.lang.reflect.Method; +import java.sql.Connection; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wamblee.cache.EhCache; +import org.wamblee.io.ClassPathResource; +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; +import org.wamblee.usermgt.GroupSet; +import org.wamblee.usermgt.NameValidator; +import org.wamblee.usermgt.RegexpNameValidator; +import org.wamblee.usermgt.User; +import org.wamblee.usermgt.UserAdministration; +import org.wamblee.usermgt.UserAdministrationImpl; +import org.wamblee.usermgt.UserAdministrationImplTest; +import org.wamblee.usermgt.UserSet; + +/** + * User administration tests with persistence based on Hibernate. This executes + * the same test cases as {@link org.wamblee.usermgt.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 JpaTester jpaTester; + + private EhCache userCache; + + private UserAdministration userAdmin; + + /* + * (non-Javadoc) + * + * @see org.wamblee.usermgt.UserAdministrationImplTest#setUp() + */ + @Override + protected void setUp() throws Exception { + jpaTester = new JpaTester(new SecurityPersistenceUnit()); + jpaTester.start(); + + userCache = new EhCache(new ClassPathResource( + "properties/org.wamblee.security.ehcache.xml"), "users"); + + TransactionProxyFactory factory = new TransactionProxyFactory( + jpaTester.getJpaBuilder(), UserAdministration.class); + + NameValidator passwordValidator = new RegexpNameValidator(".{5,}", + "INVALID_PASSWORD", "Password must have at least 5 characters"); + MessageDigester passwordDigester = new Md5HexMessageDigester(); + UserSet userset = new JpaUserSet(userCache, passwordValidator, + passwordDigester, factory.getTransactionScopedEntityManager()); + GroupSet groupset = new JpaGroupSet(factory + .getTransactionScopedEntityManager()); + + NameValidator userValidator = new RegexpNameValidator( + "[a-zA-Z]+[a-zA-Z0-9]*", "INVALID_USERNAME", ""); + NameValidator groupValidator = new RegexpNameValidator( + "[a-zA-Z]+[a-zA-Z0-9]*", "INVALID_GROUPNAME", ""); + UserAdministration userAdminImpl = new UserAdministrationImpl(userset, + groupset, userValidator, groupValidator); + userAdmin = factory.getProxy(userAdminImpl); + + super.setUp(); + clearUserCache(); + } + + @Override + protected void tearDown() throws Exception { + jpaTester.stop(); + super.tearDown(); + } + + /* + * (non-Javadoc) + * + * @see org.wamblee.usermgt.UserAdministrationImplTest#createAdmin() + */ + @Override + protected UserAdministration createAdmin() { + return userAdmin; + } + + public void testAllTestsInASeparateTransaction() throws Exception { + Method[] methods = UserAdministrationImplTest.class.getMethods(); + + for (final Method method : methods) { + if (method.getName().startsWith("test")) { + jpaTester.getDbUtils().cleanDatabase(); + clearUserCache(); + jpaTester.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() { + userCache.clear(); + } +} diff --git a/security/jpatest/src/test/java/org/wamblee/usermgt/jpa/JpaUserSetTest.java b/security/jpatest/src/test/java/org/wamblee/usermgt/jpa/JpaUserSetTest.java new file mode 100644 index 00000000..795bf597 --- /dev/null +++ b/security/jpatest/src/test/java/org/wamblee/usermgt/jpa/JpaUserSetTest.java @@ -0,0 +1,269 @@ +/* + * 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.usermgt.jpa; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.util.Set; + +import org.wamblee.cache.EhCache; +import org.wamblee.io.ClassPathResource; +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; +import org.wamblee.usermgt.Group; +import org.wamblee.usermgt.GroupSet; +import org.wamblee.usermgt.InMemoryUserSetTest; +import org.wamblee.usermgt.NameValidator; +import org.wamblee.usermgt.RegexpNameValidator; +import org.wamblee.usermgt.User; +import org.wamblee.usermgt.UserSet; + +/** + * Tests for {@link org.wamblee.usermgt.hibernate.HibernateGroupSet} + * + * @author Erik Brakkee + */ +public class JpaUserSetTest extends InMemoryUserSetTest { + private static final String USER_TABLE = "SEC_USER"; + + private static final String GROUP_TABLE = "SEC_GROUP"; + + private static final String USER_QUERY = "select * from " + USER_TABLE + + " where name = ?"; + + private static final String GROUP_QUERY = "select * from " + GROUP_TABLE + + " where name = ?"; + + private UserSet userset; + + private GroupSet groupset; + + private EhCache userCache; + + private JpaTester jpaTester; + + /* + * (non-Javadoc) + * + * @see org.wamblee.usermgt.InMemoryUserSetTest#setUp() + */ + @Override + protected void setUp() throws Exception { + jpaTester = new JpaTester(new SecurityPersistenceUnit()); + jpaTester.start(); + + userCache = new EhCache( + new ClassPathResource("properties/org.wamblee.security.ehcache.xml"), "users"); + + userset = createUserSetImpl(); + groupset = createGroupSetImpl(); + + clearUserCache(); + + super.setUp(); + } + + private UserSet createUserSetImpl() { + NameValidator passwordValidator = new RegexpNameValidator( + ".{5,}", "INVALID_PASSWORD", "Password must have at least 5 characters"); + + MessageDigester passwordDigester = new Md5HexMessageDigester(); + TransactionProxyFactory factory = new TransactionProxyFactory( + jpaTester.getJpaBuilder(), UserSet.class); + UserSet jpaUserset = new JpaUserSet(userCache, passwordValidator, passwordDigester, + factory.getTransactionScopedEntityManager()); + return factory.getProxy(jpaUserset); + } + + private GroupSet createGroupSetImpl() { + TransactionProxyFactory factory = new TransactionProxyFactory( + jpaTester.getJpaBuilder(), GroupSet.class); + GroupSet groupset = new JpaGroupSet(factory.getTransactionScopedEntityManager()); + GroupSet proxy = factory.getProxy(groupset); + return proxy; + } + + @Override + protected void tearDown() throws Exception { + jpaTester.stop(); + super.tearDown(); + } + + /** + * Clears the user cache. + */ + private void clearUserCache() { + userCache.clear(); + } + + /* + * (non-Javadoc) + * + * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupCount(int) + */ + @Override + protected void checkUserCount(int aSize) throws Exception { + super.checkUserCount(aSize); + assertEquals(aSize, jpaTester.getDbUtils().getTableSize(USER_TABLE)); + } + + /* + * (non-Javadoc) + * + * @see + * org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupExists(java.lang.String + * ) + */ + @Override + protected void checkUserExists(final String aUser) throws Exception { + assertEquals(1, countUser(aUser)); + } + + private int countUser(final String aUser) throws Exception { + int count = jpaTester.getDbUtils().executeInTransaction(new JdbcUnitOfWork() { + @Override + public Integer execute(Connection aConnection) throws Exception { + ResultSet res = jpaTester.getDbUtils().executeQuery(aConnection, USER_QUERY, aUser); + return jpaTester.getDbUtils().countResultSet(res); + } + }); + return count; + } + + /* + * (non-Javadoc) + * + * @see + * org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupNotExists(java.lang + * .String) + */ + @Override + protected void checkUserNotExists(String aUser) throws Exception { + assertEquals(0, countUser(aUser)); + } + + /* + * (non-Javadoc) + * + * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupCount(int) + */ + @Override + protected void checkGroupCount(int aSize) throws Exception { + assertEquals(aSize, jpaTester.getDbUtils().getTableSize(GROUP_TABLE)); + } + + private int countGroup(final String aGroup) throws Exception { + int count = jpaTester.getDbUtils().executeInTransaction(new JdbcUnitOfWork() { + @Override + public Integer execute(Connection aConnection) throws Exception { + ResultSet res = jpaTester.getDbUtils().executeQuery(aConnection, GROUP_QUERY, aGroup); + return jpaTester.getDbUtils().countResultSet(res); + } + }); + return count; + } + + /* + * (non-Javadoc) + * + * @see + * org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupExists(java.lang.String + * ) + */ + @Override + protected void checkGroupExists(String aGroup) throws Exception { + assertEquals(1, countGroup(aGroup)); + } + + /* + * (non-Javadoc) + * + * @see + * org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupNotExists(java.lang + * .String) + */ + @Override + protected void checkGroupNotExists(String aGroup) throws Exception { + assertEquals(0, countGroup(aGroup)); + } + + /* + * (non-Javadoc) + * + * @see org.wamblee.usermgt.InMemoryGroupSetTest#createGroupSet() + */ + @Override + protected UserSet createUserSet() { + return userset; + } + + /* + * (non-Javadoc) + * + * @see org.wamblee.usermgt.InMemoryUserSetTest#createGroupSet() + */ + @Override + protected GroupSet createGroupSet() { + return groupset; + } + + /** + * Reproduction of a bug. Create a user which is in group1 Add it to a + * second group group2. Remove the user from group1. Verify the user is in + * group2. + * + */ + public void testVerifyAddRemove() throws Exception { + jpaTester.getDbUtils().cleanDatabase(); // super class setup always creates one group. + + GroupSet groups = getGroups(); + assertEquals(0, groups.size()); + + Group group1 = createGroup("group1"); + Group group2 = createGroup("group2"); + groups.add(group1); + groups.add(group2); + checkGroupExists("group1"); + checkGroupExists("group2"); + + User user = createUser("user", PASSWORD, group1); + getUsers().add(user); + checkUserExists("user"); + + addUserToGroup(user, group2); + getUsers().userModified(user); + clearUserCache(); + + User user2 = getUsers().find("user"); + + Set userGroups = user2.getGroups(); + assertTrue(user2.isInGroup("group1")); + assertTrue(user2.isInGroup("group2")); + assertEquals(2, userGroups.size()); + + removeUserFromGroup(user, group1); + getUsers().userModified(user); + clearUserCache(); + user2 = getUsers().find("user"); + userGroups = user2.getGroups(); + assertFalse(user2.isInGroup("group1")); + assertTrue(user2.isInGroup("group2")); + assertEquals(1, userGroups.size()); + } +} -- 2.31.1