You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by ep...@apache.org on 2004/07/07 18:48:25 UTC
cvs commit: jakarta-turbine-fulcrum/security/api/src/java/org/apache/fulcrum/security/util UserSet.java SecuritySet.java RoleSet.java GroupSet.java PermissionSet.java
epugh 2004/07/07 09:48:25
Modified: security/api/src/test/org/apache/fulcrum/security/util
SecuritySetTest.java GroupSetTest.java
security/api/src/java/org/apache/fulcrum/security/util
UserSet.java SecuritySet.java RoleSet.java
GroupSet.java PermissionSet.java
Added: security/api/src/test/org/apache/fulcrum/security/util
RoleSetTest.java
Log:
Make SecuritySet and subclasses able to handle .add(Object obj) properly
Revision Changes Path
1.4 +12 -2 jakarta-turbine-fulcrum/security/api/src/test/org/apache/fulcrum/security/util/SecuritySetTest.java
Index: SecuritySetTest.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-fulcrum/security/api/src/test/org/apache/fulcrum/security/util/SecuritySetTest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- SecuritySetTest.java 5 Jul 2004 19:28:23 -0000 1.3
+++ SecuritySetTest.java 7 Jul 2004 16:48:25 -0000 1.4
@@ -93,6 +93,15 @@
assertEquals(2, array3.length);
}
+ public void testAdd() throws Exception
+ {
+ GroupSet securitySet = (GroupSet)getTestData();
+ Group g = new DynamicGroup();
+ g.setName("Michael");
+ g.setId("Michael");
+ assertTrue(securitySet.add(g));
+ }
+
private SecuritySet getTestData()
{
SecuritySet securitySet = new GroupSet();
@@ -110,5 +119,6 @@
return securitySet;
}
-
+
+
}
1.3 +23 -2 jakarta-turbine-fulcrum/security/api/src/test/org/apache/fulcrum/security/util/GroupSetTest.java
Index: GroupSetTest.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-fulcrum/security/api/src/test/org/apache/fulcrum/security/util/GroupSetTest.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- GroupSetTest.java 5 Jul 2004 19:28:23 -0000 1.2
+++ GroupSetTest.java 7 Jul 2004 16:48:25 -0000 1.3
@@ -18,7 +18,9 @@
import junit.framework.TestCase;
import org.apache.fulcrum.security.entity.Group;
+import org.apache.fulcrum.security.entity.Role;
import org.apache.fulcrum.security.model.dynamic.entity.DynamicGroup;
+import org.apache.fulcrum.security.model.dynamic.entity.DynamicRole;
/**
* @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
@@ -52,7 +54,8 @@
group.setId(new Integer(1));
group.setName("Eric");
GroupSet groupSet = new GroupSet();
- groupSet.add(group);
+ assertTrue(groupSet.add(group));
+ assertFalse(groupSet.add(group));
assertTrue(groupSet.contains(group));
Group group2 = new DynamicGroup();
@@ -69,6 +72,24 @@
assertTrue(groupSet.contains(group2));
assertTrue(groupSet.contains(group3));
assertTrue(groupSet.contains(group));
+
+ Role role = new DynamicRole();
+ role.setName("role");
+ role.setId("role");
+ try {
+ groupSet.add(role);
+ fail("Should have thrown ClassCastException");
+ }
+ catch (ClassCastException cce){
+ assertTrue(cce.getMessage().indexOf("GroupSet")>-1);
+ }
+ try {
+ ((SecuritySet)groupSet).add(role);
+ fail("Should have thrown ClassCastException");
+ }
+ catch (ClassCastException cce){
+ assertTrue(cce.getMessage().indexOf("GroupSet")>-1);
+ }
}
public void testGroupSetWithSubclass() throws Exception
1.1 jakarta-turbine-fulcrum/security/api/src/test/org/apache/fulcrum/security/util/RoleSetTest.java
Index: RoleSetTest.java
===================================================================
package org.apache.fulcrum.security.util;
/*
* Copyright 2001-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import junit.framework.TestCase;
import org.apache.fulcrum.security.entity.Role;
import org.apache.fulcrum.security.model.dynamic.entity.DynamicRole;
/**
* @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
* @version $Id: RoleSetTest.java,v 1.1 2004/07/07 16:48:25 epugh Exp $
*/
public class RoleSetTest extends TestCase
{
/**
* Defines the testcase name for JUnit.
*
* @param name the testcase's name.
*/
public RoleSetTest(String name)
{
super(name);
}
public static void main(String[] args)
{
junit.textui.TestRunner.run(RoleSetTest.class);
}
public void testNullRole() throws Exception {
RoleSet roleSet = new RoleSet();
assertFalse(roleSet.contains(null));
}
public void testAddRoles() throws Exception
{
Role role = new DynamicRole();
role.setId(new Integer(1));
role.setName("Eric");
RoleSet roleSet = new RoleSet();
assertTrue(roleSet.add(role));
assertTrue(roleSet.contains(role));
Role role2 = new DynamicRole();
role2.setName("Kate");
role2.setId(new Integer(2));
roleSet.add(role2);
Role role3 = new DynamicRole();
role3.setId(new Integer(1));
role3.setName("Eric");
roleSet.add(role3);
assertTrue(roleSet.contains(role));
assertTrue(roleSet.contains((Object) role));
assertTrue(roleSet.contains(role2));
assertTrue(roleSet.contains(role3));
assertTrue(roleSet.contains(role));
}
public void testRoleSetWithSubclass() throws Exception
{
RoleSet roleSet = new RoleSet();
Role role = new RoleSubClass();
role.setId(new Integer(1));
role.setName("Eric");
roleSet.add(role);
assertTrue(roleSet.contains(role));
Role role2 = new DynamicRole();
role2.setId(new Integer(1));
role2.setName("Eric");
assertTrue(roleSet.contains(role2));
}
class RoleSubClass extends DynamicRole
{
private String extraRoleData;
/**
* @return Returns the extraRoleData.
*/
public String getExtraRoleData()
{
return extraRoleData;
}
/**
* @param extraRoleData The extraRoleData to set.
*/
public void setExtraRoleData(String extraRoleData)
{
this.extraRoleData = extraRoleData;
}
}
}
1.3 +24 -5 jakarta-turbine-fulcrum/security/api/src/java/org/apache/fulcrum/security/util/UserSet.java
Index: UserSet.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-fulcrum/security/api/src/java/org/apache/fulcrum/security/util/UserSet.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- UserSet.java 5 Jul 2004 19:28:22 -0000 1.2
+++ UserSet.java 7 Jul 2004 16:48:25 -0000 1.3
@@ -63,11 +63,30 @@
*/
public boolean add(User user)
{
- boolean res = contains(user);
- //nameMap.put(user.getName(), user);
- idMap.put(user.getId(), user);
- return res;
+ if (contains(user)){
+ return false;
+ }
+ else {
+ idMap.put(user.getId(), user);
+ return true;
+ }
}
+
+ /**
+ * Adds a User to this UserSet.
+ *
+ * @param obj A User.
+ * @return True if User was added; false if UserSet already
+ * contained the User.
+ */
+ public boolean add(Object obj) {
+ if(obj instanceof User){
+ return add((User)obj);
+ }
+ else {
+ throw new ClassCastException("Object passed to add to UserSet is not of type User");
+ }
+ }
/**
* Adds the Users in a Collection to this UserSet.
1.4 +11 -10 jakarta-turbine-fulcrum/security/api/src/java/org/apache/fulcrum/security/util/SecuritySet.java
Index: SecuritySet.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-fulcrum/security/api/src/java/org/apache/fulcrum/security/util/SecuritySet.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- SecuritySet.java 5 Jul 2004 19:28:22 -0000 1.3
+++ SecuritySet.java 7 Jul 2004 16:48:25 -0000 1.4
@@ -54,6 +54,15 @@
//nameMap = new TreeMap();
idMap = new TreeMap();
}
+
+ /*
+ * To enable the typesafe handling, make this abstract
+ * and rely on the implementing classes like RoleSet to
+ * properly cast the Object type.
+ *
+ * @see java.util.Collection#add(java.lang.Object)
+ */
+ public abstract boolean add(Object o);
/**
* Returns a set of security objects in this object.
*
@@ -210,15 +219,7 @@
{
return getSet().toArray();
}
- /*
- * (non-Javadoc)
- *
- * @see java.util.Collection#add(java.lang.Object)
- */
- public boolean add(Object o)
- {
- throw new RuntimeException("not implemented");
- }
+
/*
* (non-Javadoc)
*
1.3 +26 -5 jakarta-turbine-fulcrum/security/api/src/java/org/apache/fulcrum/security/util/RoleSet.java
Index: RoleSet.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-fulcrum/security/api/src/java/org/apache/fulcrum/security/util/RoleSet.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- RoleSet.java 5 Jul 2004 19:28:22 -0000 1.2
+++ RoleSet.java 7 Jul 2004 16:48:25 -0000 1.3
@@ -67,10 +67,29 @@
*/
public boolean add(Role role)
{
- boolean res = contains(role);
- //nameMap.put(role.getName(), role);
- idMap.put(role.getId(), role);
- return res;
+ if (contains(role)){
+ return false;
+ }
+ else {
+ idMap.put(role.getId(), role);
+ return true;
+ }
+ }
+
+ /**
+ * Adds a Role to this RoleSet.
+ *
+ * @param obj A Role.
+ * @return True if Role was added; false if RoleSet already
+ * contained the Role.
+ */
+ public boolean add(Object obj) {
+ if(obj instanceof Role){
+ return add((Role)obj);
+ }
+ else {
+ throw new ClassCastException("Object passed to add to RoleSet is not of type Role");
+ }
}
/**
@@ -206,4 +225,6 @@
return sb.toString();
}
+
+
}
1.3 +24 -5 jakarta-turbine-fulcrum/security/api/src/java/org/apache/fulcrum/security/util/GroupSet.java
Index: GroupSet.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-fulcrum/security/api/src/java/org/apache/fulcrum/security/util/GroupSet.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- GroupSet.java 5 Jul 2004 19:28:22 -0000 1.2
+++ GroupSet.java 7 Jul 2004 16:48:25 -0000 1.3
@@ -67,11 +67,30 @@
*/
public boolean add(Group group)
{
- boolean res = contains(group);
- //nameMap.put(group.getName(), group);
- idMap.put(group.getId(), group);
- return res;
+ if (contains(group)){
+ return false;
+ }
+ else {
+ idMap.put(group.getId(), group);
+ return true;
+ }
}
+
+ /**
+ * Adds a Group to this GroupSet.
+ *
+ * @param obj A Group.
+ * @return True if Group was added; false if GroupSet already
+ * contained the Group.
+ */
+ public boolean add(Object obj) {
+ if(obj instanceof Group){
+ return add((Group)obj);
+ }
+ else {
+ throw new ClassCastException("Object passed to add to GroupSet is not of type Group");
+ }
+ }
/**
* Adds the Groups in a Collection to this GroupSet.
1.3 +24 -5 jakarta-turbine-fulcrum/security/api/src/java/org/apache/fulcrum/security/util/PermissionSet.java
Index: PermissionSet.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-fulcrum/security/api/src/java/org/apache/fulcrum/security/util/PermissionSet.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- PermissionSet.java 5 Jul 2004 19:28:22 -0000 1.2
+++ PermissionSet.java 7 Jul 2004 16:48:25 -0000 1.3
@@ -67,11 +67,30 @@
*/
public boolean add(Permission permission)
{
- boolean res = contains(permission);
- //nameMap.put(permission.getName(), permission);
- idMap.put(permission.getId(), permission);
- return res;
+ if (contains(permission)){
+ return false;
+ }
+ else {
+ idMap.put(permission.getId(), permission);
+ return true;
+ }
}
+
+ /**
+ * Adds a Permission to this PermissionSet.
+ *
+ * @param obj A Permission.
+ * @return True if Permission was added; false if PermissionSet already
+ * contained the Permission.
+ */
+ public boolean add(Object obj) {
+ if(obj instanceof Permission){
+ return add((Permission)obj);
+ }
+ else {
+ throw new ClassCastException("Object passed to add to PermissionSet is not of type Permission");
+ }
+ }
/**
* Adds the Permissions in a Collection to this PermissionSet.
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org