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.sql.ResultSet;
20 import java.sql.SQLException;
23 import org.wamblee.cache.Cache;
24 import org.wamblee.general.BeanKernel;
25 import org.wamblee.usermgt.Group;
26 import org.wamblee.usermgt.GroupSet;
27 import org.wamblee.usermgt.InMemoryUserSetTest;
28 import org.wamblee.usermgt.User;
29 import org.wamblee.usermgt.UserMgtException;
30 import org.wamblee.usermgt.UserSet;
31 import org.wamblee.usermgt.UsermgtHibernateMappingFiles;
32 import org.wamblee.usermgt.UsermgtSpringConfigFiles;
35 * Tests for {@link org.wamblee.usermgt.hibernate.HibernateGroupSet}
37 * @author Erik Brakkee
39 public class HibernateUserSetTest extends InMemoryUserSetTest {
41 private static final String USER_TABLE = "USERS";
42 private static final String GROUP_TABLE = "GROUPS";
44 private static final String USER_QUERY = "select * from " + USER_TABLE + " where name = ?";
45 private static final String GROUP_QUERY = "select * from " + GROUP_TABLE + " where name = ?";
47 public HibernateUserSetTest() {
48 super(UsermgtSpringConfigFiles.class, UsermgtHibernateMappingFiles.class);
52 * @see org.wamblee.usermgt.InMemoryUserSetTest#setUp()
55 protected void setUp() throws Exception {
61 * Clears the user cache.
63 private void clearUserCache() {
64 BeanKernel.getBeanFactory().find("userCache", Cache.class).clear();
68 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupCount(int)
71 protected void checkUserCount(int aSize) throws SQLException {
73 super.checkUserCount(aSize);
74 assertEquals(aSize, getTableSize(USER_TABLE));
78 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupExists(java.lang.String)
81 protected void checkUserExists(String aUser) throws SQLException {
83 ResultSet result = executeQuery(USER_QUERY, aUser);
84 assertEquals(1, countResultSet(result));
88 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupNotExists(java.lang.String)
91 protected void checkUserNotExists(String aUser) throws SQLException {
93 ResultSet result = executeQuery(USER_QUERY, aUser);
94 assertEquals(0, countResultSet(result));
98 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupCount(int)
101 protected void checkGroupCount(int aSize) throws SQLException {
103 assertEquals(aSize, getTableSize(GROUP_TABLE));
107 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupExists(java.lang.String)
110 protected void checkGroupExists(String aGroup) throws SQLException {
113 ResultSet result = executeQuery(GROUP_QUERY, aGroup);
114 assertEquals(1, countResultSet(result));
118 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupNotExists(java.lang.String)
121 protected void checkGroupNotExists(String aGroup) throws SQLException {
123 ResultSet result = executeQuery(GROUP_QUERY, aGroup);
124 assertEquals(0, countResultSet(result));
128 * @see org.wamblee.usermgt.InMemoryGroupSetTest#createGroupSet()
131 protected UserSet createUserSet() {
132 return BeanKernel.getBeanFactory().find(UserSet.class);
136 * @see org.wamblee.usermgt.InMemoryUserSetTest#createGroupSet()
139 protected GroupSet createGroupSet() {
140 return BeanKernel.getBeanFactory().find(GroupSet.class);
144 * Reproduction of a bug.
145 * Create a user which is in group1
146 * Add it to a second group group2.
147 * Remove the user from group1.
148 * Verify the user is in group2.
150 public void testVerifyAddRemove() throws SQLException, UserMgtException {
151 cleanDatabase(); // just to be sure.
152 GroupSet groups = getGroups();
153 assertEquals(0, groups.size());
154 Group group1 = createGroup("group1");
155 Group group2 = createGroup("group2");
158 checkGroupExists("group1");
159 checkGroupExists("group2");
161 User user = createUser("user", PASSWORD, group1);
162 getUsers().add(user);
163 checkUserExists("user");
165 addUserToGroup(user, group2);
166 getUsers().userModified(user);
168 User user2 = getUsers().find("user");
169 Set<Group> userGroups = user2.getGroups();
170 assertTrue(user2.isInGroup("group1"));
171 assertTrue(user2.isInGroup("group2"));
172 assertEquals(2, userGroups.size());
174 removeUserFromGroup(user, group1);
175 getUsers().userModified(user);
177 user2 = getUsers().find("user");
178 userGroups = user2.getGroups();
179 assertFalse(user2.isInGroup("group1"));
180 assertTrue(user2.isInGroup("group2"));
181 assertEquals(1, userGroups.size());