9ba63e3453037fe841bcb30bc351773e62cd2362
[utils] /
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */ 
16 package org.wamblee.security.authorization.hibernate;
17
18 import org.apache.log4j.Logger;
19
20 import org.hibernate.annotations.AccessType;
21 import org.hibernate.cfg.Configuration;
22
23 import org.hibernate.dialect.MySQL5Dialect;
24 import org.hibernate.dialect.MySQL5InnoDBDialect;
25
26 import org.hibernate.tool.hbm2ddl.SchemaExport;
27
28 import org.springframework.orm.hibernate3.HibernateTemplate;
29
30 import org.wamblee.general.BeanKernel;
31
32 import org.wamblee.security.authorization.AuthorizationService;
33 import org.wamblee.security.authorization.AuthorizationServiceTest;
34 import org.wamblee.security.authorization.TestUserAccessor;
35
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;
44
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;
49
50 import java.sql.SQLException;
51
52 /**
53  * Unit test for the persistent authorization service.
54  * 
55  * @author Erik Brakkee
56  */
57 @org.junit.Ignore
58 public class PersistentAuthorizationServiceTest extends
59     AuthorizationServiceTest {
60     private static final Logger LOGGER = Logger
61         .getLogger(PersistentAuthorizationServiceTest.class);
62
63     private static final String SERVICE_TABLE = "AUTHORIZATION_SERVICE";
64
65     private static final String RULES_TABLE = "AUTHORIZATION_RULES";
66
67     private static final String SERVICE_RULES_TABLE = "AUTHORIZATION_SERVICE_RULES";
68
69     private static final String OPERATIONCOND_TABLE = "OPERATION_CONDITIONS";
70
71     private static final String PATHCOND_TABLE = "PATH_CONDITIONS";
72
73     private static final String USERCOND_TABLE = "USER_CONDITIONS";
74
75     private DefaultContainer container;
76
77     private Scope scope;
78
79     private DatabaseTesterComponent databaseTester;
80
81     private UserAccessor userAccessor;
82
83     private HibernateTemplate hibernateTemplate;
84
85     private AuthorizationService authorizationService;
86
87     @Override
88     protected void setUp() throws Exception {
89         container = new DefaultContainer("top");
90         DatabaseComponentFactory.addDatabaseConfig(container);
91         container.addComponent(new DatasourceComponent("datasource"));
92
93         ClassConfiguration useraccessorConfig = new ClassConfiguration(
94             TestUserAccessor.class);
95         useraccessorConfig.getObjectConfig().getSetterConfig().initAllSetters();
96         container.addComponent("userAccessor", useraccessorConfig);
97         container
98             .addComponent(new AuthorizationComponent("authorization", true));
99
100         ClassConfiguration dbtesterConfig = new ClassConfiguration(
101             DatabaseTesterComponent.class);
102         dbtesterConfig.getObjectConfig().getSetterConfig().initAllSetters();
103         container.addComponent("databaseTester", dbtesterConfig);
104
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);
111
112         scope = container.start();
113
114         databaseTester.cleanDatabase();
115
116         super.setUp();
117     }
118
119     public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
120         databaseTester = aDatabaseTester;
121     }
122
123     public void setUserAccessor(UserAccessor aUserAccessor) {
124         userAccessor = aUserAccessor;
125     }
126
127     public void setHibernateTemplate(HibernateTemplate aHibernateTemplate) {
128         hibernateTemplate = aHibernateTemplate;
129     }
130
131     public void setAuthorizationService(
132         AuthorizationService aAuthorizationService) {
133         authorizationService = aAuthorizationService;
134     }
135
136     /*
137      * (non-Javadoc)
138      * 
139      * @see
140      * org.wamblee.security.authorization.AuthorizationServiceTest#createService
141      * ()
142      */
143     @Override
144     protected AuthorizationService createService() {
145         PersistentAuthorizationService service = new PersistentAuthorizationService(
146             "DEFAULT", hibernateTemplate, createUserAccessor(), 10000);
147
148         return service;
149     }
150
151     /*
152      * (non-Javadoc)
153      * 
154      * @see
155      * org.wamblee.security.authorization.AuthorizationServiceTest#checkRuleCount
156      * (int)
157      */
158     @Override
159     protected void checkRuleCount(int aCount) {
160         try {
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);
171         }
172     }
173
174     public void testSchemaExport() {
175         Configuration config = new Configuration();
176
177         for (String mappingFile : new AuthorizationMappingFiles()) {
178             config.addResource(mappingFile);
179         }
180
181         config.setProperty("hibernate.dialect", MySQL5InnoDBDialect.class
182             .getName());
183
184         SchemaExport exporter = new SchemaExport(config);
185         exporter.setOutputFile("target/mysql5.schema.sql");
186         exporter.create(true, false);
187     }
188
189     public void testPerformance() {
190         PersistentAuthorizationService service = (PersistentAuthorizationService) getService();
191
192         int n = 1000;
193         long time = System.currentTimeMillis();
194
195         for (int i = 0; i < n; i++) {
196             testFirstRuleGrants();
197             resetTestRules();
198             testSecondRuleDenies();
199             resetTestRules();
200             testThirdRuleGrants();
201             resetTestRules();
202             testNoRulesSupportResource();
203         }
204
205         LOGGER.info("Executed " + (4 * n) + " authorization checks in " +
206             ((float) (System.currentTimeMillis() - time) / (float) 1000) +
207             " seconds.");
208     }
209 }