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