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