align package names.
[utils] / security / jpatest / src / test / java / org / wamblee / security / authentication / jpa / JpaGroupSetTest.java
diff --git a/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/JpaGroupSetTest.java b/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/JpaGroupSetTest.java
new file mode 100644 (file)
index 0000000..fe68408
--- /dev/null
@@ -0,0 +1,78 @@
+package org.wamblee.security.authentication.jpa;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+
+import org.junit.After;
+import org.junit.Before;
+import org.wamblee.security.authentication.GroupSet;
+import org.wamblee.security.authentication.InMemoryGroupSetTest;
+import org.wamblee.security.authentication.jpa.JpaGroupSet;
+import org.wamblee.support.persistence.JpaTester;
+import org.wamblee.support.persistence.TransactionProxyFactory;
+import org.wamblee.support.persistence.DatabaseUtils.JdbcUnitOfWork;
+
+
+public class JpaGroupSetTest extends InMemoryGroupSetTest {
+
+    private static final String GROUP_TABLE = "SEC_GROUP";
+
+    private static final String GROUP_QUERY = "select * from " + GROUP_TABLE +
+        " where name = ?";
+
+    private JpaTester jpaTester;
+
+    @Before
+    public void setUp() throws Exception {
+        jpaTester = new JpaTester(new SecurityPersistenceUnit());
+        jpaTester.start();
+   
+        // Superclass setup will call createGroupSet so requires initialized JPA. 
+        super.setUp();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        jpaTester.stop();
+        super.tearDown();
+    }
+
+    @Override
+    protected void checkGroupCount(int aSize) throws Exception {
+        super.checkGroupCount(aSize);
+        assertEquals(aSize, jpaTester.getDbUtils().getTableSize(GROUP_TABLE));
+    }
+
+    @Override
+    protected void checkGroupExists(String aGroup) throws Exception {
+        super.checkGroupExists(aGroup);
+        assertTrue(groupExists(aGroup));
+    }
+
+    private boolean groupExists(final String aGroup) throws Exception {
+        return jpaTester.getDbUtils().executeInTransaction(
+            new JdbcUnitOfWork<Boolean>() {
+                @Override
+                public Boolean execute(Connection aConnection) throws Exception {
+                    ResultSet res = jpaTester.getDbUtils().executeQuery(
+                        aConnection, GROUP_QUERY, aGroup);
+                    return res.next();
+                }
+            });
+    }
+
+    @Override
+    protected void checkGroupNotExists(String aGroup) throws Exception {
+        super.checkGroupNotExists(aGroup);
+        assertFalse(groupExists(aGroup));
+    }
+
+    @Override
+    protected GroupSet createGroupSet() {
+        TransactionProxyFactory<GroupSet> factory = new TransactionProxyFactory<GroupSet>(
+            jpaTester.getJpaBuilder(), GroupSet.class);
+        GroupSet groupset = new JpaGroupSet(factory.getTransactionScopedEntityManager());
+        GroupSet proxy = factory.getProxy(groupset);
+        return proxy;
+    }
+}