--- /dev/null
+/*
+ * 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);
+ }
+}
--- /dev/null
+/*
+ * 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<RequiredInterface, String> createRequired() {
+ Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
+ 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<String, ProvidedInterface> createProvided() {
+ Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
+ provided.put(AuthorizationService.class.getName(),
+ new DefaultProvidedInterface(AuthorizationService.class
+ .getName(), AuthorizationService.class));
+ return provided;
+ }
+}
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;
*
* @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(Class<? extends SpringConfigFiles>aSpringFiles,
- Class<? extends HibernateMappingFiles> aMappings) {
- super(aSpringFiles, aMappings);
- }
-
protected AuthorizationService getService() {
return _service;
}
* 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;
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.");
}
}