2 * Copyright 2005 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.wamblee.security.authorization.hibernate;
19 import java.sql.SQLException;
21 import org.apache.log4j.Logger;
22 import org.springframework.orm.hibernate3.HibernateTemplate;
23 import org.wamblee.general.BeanKernel;
24 import org.wamblee.security.authorization.AuthorizationService;
25 import org.wamblee.security.authorization.AuthorizationServiceTest;
26 import org.wamblee.security.authorization.TestUserAccessor;
27 import org.wamblee.system.adapters.ClassConfiguration;
28 import org.wamblee.system.adapters.ClassConfigurationTest;
29 import org.wamblee.system.adapters.DefaultContainer;
30 import org.wamblee.system.adapters.ObjectConfiguration;
31 import org.wamblee.system.components.DatabaseComponentFactory;
32 import org.wamblee.system.core.Scope;
33 import org.wamblee.system.spring.component.DatabaseTesterComponent;
34 import org.wamblee.system.spring.component.DatasourceComponent;
35 import org.wamblee.usermgt.UserAccessor;
36 import org.wamblee.usermgt.hibernate.AuthorizationComponent;
37 import org.wamblee.usermgt.hibernate.HibernateUserAdministrationTest;
38 import org.wamblee.usermgt.hibernate.UserAdministrationComponent;
41 * Unit test for the persistent authorization service.
43 * @author Erik Brakkee
45 public class PersistentAuthorizationServiceTest extends
46 AuthorizationServiceTest {
48 private static final Logger LOGGER = Logger
49 .getLogger(PersistentAuthorizationServiceTest.class);
51 private static final String SERVICE_TABLE = "AUTHORIZATION_SERVICE";
52 private static final String RULES_TABLE = "AUTHORIZATION_RULES";
53 private static final String SERVICE_RULES_TABLE = "AUTHORIZATION_SERVICE_RULES";
54 private static final String OPERATIONCOND_TABLE = "OPERATION_CONDITIONS";
55 private static final String PATHCOND_TABLE = "PATH_CONDITIONS";
56 private static final String USERCOND_TABLE = "USER_CONDITIONS";
58 private DefaultContainer _container;
61 private DatabaseTesterComponent _databaseTester;
62 private UserAccessor _userAccessor;
63 private HibernateTemplate _hibernateTemplate;
64 private AuthorizationService _authorizationService;
67 protected void setUp() throws Exception {
69 _container = new DefaultContainer("top");
70 DatabaseComponentFactory.addDatabaseConfig(_container);
71 _container.addComponent(new DatasourceComponent("datasource"));
72 ClassConfiguration useraccessorConfig = new ClassConfiguration(TestUserAccessor.class);
73 useraccessorConfig.getObjectConfig().getSetterConfig().initAllSetters();
74 _container.addComponent("userAccessor", useraccessorConfig);
75 _container.addComponent(new AuthorizationComponent("authorization",
79 ClassConfiguration dbtesterConfig = new ClassConfiguration(DatabaseTesterComponent.class);
80 dbtesterConfig.getObjectConfig().getSetterConfig().initAllSetters();
82 .addComponent("databaseTester", dbtesterConfig);
84 ObjectConfiguration config = new ObjectConfiguration(
85 PersistentAuthorizationServiceTest.class);
86 config.getSetterConfig().clear().add("userAccessor").add(
87 "databaseTester").add("hibernateTemplate").add(
88 "authorizationService");
89 _container.addComponent("testcase", this, config);
91 _scope = _container.start();
93 _databaseTester.cleanDatabase();
98 public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
99 _databaseTester = aDatabaseTester;
102 public void setUserAccessor(UserAccessor aUserAccessor) {
103 _userAccessor = aUserAccessor;
106 public void setHibernateTemplate(HibernateTemplate aHibernateTemplate) {
107 _hibernateTemplate = aHibernateTemplate;
110 public void setAuthorizationService(
111 AuthorizationService aAuthorizationService) {
112 _authorizationService = aAuthorizationService;
118 * @see org.wamblee.security.authorization.AuthorizationServiceTest#createService()
121 protected AuthorizationService createService() {
122 PersistentAuthorizationService service = new PersistentAuthorizationService(
123 "DEFAULT", _hibernateTemplate, createUserAccessor(), 10000);
130 * @see org.wamblee.security.authorization.AuthorizationServiceTest#checkRuleCount(int)
133 protected void checkRuleCount(int aCount) {
135 assertEquals(1, _databaseTester.getTableSize(SERVICE_TABLE));
136 assertEquals(aCount, _databaseTester.getTableSize(RULES_TABLE));
137 assertEquals(aCount, _databaseTester.getTableSize(SERVICE_RULES_TABLE));
138 assertEquals(aCount, _databaseTester.getTableSize(USERCOND_TABLE));
139 assertEquals(aCount, _databaseTester.getTableSize(PATHCOND_TABLE));
140 assertEquals(aCount, _databaseTester.getTableSize(OPERATIONCOND_TABLE));
141 } catch (SQLException e) {
142 throw new RuntimeException(e);
147 public void testPerformance() {
149 PersistentAuthorizationService service = (PersistentAuthorizationService) getService();
152 long time = System.currentTimeMillis();
153 for (int i = 0; i < n; i++) {
154 testFirstRuleGrants();
156 testSecondRuleDenies();
158 testThirdRuleGrants();
160 testNoRulesSupportResource();
162 LOGGER.info("Executed " + 4 * n + " authorization checks in "
163 + (float) (System.currentTimeMillis() - time) / (float) 1000