2dcefee78715e77c4ceba8563b9050a0ca8041b2
[utils] /
1 /*
2  * Copyright 2005 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
17 package org.wamblee.security.authorization.hibernate;
18
19 import java.sql.SQLException;
20
21 import org.apache.log4j.Logger;
22 import org.springframework.orm.hibernate3.HibernateTemplate;
23 import org.wamblee.general.BeanKernel;
24 import org.wamblee.security.authorization.AuthorizationService;
25 import org.wamblee.security.authorization.AuthorizationServiceTest;
26 import org.wamblee.security.authorization.TestUserAccessor;
27 import org.wamblee.system.adapters.ClassConfiguration;
28 import org.wamblee.system.adapters.ClassConfigurationTest;
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;
37 import org.wamblee.usermgt.hibernate.HibernateUserAdministrationTest;
38 import org.wamblee.usermgt.hibernate.UserAdministrationComponent;
39
40 /**
41  * Unit test for the persistent authorization service.
42  * 
43  * @author Erik Brakkee
44  */
45 public class PersistentAuthorizationServiceTest extends
46         AuthorizationServiceTest {
47
48     private static final Logger LOGGER = Logger
49             .getLogger(PersistentAuthorizationServiceTest.class);
50
51     private static final String SERVICE_TABLE = "AUTHORIZATION_SERVICE";
52     private static final String RULES_TABLE = "AUTHORIZATION_RULES";
53     private static final String SERVICE_RULES_TABLE = "AUTHORIZATION_SERVICE_RULES";
54     private static final String OPERATIONCOND_TABLE = "OPERATION_CONDITIONS";
55     private static final String PATHCOND_TABLE = "PATH_CONDITIONS";
56     private static final String USERCOND_TABLE = "USER_CONDITIONS";
57
58     private DefaultContainer _container;
59     private Scope _scope;
60
61     private DatabaseTesterComponent _databaseTester;
62     private UserAccessor _userAccessor;
63     private HibernateTemplate _hibernateTemplate;
64     private AuthorizationService _authorizationService;
65
66     @Override
67     protected void setUp() throws Exception {
68
69         _container = new DefaultContainer("top");
70         DatabaseComponentFactory.addDatabaseConfig(_container);
71         _container.addComponent(new DatasourceComponent("datasource"));
72         ClassConfiguration useraccessorConfig = new ClassConfiguration(TestUserAccessor.class); 
73         useraccessorConfig.getObjectConfig().getSetterConfig().initAllSetters();
74         _container.addComponent("userAccessor", useraccessorConfig);
75         _container.addComponent(new AuthorizationComponent("authorization",
76                 true));
77
78         
79         ClassConfiguration dbtesterConfig = new ClassConfiguration(DatabaseTesterComponent.class); 
80         dbtesterConfig.getObjectConfig().getSetterConfig().initAllSetters();
81         _container
82                 .addComponent("databaseTester", dbtesterConfig);
83
84         ObjectConfiguration config = new ObjectConfiguration(
85                 PersistentAuthorizationServiceTest.class);
86         config.getSetterConfig().clear().add("setUserAccessor").add(
87                 "setDatabaseTester").add("setHibernateTemplate").add(
88                 "setAuthorizationService");
89         _container.addComponent("testcase", this, config);
90
91         _scope = _container.start();
92
93         _databaseTester.cleanDatabase();
94
95         super.setUp();
96     }
97
98     public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
99         _databaseTester = aDatabaseTester;
100     }
101
102     public void setUserAccessor(UserAccessor aUserAccessor) {
103         _userAccessor = aUserAccessor;
104     }
105
106     public void setHibernateTemplate(HibernateTemplate aHibernateTemplate) {
107         _hibernateTemplate = aHibernateTemplate;
108     }
109     
110     public void setAuthorizationService(
111             AuthorizationService aAuthorizationService) {
112         _authorizationService = aAuthorizationService;
113     }
114
115     /*
116      * (non-Javadoc)
117      * 
118      * @see org.wamblee.security.authorization.AuthorizationServiceTest#createService()
119      */
120     @Override
121     protected AuthorizationService createService() {
122         PersistentAuthorizationService service = new PersistentAuthorizationService(
123                 "DEFAULT", _hibernateTemplate, createUserAccessor(), 10000);
124         return service;
125     }
126
127     /*
128      * (non-Javadoc)
129      * 
130      * @see org.wamblee.security.authorization.AuthorizationServiceTest#checkRuleCount(int)
131      */
132     @Override
133     protected void checkRuleCount(int aCount) {
134         try {
135             assertEquals(1, _databaseTester.getTableSize(SERVICE_TABLE));
136             assertEquals(aCount, _databaseTester.getTableSize(RULES_TABLE));
137             assertEquals(aCount, _databaseTester.getTableSize(SERVICE_RULES_TABLE));
138             assertEquals(aCount, _databaseTester.getTableSize(USERCOND_TABLE));
139             assertEquals(aCount, _databaseTester.getTableSize(PATHCOND_TABLE));
140             assertEquals(aCount, _databaseTester.getTableSize(OPERATIONCOND_TABLE));
141         } catch (SQLException e) {
142             throw new RuntimeException(e);
143         }
144
145     }
146
147     public void testPerformance() {
148
149         PersistentAuthorizationService service = (PersistentAuthorizationService) getService();
150
151         int n = 1000;
152         long time = System.currentTimeMillis();
153         for (int i = 0; i < n; i++) {
154             testFirstRuleGrants();
155             resetTestRules();
156             testSecondRuleDenies();
157             resetTestRules();
158             testThirdRuleGrants();
159             resetTestRules();
160             testNoRulesSupportResource();
161         }
162         LOGGER.info("Executed " + 4 * n + " authorization checks in "
163                 + (float) (System.currentTimeMillis() - time) / (float) 1000
164                 + " seconds.");
165     }
166 }