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.
16 package org.wamblee.security.authorization.hibernate;
18 import org.apache.log4j.Logger;
20 import org.hibernate.cfg.Configuration;
22 import org.hibernate.dialect.MySQL5Dialect;
23 import org.hibernate.dialect.MySQL5InnoDBDialect;
25 import org.hibernate.tool.hbm2ddl.SchemaExport;
27 import org.springframework.orm.hibernate3.HibernateTemplate;
29 import org.wamblee.general.BeanKernel;
31 import org.wamblee.security.authorization.AuthorizationService;
32 import org.wamblee.security.authorization.AuthorizationServiceTest;
33 import org.wamblee.security.authorization.TestUserAccessor;
35 import org.wamblee.system.adapters.ClassConfiguration;
36 import org.wamblee.system.adapters.ClassConfigurationTest;
37 import org.wamblee.system.adapters.DefaultContainer;
38 import org.wamblee.system.adapters.ObjectConfiguration;
39 import org.wamblee.system.components.DatabaseComponentFactory;
40 import org.wamblee.system.core.Scope;
41 import org.wamblee.system.spring.component.DatabaseTesterComponent;
42 import org.wamblee.system.spring.component.DatasourceComponent;
44 import org.wamblee.usermgt.UserAccessor;
45 import org.wamblee.usermgt.hibernate.AuthorizationComponent;
46 import org.wamblee.usermgt.hibernate.HibernateUserAdministrationTest;
47 import org.wamblee.usermgt.hibernate.UserAdministrationComponent;
49 import java.sql.SQLException;
52 * Unit test for the persistent authorization service.
54 * @author Erik Brakkee
56 public class PersistentAuthorizationServiceTest extends
57 AuthorizationServiceTest {
58 private static final Logger LOGGER = Logger
59 .getLogger(PersistentAuthorizationServiceTest.class);
61 private static final String SERVICE_TABLE = "AUTHORIZATION_SERVICE";
63 private static final String RULES_TABLE = "AUTHORIZATION_RULES";
65 private static final String SERVICE_RULES_TABLE = "AUTHORIZATION_SERVICE_RULES";
67 private static final String OPERATIONCOND_TABLE = "OPERATION_CONDITIONS";
69 private static final String PATHCOND_TABLE = "PATH_CONDITIONS";
71 private static final String USERCOND_TABLE = "USER_CONDITIONS";
73 private DefaultContainer container;
77 private DatabaseTesterComponent databaseTester;
79 private UserAccessor userAccessor;
81 private HibernateTemplate hibernateTemplate;
83 private AuthorizationService authorizationService;
86 protected void setUp() throws Exception {
87 container = new DefaultContainer("top");
88 DatabaseComponentFactory.addDatabaseConfig(container);
89 container.addComponent(new DatasourceComponent("datasource"));
91 ClassConfiguration useraccessorConfig = new ClassConfiguration(
92 TestUserAccessor.class);
93 useraccessorConfig.getObjectConfig().getSetterConfig().initAllSetters();
94 container.addComponent("userAccessor", useraccessorConfig);
96 .addComponent(new AuthorizationComponent("authorization", true));
98 ClassConfiguration dbtesterConfig = new ClassConfiguration(
99 DatabaseTesterComponent.class);
100 dbtesterConfig.getObjectConfig().getSetterConfig().initAllSetters();
101 container.addComponent("databaseTester", dbtesterConfig);
103 ObjectConfiguration config = new ObjectConfiguration(
104 PersistentAuthorizationServiceTest.class);
105 config.getSetterConfig().clear().add("setUserAccessor").add(
106 "setDatabaseTester").add("setHibernateTemplate").add(
107 "setAuthorizationService");
108 container.addComponent("testcase", this, config);
110 scope = container.start();
112 databaseTester.cleanDatabase();
117 public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
118 databaseTester = aDatabaseTester;
121 public void setUserAccessor(UserAccessor aUserAccessor) {
122 userAccessor = aUserAccessor;
125 public void setHibernateTemplate(HibernateTemplate aHibernateTemplate) {
126 hibernateTemplate = aHibernateTemplate;
129 public void setAuthorizationService(
130 AuthorizationService aAuthorizationService) {
131 authorizationService = aAuthorizationService;
138 * org.wamblee.security.authorization.AuthorizationServiceTest#createService
142 protected AuthorizationService createService() {
143 PersistentAuthorizationService service = new PersistentAuthorizationService(
144 "DEFAULT", hibernateTemplate, createUserAccessor(), 10000);
153 * org.wamblee.security.authorization.AuthorizationServiceTest#checkRuleCount
157 protected void checkRuleCount(int aCount) {
159 assertEquals(1, databaseTester.getTableSize(SERVICE_TABLE));
160 assertEquals(aCount, databaseTester.getTableSize(RULES_TABLE));
161 assertEquals(aCount, databaseTester
162 .getTableSize(SERVICE_RULES_TABLE));
163 assertEquals(aCount, databaseTester.getTableSize(USERCOND_TABLE));
164 assertEquals(aCount, databaseTester.getTableSize(PATHCOND_TABLE));
165 assertEquals(aCount, databaseTester
166 .getTableSize(OPERATIONCOND_TABLE));
167 } catch (SQLException e) {
168 throw new RuntimeException(e);
172 public void testSchemaExport() {
173 Configuration config = new Configuration();
175 for (String mappingFile : new AuthorizationMappingFiles()) {
176 config.addResource(mappingFile);
179 config.setProperty("hibernate.dialect", MySQL5InnoDBDialect.class
182 SchemaExport exporter = new SchemaExport(config);
183 exporter.setOutputFile("target/mysql5.schema.sql");
184 exporter.create(true, false);
187 public void testPerformance() {
188 PersistentAuthorizationService service = (PersistentAuthorizationService) getService();
191 long time = System.currentTimeMillis();
193 for (int i = 0; i < n; i++) {
194 testFirstRuleGrants();
196 testSecondRuleDenies();
198 testThirdRuleGrants();
200 testNoRulesSupportResource();
203 LOGGER.info("Executed " + (4 * n) + " authorization checks in " +
204 ((float) (System.currentTimeMillis() - time) / (float) 1000) +