From fb7a92c206c85b6714d498bc7406db49fbec37a6 Mon Sep 17 00:00:00 2001 From: erik Date: Tue, 11 May 2010 08:33:18 +0000 Subject: [PATCH] --- .../jpa/JpaUserAdministrationTest.java | 43 ++------- .../jpa/UserAdministrationTester.java | 92 +++++++++++++++++++ 2 files changed, 100 insertions(+), 35 deletions(-) create mode 100644 security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/UserAdministrationTester.java 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 2e95c823..8eaa8bec 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 @@ -51,11 +51,7 @@ public class JpaUserAdministrationTest extends UserAdministrationImplTest { private static final Log LOG = LogFactory .getLog(JpaUserAdministrationTest.class); - private JpaTester jpaTester; - - private EhCache userCache; - - private UserAdministration userAdmin; + private UserAdministrationTester userAdminTester; /* * (non-Javadoc) @@ -64,38 +60,15 @@ public class JpaUserAdministrationTest extends UserAdministrationImplTest { */ @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); - + userAdminTester = new UserAdministrationTester(); + userAdminTester.start(); super.setUp(); clearUserCache(); } @Override protected void tearDown() throws Exception { - jpaTester.stop(); + userAdminTester.stop(); super.tearDown(); } @@ -106,7 +79,7 @@ public class JpaUserAdministrationTest extends UserAdministrationImplTest { */ @Override protected UserAdministration createAdmin() { - return userAdmin; + return userAdminTester.getUserAdministration(); } public void testAllTestsInASeparateTransaction() throws Exception { @@ -114,9 +87,9 @@ public class JpaUserAdministrationTest extends UserAdministrationImplTest { for (final Method method : methods) { if (method.getName().startsWith("test")) { - jpaTester.getDbUtils().cleanDatabase(); + userAdminTester.getJpaTester().getDbUtils().cleanDatabase(); clearUserCache(); - jpaTester.getDbUtils().executeInTransaction( + userAdminTester.getJpaTester().getDbUtils().executeInTransaction( new JdbcUnitOfWork() { @Override public Void execute(Connection aConnection) @@ -141,6 +114,6 @@ public class JpaUserAdministrationTest extends UserAdministrationImplTest { } private void clearUserCache() { - userCache.clear(); + userAdminTester.getUserCache().clear(); } } 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 new file mode 100644 index 00000000..366ec6a9 --- /dev/null +++ b/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/UserAdministrationTester.java @@ -0,0 +1,92 @@ +/* + * Copyright 2008-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 org.wamblee.cache.Cache; +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.UserSet; +import org.wamblee.security.encryption.Md5HexMessageDigester; +import org.wamblee.security.encryption.MessageDigester; +import org.wamblee.support.persistence.JpaTester; +import org.wamblee.support.persistence.TransactionProxyFactory; + +/** + * Setup of a security repository for unit test. This provides all the necessary wiring + * and JPA setup. + * + * @author Erik Brakkee + */ +public class UserAdministrationTester { + + private JpaTester jpaTester; + private Cache userCache; + private UserAdministration userAdmin; + + public UserAdministrationTester() { + jpaTester = new JpaTester(new SecurityPersistenceUnit()); + } + + public void start() throws Exception { + 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); + } + + + public JpaTester getJpaTester() { + return jpaTester; + } + + public Cache getUserCache() { + return userCache; + } + + public UserAdministration getUserAdministration() { + return userAdmin; + } + + public void stop() throws Exception { + jpaTester.stop(); + } + +} -- 2.31.1