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 org.apache.log4j.Logger;
20 import org.hibernate.annotations.AccessType;
21 import org.hibernate.cfg.Configuration;
23 import org.hibernate.dialect.MySQL5Dialect;
24 import org.hibernate.dialect.MySQL5InnoDBDialect;
26 import org.hibernate.tool.hbm2ddl.SchemaExport;
28 import org.springframework.orm.hibernate3.HibernateTemplate;
30 import org.wamblee.general.BeanKernel;
32 import org.wamblee.security.authorization.AuthorizationService;
33 import org.wamblee.security.authorization.AuthorizationServiceTest;
34 import org.wamblee.security.authorization.TestUserAccessor;
36 import org.wamblee.system.adapters.ClassConfiguration;
37 import org.wamblee.system.adapters.ClassConfigurationTest;
38 import org.wamblee.system.adapters.DefaultContainer;
39 import org.wamblee.system.adapters.ObjectConfiguration;
40 import org.wamblee.system.components.DatabaseComponentFactory;
41 import org.wamblee.system.core.Scope;
42 import org.wamblee.system.spring.component.DatabaseTesterComponent;
43 import org.wamblee.system.spring.component.DatasourceComponent;
45 import org.wamblee.usermgt.UserAccessor;
46 import org.wamblee.usermgt.hibernate.AuthorizationComponent;
47 import org.wamblee.usermgt.hibernate.HibernateUserAdministrationTest;
48 import org.wamblee.usermgt.hibernate.UserAdministrationComponent;
50 import java.sql.SQLException;
53 * Unit test for the persistent authorization service.
55 * @author Erik Brakkee
58 public class PersistentAuthorizationServiceTest extends
59 AuthorizationServiceTest {
60 private static final Logger LOGGER = Logger
61 .getLogger(PersistentAuthorizationServiceTest.class);
63 private static final String SERVICE_TABLE = "AUTHORIZATION_SERVICE";
65 private static final String RULES_TABLE = "AUTHORIZATION_RULES";
67 private static final String SERVICE_RULES_TABLE = "AUTHORIZATION_SERVICE_RULES";
69 private static final String OPERATIONCOND_TABLE = "OPERATION_CONDITIONS";
71 private static final String PATHCOND_TABLE = "PATH_CONDITIONS";
73 private static final String USERCOND_TABLE = "USER_CONDITIONS";
75 private DefaultContainer container;
79 private DatabaseTesterComponent databaseTester;
81 private UserAccessor userAccessor;
83 private HibernateTemplate hibernateTemplate;
85 private AuthorizationService authorizationService;
88 protected void setUp() throws Exception {
89 container = new DefaultContainer("top");
90 DatabaseComponentFactory.addDatabaseConfig(container);
91 container.addComponent(new DatasourceComponent("datasource"));
93 ClassConfiguration useraccessorConfig = new ClassConfiguration(
94 TestUserAccessor.class);
95 useraccessorConfig.getObjectConfig().getSetterConfig().initAllSetters();
96 container.addComponent("userAccessor", useraccessorConfig);
98 .addComponent(new AuthorizationComponent("authorization", true));
100 ClassConfiguration dbtesterConfig = new ClassConfiguration(
101 DatabaseTesterComponent.class);
102 dbtesterConfig.getObjectConfig().getSetterConfig().initAllSetters();
103 container.addComponent("databaseTester", dbtesterConfig);
105 ObjectConfiguration config = new ObjectConfiguration(
106 PersistentAuthorizationServiceTest.class);
107 config.getSetterConfig().clear().add("setUserAccessor").add(
108 "setDatabaseTester").add("setHibernateTemplate").add(
109 "setAuthorizationService");
110 container.addComponent("testcase", this, config);
112 scope = container.start();
114 databaseTester.cleanDatabase();
119 public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
120 databaseTester = aDatabaseTester;
123 public void setUserAccessor(UserAccessor aUserAccessor) {
124 userAccessor = aUserAccessor;
127 public void setHibernateTemplate(HibernateTemplate aHibernateTemplate) {
128 hibernateTemplate = aHibernateTemplate;
131 public void setAuthorizationService(
132 AuthorizationService aAuthorizationService) {
133 authorizationService = aAuthorizationService;
140 * org.wamblee.security.authorization.AuthorizationServiceTest#createService
144 protected AuthorizationService createService() {
145 PersistentAuthorizationService service = new PersistentAuthorizationService(
146 "DEFAULT", hibernateTemplate, createUserAccessor(), 10000);
155 * org.wamblee.security.authorization.AuthorizationServiceTest#checkRuleCount
159 protected void checkRuleCount(int aCount) {
161 assertEquals(1, databaseTester.getTableSize(SERVICE_TABLE));
162 assertEquals(aCount, databaseTester.getTableSize(RULES_TABLE));
163 assertEquals(aCount, databaseTester
164 .getTableSize(SERVICE_RULES_TABLE));
165 assertEquals(aCount, databaseTester.getTableSize(USERCOND_TABLE));
166 assertEquals(aCount, databaseTester.getTableSize(PATHCOND_TABLE));
167 assertEquals(aCount, databaseTester
168 .getTableSize(OPERATIONCOND_TABLE));
169 } catch (SQLException e) {
170 throw new RuntimeException(e);
174 public void testSchemaExport() {
175 Configuration config = new Configuration();
177 for (String mappingFile : new AuthorizationMappingFiles()) {
178 config.addResource(mappingFile);
181 config.setProperty("hibernate.dialect", MySQL5InnoDBDialect.class
184 SchemaExport exporter = new SchemaExport(config);
185 exporter.setOutputFile("target/mysql5.schema.sql");
186 exporter.create(true, false);
189 public void testPerformance() {
190 PersistentAuthorizationService service = (PersistentAuthorizationService) getService();
193 long time = System.currentTimeMillis();
195 for (int i = 0; i < n; i++) {
196 testFirstRuleGrants();
198 testSecondRuleDenies();
200 testThirdRuleGrants();
202 testNoRulesSupportResource();
205 LOGGER.info("Executed " + (4 * n) + " authorization checks in " +
206 ((float) (System.currentTimeMillis() - time) / (float) 1000) +