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 org.wamblee.cache.EhCache;
25 import org.wamblee.system.adapters.DefaultContainer;
26 import org.wamblee.system.adapters.ObjectConfiguration;
27 import org.wamblee.system.core.Scope;
28 import org.wamblee.system.spring.DatabaseTesterComponent;
29 import org.wamblee.usermgt.Group;
30 import org.wamblee.usermgt.GroupSet;
31 import org.wamblee.usermgt.InMemoryUserSetTest;
32 import org.wamblee.usermgt.User;
33 import org.wamblee.usermgt.UserMgtException;
34 import org.wamblee.usermgt.UserSet;
37 * Tests for {@link org.wamblee.usermgt.hibernate.HibernateGroupSet}
39 * @author Erik Brakkee
41 public class HibernateUserSetTest extends InMemoryUserSetTest {
43 private static final String USER_TABLE = "USERS";
44 private static final String GROUP_TABLE = "GROUPS";
46 private static final String USER_QUERY = "select * from " + USER_TABLE
48 private static final String GROUP_QUERY = "select * from " + GROUP_TABLE
51 private DefaultContainer _container;
54 private UserSet _userset;
55 private GroupSet _groupset;
56 private EhCache<Serializable, Serializable> _userCache;
57 private DatabaseTesterComponent _databaseTester;
62 * @see org.wamblee.usermgt.InMemoryUserSetTest#setUp()
65 protected void setUp() throws Exception {
67 _container = new UserMgtRepositoryTestContainer("top");
69 ObjectConfiguration config = new ObjectConfiguration(
70 HibernateUserSetTest.class);
71 config.getSetterConfig().clear().add("userset").add(
72 "groupset").add("databaseTester").add("userCache");
73 _container.addComponent("testcase", this, config);
75 _scope = _container.start();
78 _databaseTester.cleanDatabase();
83 public void setUserset(UserSet aUserset) {
87 public void setGroupset(GroupSet aGroupset) {
88 _groupset = aGroupset;
91 public void setUserCache(EhCache<Serializable, Serializable> aUserCache) {
92 _userCache = aUserCache;
95 public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
96 _databaseTester = aDatabaseTester;
100 protected void tearDown() throws Exception {
101 _container.stop(_scope);
106 * Clears the user cache.
108 private void clearUserCache() {
115 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupCount(int)
118 protected void checkUserCount(int aSize) throws SQLException {
119 _databaseTester.flush();
120 super.checkUserCount(aSize);
121 assertEquals(aSize, _databaseTester.getTableSize(USER_TABLE));
127 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupExists(java.lang.String)
130 protected void checkUserExists(String aUser) throws SQLException {
131 _databaseTester.flush();
132 ResultSet result = _databaseTester.executeQuery(USER_QUERY, aUser);
133 assertEquals(1, _databaseTester.countResultSet(result));
139 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupNotExists(java.lang.String)
142 protected void checkUserNotExists(String aUser) throws SQLException {
143 _databaseTester.flush();
144 ResultSet result = _databaseTester.executeQuery(USER_QUERY, aUser);
145 assertEquals(0, _databaseTester.countResultSet(result));
151 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupCount(int)
154 protected void checkGroupCount(int aSize) throws SQLException {
155 _databaseTester.flush();
156 assertEquals(aSize, _databaseTester.getTableSize(GROUP_TABLE));
162 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupExists(java.lang.String)
165 protected void checkGroupExists(String aGroup) throws SQLException {
166 _databaseTester.flush();
168 ResultSet result = _databaseTester.executeQuery(GROUP_QUERY, aGroup);
169 assertEquals(1, _databaseTester.countResultSet(result));
175 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupNotExists(java.lang.String)
178 protected void checkGroupNotExists(String aGroup) throws SQLException {
179 _databaseTester.flush();
180 ResultSet result = _databaseTester.executeQuery(GROUP_QUERY, aGroup);
181 assertEquals(0, _databaseTester.countResultSet(result));
187 * @see org.wamblee.usermgt.InMemoryGroupSetTest#createGroupSet()
190 protected UserSet createUserSet() {
197 * @see org.wamblee.usermgt.InMemoryUserSetTest#createGroupSet()
200 protected GroupSet createGroupSet() {
205 * Reproduction of a bug. Create a user which is in group1 Add it to a
206 * second group group2. Remove the user from group1. Verify the user is in
209 public void testVerifyAddRemove() throws SQLException, UserMgtException {
210 _databaseTester.cleanDatabase(); // just to be sure.
211 GroupSet groups = getGroups();
212 assertEquals(0, groups.size());
213 Group group1 = createGroup("group1");
214 Group group2 = createGroup("group2");
217 checkGroupExists("group1");
218 checkGroupExists("group2");
220 User user = createUser("user", PASSWORD, group1);
221 getUsers().add(user);
222 checkUserExists("user");
224 addUserToGroup(user, group2);
225 getUsers().userModified(user);
227 User user2 = getUsers().find("user");
228 Set<Group> userGroups = user2.getGroups();
229 assertTrue(user2.isInGroup("group1"));
230 assertTrue(user2.isInGroup("group2"));
231 assertEquals(2, userGroups.size());
233 removeUserFromGroup(user, group1);
234 getUsers().userModified(user);
236 user2 = getUsers().find("user");
237 userGroups = user2.getGroups();
238 assertFalse(user2.isInGroup("group1"));
239 assertTrue(user2.isInGroup("group2"));
240 assertEquals(1, userGroups.size());