2 * Copyright 2005 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.wamblee.usermgt.hibernate;
19 import java.io.Serializable;
20 import java.sql.ResultSet;
21 import java.sql.SQLException;
24 import javax.sql.DataSource;
26 import org.wamblee.cache.Cache;
27 import org.wamblee.cache.EhCache;
28 import org.wamblee.general.BeanKernel;
29 import org.wamblee.system.adapters.DefaultContainer;
30 import org.wamblee.system.adapters.ObjectConfiguration;
31 import org.wamblee.system.core.Scope;
32 import org.wamblee.system.spring.DatabaseTesterComponent;
33 import org.wamblee.usermgt.Group;
34 import org.wamblee.usermgt.GroupSet;
35 import org.wamblee.usermgt.InMemoryUserSetTest;
36 import org.wamblee.usermgt.User;
37 import org.wamblee.usermgt.UserMgtException;
38 import org.wamblee.usermgt.UserSet;
39 import org.wamblee.usermgt.UsermgtHibernateMappingFiles;
40 import org.wamblee.usermgt.UsermgtSpringConfigFiles;
43 * Tests for {@link org.wamblee.usermgt.hibernate.HibernateGroupSet}
45 * @author Erik Brakkee
47 public class HibernateUserSetTest extends InMemoryUserSetTest {
49 private static final String USER_TABLE = "USERS";
50 private static final String GROUP_TABLE = "GROUPS";
52 private static final String USER_QUERY = "select * from " + USER_TABLE
54 private static final String GROUP_QUERY = "select * from " + GROUP_TABLE
57 public HibernateUserSetTest() {
58 super(UsermgtSpringConfigFiles.class,
59 UsermgtHibernateMappingFiles.class);
62 private DefaultContainer _container;
65 private DataSource _datasource;
66 private UserSet _userset;
67 private GroupSet _groupset;
68 private EhCache<Serializable, Serializable> _userCache;
69 private DatabaseTesterComponent _databaseTester;
74 * @see org.wamblee.usermgt.InMemoryUserSetTest#setUp()
77 protected void setUp() throws Exception {
79 _container = new DefaultContainer("top");
80 _container.addComponent(new ExternalDatasourceComponent("datasource"));
81 _container.addComponent("mappingFiles",
82 new UsermgtHibernateMappingFiles());
83 _container.addComponent(new HibernateComponent("hibernate"));
85 .addComponent(new UserGroupRepositoryComponent("usersgroups"));
87 .addComponent("databaseTester", DatabaseTesterComponent.class);
89 ObjectConfiguration config = new ObjectConfiguration(
90 HibernateUserSetTest.class);
91 config.getSetterConfig().clear().add("datasource").add("userset").add(
92 "groupset").add("databaseTester").add("userCache");
93 _container.addComponent("testcase", this, config);
95 _scope = _container.start();
98 _databaseTester.cleanDatabase();
103 public void setDatasource(DataSource aDatasource) {
104 _datasource = aDatasource;
107 public void setUserset(UserSet aUserset) {
111 public void setGroupset(GroupSet aGroupset) {
112 _groupset = aGroupset;
115 public void setUserCache(EhCache<Serializable, Serializable> aUserCache) {
116 _userCache = aUserCache;
119 public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
120 _databaseTester = aDatabaseTester;
124 protected void tearDown() throws Exception {
125 _container.stop(_scope);
130 * Clears the user cache.
132 private void clearUserCache() {
139 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupCount(int)
142 protected void checkUserCount(int aSize) throws SQLException {
143 _databaseTester.flush();
144 super.checkUserCount(aSize);
145 assertEquals(aSize, _databaseTester.getTableSize(USER_TABLE));
151 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupExists(java.lang.String)
154 protected void checkUserExists(String aUser) throws SQLException {
155 _databaseTester.flush();
156 ResultSet result = _databaseTester.executeQuery(USER_QUERY, aUser);
157 assertEquals(1, _databaseTester.countResultSet(result));
163 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupNotExists(java.lang.String)
166 protected void checkUserNotExists(String aUser) throws SQLException {
167 _databaseTester.flush();
168 ResultSet result = _databaseTester.executeQuery(USER_QUERY, aUser);
169 assertEquals(0, _databaseTester.countResultSet(result));
175 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupCount(int)
178 protected void checkGroupCount(int aSize) throws SQLException {
179 _databaseTester.flush();
180 assertEquals(aSize, _databaseTester.getTableSize(GROUP_TABLE));
186 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupExists(java.lang.String)
189 protected void checkGroupExists(String aGroup) throws SQLException {
190 _databaseTester.flush();
192 ResultSet result = _databaseTester.executeQuery(GROUP_QUERY, aGroup);
193 assertEquals(1, _databaseTester.countResultSet(result));
199 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupNotExists(java.lang.String)
202 protected void checkGroupNotExists(String aGroup) throws SQLException {
203 _databaseTester.flush();
204 ResultSet result = _databaseTester.executeQuery(GROUP_QUERY, aGroup);
205 assertEquals(0, _databaseTester.countResultSet(result));
211 * @see org.wamblee.usermgt.InMemoryGroupSetTest#createGroupSet()
214 protected UserSet createUserSet() {
221 * @see org.wamblee.usermgt.InMemoryUserSetTest#createGroupSet()
224 protected GroupSet createGroupSet() {
229 * Reproduction of a bug. Create a user which is in group1 Add it to a
230 * second group group2. Remove the user from group1. Verify the user is in
233 public void testVerifyAddRemove() throws SQLException, UserMgtException {
234 _databaseTester.cleanDatabase(); // just to be sure.
235 GroupSet groups = getGroups();
236 assertEquals(0, groups.size());
237 Group group1 = createGroup("group1");
238 Group group2 = createGroup("group2");
241 checkGroupExists("group1");
242 checkGroupExists("group2");
244 User user = createUser("user", PASSWORD, group1);
245 getUsers().add(user);
246 checkUserExists("user");
248 addUserToGroup(user, group2);
249 getUsers().userModified(user);
251 User user2 = getUsers().find("user");
252 Set<Group> userGroups = user2.getGroups();
253 assertTrue(user2.isInGroup("group1"));
254 assertTrue(user2.isInGroup("group2"));
255 assertEquals(2, userGroups.size());
257 removeUserFromGroup(user, group1);
258 getUsers().userModified(user);
260 user2 = getUsers().find("user");
261 userGroups = user2.getGroups();
262 assertFalse(user2.isInGroup("group1"));
263 assertTrue(user2.isInGroup("group2"));
264 assertEquals(1, userGroups.size());