2 * Copyright 2005-2010 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 java.sql.SQLException;
20 import org.apache.log4j.Logger;
21 import org.hibernate.cfg.Configuration;
22 import org.hibernate.dialect.MySQL5InnoDBDialect;
23 import org.hibernate.tool.hbm2ddl.SchemaExport;
24 import org.springframework.orm.hibernate3.HibernateTemplate;
25 import org.wamblee.security.authorization.AuthorizationService;
26 import org.wamblee.security.authorization.AuthorizationServiceTest;
27 import org.wamblee.security.authorization.TestUserAccessor;
28 import org.wamblee.system.adapters.ClassConfiguration;
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;
39 * Unit test for the persistent authorization service.
41 * @author Erik Brakkee
44 public class PersistentAuthorizationServiceTest extends
45 AuthorizationServiceTest {
46 private static final Logger LOGGER = Logger
47 .getLogger(PersistentAuthorizationServiceTest.class);
49 private static final String SERVICE_TABLE = "AUTHORIZATION_SERVICE";
51 private static final String RULES_TABLE = "AUTHORIZATION_RULES";
53 private static final String SERVICE_RULES_TABLE = "AUTHORIZATION_SERVICE_RULES";
55 private static final String OPERATIONCOND_TABLE = "OPERATION_CONDITIONS";
57 private static final String PATHCOND_TABLE = "PATH_CONDITIONS";
59 private static final String USERCOND_TABLE = "USER_CONDITIONS";
61 private DefaultContainer container;
65 private DatabaseTesterComponent databaseTester;
67 private UserAccessor userAccessor;
69 private HibernateTemplate hibernateTemplate;
71 private AuthorizationService authorizationService;
74 protected void setUp() throws Exception {
75 container = new DefaultContainer("top");
76 DatabaseComponentFactory.addDatabaseConfig(container);
77 container.addComponent(new DatasourceComponent("datasource"));
79 ClassConfiguration useraccessorConfig = new ClassConfiguration(
80 TestUserAccessor.class);
81 useraccessorConfig.getObjectConfig().getSetterConfig().initAllSetters();
82 container.addComponent("userAccessor", useraccessorConfig);
84 .addComponent(new AuthorizationComponent("authorization", true));
86 ClassConfiguration dbtesterConfig = new ClassConfiguration(
87 DatabaseTesterComponent.class);
88 dbtesterConfig.getObjectConfig().getSetterConfig().initAllSetters();
89 container.addComponent("databaseTester", dbtesterConfig);
91 ObjectConfiguration config = new ObjectConfiguration(
92 PersistentAuthorizationServiceTest.class);
93 config.getSetterConfig().clear().add("setUserAccessor").add(
94 "setDatabaseTester").add("setHibernateTemplate").add(
95 "setAuthorizationService");
96 container.addComponent("testcase", this, config);
98 scope = container.start();
100 databaseTester.cleanDatabase();
105 public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
106 databaseTester = aDatabaseTester;
109 public void setUserAccessor(UserAccessor aUserAccessor) {
110 userAccessor = aUserAccessor;
113 public void setHibernateTemplate(HibernateTemplate aHibernateTemplate) {
114 hibernateTemplate = aHibernateTemplate;
117 public void setAuthorizationService(
118 AuthorizationService aAuthorizationService) {
119 authorizationService = aAuthorizationService;
126 * org.wamblee.security.authorization.AuthorizationServiceTest#createService
130 protected AuthorizationService createService() {
131 PersistentAuthorizationService service = new PersistentAuthorizationService(
132 "DEFAULT", hibernateTemplate, createUserAccessor(), 10000);
141 * org.wamblee.security.authorization.AuthorizationServiceTest#checkRuleCount
145 protected void checkRuleCount(int aCount) {
147 assertEquals(1, databaseTester.getTableSize(SERVICE_TABLE));
148 assertEquals(aCount, databaseTester.getTableSize(RULES_TABLE));
149 assertEquals(aCount, databaseTester
150 .getTableSize(SERVICE_RULES_TABLE));
151 assertEquals(aCount, databaseTester.getTableSize(USERCOND_TABLE));
152 assertEquals(aCount, databaseTester.getTableSize(PATHCOND_TABLE));
153 assertEquals(aCount, databaseTester
154 .getTableSize(OPERATIONCOND_TABLE));
155 } catch (SQLException e) {
156 throw new RuntimeException(e);
160 public void testSchemaExport() {
161 Configuration config = new Configuration();
163 for (String mappingFile : new AuthorizationMappingFiles()) {
164 config.addResource(mappingFile);
167 config.setProperty("hibernate.dialect", MySQL5InnoDBDialect.class
170 SchemaExport exporter = new SchemaExport(config);
171 exporter.setOutputFile("target/mysql5.schema.sql");
172 exporter.create(true, false);
175 public void testPerformance() {
176 PersistentAuthorizationService service = (PersistentAuthorizationService) getService();
179 long time = System.currentTimeMillis();
181 for (int i = 0; i < n; i++) {
182 testFirstRuleGrants();
184 testSecondRuleDenies();
186 testThirdRuleGrants();
188 testNoRulesSupportResource();
191 LOGGER.info("Executed " + (4 * n) + " authorization checks in " +
192 ((float) (System.currentTimeMillis() - time) / (float) 1000) +