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 javax.sql.DataSource;
25 import org.wamblee.cache.Cache;
26 import org.wamblee.general.BeanKernel;
27 import org.wamblee.system.adapters.DefaultContainer;
28 import org.wamblee.system.adapters.ObjectConfiguration;
29 import org.wamblee.system.core.Component;
30 import org.wamblee.system.core.Scope;
31 import org.wamblee.usermgt.Group;
32 import org.wamblee.usermgt.GroupSet;
33 import org.wamblee.usermgt.InMemoryUserSetTest;
34 import org.wamblee.usermgt.User;
35 import org.wamblee.usermgt.UserMgtException;
36 import org.wamblee.usermgt.UserSet;
37 import org.wamblee.usermgt.UsermgtHibernateMappingFiles;
38 import org.wamblee.usermgt.UsermgtSpringConfigFiles;
41 * Tests for {@link org.wamblee.usermgt.hibernate.HibernateGroupSet}
43 * @author Erik Brakkee
45 public class HibernateUserSetTest extends InMemoryUserSetTest {
47 private static final String USER_TABLE = "USERS";
48 private static final String GROUP_TABLE = "GROUPS";
50 private static final String USER_QUERY = "select * from " + USER_TABLE + " where name = ?";
51 private static final String GROUP_QUERY = "select * from " + GROUP_TABLE + " where name = ?";
53 public HibernateUserSetTest() {
54 super(UsermgtSpringConfigFiles.class, UsermgtHibernateMappingFiles.class);
57 private DefaultContainer _container;
60 private DataSource _datasource;
63 * @see org.wamblee.usermgt.InMemoryUserSetTest#setUp()
66 protected void setUp() throws Exception {
69 _container = new DefaultContainer("top");
70 Component ds = new ExternalDatasourceComponent("datasource");
71 _container.addComponent(ds);
73 ObjectConfiguration config = new ObjectConfiguration(HibernateUserSetTest.class);
74 config.getSetterConfig().clear().add("datasource");
75 _container.addComponent("testcase", this, config);
76 _scope = _container.start();
78 Object my = _scope.getInterfaceImplementation(ds.getProvidedInterfaces()[0], Object.class);
83 public void setDatasource(DataSource aDatasource) {
84 _datasource = aDatasource;
89 protected void tearDown() throws Exception {
90 _container.stop(_scope);
95 * Clears the user cache.
97 private void clearUserCache() {
98 BeanKernel.getBeanFactory().find("userCache", Cache.class).clear();
102 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupCount(int)
105 protected void checkUserCount(int aSize) throws SQLException {
107 super.checkUserCount(aSize);
108 assertEquals(aSize, getTableSize(USER_TABLE));
112 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupExists(java.lang.String)
115 protected void checkUserExists(String aUser) throws SQLException {
117 ResultSet result = executeQuery(USER_QUERY, aUser);
118 assertEquals(1, countResultSet(result));
122 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupNotExists(java.lang.String)
125 protected void checkUserNotExists(String aUser) throws SQLException {
127 ResultSet result = executeQuery(USER_QUERY, aUser);
128 assertEquals(0, countResultSet(result));
132 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupCount(int)
135 protected void checkGroupCount(int aSize) throws SQLException {
137 assertEquals(aSize, getTableSize(GROUP_TABLE));
141 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupExists(java.lang.String)
144 protected void checkGroupExists(String aGroup) throws SQLException {
147 ResultSet result = executeQuery(GROUP_QUERY, aGroup);
148 assertEquals(1, countResultSet(result));
152 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupNotExists(java.lang.String)
155 protected void checkGroupNotExists(String aGroup) throws SQLException {
157 ResultSet result = executeQuery(GROUP_QUERY, aGroup);
158 assertEquals(0, countResultSet(result));
162 * @see org.wamblee.usermgt.InMemoryGroupSetTest#createGroupSet()
165 protected UserSet createUserSet() {
166 return BeanKernel.getBeanFactory().find(UserSet.class);
170 * @see org.wamblee.usermgt.InMemoryUserSetTest#createGroupSet()
173 protected GroupSet createGroupSet() {
174 return BeanKernel.getBeanFactory().find(GroupSet.class);
178 * Reproduction of a bug.
179 * Create a user which is in group1
180 * Add it to a second group group2.
181 * Remove the user from group1.
182 * Verify the user is in group2.
184 public void testVerifyAddRemove() throws SQLException, UserMgtException {
185 cleanDatabase(); // just to be sure.
186 GroupSet groups = getGroups();
187 assertEquals(0, groups.size());
188 Group group1 = createGroup("group1");
189 Group group2 = createGroup("group2");
192 checkGroupExists("group1");
193 checkGroupExists("group2");
195 User user = createUser("user", PASSWORD, group1);
196 getUsers().add(user);
197 checkUserExists("user");
199 addUserToGroup(user, group2);
200 getUsers().userModified(user);
202 User user2 = getUsers().find("user");
203 Set<Group> userGroups = user2.getGroups();
204 assertTrue(user2.isInGroup("group1"));
205 assertTrue(user2.isInGroup("group2"));
206 assertEquals(2, userGroups.size());
208 removeUserFromGroup(user, group1);
209 getUsers().userModified(user);
211 user2 = getUsers().find("user");
212 userGroups = user2.getGroups();
213 assertFalse(user2.isInGroup("group1"));
214 assertTrue(user2.isInGroup("group2"));
215 assertEquals(1, userGroups.size());