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;
53 * Unit test for the persistent authorization service.
55 * @author Erik Brakkee
57 public class PersistentAuthorizationServiceTest extends AuthorizationServiceTest {
61 private static final Logger LOGGER = Logger.getLogger(PersistentAuthorizationServiceTest.class);
66 private static final String SERVICE_TABLE = "AUTHORIZATION_SERVICE";
71 private static final String RULES_TABLE = "AUTHORIZATION_RULES";
76 private static final String SERVICE_RULES_TABLE = "AUTHORIZATION_SERVICE_RULES";
81 private static final String OPERATIONCOND_TABLE = "OPERATION_CONDITIONS";
86 private static final String PATHCOND_TABLE = "PATH_CONDITIONS";
91 private static final String USERCOND_TABLE = "USER_CONDITIONS";
96 private DefaultContainer container;
106 private DatabaseTesterComponent databaseTester;
111 private UserAccessor userAccessor;
116 private HibernateTemplate hibernateTemplate;
121 private AuthorizationService authorizationService;
126 * @throws Exception DOCUMENT ME!
129 protected void setUp() throws Exception {
130 container = new DefaultContainer("top");
131 DatabaseComponentFactory.addDatabaseConfig(container);
132 container.addComponent(new DatasourceComponent("datasource"));
134 ClassConfiguration useraccessorConfig = new ClassConfiguration(TestUserAccessor.class);
135 useraccessorConfig.getObjectConfig().getSetterConfig().initAllSetters();
136 container.addComponent("userAccessor", useraccessorConfig);
137 container.addComponent(new AuthorizationComponent("authorization", true));
139 ClassConfiguration dbtesterConfig = new ClassConfiguration(DatabaseTesterComponent.class);
140 dbtesterConfig.getObjectConfig().getSetterConfig().initAllSetters();
141 container.addComponent("databaseTester", dbtesterConfig);
143 ObjectConfiguration config = new ObjectConfiguration(PersistentAuthorizationServiceTest.class);
144 config.getSetterConfig().clear().add("setUserAccessor")
145 .add("setDatabaseTester").add("setHibernateTemplate")
146 .add("setAuthorizationService");
147 container.addComponent("testcase", this, config);
149 scope = container.start();
151 databaseTester.cleanDatabase();
159 * @param aDatabaseTester DOCUMENT ME!
161 public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
162 databaseTester = aDatabaseTester;
168 * @param aUserAccessor DOCUMENT ME!
170 public void setUserAccessor(UserAccessor aUserAccessor) {
171 userAccessor = aUserAccessor;
177 * @param aHibernateTemplate DOCUMENT ME!
179 public void setHibernateTemplate(HibernateTemplate aHibernateTemplate) {
180 hibernateTemplate = aHibernateTemplate;
186 * @param aAuthorizationService DOCUMENT ME!
188 public void setAuthorizationService(
189 AuthorizationService aAuthorizationService) {
190 authorizationService = aAuthorizationService;
197 * org.wamblee.security.authorization.AuthorizationServiceTest#createService
203 * @return DOCUMENT ME!
206 protected AuthorizationService createService() {
207 PersistentAuthorizationService service = new PersistentAuthorizationService("DEFAULT",
208 hibernateTemplate, createUserAccessor(), 10000);
217 * org.wamblee.security.authorization.AuthorizationServiceTest#checkRuleCount
223 * @param aCount DOCUMENT ME!
225 * @throws RuntimeException DOCUMENT ME!
228 protected void checkRuleCount(int aCount) {
230 assertEquals(1, databaseTester.getTableSize(SERVICE_TABLE));
231 assertEquals(aCount, databaseTester.getTableSize(RULES_TABLE));
233 databaseTester.getTableSize(SERVICE_RULES_TABLE));
234 assertEquals(aCount, databaseTester.getTableSize(USERCOND_TABLE));
235 assertEquals(aCount, databaseTester.getTableSize(PATHCOND_TABLE));
237 databaseTester.getTableSize(OPERATIONCOND_TABLE));
238 } catch (SQLException e) {
239 throw new RuntimeException(e);
246 public void testSchemaExport() {
247 Configuration config = new Configuration();
249 for (String mappingFile : new AuthorizationMappingFiles()) {
250 config.addResource(mappingFile);
253 config.setProperty("hibernate.dialect",
254 MySQL5InnoDBDialect.class.getName());
256 SchemaExport exporter = new SchemaExport(config);
257 exporter.setOutputFile("target/mysql5.schema.sql");
258 exporter.create(true, false);
264 public void testPerformance() {
265 PersistentAuthorizationService service = (PersistentAuthorizationService) getService();
268 long time = System.currentTimeMillis();
270 for (int i = 0; i < n; i++) {
271 testFirstRuleGrants();
273 testSecondRuleDenies();
275 testThirdRuleGrants();
277 testNoRulesSupportResource();
280 LOGGER.info("Executed " + (4 * n) + " authorization checks in "
281 + ((float) (System.currentTimeMillis() - time) / (float) 1000)