From 1649e57210e0ce7319ec1816a3e5de24883807db Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Fri, 30 Apr 2010 10:14:16 +0000 Subject: [PATCH] --- .../wamblee/security/AbstractPersistent.java | 22 +++++--------- .../main/java/org/wamblee/usermgt/Group.java | 2 ++ .../main/java/org/wamblee/usermgt/User.java | 29 +++++++++++++++---- .../wamblee/usermgt/UserAdministration.java | 1 + .../wamblee/usermgt/InMemoryUserSetTest.java | 26 ++++++++--------- .../usermgt/hibernate/HibernateUserSet.java | 2 +- .../hibernate/HibernateUserSetTest.java | 2 +- .../wamblee/usermgt/jpa/JpaGroupSetTest.java | 4 ++- 8 files changed, 51 insertions(+), 37 deletions(-) diff --git a/security/impl/src/main/java/org/wamblee/security/AbstractPersistent.java b/security/impl/src/main/java/org/wamblee/security/AbstractPersistent.java index f5944843..cd2837dd 100644 --- a/security/impl/src/main/java/org/wamblee/security/AbstractPersistent.java +++ b/security/impl/src/main/java/org/wamblee/security/AbstractPersistent.java @@ -17,11 +17,6 @@ package org.wamblee.security; import java.io.Serializable; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.Version; - import org.wamblee.persistence.Persistent; /** @@ -30,13 +25,10 @@ import org.wamblee.persistence.Persistent; * @author Erik Brakkee */ public abstract class AbstractPersistent implements Persistent { - - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private Long primaryKey; + + private Serializable primaryKey; - @Version - private int version; + private Number version; protected AbstractPersistent() { primaryKey = null; @@ -60,7 +52,7 @@ public abstract class AbstractPersistent implements Persistent { * @see org.wamblee.persistence.Persistent#getPrimaryKey() */ @Override - public Long getPrimaryKey() { + public Serializable getPrimaryKey() { return primaryKey; } @@ -71,7 +63,7 @@ public abstract class AbstractPersistent implements Persistent { * org.wamblee.persistence.Persistent#setPrimaryKey(java.io.Serializable) */ @Override - public void setPrimaryKey(Long aKey) { + public void setPrimaryKey(Serializable aKey) { primaryKey = aKey; } @@ -80,7 +72,7 @@ public abstract class AbstractPersistent implements Persistent { * * @see org.wamblee.persistence.Persistent#getPersistedVersion() */ - public int getPersistedVersion() { + public Number getPersistedVersion() { return version; } @@ -89,7 +81,7 @@ public abstract class AbstractPersistent implements Persistent { * * @see org.wamblee.persistence.Persistent#setPersistedVersion(int) */ - public void setPersistedVersion(int aVersion) { + public void setPersistedVersion(Number aVersion) { version = aVersion; } } diff --git a/security/impl/src/main/java/org/wamblee/usermgt/Group.java b/security/impl/src/main/java/org/wamblee/usermgt/Group.java index 275cc081..697f1b5b 100644 --- a/security/impl/src/main/java/org/wamblee/usermgt/Group.java +++ b/security/impl/src/main/java/org/wamblee/usermgt/Group.java @@ -77,6 +77,8 @@ public class Group implements Serializable, Comparable { * */ public Group(Group aGroup) { + primaryKey = aGroup.primaryKey; + version = aGroup.version; name = aGroup.name; } diff --git a/security/impl/src/main/java/org/wamblee/usermgt/User.java b/security/impl/src/main/java/org/wamblee/usermgt/User.java index 8bccfaba..00280bcc 100644 --- a/security/impl/src/main/java/org/wamblee/usermgt/User.java +++ b/security/impl/src/main/java/org/wamblee/usermgt/User.java @@ -25,11 +25,15 @@ import java.io.Serializable; import java.util.Set; import java.util.TreeSet; +import javax.persistence.CascadeType; import javax.persistence.Entity; +import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToMany; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; import javax.persistence.Table; import javax.persistence.Transient; import javax.persistence.Version; @@ -41,16 +45,27 @@ import javax.persistence.Version; */ @Entity @Table(name = "SEC_USER") -public class User implements Serializable, - Comparable { - +@NamedQueries( { + @NamedQuery(name = User.QUERY_FIND_BY_NAME, query = "select u from User u where u.name = :" + + User.NAME_PARAM), + @NamedQuery(name = User.QUERY_FIND_BY_GROUP_NAME, query = "select user from User user join user.groups grp where grp.name = :name"), + @NamedQuery(name = User.QUERY_COUNT_USERS, query = "select count(u) from User u"), + @NamedQuery(name = User.QUERY_ALL_USERS, query = "select u from User u")}) +public class User implements Serializable, Comparable { + + public static final String QUERY_FIND_BY_NAME = "User.findByName"; + public static final String QUERY_FIND_BY_GROUP_NAME = "User.findByGroupName"; + public static final String QUERY_COUNT_USERS = "User.count"; + public static final String QUERY_ALL_USERS = "User.all"; + public static final String NAME_PARAM = "name"; + @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long primaryKey; @Version private int version; - + /** * User name. */ @@ -64,7 +79,7 @@ public class User implements Serializable, /** * Groups the user belongs to. */ - @ManyToMany + @ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.EAGER) private Set groups; /** @@ -107,6 +122,8 @@ public class User implements Serializable, * */ public User(User aUser) { + primaryKey = aUser.primaryKey; + version = aUser.version; name = aUser.name; password = aUser.password; groups = new TreeSet(); @@ -385,7 +402,7 @@ public class User implements Serializable, public int compareTo(Object aUser) { return name.compareTo(((User) aUser).name); } - + public Long getPrimaryKey() { return primaryKey; } diff --git a/security/impl/src/main/java/org/wamblee/usermgt/UserAdministration.java b/security/impl/src/main/java/org/wamblee/usermgt/UserAdministration.java index 4ebfe3b4..28e05b9d 100644 --- a/security/impl/src/main/java/org/wamblee/usermgt/UserAdministration.java +++ b/security/impl/src/main/java/org/wamblee/usermgt/UserAdministration.java @@ -71,6 +71,7 @@ public interface UserAdministration { * * @param aUser * User. + * @return The modified user. The user passed in to this call should be considered invalid. */ void userModified(User aUser); diff --git a/security/impl/src/test/java/org/wamblee/usermgt/InMemoryUserSetTest.java b/security/impl/src/test/java/org/wamblee/usermgt/InMemoryUserSetTest.java index d242727f..5c885376 100644 --- a/security/impl/src/test/java/org/wamblee/usermgt/InMemoryUserSetTest.java +++ b/security/impl/src/test/java/org/wamblee/usermgt/InMemoryUserSetTest.java @@ -108,7 +108,7 @@ public class InMemoryUserSetTest extends TestCase { * User to check for existence. * */ - protected void checkUserExists(String aUser) throws SQLException { + protected void checkUserExists(String aUser) throws Exception { // Empty } @@ -119,7 +119,7 @@ public class InMemoryUserSetTest extends TestCase { * User to check for non-existence. * */ - protected void checkUserNotExists(String aUser) throws SQLException { + protected void checkUserNotExists(String aUser) throws Exception { // Empty } @@ -130,7 +130,7 @@ public class InMemoryUserSetTest extends TestCase { * Expected number of users. * */ - protected void checkUserCount(int aSize) throws SQLException { + protected void checkUserCount(int aSize) throws Exception { assertEquals(aSize, users.size()); } @@ -141,7 +141,7 @@ public class InMemoryUserSetTest extends TestCase { * User to check for existence. * */ - protected void checkGroupExists(String aUser) throws SQLException { + protected void checkGroupExists(String aUser) throws Exception { // Empty } @@ -152,7 +152,7 @@ public class InMemoryUserSetTest extends TestCase { * User to check for non-existence. * */ - protected void checkGroupNotExists(String aUser) throws SQLException { + protected void checkGroupNotExists(String aUser) throws Exception { // Empty } @@ -163,7 +163,7 @@ public class InMemoryUserSetTest extends TestCase { * Expected number of users. * */ - protected void checkGroupCount(int aSize) throws SQLException { + protected void checkGroupCount(int aSize) throws Exception { // Empty } @@ -172,7 +172,7 @@ public class InMemoryUserSetTest extends TestCase { * contains(). * */ - public void testAdd() throws SQLException, UserMgtException { + public void testAdd() throws Exception { User user = createUser("user1", PASSWORD, group); assertTrue(users.add(user)); checkUserExists(user.getName()); @@ -191,7 +191,7 @@ public class InMemoryUserSetTest extends TestCase { * Tries to find a non-existing user. Verifies that null is returned. * */ - public void testFindUnknownUser() throws SQLException, UserMgtException { + public void testFindUnknownUser() throws Exception { User user1 = createUser("user1", PASSWORD, group); User user2 = createUser("user2", PASSWORD, group); users.add(user1); @@ -207,7 +207,7 @@ public class InMemoryUserSetTest extends TestCase { * Adds duplicate user. Verifies that the existing user is left untouched. * */ - public void testAddDuplicateUser() throws SQLException, UserMgtException { + public void testAddDuplicateUser() throws Exception { User user1 = createUser("user1", PASSWORD, group); users.add(user1); @@ -226,7 +226,7 @@ public class InMemoryUserSetTest extends TestCase { * true. * */ - public void testRemoveUser() throws SQLException, UserMgtException { + public void testRemoveUser() throws Exception { User user1 = createUser("user1", PASSWORD, group); users.add(user1); assertTrue(users.contains(user1)); @@ -244,7 +244,7 @@ public class InMemoryUserSetTest extends TestCase { * the return value is true. * */ - public void testRemoveNonExistingUser() throws SQLException, + public void testRemoveNonExistingUser() throws Exception, UserMgtException { User user1 = createUser("user1", PASSWORD, group); users.add(user1); @@ -262,7 +262,7 @@ public class InMemoryUserSetTest extends TestCase { * all. * */ - public void testList() throws SQLException, UserMgtException { + public void testList() throws Exception { User user1 = createUser("user1", PASSWORD, group); User user2 = createUser("user2", PASSWORD, group); User user3 = createUser("user3", PASSWORD, group); @@ -288,7 +288,7 @@ public class InMemoryUserSetTest extends TestCase { * * @throws SQLException */ - public void testListByGroup() throws SQLException, UserMgtException { + public void testListByGroup() throws Exception { Group group1 = new Group("group1"); Group group2 = new Group("group2"); Group group3 = new Group("group3"); diff --git a/security/jpatest/src/main/java/org/wamblee/usermgt/hibernate/HibernateUserSet.java b/security/jpatest/src/main/java/org/wamblee/usermgt/hibernate/HibernateUserSet.java index f8e02bd5..44348ec9 100644 --- a/security/jpatest/src/main/java/org/wamblee/usermgt/hibernate/HibernateUserSet.java +++ b/security/jpatest/src/main/java/org/wamblee/usermgt/hibernate/HibernateUserSet.java @@ -98,7 +98,7 @@ public class HibernateUserSet extends AbstractUserSet { */ public void userModified(User aUser) { assert aUser.getPrimaryKey() != null; - //hibernateSupport.merge(aUser); + //hibernateSupport.merge(aUser) cache.remove(aUser.getName()); setPasswordInfo(aUser); cache.put(aUser.getName(), new User(aUser)); diff --git a/security/jpatest/src/test/java/org/wamblee/usermgt/hibernate/HibernateUserSetTest.java b/security/jpatest/src/test/java/org/wamblee/usermgt/hibernate/HibernateUserSetTest.java index b0634ee8..11403102 100644 --- a/security/jpatest/src/test/java/org/wamblee/usermgt/hibernate/HibernateUserSetTest.java +++ b/security/jpatest/src/test/java/org/wamblee/usermgt/hibernate/HibernateUserSetTest.java @@ -124,7 +124,7 @@ public class HibernateUserSetTest extends InMemoryUserSetTest { * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupCount(int) */ @Override - protected void checkUserCount(int aSize) throws SQLException { + protected void checkUserCount(int aSize) throws Exception { databaseTester.flush(); super.checkUserCount(aSize); assertEquals(aSize, databaseTester.getTableSize(USER_TABLE)); diff --git a/security/jpatest/src/test/java/org/wamblee/usermgt/jpa/JpaGroupSetTest.java b/security/jpatest/src/test/java/org/wamblee/usermgt/jpa/JpaGroupSetTest.java index 4d44124e..49bb6c03 100644 --- a/security/jpatest/src/test/java/org/wamblee/usermgt/jpa/JpaGroupSetTest.java +++ b/security/jpatest/src/test/java/org/wamblee/usermgt/jpa/JpaGroupSetTest.java @@ -6,12 +6,14 @@ import java.sql.SQLException; import org.junit.After; import org.junit.Before; +import org.junit.Ignore; import org.wamblee.support.persistence.JpaTester; import org.wamblee.support.persistence.TransactionProxyFactory; import org.wamblee.support.persistence.DatabaseUtils.JdbcUnitOfWork; import org.wamblee.usermgt.GroupSet; import org.wamblee.usermgt.InMemoryGroupSetTest; + public class JpaGroupSetTest extends InMemoryGroupSetTest { private static final String GROUP_TABLE = "SEC_GROUP"; @@ -25,6 +27,7 @@ public class JpaGroupSetTest extends InMemoryGroupSetTest { public void setUp() throws Exception { jpaTester = new JpaTester(new SecurityPersistenceUnit()); jpaTester.start(); + // Superclass setup will call createGroupSet so requires initialized JPA. super.setUp(); } @@ -73,5 +76,4 @@ public class JpaGroupSetTest extends InMemoryGroupSetTest { GroupSet proxy = factory.getProxy(groupset); return proxy; } - } -- 2.31.1