source code formatting.
[utils] / security / src / test / java / org / wamblee / usermgt / hibernate / HibernateGroupSetTest.java
1 /*
2  * Copyright 2005 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.wamblee.system.adapters.DefaultContainer;
19 import org.wamblee.system.adapters.ObjectConfiguration;
20 import org.wamblee.system.core.Scope;
21 import org.wamblee.system.spring.component.DatabaseTesterComponent;
22
23 import org.wamblee.test.spring.TestTransactionCallback;
24
25 import org.wamblee.usermgt.GroupSet;
26 import org.wamblee.usermgt.InMemoryGroupSetTest;
27
28 import java.sql.ResultSet;
29 import java.sql.SQLException;
30
31 import java.util.HashMap;
32 import java.util.Map;
33
34
35 /**
36  * Tests for {@link org.wamblee.usermgt.hibernate.HibernateGroupSet}
37  *
38  * @author Erik Brakkee
39  */
40 public class HibernateGroupSetTest extends InMemoryGroupSetTest {
41     /**
42      * DOCUMENT ME!
43      */
44     private static final String GROUP_TABLE = "GROUPS";
45
46     /**
47      * DOCUMENT ME!
48      */
49     private static final String GROUP_QUERY = "select * from " + GROUP_TABLE
50         + " where name = ?";
51
52     /**
53      * DOCUMENT ME!
54      */
55     private DefaultContainer container;
56
57     /**
58      * DOCUMENT ME!
59      */
60     private Scope scope;
61
62     /**
63      * DOCUMENT ME!
64      */
65     private DatabaseTesterComponent databaseTester;
66
67     /**
68      * DOCUMENT ME!
69      */
70     private GroupSet groupSet;
71
72     /**
73      * DOCUMENT ME!
74      *
75      * @throws Exception DOCUMENT ME!
76      */
77     @Override
78     protected void setUp() throws Exception {
79         container = new UserMgtRepositoryTestContainer("top");
80
81         ObjectConfiguration config = new ObjectConfiguration(HibernateGroupSetTest.class);
82         config.getSetterConfig().clear().add("setGroupSet")
83         .add("setDatabaseTester");
84         container.addComponent("testcase", this, config);
85
86         scope = container.start();
87
88         databaseTester.cleanDatabase();
89         super.setUp();
90     }
91
92     /**
93      * DOCUMENT ME!
94      *
95      * @throws Exception DOCUMENT ME!
96      */
97     @Override
98     protected void tearDown() throws Exception {
99         container.stop(scope);
100         super.tearDown();
101     }
102
103     /**
104      * DOCUMENT ME!
105      *
106      * @param aDatabaseTester DOCUMENT ME!
107      */
108     public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
109         databaseTester = aDatabaseTester;
110     }
111
112     /**
113      * DOCUMENT ME!
114      *
115      * @param aGroupSet DOCUMENT ME!
116      */
117     public void setGroupSet(GroupSet aGroupSet) {
118         groupSet = aGroupSet;
119     }
120
121     /* (non-Javadoc)
122      * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupCount(int)
123      */
124     /**
125      * DOCUMENT ME!
126      *
127      * @param aSize DOCUMENT ME!
128      *
129      * @throws SQLException DOCUMENT ME!
130      */
131     @Override
132     protected void checkGroupCount(int aSize) throws SQLException {
133         databaseTester.flush();
134         super.checkGroupCount(aSize);
135         assertEquals(aSize, databaseTester.getTableSize(GROUP_TABLE));
136     }
137
138     /* (non-Javadoc)
139      * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupExists(java.lang.String)
140      */
141     /**
142      * DOCUMENT ME!
143      *
144      * @param aGroup DOCUMENT ME!
145      *
146      * @throws SQLException DOCUMENT ME!
147      */
148     @Override
149     protected void checkGroupExists(final String aGroup)
150         throws SQLException {
151         databaseTester.flush();
152
153         Map<String, Integer> result = databaseTester.executeTransaction(new TestTransactionCallback() {
154                     /* (non-Javadoc)
155                      * @see org.wamblee.test.TestTransactionCallback#execute()
156                      */
157                     @Override
158                     public Map execute() throws Exception {
159                         ResultSet            result = databaseTester
160                             .executeQuery(GROUP_QUERY, aGroup);
161                         Map<String, Integer> res = new HashMap<String, Integer>();
162                         res.put("result", databaseTester.countResultSet(result));
163
164                         return res;
165                     }
166                 });
167
168         int count = result.get("result");
169         assertEquals(1, count);
170     }
171
172     /* (non-Javadoc)
173      * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupNotExists(java.lang.String)
174      */
175     /**
176      * DOCUMENT ME!
177      *
178      * @param aGroup DOCUMENT ME!
179      *
180      * @throws SQLException DOCUMENT ME!
181      */
182     @Override
183     protected void checkGroupNotExists(String aGroup) throws SQLException {
184         databaseTester.flush();
185
186         ResultSet result = databaseTester.executeQuery(GROUP_QUERY, aGroup);
187         assertEquals(0, databaseTester.countResultSet(result));
188     }
189
190     /* (non-Javadoc)
191      * @see org.wamblee.usermgt.InMemoryGroupSetTest#createGroupSet()
192      */
193     /**
194      * DOCUMENT ME!
195      *
196      * @return DOCUMENT ME!
197      */
198     @Override
199     protected GroupSet createGroupSet() {
200         return groupSet;
201     }
202 }