From: Erik Brakkee Date: Fri, 30 Apr 2010 15:42:36 +0000 (+0000) Subject: (no commit message) X-Git-Tag: wamblee-utils-0.7~500 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=476ab93a6ea0c57bbcc75594886de985ea8f04c2;p=utils --- 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 697f1b5b..ad3e3ec2 100644 --- a/security/impl/src/main/java/org/wamblee/usermgt/Group.java +++ b/security/impl/src/main/java/org/wamblee/usermgt/Group.java @@ -116,6 +116,9 @@ public class Group implements Serializable, Comparable { */ @Override public boolean equals(Object aGroup) { + if (aGroup == null) { + return false; + } if (!(aGroup instanceof Group)) { return false; } @@ -141,10 +144,14 @@ public class Group implements Serializable, Comparable { public int compareTo(Object aGroup) { return name.compareTo(((Group) aGroup).name); } - + public Long getPrimaryKey() { return primaryKey; } + + public void setPrimaryKey(Long aKey) { + primaryKey = aKey; + } /* * (non-Javadoc) diff --git a/security/impl/src/main/java/org/wamblee/usermgt/InMemoryGroupSet.java b/security/impl/src/main/java/org/wamblee/usermgt/InMemoryGroupSet.java index 609b114c..227e8853 100644 --- a/security/impl/src/main/java/org/wamblee/usermgt/InMemoryGroupSet.java +++ b/security/impl/src/main/java/org/wamblee/usermgt/InMemoryGroupSet.java @@ -15,8 +15,11 @@ */ package org.wamblee.usermgt; +import java.util.ArrayList; +import java.util.List; import java.util.Set; import java.util.TreeSet; +import java.util.concurrent.atomic.AtomicLong; /** * In-memory group set implementation. @@ -24,16 +27,19 @@ import java.util.TreeSet; * @author Erik Brakkee */ public class InMemoryGroupSet implements GroupSet { + + private AtomicLong pk = new AtomicLong(1l); + /** * Groups. */ - private Set groups; + private List groups; /** * Constructs an empty group set. */ public InMemoryGroupSet() { - groups = new TreeSet(); + groups = new ArrayList(); } /* @@ -43,8 +49,13 @@ public class InMemoryGroupSet implements GroupSet { * org.wamblee.usermgt.GroupSet#groupModified(org.wamblee.usermgt.Group) */ public void groupModified(Group aGroup) { - groups.remove(aGroup); - groups.add(aGroup); + for (int i = 0; i < groups.size(); i++) { + if (groups.get(i).getPrimaryKey().equals(aGroup.getPrimaryKey())) { + groups.remove(i); + groups.add(aGroup); + return; + } + } } /* @@ -55,7 +66,7 @@ public class InMemoryGroupSet implements GroupSet { public Group find(String aName) { for (Group group : groups) { if (group.getName().equals(aName)) { - return new Group(group); + return group; } } @@ -77,6 +88,10 @@ public class InMemoryGroupSet implements GroupSet { * @see org.wamblee.usermgt.GroupSet#add(org.wamblee.usermgt.Group) */ public boolean add(Group aGroup) { + aGroup.setPrimaryKey(pk.getAndIncrement()); + if ( find(aGroup.getName()) != null ) { + return false; + } return groups.add(aGroup); } diff --git a/security/impl/src/main/java/org/wamblee/usermgt/UserAdministrationImpl.java b/security/impl/src/main/java/org/wamblee/usermgt/UserAdministrationImpl.java index 5c3570f9..6866d454 100644 --- a/security/impl/src/main/java/org/wamblee/usermgt/UserAdministrationImpl.java +++ b/security/impl/src/main/java/org/wamblee/usermgt/UserAdministrationImpl.java @@ -215,11 +215,9 @@ public class UserAdministrationImpl implements UserAdministration { } userValidator.validate(aUserName); - // we are modifying the user so we should re-insert it into the set - // after renaming it. - users.remove(aUser); + aUser.setName(aUserName); - users.add(aUser); + users.userModified(aUser); } /* @@ -242,11 +240,9 @@ public class UserAdministrationImpl implements UserAdministration { } groupValidator.validate(aGroupName); - // we are renaming the group so we should re-insert it into the set - // after renaming it. - groups.remove(aGroup); + aGroup.setName(aGroupName); - groups.add(aGroup); + groups.groupModified(aGroup); } /* diff --git a/security/impl/src/main/java/org/wamblee/usermgt/jpa/JpaGroupSet.java b/security/impl/src/main/java/org/wamblee/usermgt/jpa/JpaGroupSet.java index 6d6cde0f..763fced4 100644 --- a/security/impl/src/main/java/org/wamblee/usermgt/jpa/JpaGroupSet.java +++ b/security/impl/src/main/java/org/wamblee/usermgt/jpa/JpaGroupSet.java @@ -54,6 +54,10 @@ public class JpaGroupSet implements GroupSet { public void groupModified(Group aGroup) { assert aGroup.getPrimaryKey() != null; Group merged = em.merge(aGroup); + // Need to flush so that version of the merged instance is updated so we can use + // the updated version in the original group passed in. That allows the same + // group object to continue to be used as a detached object. + em.flush(); JpaMergeSupport.merge(merged, aGroup); } diff --git a/security/impl/src/test/java/org/wamblee/usermgt/InMemoryGroupSetTest.java b/security/impl/src/test/java/org/wamblee/usermgt/InMemoryGroupSetTest.java index 9a65a143..49b631df 100644 --- a/security/impl/src/test/java/org/wamblee/usermgt/InMemoryGroupSetTest.java +++ b/security/impl/src/test/java/org/wamblee/usermgt/InMemoryGroupSetTest.java @@ -195,4 +195,15 @@ public class InMemoryGroupSetTest extends TestCase { checkGroupCount(3); } + + public void testRenameGroupTwice() { + Group group = new Group("x"); + groups.add(group); + groups.groupModified(group); + group.setName("y"); + groups.groupModified(group); + Group g = groups.find("y"); + assertNotNull(g); + groups.groupModified(group); + } } diff --git a/security/impl/src/test/java/org/wamblee/usermgt/UserAdministrationImplTest.java b/security/impl/src/test/java/org/wamblee/usermgt/UserAdministrationImplTest.java index a9dce459..0a27865b 100644 --- a/security/impl/src/test/java/org/wamblee/usermgt/UserAdministrationImplTest.java +++ b/security/impl/src/test/java/org/wamblee/usermgt/UserAdministrationImplTest.java @@ -434,8 +434,6 @@ public class UserAdministrationImplTest extends TestCase { assertEquals(USER2, user1.getName()); assertEquals(user1, admin.getUser(USER2)); - admin.createUser(USER1, PASS1, group); - try { admin.renameUser(user1, USER1); } catch (UserMgtException e) {