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 private DefaultContainer _container;
60 private UserSet _userset;
61 private GroupSet _groupset;
62 private EhCache<Serializable, Serializable> _userCache;
63 private DatabaseTesterComponent _databaseTester;
68 * @see org.wamblee.usermgt.InMemoryUserSetTest#setUp()
71 protected void setUp() throws Exception {
73 _container = new UserMgtRepositoryTestContainer("top");
75 ObjectConfiguration config = new ObjectConfiguration(
76 HibernateUserSetTest.class);
77 config.getSetterConfig().clear().add("userset").add(
78 "groupset").add("databaseTester").add("userCache");
79 _container.addComponent("testcase", this, config);
81 _scope = _container.start();
84 _databaseTester.cleanDatabase();
89 public void setUserset(UserSet aUserset) {
93 public void setGroupset(GroupSet aGroupset) {
94 _groupset = aGroupset;
97 public void setUserCache(EhCache<Serializable, Serializable> aUserCache) {
98 _userCache = aUserCache;
101 public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
102 _databaseTester = aDatabaseTester;
106 protected void tearDown() throws Exception {
107 _container.stop(_scope);
112 * Clears the user cache.
114 private void clearUserCache() {
121 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupCount(int)
124 protected void checkUserCount(int aSize) throws SQLException {
125 _databaseTester.flush();
126 super.checkUserCount(aSize);
127 assertEquals(aSize, _databaseTester.getTableSize(USER_TABLE));
133 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupExists(java.lang.String)
136 protected void checkUserExists(String aUser) throws SQLException {
137 _databaseTester.flush();
138 ResultSet result = _databaseTester.executeQuery(USER_QUERY, aUser);
139 assertEquals(1, _databaseTester.countResultSet(result));
145 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupNotExists(java.lang.String)
148 protected void checkUserNotExists(String aUser) throws SQLException {
149 _databaseTester.flush();
150 ResultSet result = _databaseTester.executeQuery(USER_QUERY, aUser);
151 assertEquals(0, _databaseTester.countResultSet(result));
157 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupCount(int)
160 protected void checkGroupCount(int aSize) throws SQLException {
161 _databaseTester.flush();
162 assertEquals(aSize, _databaseTester.getTableSize(GROUP_TABLE));
168 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupExists(java.lang.String)
171 protected void checkGroupExists(String aGroup) throws SQLException {
172 _databaseTester.flush();
174 ResultSet result = _databaseTester.executeQuery(GROUP_QUERY, aGroup);
175 assertEquals(1, _databaseTester.countResultSet(result));
181 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupNotExists(java.lang.String)
184 protected void checkGroupNotExists(String aGroup) throws SQLException {
185 _databaseTester.flush();
186 ResultSet result = _databaseTester.executeQuery(GROUP_QUERY, aGroup);
187 assertEquals(0, _databaseTester.countResultSet(result));
193 * @see org.wamblee.usermgt.InMemoryGroupSetTest#createGroupSet()
196 protected UserSet createUserSet() {
203 * @see org.wamblee.usermgt.InMemoryUserSetTest#createGroupSet()
206 protected GroupSet createGroupSet() {
211 * Reproduction of a bug. Create a user which is in group1 Add it to a
212 * second group group2. Remove the user from group1. Verify the user is in
215 public void testVerifyAddRemove() throws SQLException, UserMgtException {
216 _databaseTester.cleanDatabase(); // just to be sure.
217 GroupSet groups = getGroups();
218 assertEquals(0, groups.size());
219 Group group1 = createGroup("group1");
220 Group group2 = createGroup("group2");
223 checkGroupExists("group1");
224 checkGroupExists("group2");
226 User user = createUser("user", PASSWORD, group1);
227 getUsers().add(user);
228 checkUserExists("user");
230 addUserToGroup(user, group2);
231 getUsers().userModified(user);
233 User user2 = getUsers().find("user");
234 Set<Group> userGroups = user2.getGroups();
235 assertTrue(user2.isInGroup("group1"));
236 assertTrue(user2.isInGroup("group2"));
237 assertEquals(2, userGroups.size());
239 removeUserFromGroup(user, group1);
240 getUsers().userModified(user);
242 user2 = getUsers().find("user");
243 userGroups = user2.getGroups();
244 assertFalse(user2.isInGroup("group1"));
245 assertTrue(user2.isInGroup("group2"));
246 assertEquals(1, userGroups.size());