ea5036f2f8f08c78dcfca713958f3272dcf2ac8a
[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.jpa;
17
18
19 import java.util.logging.Logger;
20
21 import org.wamblee.security.authentication.jpa.SecurityPersistenceUnit;
22 import org.wamblee.security.authorization.AuthorizationService;
23 import org.wamblee.security.authorization.AuthorizationServiceTest;
24 import org.wamblee.test.persistence.JpaTester;
25 import org.wamblee.test.persistence.TransactionProxyFactory;
26
27 /**
28  * Unit test for the persistent authorization service.
29  * 
30  * @author Erik Brakkee
31  */
32 public class JpaAuthorizationServiceTest extends AuthorizationServiceTest {
33     private static final Logger LOGGER = Logger
34         .getLogger(JpaAuthorizationServiceTest.class.getName());
35
36     private static final String SERVICE_TABLE = "SEC_AUTH_SVC";
37
38     private static final String RULES_TABLE = "SEC_AUTH_RULE";
39
40     private static final String SERVICE_RULES_TABLE = "SEC_AUTH_SVC_RULE";
41
42     private static final String OPERATIONCOND_TABLE = "SEC_OPERATION_CONDITION";
43
44     private static final String PATHCOND_TABLE = "SEC_PATH_CONDITION";
45
46     private static final String USERCOND_TABLE = "SEC_USER_CONDITION";
47
48     private JpaTester jpaTester;
49
50     @Override
51     protected void setUp() throws Exception {
52         jpaTester = new JpaTester(new SecurityPersistenceUnit());
53         jpaTester.start();
54
55         super.setUp();
56     }
57
58     @Override
59     protected void tearDown() throws Exception {
60         jpaTester.stop();
61         super.tearDown();
62     }
63
64     /*
65      * (non-Javadoc)
66      * 
67      * @see
68      * org.wamblee.security.authorization.AuthorizationServiceTest#createService
69      * ()
70      */
71     @Override
72     protected AuthorizationService createService() {
73         TransactionProxyFactory<AuthorizationService> factory = new TransactionProxyFactory<AuthorizationService>(
74             jpaTester.getJpaBuilder(), AuthorizationService.class);
75         JpaAuthorizationService service = new JpaAuthorizationService(
76             "DEFAULT", factory.getTransactionScopedEntityManager(),
77             getUserAccessor(), getUserAdministration(), 10000);
78
79         return factory.getProxy(service);
80     }
81
82     /*
83      * (non-Javadoc)
84      * 
85      * @see
86      * org.wamblee.security.authorization.AuthorizationServiceTest#checkRuleCount
87      * (int)
88      */
89     @Override
90     protected void checkRuleCount(int aCount) {
91         try {
92             assertEquals(1, jpaTester.getDbUtils().getTableSize(SERVICE_TABLE));
93             assertEquals(aCount, jpaTester.getDbUtils().getTableSize(
94                 RULES_TABLE));
95             assertEquals(aCount, jpaTester.getDbUtils().getTableSize(
96                 SERVICE_RULES_TABLE));
97             assertEquals(aCount, jpaTester.getDbUtils().getTableSize(
98                 USERCOND_TABLE));
99             assertEquals(aCount, jpaTester.getDbUtils().getTableSize(
100                 PATHCOND_TABLE));
101             assertEquals(aCount, jpaTester.getDbUtils().getTableSize(
102                 OPERATIONCOND_TABLE));
103         } catch (Exception e) {
104             throw new RuntimeException(e);
105         }
106     }
107
108     public void testPerformance() {
109         int n = 1000;
110         long time = System.currentTimeMillis();
111
112         for (int i = 0; i < n; i++) {
113             testFirstRuleGrants();
114             resetTestRules();
115             testSecondRuleDenies();
116             resetTestRules();
117             testThirdRuleGrants();
118             resetTestRules();
119             testNoRulesSupportResource();
120         }
121
122         LOGGER.info("Executed " + (4 * n) + " authorization checks in " +
123             ((float) (System.currentTimeMillis() - time) / (float) 1000) +
124             " seconds.");
125     }
126 }