(no commit message)
authorErik Brakkee <erik@brakkee.org>
Fri, 30 Apr 2010 15:42:36 +0000 (15:42 +0000)
committerErik Brakkee <erik@brakkee.org>
Fri, 30 Apr 2010 15:42:36 +0000 (15:42 +0000)
security/impl/src/main/java/org/wamblee/usermgt/Group.java
security/impl/src/main/java/org/wamblee/usermgt/InMemoryGroupSet.java
security/impl/src/main/java/org/wamblee/usermgt/UserAdministrationImpl.java
security/impl/src/main/java/org/wamblee/usermgt/jpa/JpaGroupSet.java
security/impl/src/test/java/org/wamblee/usermgt/InMemoryGroupSetTest.java
security/impl/src/test/java/org/wamblee/usermgt/UserAdministrationImplTest.java

index 697f1b5b343a75a84b6b0905d3f526df7bddc262..ad3e3ec2edcab6d4ca14e444660f3cc097920d48 100644 (file)
@@ -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)
index 609b114c5966de866414944f86cc525210caa2c4..227e8853bca8b980e8821c571eb268da5c247dc9 100644 (file)
  */ 
 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<Group> groups;
+    private List<Group> groups;
 
     /**
      * Constructs an empty group set.
      */
     public InMemoryGroupSet() {
-        groups = new TreeSet<Group>();
+        groups = new ArrayList<Group>();
     }
 
     /*
@@ -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);
     }
 
index 5c3570f9df73b92bd05cd6b99e1e5f9b5171887b..6866d45416a6e93f9eb4b9abfff262d62ff14d66 100644 (file)
@@ -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);
     }
 
     /*
index 6d6cde0f684c3c2e155d3c8838b884fdfd6cacf8..763fced4e464512ef6e927cecfb93be7785651ec 100644 (file)
@@ -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);
     }
 
index 9a65a1430f2a8f7f9481109c3afe6f9b62b55c3c..49b631dffa943b8df24cbf86463d53d748c37629 100644 (file)
@@ -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);
+    }
 }
index a9dce459e0c804eb8b61066fb4ac6742892ad618..0a27865b525841b83d58c02ac9e69f289e2465a0 100644 (file)
@@ -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) {