From 7911867f17b475f221d93f106c3f1dfecd0b295e Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Sat, 31 Jul 2010 17:31:57 +0000 Subject: [PATCH] --- .../authentication/jpa/JpaGroupSetTest.java | 9 ++- .../jpa/JpaUserAdministrationTest.java | 24 +++--- .../authentication/jpa/JpaUserSetTest.java | 77 +++++++++++-------- .../jpa/SecurityPersistenceUnit.java | 2 +- .../jpa/UserAdministrationTester.java | 25 +++--- 5 files changed, 75 insertions(+), 62 deletions(-) diff --git a/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/JpaGroupSetTest.java b/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/JpaGroupSetTest.java index 6f791cdd..0c645ff6 100644 --- a/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/JpaGroupSetTest.java +++ b/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/JpaGroupSetTest.java @@ -11,7 +11,6 @@ import org.wamblee.test.persistence.JpaTester; import org.wamblee.test.persistence.DatabaseUtils.JdbcUnitOfWork; import org.wamblee.test.transactions.TransactionProxyFactory; - public class JpaGroupSetTest extends InMemoryGroupSetTest { private static final String GROUP_TABLE = "SEC_GROUP"; @@ -25,8 +24,9 @@ public class JpaGroupSetTest extends InMemoryGroupSetTest { public void setUp() throws Exception { jpaTester = new JpaTester(new SecurityPersistenceUnit()); jpaTester.start(); - - // Superclass setup will call createGroupSet so requires initialized JPA. + + // Superclass setup will call createGroupSet so requires initialized + // JPA. super.setUp(); } @@ -70,7 +70,8 @@ public class JpaGroupSetTest extends InMemoryGroupSetTest { protected GroupSet createGroupSet() { TransactionProxyFactory factory = new TransactionProxyFactory( jpaTester.getJpaBuilder(), GroupSet.class); - GroupSet groupset = new JpaGroupSet(factory.getTransactionScopedEntityManager()); + GroupSet groupset = new JpaGroupSet(factory + .getTransactionScopedEntityManager()); GroupSet proxy = factory.getProxy(groupset); return proxy; } diff --git a/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/JpaUserAdministrationTest.java b/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/JpaUserAdministrationTest.java index 548c83b3..f165b531 100644 --- a/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/JpaUserAdministrationTest.java +++ b/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/JpaUserAdministrationTest.java @@ -26,9 +26,10 @@ import org.wamblee.test.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. + * 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 */ @@ -36,7 +37,7 @@ public class JpaUserAdministrationTest extends UserAdministrationImplTest { private static final Logger LOG = Logger .getLogger(JpaUserAdministrationTest.class.getName()); - private UserAdministrationTester userAdminTester; + private UserAdministrationTester userAdminTester; /* * (non-Javadoc) @@ -45,7 +46,7 @@ public class JpaUserAdministrationTest extends UserAdministrationImplTest { */ @Override protected void setUp() throws Exception { - userAdminTester = new UserAdministrationTester(); + userAdminTester = new UserAdministrationTester(); userAdminTester.start(); super.setUp(); clearUserCache(); @@ -66,16 +67,17 @@ public class JpaUserAdministrationTest extends UserAdministrationImplTest { 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(new SecurityTables()); + userAdminTester.getJpaTester().getDbUtils().cleanDatabase( + new SecurityTables()); clearUserCache(); - userAdminTester.getJpaTester().getDbUtils().executeInTransaction( - new JdbcUnitOfWork() { + userAdminTester.getJpaTester().getDbUtils() + .executeInTransaction(new JdbcUnitOfWork() { @Override public Void execute(Connection aConnection) throws Exception { @@ -84,8 +86,8 @@ public class JpaUserAdministrationTest extends UserAdministrationImplTest { try { method.invoke(JpaUserAdministrationTest.this); } catch (Throwable t) { - LOG.log(Level.WARNING, "Test " + method.getName() + - " failed", t); + LOG.log(Level.WARNING, "Test " + + method.getName() + " failed", t); throw new RuntimeException(t.getMessage(), t); } finally { LOG.info("Test " + method.getName() + diff --git a/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/JpaUserSetTest.java b/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/JpaUserSetTest.java index 7c5274dd..99b61865 100644 --- a/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/JpaUserSetTest.java +++ b/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/JpaUserSetTest.java @@ -12,7 +12,7 @@ * 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.sql.Connection; @@ -56,7 +56,7 @@ public class JpaUserSetTest extends InMemoryUserSetTest { private EhCache userCache; - private JpaTester jpaTester; + private JpaTester jpaTester; /* * (non-Javadoc) @@ -68,33 +68,34 @@ public class JpaUserSetTest extends InMemoryUserSetTest { jpaTester = new JpaTester(new SecurityPersistenceUnit()); jpaTester.start(); - userCache = new EhCache( - new ClassPathResource("properties/org.wamblee.security.ehcache.xml"), "users"); - + 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"); - + 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()); + 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 groups = new JpaGroupSet(factory.getTransactionScopedEntityManager()); + GroupSet groups = new JpaGroupSet(factory + .getTransactionScopedEntityManager()); GroupSet proxy = factory.getProxy(groups); return proxy; } @@ -136,13 +137,15 @@ public class JpaUserSetTest extends InMemoryUserSetTest { } 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); - } - }); + 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; } @@ -167,15 +170,17 @@ public class JpaUserSetTest extends InMemoryUserSetTest { 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); - } - }); + 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; } @@ -230,8 +235,14 @@ public class JpaUserSetTest extends InMemoryUserSetTest { * */ public void testVerifyAddRemove() throws Exception { - jpaTester.getDbUtils().cleanDatabase(new SecurityTables()); // super class setup always creates one group. - + jpaTester.getDbUtils().cleanDatabase(new SecurityTables()); // super + // class + // setup + // always + // creates + // one + // group. + GroupSet groups = getGroups(); assertEquals(0, groups.size()); @@ -249,9 +260,9 @@ public class JpaUserSetTest extends InMemoryUserSetTest { addUserToGroup(user, group2); getUsers().userModified(user); clearUserCache(); - + User user2 = getUsers().find("user"); - + Set userGroups = user2.getGroups(); assertTrue(user2.isInGroup("group1")); assertTrue(user2.isInGroup("group2")); diff --git a/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/SecurityPersistenceUnit.java b/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/SecurityPersistenceUnit.java index cf6d4235..5ce850bd 100644 --- a/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/SecurityPersistenceUnit.java +++ b/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/SecurityPersistenceUnit.java @@ -4,7 +4,7 @@ import org.wamblee.test.persistence.PersistenceUnitDescription; public class SecurityPersistenceUnit extends PersistenceUnitDescription { - public SecurityPersistenceUnit() { + public SecurityPersistenceUnit() { super("jdbc/security", "securitytest"); } } diff --git a/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/UserAdministrationTester.java b/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/UserAdministrationTester.java index e11c97cf..c90afc8e 100644 --- a/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/UserAdministrationTester.java +++ b/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/UserAdministrationTester.java @@ -32,29 +32,28 @@ import org.wamblee.test.persistence.PersistenceUnitDescription; import org.wamblee.test.transactions.TransactionProxyFactory; /** - * Setup of a security repository for unit test. This provides all the necessary wiring - * and JPA setup. + * Setup of a security repository for unit test. This provides all the necessary + * wiring and JPA setup. * * @author Erik Brakkee */ public class UserAdministrationTester { - private PersistenceUnitDescription persistenceUnit; + private PersistenceUnitDescription persistenceUnit; private JpaTester jpaTester; private Cache userCache; - private UserAdministration userAdmin; + private UserAdministration userAdmin; private MessageDigester passwordDigester; public UserAdministrationTester() { - persistenceUnit = new SecurityPersistenceUnit(); + persistenceUnit = new SecurityPersistenceUnit(); jpaTester = new JpaTester(persistenceUnit); } public void start() throws Exception { jpaTester.start(); - userCache = new EhCache( - new ClassPathResource("properties/org.wamblee.security.ehcache.xml"), - "users"); + userCache = new EhCache(new ClassPathResource( + "properties/org.wamblee.security.ehcache.xml"), "users"); TransactionProxyFactory factory = new TransactionProxyFactory( jpaTester.getJpaBuilder(), UserAdministration.class); @@ -75,15 +74,15 @@ public class UserAdministrationTester { groupset, userValidator, groupValidator); userAdmin = factory.getProxy(userAdminImpl); } - + public PersistenceUnitDescription getPersistenceUnit() { return persistenceUnit; } - + public JpaTester getJpaTester() { return jpaTester; } - + public Cache getUserCache() { return userCache; } @@ -91,8 +90,8 @@ public class UserAdministrationTester { public UserAdministration getUserAdministration() { return userAdmin; } - - public MessageDigester getPasswordEncoder() { + + public MessageDigester getPasswordEncoder() { return passwordDigester; } -- 2.31.1