*/
@Override
public boolean equals(Object aGroup) {
+ if (aGroup == null) {
+ return false;
+ }
if (!(aGroup instanceof Group)) {
return false;
}
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)
*/
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.
* @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>();
}
/*
* 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;
+ }
+ }
}
/*
public Group find(String aName) {
for (Group group : groups) {
if (group.getName().equals(aName)) {
- return new Group(group);
+ return group;
}
}
* @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);
}
}
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);
}
/*
}
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);
}
/*
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);
}
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);
+ }
}
assertEquals(USER2, user1.getName());
assertEquals(user1, admin.getUser(USER2));
- admin.createUser(USER1, PASS1, group);
-
try {
admin.renameUser(user1, USER1);
} catch (UserMgtException e) {