2 * Copyright 2005-2010 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.
16 package org.wamblee.security.authentication.jpa;
18 import java.sql.Connection;
19 import java.sql.ResultSet;
22 import org.wamblee.cache.EhCache;
23 import org.wamblee.io.ClassPathResource;
24 import org.wamblee.security.authentication.Group;
25 import org.wamblee.security.authentication.GroupSet;
26 import org.wamblee.security.authentication.InMemoryUserSetTest;
27 import org.wamblee.security.authentication.NameValidator;
28 import org.wamblee.security.authentication.RegexpNameValidator;
29 import org.wamblee.security.authentication.User;
30 import org.wamblee.security.authentication.UserSet;
31 import org.wamblee.security.authentication.jpa.JpaGroupSet;
32 import org.wamblee.security.authentication.jpa.JpaUserSet;
33 import org.wamblee.security.encryption.Md5HexMessageDigester;
34 import org.wamblee.security.encryption.MessageDigester;
35 import org.wamblee.support.persistence.JpaTester;
36 import org.wamblee.support.persistence.TransactionProxyFactory;
37 import org.wamblee.support.persistence.DatabaseUtils.JdbcUnitOfWork;
40 * Tests for {@link org.wamblee.usermgt.hibernate.HibernateGroupSet}
42 * @author Erik Brakkee
44 public class JpaUserSetTest extends InMemoryUserSetTest {
45 private static final String USER_TABLE = "SEC_USER";
47 private static final String GROUP_TABLE = "SEC_GROUP";
49 private static final String USER_QUERY = "select * from " + USER_TABLE +
52 private static final String GROUP_QUERY = "select * from " + GROUP_TABLE +
55 private UserSet userset;
57 private GroupSet groupset;
59 private EhCache<String, User> userCache;
61 private JpaTester jpaTester;
66 * @see org.wamblee.usermgt.InMemoryUserSetTest#setUp()
69 protected void setUp() throws Exception {
70 jpaTester = new JpaTester(new SecurityPersistenceUnit());
73 userCache = new EhCache<String, User>(
74 new ClassPathResource("properties/org.wamblee.security.ehcache.xml"), "users");
76 userset = createUserSetImpl();
77 groupset = createGroupSetImpl();
84 private UserSet createUserSetImpl() {
85 NameValidator passwordValidator = new RegexpNameValidator(
86 ".{5,}", "INVALID_PASSWORD", "Password must have at least 5 characters");
88 MessageDigester passwordDigester = new Md5HexMessageDigester();
89 TransactionProxyFactory<UserSet> factory = new TransactionProxyFactory<UserSet>(
90 jpaTester.getJpaBuilder(), UserSet.class);
91 UserSet jpaUserset = new JpaUserSet(userCache, passwordValidator, passwordDigester,
92 factory.getTransactionScopedEntityManager());
93 return factory.getProxy(jpaUserset);
96 private GroupSet createGroupSetImpl() {
97 TransactionProxyFactory<GroupSet> factory = new TransactionProxyFactory<GroupSet>(
98 jpaTester.getJpaBuilder(), GroupSet.class);
99 GroupSet groups = new JpaGroupSet(factory.getTransactionScopedEntityManager());
100 GroupSet proxy = factory.getProxy(groups);
105 protected void tearDown() throws Exception {
111 * Clears the user cache.
113 private void clearUserCache() {
120 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupCount(int)
123 protected void checkUserCount(int aSize) throws Exception {
124 super.checkUserCount(aSize);
125 assertEquals(aSize, jpaTester.getDbUtils().getTableSize(USER_TABLE));
132 * org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupExists(java.lang.String
136 protected void checkUserExists(final String aUser) throws Exception {
137 assertEquals(1, countUser(aUser));
140 private int countUser(final String aUser) throws Exception {
141 int count = jpaTester.getDbUtils().executeInTransaction(new JdbcUnitOfWork<Integer>() {
143 public Integer execute(Connection aConnection) throws Exception {
144 ResultSet res = jpaTester.getDbUtils().executeQuery(aConnection, USER_QUERY, aUser);
145 return jpaTester.getDbUtils().countResultSet(res);
155 * org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupNotExists(java.lang
159 protected void checkUserNotExists(String aUser) throws Exception {
160 assertEquals(0, countUser(aUser));
166 * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupCount(int)
169 protected void checkGroupCount(int aSize) throws Exception {
170 assertEquals(aSize, jpaTester.getDbUtils().getTableSize(GROUP_TABLE));
173 private int countGroup(final String aGroup) throws Exception {
174 int count = jpaTester.getDbUtils().executeInTransaction(new JdbcUnitOfWork<Integer>() {
176 public Integer execute(Connection aConnection) throws Exception {
177 ResultSet res = jpaTester.getDbUtils().executeQuery(aConnection, GROUP_QUERY, aGroup);
178 return jpaTester.getDbUtils().countResultSet(res);
188 * org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupExists(java.lang.String
192 protected void checkGroupExists(String aGroup) throws Exception {
193 assertEquals(1, countGroup(aGroup));
200 * org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupNotExists(java.lang
204 protected void checkGroupNotExists(String aGroup) throws Exception {
205 assertEquals(0, countGroup(aGroup));
211 * @see org.wamblee.usermgt.InMemoryGroupSetTest#createGroupSet()
214 protected UserSet createUserSet() {
221 * @see org.wamblee.usermgt.InMemoryUserSetTest#createGroupSet()
224 protected GroupSet createGroupSet() {
229 * Reproduction of a bug. Create a user which is in group1 Add it to a
230 * second group group2. Remove the user from group1. Verify the user is in
234 public void testVerifyAddRemove() throws Exception {
235 jpaTester.getDbUtils().cleanDatabase(); // super class setup always creates one group.
237 GroupSet groups = getGroups();
238 assertEquals(0, groups.size());
240 Group group1 = createGroup("group1");
241 Group group2 = createGroup("group2");
244 checkGroupExists("group1");
245 checkGroupExists("group2");
247 User user = createUser("user", PASSWORD, group1);
248 getUsers().add(user);
249 checkUserExists("user");
251 addUserToGroup(user, group2);
252 getUsers().userModified(user);
255 User user2 = getUsers().find("user");
257 Set<Group> userGroups = user2.getGroups();
258 assertTrue(user2.isInGroup("group1"));
259 assertTrue(user2.isInGroup("group2"));
260 assertEquals(2, userGroups.size());
262 removeUserFromGroup(user, group1);
263 getUsers().userModified(user);
265 user2 = getUsers().find("user");
266 userGroups = user2.getGroups();
267 assertFalse(user2.isInGroup("group1"));
268 assertTrue(user2.isInGroup("group2"));
269 assertEquals(1, userGroups.size());