2 * Copyright 2005-2010 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.jpa;
18 import javax.persistence.EntityManager;
20 import org.apache.log4j.Logger;
21 import org.wamblee.security.authorization.AbstractAuthorizationService;
22 import org.wamblee.security.authorization.AllOperation;
23 import org.wamblee.security.authorization.AuthorizationResult;
24 import org.wamblee.security.authorization.AuthorizationService;
25 import org.wamblee.security.authorization.AuthorizationServiceTest;
26 import org.wamblee.security.authorization.TestAuthorizationRule;
27 import org.wamblee.support.persistence.JpaTester;
28 import org.wamblee.support.persistence.TransactionProxyFactory;
29 import org.wamblee.support.persistence.JpaBuilder.JpaUnitOfWork;
30 import org.wamblee.usermgt.jpa.SecurityPersistenceUnit;
33 * Unit test for the persistent authorization service.
35 * @author Erik Brakkee
37 public class JpaAuthorizationServiceTest extends AuthorizationServiceTest {
38 private static final Logger LOGGER = Logger
39 .getLogger(JpaAuthorizationServiceTest.class);
41 private static final String SERVICE_TABLE = "SEC_AUTH_SVC";
43 private static final String RULES_TABLE = "SEC_AUTH_RULE";
45 private static final String SERVICE_RULES_TABLE = "SEC_AUTH_SVC_SEC_AUTH_RULE";
47 private static final String OPERATIONCOND_TABLE = "SEC_OPERATION_CONDITION";
49 private static final String PATHCOND_TABLE = "SEC_PATH_CONDITION";
51 private static final String USERCOND_TABLE = "SEC_USER_CONDITION";
53 private JpaTester jpaTester;
55 private AuthorizationService authorizationService;
58 protected void setUp() throws Exception {
59 jpaTester = new JpaTester(new SecurityPersistenceUnit());
69 * org.wamblee.security.authorization.AuthorizationServiceTest#createService
73 protected AuthorizationService createService() {
74 TransactionProxyFactory<AuthorizationService> factory = new TransactionProxyFactory<AuthorizationService>(
75 jpaTester.getJpaBuilder(), AuthorizationService.class);
76 JpaAuthorizationService service = new JpaAuthorizationService(
77 "DEFAULT", factory.getTransactionScopedEntityManager(),
78 createUserAccessor(), 10000);
80 return factory.getProxy(service);
87 * org.wamblee.security.authorization.AuthorizationServiceTest#checkRuleCount
91 protected void checkRuleCount(int aCount) {
93 assertEquals(1, jpaTester.getDbUtils().getTableSize(SERVICE_TABLE));
94 assertEquals(aCount, jpaTester.getDbUtils().getTableSize(
96 assertEquals(aCount, jpaTester.getDbUtils().getTableSize(
97 SERVICE_RULES_TABLE));
98 assertEquals(aCount, jpaTester.getDbUtils().getTableSize(
100 assertEquals(aCount, jpaTester.getDbUtils().getTableSize(
102 assertEquals(aCount, jpaTester.getDbUtils().getTableSize(
103 OPERATIONCOND_TABLE));
104 } catch (Exception e) {
105 throw new RuntimeException(e);
109 public void testPerformance() {
111 long time = System.currentTimeMillis();
113 for (int i = 0; i < n; i++) {
114 testFirstRuleGrants();
116 testSecondRuleDenies();
118 testThirdRuleGrants();
120 testNoRulesSupportResource();
123 LOGGER.info("Executed " + (4 * n) + " authorization checks in " +
124 ((float) (System.currentTimeMillis() - time) / (float) 1000) +