Added author to all java files with class comments.
[utils] / security / src / test / java / org / wamblee / security / authorization / hibernate / PersistentAuthorizationServiceTest.java
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
27 /**
28  * Unit test for the persistent authorization service. 
29  *
30  * @author Erik Brakkee
31  */
32 public class PersistentAuthorizationServiceTest extends AuthorizationServiceTest {
33     
34     private static final Logger LOGGER = Logger.getLogger(PersistentAuthorizationServiceTest.class);
35    
36     private static final String SERVICE_TABLE = "AUTHORIZATION_SERVICE";
37     private static final String RULES_TABLE = "AUTHORIZATION_RULES"; 
38     private static final String SERVICE_RULES_TABLE = "AUTHORIZATION_SERVICE_RULES";
39     private static final String OPERATIONCOND_TABLE = "OPERATION_CONDITIONS";
40     private static final String PATHCOND_TABLE = "PATH_CONDITIONS";
41     private static final String USERCOND_TABLE = "USER_CONDITIONS";
42    
43
44     public PersistentAuthorizationServiceTest() { 
45         super(AuthorizationSpringConfigFiles.class, AuthorizationMappingFiles.class);
46     }
47     
48     /* (non-Javadoc)
49      * @see org.wamblee.security.authorization.AuthorizationServiceTest#createService()
50      */
51     @Override
52     protected AuthorizationService createService() {
53         PersistentAuthorizationService service = new PersistentAuthorizationService("DEFAULT", 
54                 BeanKernel.getBeanFactory().find(HibernateTemplate.class), createUserAccessor(), 10000);
55         return service; 
56     }
57     
58     /* (non-Javadoc)
59      * @see org.wamblee.security.authorization.AuthorizationServiceTest#checkRuleCount(int)
60      */
61     @Override
62     protected void checkRuleCount(int aCount) {
63         try { 
64             assertEquals(1, getTableSize(SERVICE_TABLE)); 
65             assertEquals(aCount, getTableSize(RULES_TABLE));
66             assertEquals(aCount, getTableSize(SERVICE_RULES_TABLE));
67             assertEquals(aCount, getTableSize(USERCOND_TABLE));
68             assertEquals(aCount, getTableSize(PATHCOND_TABLE));
69             assertEquals(aCount, getTableSize(OPERATIONCOND_TABLE));
70         } catch (SQLException e) {  
71             throw new RuntimeException(e);
72         }
73        
74     }
75     
76     public void testPerformance() { 
77        
78         PersistentAuthorizationService service = (PersistentAuthorizationService)getService(); 
79         
80         int n = 1000; 
81         long time = System.currentTimeMillis();
82         for (int i = 0; i < n; i++) { 
83             testFirstRuleGrants(); 
84             resetTestRules();
85             testSecondRuleDenies();
86             resetTestRules();
87             testThirdRuleGrants();
88             resetTestRules(); 
89             testNoRulesSupportResource();
90         }
91         LOGGER.info("Executed " + 4*n + " authorization checks in " + (float)(System.currentTimeMillis()-time)/(float)1000 + " seconds.");
92     }
93 }