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