43bf8c899052fcba7c936dd6cffeb7d95c4fab57
[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
17 package org.wamblee.usermgt.hibernate;
18
19 import java.sql.ResultSet;
20 import java.sql.SQLException;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import org.wamblee.system.adapters.DefaultContainer;
25 import org.wamblee.system.adapters.ObjectConfiguration;
26 import org.wamblee.system.core.Scope;
27 import org.wamblee.system.spring.component.DatabaseTesterComponent;
28 import org.wamblee.test.spring.TestTransactionCallback;
29 import org.wamblee.usermgt.GroupSet;
30 import org.wamblee.usermgt.InMemoryGroupSetTest;
31
32 /**
33  * Tests for {@link org.wamblee.usermgt.hibernate.HibernateGroupSet} 
34  *
35  * @author Erik Brakkee
36  */
37 public class HibernateGroupSetTest extends InMemoryGroupSetTest {
38     
39     private static final String GROUP_TABLE = "GROUPS"; 
40     
41     private static final String GROUP_QUERY = "select * from " + GROUP_TABLE + " where name = ?";
42     
43     private DefaultContainer container;
44     private Scope scope;
45     
46     private DatabaseTesterComponent databaseTester;
47     private GroupSet groupSet; 
48     
49     @Override
50     protected void setUp() throws Exception {
51         
52         container = new UserMgtRepositoryTestContainer("top");
53         
54         ObjectConfiguration config = new ObjectConfiguration(
55                 HibernateGroupSetTest.class);
56         config.getSetterConfig().clear().add(
57                 "setGroupSet").add("setDatabaseTester");
58         container.addComponent("testcase", this, config);
59
60         scope = container.start();
61
62         databaseTester.cleanDatabase();
63         super.setUp();
64     }
65
66     @Override
67     protected void tearDown() throws Exception { 
68         container.stop(scope);
69        super.tearDown();
70     }
71     
72     public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
73         databaseTester = aDatabaseTester;
74     }
75    
76     public void setGroupSet(GroupSet aGroupSet) {
77         groupSet = aGroupSet;
78     }
79
80     /* (non-Javadoc)
81      * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupCount(int)
82      */
83     @Override
84     protected void checkGroupCount(int aSize) throws SQLException {
85         databaseTester.flush(); 
86         super.checkGroupCount(aSize);
87         assertEquals(aSize, databaseTester.getTableSize(GROUP_TABLE));
88     }
89     
90     /* (non-Javadoc)
91      * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupExists(java.lang.String)
92      */
93     @Override
94     protected void checkGroupExists(final String aGroup) throws SQLException {
95         databaseTester.flush();
96         Map<String,Integer> result = 
97         databaseTester.executeTransaction(new TestTransactionCallback()  { 
98             /* (non-Javadoc)
99              * @see org.wamblee.test.TestTransactionCallback#execute()
100              */
101             @Override
102             public Map execute() throws Exception {
103                 ResultSet result = databaseTester.executeQuery(GROUP_QUERY, aGroup);
104                 Map<String,Integer> res = new HashMap<String,Integer>(); 
105                 res.put("result", databaseTester.countResultSet(result));
106                 return res; 
107             }
108         }); 
109       
110         int count = result.get("result");
111         assertEquals(1, count);
112     }
113     
114     /* (non-Javadoc)
115      * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupNotExists(java.lang.String)
116      */
117     @Override
118     protected void checkGroupNotExists(String aGroup) throws SQLException {
119        databaseTester.flush(); 
120        ResultSet result = databaseTester.executeQuery(GROUP_QUERY, aGroup); 
121        assertEquals(0, databaseTester.countResultSet(result));
122     }
123     
124     /* (non-Javadoc)
125      * @see org.wamblee.usermgt.InMemoryGroupSetTest#createGroupSet()
126      */
127     @Override
128     protected GroupSet createGroupSet() {
129         return groupSet; 
130     }
131     
132 }