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.core.Scope;
30 import org.wamblee.system.spring.DatabaseTesterComponent;
31 import org.wamblee.usermgt.UserAccessor;
32 import org.wamblee.usermgt.hibernate.AuthorizationComponent;
33 import org.wamblee.usermgt.hibernate.ExternalDatasourceComponent;
34 import org.wamblee.usermgt.hibernate.HibernateUserAdministrationTest;
35 import org.wamblee.usermgt.hibernate.UserAdministrationComponent;
38 * Unit test for the persistent authorization service.
40 * @author Erik Brakkee
42 public class PersistentAuthorizationServiceTest extends
43 AuthorizationServiceTest {
45 private static final Logger LOGGER = Logger
46 .getLogger(PersistentAuthorizationServiceTest.class);
48 private static final String SERVICE_TABLE = "AUTHORIZATION_SERVICE";
49 private static final String RULES_TABLE = "AUTHORIZATION_RULES";
50 private static final String SERVICE_RULES_TABLE = "AUTHORIZATION_SERVICE_RULES";
51 private static final String OPERATIONCOND_TABLE = "OPERATION_CONDITIONS";
52 private static final String PATHCOND_TABLE = "PATH_CONDITIONS";
53 private static final String USERCOND_TABLE = "USER_CONDITIONS";
55 private DefaultContainer _container;
58 private DatabaseTesterComponent _databaseTester;
59 private UserAccessor _userAccessor;
60 private HibernateTemplate _hibernateTemplate;
61 private AuthorizationService _authorizationService;
64 protected void setUp() throws Exception {
66 _container = new DefaultContainer("top");
67 _container.addComponent(new ExternalDatasourceComponent("datasource"));
68 _container.addComponent("userAccessor", TestUserAccessor.class);
69 _container.addComponent(new AuthorizationComponent("authorization",
73 .addComponent("databaseTester", DatabaseTesterComponent.class);
75 ObjectConfiguration config = new ObjectConfiguration(
76 PersistentAuthorizationServiceTest.class);
77 config.getSetterConfig().clear().add("userAccessor").add(
78 "databaseTester").add("hibernateTemplate").add(
79 "authorizationService");
80 _container.addComponent("testcase", this, config);
82 _scope = _container.start();
84 _databaseTester.cleanDatabase();
89 public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
90 _databaseTester = aDatabaseTester;
93 public void setUserAccessor(UserAccessor aUserAccessor) {
94 _userAccessor = aUserAccessor;
97 public void setHibernateTemplate(HibernateTemplate aHibernateTemplate) {
98 _hibernateTemplate = aHibernateTemplate;
101 public void setAuthorizationService(
102 AuthorizationService aAuthorizationService) {
103 _authorizationService = aAuthorizationService;
109 * @see org.wamblee.security.authorization.AuthorizationServiceTest#createService()
112 protected AuthorizationService createService() {
113 PersistentAuthorizationService service = new PersistentAuthorizationService(
114 "DEFAULT", _hibernateTemplate, createUserAccessor(), 10000);
121 * @see org.wamblee.security.authorization.AuthorizationServiceTest#checkRuleCount(int)
124 protected void checkRuleCount(int aCount) {
126 assertEquals(1, _databaseTester.getTableSize(SERVICE_TABLE));
127 assertEquals(aCount, _databaseTester.getTableSize(RULES_TABLE));
128 assertEquals(aCount, _databaseTester.getTableSize(SERVICE_RULES_TABLE));
129 assertEquals(aCount, _databaseTester.getTableSize(USERCOND_TABLE));
130 assertEquals(aCount, _databaseTester.getTableSize(PATHCOND_TABLE));
131 assertEquals(aCount, _databaseTester.getTableSize(OPERATIONCOND_TABLE));
132 } catch (SQLException e) {
133 throw new RuntimeException(e);
138 public void testPerformance() {
140 PersistentAuthorizationService service = (PersistentAuthorizationService) getService();
143 long time = System.currentTimeMillis();
144 for (int i = 0; i < n; i++) {
145 testFirstRuleGrants();
147 testSecondRuleDenies();
149 testThirdRuleGrants();
151 testNoRulesSupportResource();
153 LOGGER.info("Executed " + 4 * n + " authorization checks in "
154 + (float) (System.currentTimeMillis() - time) / (float) 1000