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.UsermgtSpringConfigFiles;
42 * Tests for {@link org.wamblee.usermgt.hibernate.HibernateGroupSet}
44 * @author Erik Brakkee
46 public class HibernateUserSetTest extends InMemoryUserSetTest {
48 private static final String USER_TABLE = "USERS";
49 private static final String GROUP_TABLE = "GROUPS";
51 private static final String USER_QUERY = "select * from " + USER_TABLE
53 private static final String GROUP_QUERY = "select * from " + GROUP_TABLE
56 private DefaultContainer _container;
59 private UserSet _userset;
60 private GroupSet _groupset;
61 private EhCache<Serializable, Serializable> _userCache;
62 private DatabaseTesterComponent _databaseTester;
67 * @see org.wamblee.usermgt.InMemoryUserSetTest#setUp()
70 protected void setUp() throws Exception {
72 _container = new UserMgtRepositoryTestContainer("top");
74 ObjectConfiguration config = new ObjectConfiguration(
75 HibernateUserSetTest.class);
76 config.getSetterConfig().clear().add("userset").add(
77 "groupset").add("databaseTester").add("userCache");
78 _container.addComponent("testcase", this, config);
80 _scope = _container.start();
83 _databaseTester.cleanDatabase();
88 public void setUserset(UserSet aUserset) {
92 public void setGroupset(GroupSet aGroupset) {
93 _groupset = aGroupset;
96 public void setUserCache(EhCache<Serializable, Serializable> aUserCache) {
97 _userCache = aUserCache;
100 public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
101 _databaseTester = aDatabaseTester;
105 protected void tearDown() throws Exception {
106 _container.stop(_scope);
111 * Clears the user cache.
113 private void clearUserCache() {
120 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupCount(int)
123 protected void checkUserCount(int aSize) throws SQLException {
124 _databaseTester.flush();
125 super.checkUserCount(aSize);
126 assertEquals(aSize, _databaseTester.getTableSize(USER_TABLE));
132 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupExists(java.lang.String)
135 protected void checkUserExists(String aUser) throws SQLException {
136 _databaseTester.flush();
137 ResultSet result = _databaseTester.executeQuery(USER_QUERY, aUser);
138 assertEquals(1, _databaseTester.countResultSet(result));
144 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupNotExists(java.lang.String)
147 protected void checkUserNotExists(String aUser) throws SQLException {
148 _databaseTester.flush();
149 ResultSet result = _databaseTester.executeQuery(USER_QUERY, aUser);
150 assertEquals(0, _databaseTester.countResultSet(result));
156 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupCount(int)
159 protected void checkGroupCount(int aSize) throws SQLException {
160 _databaseTester.flush();
161 assertEquals(aSize, _databaseTester.getTableSize(GROUP_TABLE));
167 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupExists(java.lang.String)
170 protected void checkGroupExists(String aGroup) throws SQLException {
171 _databaseTester.flush();
173 ResultSet result = _databaseTester.executeQuery(GROUP_QUERY, aGroup);
174 assertEquals(1, _databaseTester.countResultSet(result));
180 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupNotExists(java.lang.String)
183 protected void checkGroupNotExists(String aGroup) throws SQLException {
184 _databaseTester.flush();
185 ResultSet result = _databaseTester.executeQuery(GROUP_QUERY, aGroup);
186 assertEquals(0, _databaseTester.countResultSet(result));
192 * @see org.wamblee.usermgt.InMemoryGroupSetTest#createGroupSet()
195 protected UserSet createUserSet() {
202 * @see org.wamblee.usermgt.InMemoryUserSetTest#createGroupSet()
205 protected GroupSet createGroupSet() {
210 * Reproduction of a bug. Create a user which is in group1 Add it to a
211 * second group group2. Remove the user from group1. Verify the user is in
214 public void testVerifyAddRemove() throws SQLException, UserMgtException {
215 _databaseTester.cleanDatabase(); // just to be sure.
216 GroupSet groups = getGroups();
217 assertEquals(0, groups.size());
218 Group group1 = createGroup("group1");
219 Group group2 = createGroup("group2");
222 checkGroupExists("group1");
223 checkGroupExists("group2");
225 User user = createUser("user", PASSWORD, group1);
226 getUsers().add(user);
227 checkUserExists("user");
229 addUserToGroup(user, group2);
230 getUsers().userModified(user);
232 User user2 = getUsers().find("user");
233 Set<Group> userGroups = user2.getGroups();
234 assertTrue(user2.isInGroup("group1"));
235 assertTrue(user2.isInGroup("group2"));
236 assertEquals(2, userGroups.size());
238 removeUserFromGroup(user, group1);
239 getUsers().userModified(user);
241 user2 = getUsers().find("user");
242 userGroups = user2.getGroups();
243 assertFalse(user2.isInGroup("group1"));
244 assertTrue(user2.isInGroup("group2"));
245 assertEquals(1, userGroups.size());