25bb7f831d22a1ffb99af68addf3576d0a816b1a
[utils] / security / jpatest / src / test / java / org / wamblee / usermgt / hibernate / HibernateGroupSetTest.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16 package org.wamblee.usermgt.hibernate;
17
18 import org.junit.Ignore;
19 import org.wamblee.system.adapters.DefaultContainer;
20 import org.wamblee.system.adapters.ObjectConfiguration;
21 import org.wamblee.system.core.Scope;
22 import org.wamblee.system.spring.component.DatabaseTesterComponent;
23
24 import org.wamblee.test.spring.TestTransactionCallback;
25
26 import org.wamblee.usermgt.GroupSet;
27 import org.wamblee.usermgt.InMemoryGroupSetTest;
28
29 import java.sql.ResultSet;
30 import java.sql.SQLException;
31
32 import java.util.HashMap;
33 import java.util.Map;
34
35 /**
36  * Tests for {@link org.wamblee.usermgt.hibernate.HibernateGroupSet}
37  * 
38  * @author Erik Brakkee
39  */
40 @Ignore
41 public class HibernateGroupSetTest extends InMemoryGroupSetTest {
42     private static final String GROUP_TABLE = "GROUPS";
43
44     private static final String GROUP_QUERY = "select * from " + GROUP_TABLE +
45         " where name = ?";
46
47     private DefaultContainer container;
48
49     private Scope scope;
50
51     private DatabaseTesterComponent databaseTester;
52
53     private GroupSet groupSet;
54
55     @Override
56     protected void setUp() throws Exception {
57         container = new UserMgtRepositoryTestContainer("top");
58
59         ObjectConfiguration config = new ObjectConfiguration(
60             HibernateGroupSetTest.class);
61         config.getSetterConfig().clear().add("setGroupSet").add(
62             "setDatabaseTester");
63         container.addComponent("testcase", this, config);
64
65         scope = container.start();
66
67         databaseTester.cleanDatabase();
68         super.setUp();
69     }
70
71     @Override
72     protected void tearDown() throws Exception {
73         container.stop(scope);
74         super.tearDown();
75     }
76
77     public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
78         databaseTester = aDatabaseTester;
79     }
80
81     public void setGroupSet(GroupSet aGroupSet) {
82         groupSet = aGroupSet;
83     }
84
85     /*
86      * (non-Javadoc)
87      * 
88      * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupCount(int)
89      */
90     @Override
91     protected void checkGroupCount(int aSize) throws Exception {
92         databaseTester.flush();
93         super.checkGroupCount(aSize);
94         assertEquals(aSize, databaseTester.getTableSize(GROUP_TABLE));
95     }
96
97     /*
98      * (non-Javadoc)
99      * 
100      * @see
101      * org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupExists(java.lang.String
102      * )
103      */
104     @Override
105     protected void checkGroupExists(final String aGroup) throws SQLException {
106         databaseTester.flush();
107
108         Map<String, Integer> result = databaseTester
109             .executeTransaction(new TestTransactionCallback() {
110                 /*
111                  * (non-Javadoc)
112                  * 
113                  * @see org.wamblee.test.TestTransactionCallback#execute()
114                  */
115                 @Override
116                 public Map execute() throws Exception {
117                     ResultSet result = databaseTester.executeQuery(GROUP_QUERY,
118                         aGroup);
119                     Map<String, Integer> res = new HashMap<String, Integer>();
120                     res.put("result", databaseTester.countResultSet(result));
121
122                     return res;
123                 }
124             });
125
126         int count = result.get("result");
127         assertEquals(1, count);
128     }
129
130     /*
131      * (non-Javadoc)
132      * 
133      * @see
134      * org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupNotExists(java.lang
135      * .String)
136      */
137     @Override
138     protected void checkGroupNotExists(String aGroup) throws SQLException {
139         databaseTester.flush();
140
141         ResultSet result = databaseTester.executeQuery(GROUP_QUERY, aGroup);
142         assertEquals(0, databaseTester.countResultSet(result));
143     }
144
145     /*
146      * (non-Javadoc)
147      * 
148      * @see org.wamblee.usermgt.InMemoryGroupSetTest#createGroupSet()
149      */
150     @Override
151     protected GroupSet createGroupSet() {
152         return groupSet;
153     }
154 }