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.hibernate.cfg.Configuration;
23 import org.hibernate.dialect.MySQL5Dialect;
24 import org.hibernate.dialect.MySQL5InnoDBDialect;
25 import org.hibernate.tool.hbm2ddl.SchemaExport;
26 import org.springframework.orm.hibernate3.HibernateTemplate;
27 import org.wamblee.general.BeanKernel;
28 import org.wamblee.security.authorization.AuthorizationService;
29 import org.wamblee.security.authorization.AuthorizationServiceTest;
30 import org.wamblee.security.authorization.TestUserAccessor;
31 import org.wamblee.system.adapters.ClassConfiguration;
32 import org.wamblee.system.adapters.ClassConfigurationTest;
33 import org.wamblee.system.adapters.DefaultContainer;
34 import org.wamblee.system.adapters.ObjectConfiguration;
35 import org.wamblee.system.components.DatabaseComponentFactory;
36 import org.wamblee.system.core.Scope;
37 import org.wamblee.system.spring.component.DatabaseTesterComponent;
38 import org.wamblee.system.spring.component.DatasourceComponent;
39 import org.wamblee.usermgt.UserAccessor;
40 import org.wamblee.usermgt.hibernate.AuthorizationComponent;
41 import org.wamblee.usermgt.hibernate.HibernateUserAdministrationTest;
42 import org.wamblee.usermgt.hibernate.UserAdministrationComponent;
45 * Unit test for the persistent authorization service.
47 * @author Erik Brakkee
49 public class PersistentAuthorizationServiceTest extends
50 AuthorizationServiceTest {
52 private static final Logger LOGGER = Logger
53 .getLogger(PersistentAuthorizationServiceTest.class);
55 private static final String SERVICE_TABLE = "AUTHORIZATION_SERVICE";
56 private static final String RULES_TABLE = "AUTHORIZATION_RULES";
57 private static final String SERVICE_RULES_TABLE = "AUTHORIZATION_SERVICE_RULES";
58 private static final String OPERATIONCOND_TABLE = "OPERATION_CONDITIONS";
59 private static final String PATHCOND_TABLE = "PATH_CONDITIONS";
60 private static final String USERCOND_TABLE = "USER_CONDITIONS";
62 private DefaultContainer container;
65 private DatabaseTesterComponent databaseTester;
66 private UserAccessor userAccessor;
67 private HibernateTemplate hibernateTemplate;
68 private AuthorizationService authorizationService;
71 protected void setUp() throws Exception {
73 container = new DefaultContainer("top");
74 DatabaseComponentFactory.addDatabaseConfig(container);
75 container.addComponent(new DatasourceComponent("datasource"));
76 ClassConfiguration useraccessorConfig = new ClassConfiguration(
77 TestUserAccessor.class);
78 useraccessorConfig.getObjectConfig().getSetterConfig().initAllSetters();
79 container.addComponent("userAccessor", useraccessorConfig);
80 container.addComponent(new AuthorizationComponent("authorization",
83 ClassConfiguration dbtesterConfig = new ClassConfiguration(
84 DatabaseTesterComponent.class);
85 dbtesterConfig.getObjectConfig().getSetterConfig().initAllSetters();
86 container.addComponent("databaseTester", dbtesterConfig);
88 ObjectConfiguration config = new ObjectConfiguration(
89 PersistentAuthorizationServiceTest.class);
90 config.getSetterConfig().clear().add("setUserAccessor").add(
91 "setDatabaseTester").add("setHibernateTemplate").add(
92 "setAuthorizationService");
93 container.addComponent("testcase", this, config);
95 scope = container.start();
97 databaseTester.cleanDatabase();
102 public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
103 databaseTester = aDatabaseTester;
106 public void setUserAccessor(UserAccessor aUserAccessor) {
107 userAccessor = aUserAccessor;
110 public void setHibernateTemplate(HibernateTemplate aHibernateTemplate) {
111 hibernateTemplate = aHibernateTemplate;
114 public void setAuthorizationService(
115 AuthorizationService aAuthorizationService) {
116 authorizationService = aAuthorizationService;
123 * org.wamblee.security.authorization.AuthorizationServiceTest#createService
127 protected AuthorizationService createService() {
128 PersistentAuthorizationService service = new PersistentAuthorizationService(
129 "DEFAULT", hibernateTemplate, createUserAccessor(), 10000);
137 * org.wamblee.security.authorization.AuthorizationServiceTest#checkRuleCount
141 protected void checkRuleCount(int aCount) {
143 assertEquals(1, databaseTester.getTableSize(SERVICE_TABLE));
144 assertEquals(aCount, databaseTester.getTableSize(RULES_TABLE));
145 assertEquals(aCount, databaseTester
146 .getTableSize(SERVICE_RULES_TABLE));
147 assertEquals(aCount, databaseTester.getTableSize(USERCOND_TABLE));
148 assertEquals(aCount, databaseTester.getTableSize(PATHCOND_TABLE));
149 assertEquals(aCount, databaseTester
150 .getTableSize(OPERATIONCOND_TABLE));
151 } catch (SQLException e) {
152 throw new RuntimeException(e);
157 public void testSchemaExport() {
158 Configuration config = new Configuration();
159 for (String mappingFile: new AuthorizationMappingFiles()) {
160 config.addResource(mappingFile);
162 config.setProperty("hibernate.dialect", MySQL5InnoDBDialect.class.getName());
163 SchemaExport exporter = new SchemaExport(config);
164 exporter.setOutputFile("target/mysql5.schema.sql");
165 exporter.create(true,false);
168 public void testPerformance() {
170 PersistentAuthorizationService service = (PersistentAuthorizationService) getService();
173 long time = System.currentTimeMillis();
174 for (int i = 0; i < n; i++) {
175 testFirstRuleGrants();
177 testSecondRuleDenies();
179 testThirdRuleGrants();
181 testNoRulesSupportResource();
183 LOGGER.info("Executed " + 4 * n + " authorization checks in "
184 + (float) (System.currentTimeMillis() - time) / (float) 1000