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 static org.wamblee.security.authorization.AuthorizationResult.*;
20 import javax.persistence.EntityManager;
22 import org.apache.log4j.Logger;
23 import org.wamblee.security.authentication.jpa.SecurityPersistenceUnit;
24 import org.wamblee.security.authorization.AllOperation;
25 import org.wamblee.security.authorization.AuthorizationRule;
26 import org.wamblee.security.authorization.AuthorizationService;
27 import org.wamblee.security.authorization.AuthorizationServiceTest;
28 import org.wamblee.security.authorization.DefaultAuthorizationService;
29 import org.wamblee.support.persistence.JpaTester;
30 import org.wamblee.support.persistence.TransactionProxyFactory;
31 import org.wamblee.support.persistence.JpaBuilder.JpaUnitOfWork;
34 * Unit test for the persistent authorization service.
36 * @author Erik Brakkee
38 public class JpaAuthorizationServiceTest extends AuthorizationServiceTest {
39 private static final Logger LOGGER = Logger
40 .getLogger(JpaAuthorizationServiceTest.class);
42 private static final String SERVICE_TABLE = "SEC_AUTH_SVC";
44 private static final String RULES_TABLE = "SEC_AUTH_RULE";
46 private static final String SERVICE_RULES_TABLE = "SEC_AUTH_SVC_SEC_AUTH_RULE";
48 private static final String OPERATIONCOND_TABLE = "SEC_OPERATION_CONDITION";
50 private static final String PATHCOND_TABLE = "SEC_PATH_CONDITION";
52 private static final String USERCOND_TABLE = "SEC_USER_CONDITION";
54 private JpaTester jpaTester;
56 private AuthorizationService authorizationService;
59 protected void setUp() throws Exception {
60 jpaTester = new JpaTester(new SecurityPersistenceUnit());
67 protected void tearDown() throws Exception {
76 * org.wamblee.security.authorization.AuthorizationServiceTest#createService
80 protected AuthorizationService createService() {
81 TransactionProxyFactory<AuthorizationService> factory = new TransactionProxyFactory<AuthorizationService>(
82 jpaTester.getJpaBuilder(), AuthorizationService.class);
83 JpaAuthorizationService service = new JpaAuthorizationService(
84 "DEFAULT", factory.getTransactionScopedEntityManager(),
85 createUserAccessor(), 10000);
87 return factory.getProxy(service);
94 * org.wamblee.security.authorization.AuthorizationServiceTest#checkRuleCount
98 protected void checkRuleCount(int aCount) {
100 assertEquals(1, jpaTester.getDbUtils().getTableSize(SERVICE_TABLE));
101 assertEquals(aCount, jpaTester.getDbUtils().getTableSize(
103 assertEquals(aCount, jpaTester.getDbUtils().getTableSize(
104 SERVICE_RULES_TABLE));
105 assertEquals(aCount, jpaTester.getDbUtils().getTableSize(
107 assertEquals(aCount, jpaTester.getDbUtils().getTableSize(
109 assertEquals(aCount, jpaTester.getDbUtils().getTableSize(
110 OPERATIONCOND_TABLE));
111 } catch (Exception e) {
112 throw new RuntimeException(e);
116 public void testPerformance() {
118 long time = System.currentTimeMillis();
120 for (int i = 0; i < n; i++) {
121 testFirstRuleGrants();
123 testSecondRuleDenies();
125 testThirdRuleGrants();
127 testNoRulesSupportResource();
130 LOGGER.info("Executed " + (4 * n) + " authorization checks in " +
131 ((float) (System.currentTimeMillis() - time) / (float) 1000) +