From 564daa4c8e8bbe8f6ef5cea480a78138bf59c865 Mon Sep 17 00:00:00 2001 From: erik Date: Wed, 14 May 2008 19:14:22 +0000 Subject: [PATCH] PersistentAuthorizationTest is now componentized. --- .../hibernate/AuthorizationMappingFiles.java | 0 .../hibernate/AuthorizationComponent.java | 68 +++++++++ .../AuthorizationLightComponent.java | 66 +++++++++ .../AuthorizationServiceTest.java | 13 +- .../PersistentAuthorizationServiceTest.java | 136 +++++++++++++----- 5 files changed, 236 insertions(+), 47 deletions(-) rename security/src/{test => main}/java/org/wamblee/security/authorization/hibernate/AuthorizationMappingFiles.java (100%) create mode 100644 security/src/main/java/org/wamblee/usermgt/hibernate/AuthorizationComponent.java create mode 100644 security/src/main/java/org/wamblee/usermgt/hibernate/AuthorizationLightComponent.java diff --git a/security/src/test/java/org/wamblee/security/authorization/hibernate/AuthorizationMappingFiles.java b/security/src/main/java/org/wamblee/security/authorization/hibernate/AuthorizationMappingFiles.java similarity index 100% rename from security/src/test/java/org/wamblee/security/authorization/hibernate/AuthorizationMappingFiles.java rename to security/src/main/java/org/wamblee/security/authorization/hibernate/AuthorizationMappingFiles.java diff --git a/security/src/main/java/org/wamblee/usermgt/hibernate/AuthorizationComponent.java b/security/src/main/java/org/wamblee/usermgt/hibernate/AuthorizationComponent.java new file mode 100644 index 00000000..1cb21bcf --- /dev/null +++ b/security/src/main/java/org/wamblee/usermgt/hibernate/AuthorizationComponent.java @@ -0,0 +1,68 @@ +/* + * Copyright 2008 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.hibernate; + +import java.io.IOException; + +import javax.sql.DataSource; + +import org.springframework.orm.hibernate3.HibernateTemplate; +import org.springframework.transaction.PlatformTransactionManager; +import org.wamblee.cache.EhCache; +import org.wamblee.security.authorization.AuthorizationService; +import org.wamblee.security.authorization.hibernate.AuthorizationMappingFiles; +import org.wamblee.system.adapters.DefaultContainer; +import org.wamblee.system.core.Component; +import org.wamblee.system.core.DefaultProvidedInterface; +import org.wamblee.system.core.DefaultRequiredInterface; +import org.wamblee.system.core.ProvidedInterface; +import org.wamblee.system.core.Scope; +import org.wamblee.usermgt.UserAccessor; +import org.wamblee.usermgt.UserAdministration; +import org.wamblee.usermgt.UserGroupRepositoryComponent; + +public class AuthorizationComponent extends DefaultContainer { + + private ProvidedInterface TRANSACTION_MGR = new DefaultProvidedInterface( + "transactionManager", PlatformTransactionManager.class); + private ProvidedInterface HIBERNATE_TEMPLATE = new DefaultProvidedInterface( + "hibernateTemplate", HibernateTemplate.class); + private ProvidedInterface AUTHORIZATION_SERVICE = new DefaultProvidedInterface( + "authorizationService", AuthorizationService.class); + + public AuthorizationComponent(String aName, boolean aExposeInternals) + throws IOException { + super(aName); + + addComponent("mappingFiles", new AuthorizationMappingFiles()); + + Component hibernate = new HibernateComponent("hibernate"); + addComponent(hibernate); + + Component authorization = new AuthorizationLightComponent("authorization"); + addComponent(authorization); + + addRequiredInterface(new DefaultRequiredInterface("datasource", DataSource.class)); + addRequiredInterface(new DefaultRequiredInterface("userAccessor", + UserAccessor.class)); + + if (aExposeInternals) { + addProvidedInterface(TRANSACTION_MGR); + addProvidedInterface(HIBERNATE_TEMPLATE); + } + addProvidedInterface(AUTHORIZATION_SERVICE); + } +} diff --git a/security/src/main/java/org/wamblee/usermgt/hibernate/AuthorizationLightComponent.java b/security/src/main/java/org/wamblee/usermgt/hibernate/AuthorizationLightComponent.java new file mode 100644 index 00000000..0825edd3 --- /dev/null +++ b/security/src/main/java/org/wamblee/usermgt/hibernate/AuthorizationLightComponent.java @@ -0,0 +1,66 @@ +/* + * Copyright 2008 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.hibernate; + +import java.util.HashMap; +import java.util.Map; + +import org.springframework.orm.hibernate3.HibernateTemplate; +import org.wamblee.security.authorization.AuthorizationService; +import org.wamblee.system.core.DefaultProvidedInterface; +import org.wamblee.system.core.DefaultRequiredInterface; +import org.wamblee.system.core.ProvidedInterface; +import org.wamblee.system.core.RequiredInterface; +import org.wamblee.system.spring.SpringComponent; +import org.wamblee.usermgt.GroupSet; +import org.wamblee.usermgt.UserAccessor; +import org.wamblee.usermgt.UserAdministration; +import org.wamblee.usermgt.UserSet; + +/** + * Light version of the user administration component that requires external + * datasource, and userset and group set components, as well as an external + * hibernate session factory. + * + * @author Erik Brakkee + * + */ +public class AuthorizationLightComponent extends SpringComponent { + + public AuthorizationLightComponent(String aName) { + super( + aName, + new String[] { "spring/test.org.wamblee.security.authorization.xml" }, + createProvided(), createRequired()); + } + + private static Map createRequired() { + Map required = new HashMap(); + required.put(new DefaultRequiredInterface("userArccessor", + UserAccessor.class), UserAccessor.class.getName()); + required.put(new DefaultRequiredInterface("hibernateTemplate", + HibernateTemplate.class), HibernateTemplate.class.getName()); + return required; + } + + private static Map createProvided() { + Map provided = new HashMap(); + provided.put(AuthorizationService.class.getName(), + new DefaultProvidedInterface(AuthorizationService.class + .getName(), AuthorizationService.class)); + return provided; + } +} diff --git a/security/src/test/java/org/wamblee/security/authorization/AuthorizationServiceTest.java b/security/src/test/java/org/wamblee/security/authorization/AuthorizationServiceTest.java index fd042bca..502000e6 100644 --- a/security/src/test/java/org/wamblee/security/authorization/AuthorizationServiceTest.java +++ b/security/src/test/java/org/wamblee/security/authorization/AuthorizationServiceTest.java @@ -18,6 +18,7 @@ package org.wamblee.security.authorization; import static org.wamblee.security.authorization.AuthorizationResult.DENIED; import static org.wamblee.security.authorization.AuthorizationResult.GRANTED; +import junit.framework.TestCase; import org.wamblee.persistence.hibernate.HibernateMappingFiles; import org.wamblee.test.spring.SpringTestCase; @@ -29,23 +30,13 @@ import org.wamblee.usermgt.UserAccessor; * * @author Erik Brakkee */ -public class AuthorizationServiceTest extends SpringTestCase { +public class AuthorizationServiceTest extends TestCase { private AuthorizationRule _rule1; private AuthorizationRule _rule2; private AuthorizationRule _rule3; private AuthorizationService _service; - - public AuthorizationServiceTest() { - super(SpringConfigFiles.class, HibernateMappingFiles.class); - } - - public AuthorizationServiceTest(ClassaSpringFiles, - Class aMappings) { - super(aSpringFiles, aMappings); - } - protected AuthorizationService getService() { return _service; } diff --git a/security/src/test/java/org/wamblee/security/authorization/hibernate/PersistentAuthorizationServiceTest.java b/security/src/test/java/org/wamblee/security/authorization/hibernate/PersistentAuthorizationServiceTest.java index 6cbbd39e..453cf1a9 100644 --- a/security/src/test/java/org/wamblee/security/authorization/hibernate/PersistentAuthorizationServiceTest.java +++ b/security/src/test/java/org/wamblee/security/authorization/hibernate/PersistentAuthorizationServiceTest.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.authorization.hibernate; @@ -23,71 +23,135 @@ import org.springframework.orm.hibernate3.HibernateTemplate; import org.wamblee.general.BeanKernel; import org.wamblee.security.authorization.AuthorizationService; import org.wamblee.security.authorization.AuthorizationServiceTest; +import org.wamblee.security.authorization.TestUserAccessor; +import org.wamblee.system.adapters.DefaultContainer; +import org.wamblee.system.adapters.ObjectConfiguration; +import org.wamblee.system.core.Scope; +import org.wamblee.system.spring.DatabaseTesterComponent; +import org.wamblee.usermgt.UserAccessor; +import org.wamblee.usermgt.hibernate.AuthorizationComponent; +import org.wamblee.usermgt.hibernate.ExternalDatasourceComponent; +import org.wamblee.usermgt.hibernate.HibernateUserAdministrationTest; +import org.wamblee.usermgt.hibernate.UserAdministrationComponent; /** - * Unit test for the persistent authorization service. - * + * Unit test for the persistent authorization service. + * * @author Erik Brakkee */ -public class PersistentAuthorizationServiceTest extends AuthorizationServiceTest { - - private static final Logger LOGGER = Logger.getLogger(PersistentAuthorizationServiceTest.class); - +public class PersistentAuthorizationServiceTest extends + AuthorizationServiceTest { + + private static final Logger LOGGER = Logger + .getLogger(PersistentAuthorizationServiceTest.class); + private static final String SERVICE_TABLE = "AUTHORIZATION_SERVICE"; - private static final String RULES_TABLE = "AUTHORIZATION_RULES"; + private static final String RULES_TABLE = "AUTHORIZATION_RULES"; private static final String SERVICE_RULES_TABLE = "AUTHORIZATION_SERVICE_RULES"; private static final String OPERATIONCOND_TABLE = "OPERATION_CONDITIONS"; private static final String PATHCOND_TABLE = "PATH_CONDITIONS"; private static final String USERCOND_TABLE = "USER_CONDITIONS"; - - public PersistentAuthorizationServiceTest() { - super(AuthorizationSpringConfigFiles.class, AuthorizationMappingFiles.class); + private DefaultContainer _container; + private Scope _scope; + + private DatabaseTesterComponent _databaseTester; + private UserAccessor _userAccessor; + private HibernateTemplate _hibernateTemplate; + private AuthorizationService _authorizationService; + + @Override + protected void setUp() throws Exception { + + _container = new DefaultContainer("top"); + _container.addComponent(new ExternalDatasourceComponent("datasource")); + _container.addComponent("userAccessor", TestUserAccessor.class); + _container.addComponent(new AuthorizationComponent("authorization", + true)); + + _container + .addComponent("databaseTester", DatabaseTesterComponent.class); + + ObjectConfiguration config = new ObjectConfiguration( + PersistentAuthorizationServiceTest.class); + config.getSetterConfig().clear().add("userAccessor").add( + "databaseTester").add("hibernateTemplate").add( + "authorizationService"); + _container.addComponent("testcase", this, config); + + _scope = _container.start(); + + _databaseTester.cleanDatabase(); + + super.setUp(); + } + + public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) { + _databaseTester = aDatabaseTester; + } + + public void setUserAccessor(UserAccessor aUserAccessor) { + _userAccessor = aUserAccessor; + } + + public void setHibernateTemplate(HibernateTemplate aHibernateTemplate) { + _hibernateTemplate = aHibernateTemplate; } - /* (non-Javadoc) + public void setAuthorizationService( + AuthorizationService aAuthorizationService) { + _authorizationService = aAuthorizationService; + } + + /* + * (non-Javadoc) + * * @see org.wamblee.security.authorization.AuthorizationServiceTest#createService() */ @Override protected AuthorizationService createService() { - PersistentAuthorizationService service = new PersistentAuthorizationService("DEFAULT", - BeanKernel.getBeanFactory().find(HibernateTemplate.class), createUserAccessor(), 10000); - return service; + PersistentAuthorizationService service = new PersistentAuthorizationService( + "DEFAULT", _hibernateTemplate, createUserAccessor(), 10000); + return service; } - - /* (non-Javadoc) + + /* + * (non-Javadoc) + * * @see org.wamblee.security.authorization.AuthorizationServiceTest#checkRuleCount(int) */ @Override protected void checkRuleCount(int aCount) { - try { - assertEquals(1, getTableSize(SERVICE_TABLE)); - assertEquals(aCount, getTableSize(RULES_TABLE)); - assertEquals(aCount, getTableSize(SERVICE_RULES_TABLE)); - assertEquals(aCount, getTableSize(USERCOND_TABLE)); - assertEquals(aCount, getTableSize(PATHCOND_TABLE)); - assertEquals(aCount, getTableSize(OPERATIONCOND_TABLE)); - } catch (SQLException e) { + try { + assertEquals(1, _databaseTester.getTableSize(SERVICE_TABLE)); + assertEquals(aCount, _databaseTester.getTableSize(RULES_TABLE)); + assertEquals(aCount, _databaseTester.getTableSize(SERVICE_RULES_TABLE)); + assertEquals(aCount, _databaseTester.getTableSize(USERCOND_TABLE)); + assertEquals(aCount, _databaseTester.getTableSize(PATHCOND_TABLE)); + assertEquals(aCount, _databaseTester.getTableSize(OPERATIONCOND_TABLE)); + } catch (SQLException e) { throw new RuntimeException(e); } - + } - - public void testPerformance() { - - PersistentAuthorizationService service = (PersistentAuthorizationService)getService(); - - int n = 1000; + + public void testPerformance() { + + PersistentAuthorizationService service = (PersistentAuthorizationService) getService(); + + int n = 1000; long time = System.currentTimeMillis(); - for (int i = 0; i < n; i++) { - testFirstRuleGrants(); + for (int i = 0; i < n; i++) { + testFirstRuleGrants(); resetTestRules(); testSecondRuleDenies(); resetTestRules(); testThirdRuleGrants(); - resetTestRules(); + resetTestRules(); testNoRulesSupportResource(); } - LOGGER.info("Executed " + 4*n + " authorization checks in " + (float)(System.currentTimeMillis()-time)/(float)1000 + " seconds."); + LOGGER.info("Executed " + 4 * n + " authorization checks in " + + (float) (System.currentTimeMillis() - time) / (float) 1000 + + " seconds."); } } -- 2.31.1