Revision: 1152
Author:   lstreepy
Date:     2006-05-14 07:07:13 -0700 (Sun, 14 May 2006)
ViewCVS:  http://svn.sourceforge.net/spring-rich-c/?rev=1152&view=rev

Log Message:
-----------
Fix RCP-338 and RCP-281: allow String[] in constructor and fix 
ClassCastException.
Also, add tests for this constraint.

Modified Paths:
--------------
    
trunk/spring-richclient/support/src/main/java/org/springframework/rules/constraint/property/RequiredIfOthersPresent.java
    
trunk/spring-richclient/support/src/test/java/org/springframework/rules/Person.java
    
trunk/spring-richclient/support/src/test/java/org/springframework/rules/RulesTests.java
Modified: 
trunk/spring-richclient/support/src/main/java/org/springframework/rules/constraint/property/RequiredIfOthersPresent.java
===================================================================
--- 
trunk/spring-richclient/support/src/main/java/org/springframework/rules/constraint/property/RequiredIfOthersPresent.java
    2006-05-13 15:28:41 UTC (rev 1151)
+++ 
trunk/spring-richclient/support/src/main/java/org/springframework/rules/constraint/property/RequiredIfOthersPresent.java
    2006-05-14 14:07:13 UTC (rev 1152)
@@ -15,9 +15,6 @@
  */
 package org.springframework.rules.constraint.property;
 
-import java.util.Iterator;
-import java.util.Set;
-
 import org.springframework.rules.constraint.CompoundConstraint;
 import org.springframework.rules.constraint.LogicalOperator;
 import org.springframework.util.Assert;
@@ -28,46 +25,67 @@
  */
 public class RequiredIfOthersPresent extends RequiredIfTrue {
 
-       /**
-        * Tests that the property is required if all "other properties" are
-        * present. Present means they are "non null."
-        *
-        * @param otherPropertyNames
-        *            one or more other properties, delimited by commas.
-        */
-       public RequiredIfOthersPresent(String propertyName, String 
otherPropertyNames) {
-               this(propertyName, otherPropertyNames, LogicalOperator.AND);
-       }
+    /**
+     * Tests that the property is required if all "other properties" are 
present. Present
+     * means they are "non null."
+     * 
+     * @param otherPropertyNames to test
+     */
+    public RequiredIfOthersPresent( String propertyName, String[] 
otherPropertyNames ) {
+        this(propertyName, otherPropertyNames, LogicalOperator.AND);
+    }
 
-       public boolean isDependentOn(String propertyName) {
-               return getPropertyName().equals(propertyName) || 
((CompoundPropertyConstraint)getConstraint()).isDependentOn(propertyName);
-       }
+    /**
+     * Tests that the property is required if "other properties" are present. 
Present
+     * means they are "non null." The operator parameter determines how the 
set of other
+     * property names is handled. If AND, then all must be present before the 
primary
+     * proeprty will be required. If OR, then if any of the other properties 
are present,
+     * then the primary property will be required. the logical operator, 
either AND or OR.
+     * 
+     * @param otherPropertyNames to test
+     * @param operator Either AND or OR.
+     */
+    public RequiredIfOthersPresent( String propertyName, String[] 
otherPropertyNames, LogicalOperator operator ) {
+        super(propertyName);
+        Assert.notNull(otherPropertyNames, "otherPropertyNames is required");
+        Assert.notNull(operator, "operator is required");
+        Assert.notEmpty(otherPropertyNames, "otherPropertyNames must consist 
of at least one name");
 
-       public boolean isCompoundRule() {
-               return true;
-       }
+        CompoundConstraint compoundConstraint = operator.createConstraint();
+        CompoundPropertyConstraint propertyConstraint = new 
CompoundPropertyConstraint(compoundConstraint);
+        for( int i = 0; i < otherPropertyNames.length; i++ ) {
+            propertyConstraint.add(new PropertyPresent(otherPropertyNames[i]));
+        }
+        setConstraint(propertyConstraint);
+    }
 
-       /**
-        * Tests that the property is required if all or any of the "other
-        * properties" are present.
-        *
-        * @param otherPropertyNames
-        *            one or more other properties, delimited by commas.
-        * @param operator
-        *            the logical operator, either AND or OR.
-        */
-       public RequiredIfOthersPresent(String propertyName, String 
otherPropertyNames, LogicalOperator operator) {
-               super(propertyName);
-               Assert.notNull(otherPropertyNames, "otherPropertyNames is 
required");
-               Assert.notNull(operator, "operator is required");
-               Set set = 
StringUtils.commaDelimitedListToSet(otherPropertyNames);
-               Assert.notEmpty(set, "otherPropertyNames must consist of at 
least one name");
-               CompoundConstraint compoundConstraint = 
operator.createConstraint();
-               CompoundPropertyConstraint propertyConstraint = new 
CompoundPropertyConstraint(compoundConstraint);
-               for (Iterator i = set.iterator(); i.hasNext();) {
-                       propertyConstraint.add(new 
PropertyPresent((String)i.next()));
-               }
-               setConstraint(compoundConstraint);
-       }
+    /**
+     * Tests that the property is required if all "other properties" are 
present. Present
+     * means they are "non null."
+     * 
+     * @param otherPropertyNames one or more other properties, delimited by 
commas.
+     */
+    public RequiredIfOthersPresent( String propertyName, String 
otherPropertyNames ) {
+        this(propertyName, otherPropertyNames, LogicalOperator.AND);
+    }
 
-}
\ No newline at end of file
+    /**
+     * Tests that the property is required if all or any of the "other 
properties" are
+     * present.
+     * 
+     * @param otherPropertyNames one or more other properties, delimited by 
commas.
+     * @param operator the logical operator, either AND or OR.
+     */
+    public RequiredIfOthersPresent( String propertyName, String 
otherPropertyNames, LogicalOperator operator ) {
+        this(propertyName, 
StringUtils.commaDelimitedListToStringArray(otherPropertyNames), operator);
+    }
+
+    public boolean isDependentOn( String propertyName ) {
+        return getPropertyName().equals(propertyName)
+                || ((CompoundPropertyConstraint) 
getConstraint()).isDependentOn(propertyName);
+    }
+
+    public boolean isCompoundRule() {
+        return true;
+    }
+}

Modified: 
trunk/spring-richclient/support/src/test/java/org/springframework/rules/Person.java
===================================================================
--- 
trunk/spring-richclient/support/src/test/java/org/springframework/rules/Person.java
 2006-05-13 15:28:41 UTC (rev 1151)
+++ 
trunk/spring-richclient/support/src/test/java/org/springframework/rules/Person.java
 2006-05-14 14:07:13 UTC (rev 1152)
@@ -22,6 +22,9 @@
     /** Holds value of property telephone. */
     private String telephone;
 
+    private String state;
+    private String zip;
+    
     /**
      * Getter for property firstName.
      * 
@@ -117,4 +120,32 @@
         this.telephone = telephone;
     }
 
+    /**
+     * @return the state
+     */
+    public String getState() {
+        return state;
+    }
+
+    /**
+     * @param state the state to set
+     */
+    public void setState( String state ) {
+        this.state = state;
+    }
+
+    /**
+     * @return the zip
+     */
+    public String getZip() {
+        return zip;
+    }
+
+    /**
+     * @param zip the zip to set
+     */
+    public void setZip( String zip ) {
+        this.zip = zip;
+    }
+
 }
\ No newline at end of file

Modified: 
trunk/spring-richclient/support/src/test/java/org/springframework/rules/RulesTests.java
===================================================================
--- 
trunk/spring-richclient/support/src/test/java/org/springframework/rules/RulesTests.java
     2006-05-13 15:28:41 UTC (rev 1151)
+++ 
trunk/spring-richclient/support/src/test/java/org/springframework/rules/RulesTests.java
     2006-05-14 14:07:13 UTC (rev 1152)
@@ -34,6 +34,7 @@
 import org.springframework.rules.constraint.GreaterThanEqualTo;
 import org.springframework.rules.constraint.LessThan;
 import org.springframework.rules.constraint.LessThanEqualTo;
+import org.springframework.rules.constraint.LogicalOperator;
 import org.springframework.rules.constraint.Not;
 import org.springframework.rules.constraint.Or;
 import org.springframework.rules.constraint.ParameterizedBinaryConstraint;
@@ -46,6 +47,7 @@
 import org.springframework.rules.constraint.property.PropertiesConstraint;
 import org.springframework.rules.constraint.property.PropertyConstraint;
 import org.springframework.rules.constraint.property.PropertyValueConstraint;
+import org.springframework.rules.constraint.property.RequiredIfOthersPresent;
 import org.springframework.rules.factory.Constraints;
 import org.springframework.util.Assert;
 
@@ -170,6 +172,52 @@
         assertTrue(req.test(Arrays.asList(new Object[1])));
        }
 
+    public void testRequiredIfOthersPresent() {
+        Rules r = new Rules(Person.class);
+        PropertyConstraint c = new RequiredIfOthersPresent("zip", 
"city,state");
+        r.add(c);
+
+        // Ensure that it properly reports all property dependencies
+        assertTrue(c.isDependentOn("zip"));
+        assertTrue(c.isDependentOn("city"));
+        assertTrue(c.isDependentOn("state"));
+
+        Person p = new Person();
+        
+        assertTrue(r.test(p)); // No city or state, so not required
+        
+        p.setCity("city");
+        assertTrue(r.test(p)); // Need both city and state, so not required
+        
+        p.setState("state");
+        assertFalse(r.test(p));
+        
+        p.setZip("zip");
+        assertTrue(r.test(p));
+        
+        // Now test the OR version
+        r = new Rules(Person.class);
+        c = new RequiredIfOthersPresent("zip", "city,state", 
LogicalOperator.OR);
+        r.add(c);
+        
+        assertTrue(c.isDependentOn("zip"));
+        assertTrue(c.isDependentOn("city"));
+        assertTrue(c.isDependentOn("state"));
+
+        p = new Person();
+        
+        assertTrue(r.test(p)); // No city or state, so not required
+        
+        p.setCity("city");
+        assertFalse(r.test(p)); // Need either city and state, so required
+        
+        p.setState("state");
+        assertFalse(r.test(p));
+        
+        p.setZip("zip");
+        assertTrue(r.test(p));
+    }
+
        public void testMaxLengthConstraint() {
                Constraint p = new StringLengthConstraint(5);
                assertTrue(p.test(null));


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
spring-rich-c-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/spring-rich-c-cvs

Reply via email to