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.DefaultContainer;
28 import org.wamblee.system.adapters.ObjectConfiguration;
29 import org.wamblee.system.components.DatabaseComponentFactory;
30 import org.wamblee.system.core.Scope;
31 import org.wamblee.system.spring.component.DatabaseTesterComponent;
32 import org.wamblee.system.spring.component.DatasourceComponent;
33 import org.wamblee.usermgt.UserAccessor;
34 import org.wamblee.usermgt.hibernate.AuthorizationComponent;
35 import org.wamblee.usermgt.hibernate.HibernateUserAdministrationTest;
36 import org.wamblee.usermgt.hibernate.UserAdministrationComponent;
39 * Unit test for the persistent authorization service.
41 * @author Erik Brakkee
43 public class PersistentAuthorizationServiceTest extends
44 AuthorizationServiceTest {
46 private static final Logger LOGGER = Logger
47 .getLogger(PersistentAuthorizationServiceTest.class);
49 private static final String SERVICE_TABLE = "AUTHORIZATION_SERVICE";
50 private static final String RULES_TABLE = "AUTHORIZATION_RULES";
51 private static final String SERVICE_RULES_TABLE = "AUTHORIZATION_SERVICE_RULES";
52 private static final String OPERATIONCOND_TABLE = "OPERATION_CONDITIONS";
53 private static final String PATHCOND_TABLE = "PATH_CONDITIONS";
54 private static final String USERCOND_TABLE = "USER_CONDITIONS";
56 private DefaultContainer _container;
59 private DatabaseTesterComponent _databaseTester;
60 private UserAccessor _userAccessor;
61 private HibernateTemplate _hibernateTemplate;
62 private AuthorizationService _authorizationService;
65 protected void setUp() throws Exception {
67 _container = new DefaultContainer("top");
68 DatabaseComponentFactory.addDatabaseConfig(_container);
69 _container.addComponent(new DatasourceComponent("datasource"));
70 _container.addComponent("userAccessor", TestUserAccessor.class);
71 _container.addComponent(new AuthorizationComponent("authorization",
75 .addComponent("databaseTester", DatabaseTesterComponent.class);
77 ObjectConfiguration config = new ObjectConfiguration(
78 PersistentAuthorizationServiceTest.class);
79 config.getSetterConfig().clear().add("userAccessor").add(
80 "databaseTester").add("hibernateTemplate").add(
81 "authorizationService");
82 _container.addComponent("testcase", this, config);
84 _scope = _container.start();
86 _databaseTester.cleanDatabase();
91 public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
92 _databaseTester = aDatabaseTester;
95 public void setUserAccessor(UserAccessor aUserAccessor) {
96 _userAccessor = aUserAccessor;
99 public void setHibernateTemplate(HibernateTemplate aHibernateTemplate) {
100 _hibernateTemplate = aHibernateTemplate;
103 public void setAuthorizationService(
104 AuthorizationService aAuthorizationService) {
105 _authorizationService = aAuthorizationService;
111 * @see org.wamblee.security.authorization.AuthorizationServiceTest#createService()
114 protected AuthorizationService createService() {
115 PersistentAuthorizationService service = new PersistentAuthorizationService(
116 "DEFAULT", _hibernateTemplate, createUserAccessor(), 10000);
123 * @see org.wamblee.security.authorization.AuthorizationServiceTest#checkRuleCount(int)
126 protected void checkRuleCount(int aCount) {
128 assertEquals(1, _databaseTester.getTableSize(SERVICE_TABLE));
129 assertEquals(aCount, _databaseTester.getTableSize(RULES_TABLE));
130 assertEquals(aCount, _databaseTester.getTableSize(SERVICE_RULES_TABLE));
131 assertEquals(aCount, _databaseTester.getTableSize(USERCOND_TABLE));
132 assertEquals(aCount, _databaseTester.getTableSize(PATHCOND_TABLE));
133 assertEquals(aCount, _databaseTester.getTableSize(OPERATIONCOND_TABLE));
134 } catch (SQLException e) {
135 throw new RuntimeException(e);
140 public void testPerformance() {
142 PersistentAuthorizationService service = (PersistentAuthorizationService) getService();
145 long time = System.currentTimeMillis();
146 for (int i = 0; i < n; i++) {
147 testFirstRuleGrants();
149 testSecondRuleDenies();
151 testThirdRuleGrants();
153 testNoRulesSupportResource();
155 LOGGER.info("Executed " + 4 * n + " authorization checks in "
156 + (float) (System.currentTimeMillis() - time) / (float) 1000