(no commit message)
[utils] / security / jpatest / src / test / java / org / wamblee / security / authentication / jpa / JpaGroupSetTest.java
1 package org.wamblee.security.authentication.jpa;
2
3 import java.sql.Connection;
4 import java.sql.ResultSet;
5
6 import org.junit.After;
7 import org.junit.Before;
8 import org.wamblee.security.authentication.GroupSet;
9 import org.wamblee.security.authentication.InMemoryGroupSetTest;
10 import org.wamblee.test.persistence.JpaTester;
11 import org.wamblee.test.persistence.DatabaseUtils.JdbcUnitOfWork;
12 import org.wamblee.test.transactions.TransactionProxyFactory;
13
14
15 public class JpaGroupSetTest extends InMemoryGroupSetTest {
16
17     private static final String GROUP_TABLE = "SEC_GROUP";
18
19     private static final String GROUP_QUERY = "select * from " + GROUP_TABLE +
20         " where name = ?";
21
22     private JpaTester jpaTester;
23
24     @Before
25     public void setUp() throws Exception {
26         jpaTester = new JpaTester(new SecurityPersistenceUnit());
27         jpaTester.start();
28    
29         // Superclass setup will call createGroupSet so requires initialized JPA. 
30         super.setUp();
31     }
32
33     @After
34     public void tearDown() throws Exception {
35         jpaTester.stop();
36         super.tearDown();
37     }
38
39     @Override
40     protected void checkGroupCount(int aSize) throws Exception {
41         super.checkGroupCount(aSize);
42         assertEquals(aSize, jpaTester.getDbUtils().getTableSize(GROUP_TABLE));
43     }
44
45     @Override
46     protected void checkGroupExists(String aGroup) throws Exception {
47         super.checkGroupExists(aGroup);
48         assertTrue(groupExists(aGroup));
49     }
50
51     private boolean groupExists(final String aGroup) throws Exception {
52         return jpaTester.getDbUtils().executeInTransaction(
53             new JdbcUnitOfWork<Boolean>() {
54                 @Override
55                 public Boolean execute(Connection aConnection) throws Exception {
56                     ResultSet res = jpaTester.getDbUtils().executeQuery(
57                         aConnection, GROUP_QUERY, aGroup);
58                     return res.next();
59                 }
60             });
61     }
62
63     @Override
64     protected void checkGroupNotExists(String aGroup) throws Exception {
65         super.checkGroupNotExists(aGroup);
66         assertFalse(groupExists(aGroup));
67     }
68
69     @Override
70     protected GroupSet createGroupSet() {
71         TransactionProxyFactory<GroupSet> factory = new TransactionProxyFactory<GroupSet>(
72             jpaTester.getJpaBuilder(), GroupSet.class);
73         GroupSet groupset = new JpaGroupSet(factory.getTransactionScopedEntityManager());
74         GroupSet proxy = factory.getProxy(groupset);
75         return proxy;
76     }
77 }