(no commit message)
authorErik Brakkee <erik@brakkee.org>
Fri, 30 Apr 2010 22:05:42 +0000 (22:05 +0000)
committerErik Brakkee <erik@brakkee.org>
Fri, 30 Apr 2010 22:05:42 +0000 (22:05 +0000)
security/jpatest/src/test/java/org/wamblee/security/authorization/jpa/JpaAuthorizationServiceTest.java [new file with mode: 0644]

diff --git a/security/jpatest/src/test/java/org/wamblee/security/authorization/jpa/JpaAuthorizationServiceTest.java b/security/jpatest/src/test/java/org/wamblee/security/authorization/jpa/JpaAuthorizationServiceTest.java
new file mode 100644 (file)
index 0000000..1573f47
--- /dev/null
@@ -0,0 +1,127 @@
+/*
+ * Copyright 2005-2010 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.security.authorization.jpa;
+
+import javax.persistence.EntityManager;
+
+import org.apache.log4j.Logger;
+import org.wamblee.security.authorization.AbstractAuthorizationService;
+import org.wamblee.security.authorization.AllOperation;
+import org.wamblee.security.authorization.AuthorizationResult;
+import org.wamblee.security.authorization.AuthorizationService;
+import org.wamblee.security.authorization.AuthorizationServiceTest;
+import org.wamblee.security.authorization.TestAuthorizationRule;
+import org.wamblee.support.persistence.JpaTester;
+import org.wamblee.support.persistence.TransactionProxyFactory;
+import org.wamblee.support.persistence.JpaBuilder.JpaUnitOfWork;
+import org.wamblee.usermgt.jpa.SecurityPersistenceUnit;
+
+/**
+ * Unit test for the persistent authorization service.
+ * 
+ * @author Erik Brakkee
+ */
+public class JpaAuthorizationServiceTest extends AuthorizationServiceTest {
+    private static final Logger LOGGER = Logger
+        .getLogger(JpaAuthorizationServiceTest.class);
+
+    private static final String SERVICE_TABLE = "SEC_AUTH_SVC";
+
+    private static final String RULES_TABLE = "SEC_AUTH_RULE";
+
+    private static final String SERVICE_RULES_TABLE = "SEC_AUTH_SVC_SEC_AUTH_RULE";
+
+    private static final String OPERATIONCOND_TABLE = "SEC_OPERATION_CONDITION";
+
+    private static final String PATHCOND_TABLE = "SEC_PATH_CONDITION";
+
+    private static final String USERCOND_TABLE = "SEC_USER_CONDITION";
+
+    private JpaTester jpaTester;
+
+    private AuthorizationService authorizationService;
+
+    @Override
+    protected void setUp() throws Exception {
+        jpaTester = new JpaTester(new SecurityPersistenceUnit());
+        jpaTester.start();
+
+        super.setUp();
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see
+     * org.wamblee.security.authorization.AuthorizationServiceTest#createService
+     * ()
+     */
+    @Override
+    protected AuthorizationService createService() {
+        TransactionProxyFactory<AuthorizationService> factory = new TransactionProxyFactory<AuthorizationService>(
+            jpaTester.getJpaBuilder(), AuthorizationService.class);
+        JpaAuthorizationService service = new JpaAuthorizationService(
+            "DEFAULT", factory.getTransactionScopedEntityManager(),
+            createUserAccessor(), 10000);
+
+        return factory.getProxy(service);
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see
+     * org.wamblee.security.authorization.AuthorizationServiceTest#checkRuleCount
+     * (int)
+     */
+    @Override
+    protected void checkRuleCount(int aCount) {
+        try {
+            assertEquals(1, jpaTester.getDbUtils().getTableSize(SERVICE_TABLE));
+            assertEquals(aCount, jpaTester.getDbUtils().getTableSize(
+                RULES_TABLE));
+            assertEquals(aCount, jpaTester.getDbUtils().getTableSize(
+                SERVICE_RULES_TABLE));
+            assertEquals(aCount, jpaTester.getDbUtils().getTableSize(
+                USERCOND_TABLE));
+            assertEquals(aCount, jpaTester.getDbUtils().getTableSize(
+                PATHCOND_TABLE));
+            assertEquals(aCount, jpaTester.getDbUtils().getTableSize(
+                OPERATIONCOND_TABLE));
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public void testPerformance() {
+        int n = 1000;
+        long time = System.currentTimeMillis();
+
+        for (int i = 0; i < n; i++) {
+            testFirstRuleGrants();
+            resetTestRules();
+            testSecondRuleDenies();
+            resetTestRules();
+            testThirdRuleGrants();
+            resetTestRules();
+            testNoRulesSupportResource();
+        }
+
+        LOGGER.info("Executed " + (4 * n) + " authorization checks in " +
+            ((float) (System.currentTimeMillis() - time) / (float) 1000) +
+            " seconds.");
+    }
+}