Revision: 1608
          http://svn.sourceforge.net/spring-rich-c/?rev=1608&view=rev
Author:   mathiasbr
Date:     2006-12-20 05:06:00 -0800 (Wed, 20 Dec 2006)

Log Message:
-----------
removed present constraint
required constraint enhanced make it possible to determine for the message 
translator to separate required from present
required constraints can now check Maps
required now uses isEmpty instead of size on collection
+ tests

Modified Paths:
--------------
    
trunk/spring-richclient/support/src/main/java/org/springframework/rules/constraint/Required.java
    
trunk/spring-richclient/support/src/main/java/org/springframework/rules/factory/Constraints.java
    
trunk/spring-richclient/support/src/test/java/org/springframework/rules/RulesTests.java

Removed Paths:
-------------
    
trunk/spring-richclient/support/src/main/java/org/springframework/rules/constraint/Present.java

Deleted: 
trunk/spring-richclient/support/src/main/java/org/springframework/rules/constraint/Present.java
===================================================================
--- 
trunk/spring-richclient/support/src/main/java/org/springframework/rules/constraint/Present.java
     2006-12-20 12:07:01 UTC (rev 1607)
+++ 
trunk/spring-richclient/support/src/main/java/org/springframework/rules/constraint/Present.java
     2006-12-20 13:06:00 UTC (rev 1608)
@@ -1,36 +0,0 @@
-/*
- * Copyright 2002-2006 the original author or authors.
- * 
- * 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.
- */
-package org.springframework.rules.constraint;
-
-import org.springframework.core.closure.Constraint;
-
-/**
- * A simple constraint to separate required and present semantics
- * 
- * @author Mathias Broekelmann
- * 
- */
-public class Present extends Required {
-       private static final Present instance = new Present();
-
-       public String toString() {
-               return "present";
-       }
-
-       public static Constraint instance() {
-               return instance;
-       }
-}

Modified: 
trunk/spring-richclient/support/src/main/java/org/springframework/rules/constraint/Required.java
===================================================================
--- 
trunk/spring-richclient/support/src/main/java/org/springframework/rules/constraint/Required.java
    2006-12-20 12:07:01 UTC (rev 1607)
+++ 
trunk/spring-richclient/support/src/main/java/org/springframework/rules/constraint/Required.java
    2006-12-20 13:06:00 UTC (rev 1608)
@@ -16,47 +16,97 @@
 package org.springframework.rules.constraint;
 
 import java.util.Collection;
+import java.util.Map;
 
 import org.springframework.core.closure.Constraint;
+import org.springframework.rules.reporting.TypeResolvable;
 import org.springframework.util.StringUtils;
 
 /**
  * Validates a required property. Required is defined as non-null and, if the
- * object is a string, not empty and not blank.
+ * object is a string, not empty and not blank. If the object is an instance of
+ * [EMAIL PROTECTED] Collection}, [EMAIL PROTECTED] Map} or an array it will 
test if it is empty.
  * 
  * @author Keith Donald
+ * @author Mathias Broekelmann
  */
-public class Required implements Constraint {
-    private static final Required instance = new Required();
+public class Required implements Constraint, TypeResolvable {
+       private static final Required required = new Required();
 
-    /**
-     * Tests if this argument is present (non-null, not-empty, not blank)
-     * 
-     * @see org.springframework.core.closure.Constraint#test(java.lang.Object)
-     */
-    public boolean test(Object argument) {
-        if (argument != null) {
-            if (argument instanceof String) {
-                if (StringUtils.hasText((String)argument)) {
-                    return true;
-                }
-            } else if (argument instanceof Collection) {
-                return ((Collection)argument).size() > 0;
-            } else if( argument.getClass().isArray() ) {
-                return ((Object[])argument).length > 0;
-            } else {
-                return true;
-            }
-        }
-        return false;
-    }
+       private static final Required present = new Required("present");
 
-    public static Constraint instance() {
-        return instance;
-    }
-    
-    public String toString() {
-        return "required";
-    }
+       private final String type;
 
+       /**
+        * Creates a required constraint by using a custom type. This is 
primarly
+        * used to separate required and present constraint. These constraints 
where
+        * equaly implemented but where used differently
+        * 
+        * @param type the type of the constraint. see [EMAIL PROTECTED] 
#getType()}
+        */
+       public Required(String type) {
+               this.type = type;
+       }
+
+       /**
+        * Default constructor which creates a required constraint by using the 
type
+        * <code>required</code>
+        */
+       public Required() {
+               this("required");
+       }
+
+       /**
+        * Tests if this argument is present (non-null, not-empty, not blank)
+        * 
+        * @see 
org.springframework.core.closure.Constraint#test(java.lang.Object)
+        */
+       public boolean test(Object argument) {
+               if (argument != null) {
+                       if (argument instanceof String) {
+                               if (StringUtils.hasText((String) argument)) {
+                                       return true;
+                               }
+                       }
+                       else if (argument instanceof Collection) {
+                               return !((Collection) argument).isEmpty();
+                       }
+                       else if (argument instanceof Map) {
+                               return !((Map) argument).isEmpty();
+                       }
+                       else if (argument.getClass().isArray()) {
+                               return ((Object[]) argument).length > 0;
+                       }
+                       else {
+                               return true;
+                       }
+               }
+               return false;
+       }
+
+       /**
+        * returns the required instance
+        * 
+        * @return the required instance (never null)
+        */
+       public static Required instance() {
+               return required;
+       }
+
+       /**
+        * returns the present instance
+        * 
+        * @return the present instance (never null)
+        */
+       public static Required present() {
+               return present;
+       }
+
+       public String toString() {
+               return getType();
+       }
+
+       public String getType() {
+               return type;
+       }
 }
\ No newline at end of file

Modified: 
trunk/spring-richclient/support/src/main/java/org/springframework/rules/factory/Constraints.java
===================================================================
--- 
trunk/spring-richclient/support/src/main/java/org/springframework/rules/factory/Constraints.java
    2006-12-20 12:07:01 UTC (rev 1607)
+++ 
trunk/spring-richclient/support/src/main/java/org/springframework/rules/factory/Constraints.java
    2006-12-20 13:06:00 UTC (rev 1608)
@@ -36,7 +36,6 @@
 import org.springframework.rules.constraint.Not;
 import org.springframework.rules.constraint.Or;
 import org.springframework.rules.constraint.ParameterizedBinaryConstraint;
-import org.springframework.rules.constraint.Present;
 import org.springframework.rules.constraint.Range;
 import org.springframework.rules.constraint.RegexpConstraint;
 import org.springframework.rules.constraint.RelationalOperator;
@@ -301,7 +300,7 @@
     }
 
     public Constraint present() {
-        return Present.instance();
+        return Required.present();
     }
 
        /**

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-12-20 12:07:01 UTC (rev 1607)
+++ 
trunk/spring-richclient/support/src/test/java/org/springframework/rules/RulesTests.java
     2006-12-20 13:06:00 UTC (rev 1608)
@@ -18,7 +18,9 @@
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Map;
 import java.util.Set;
 
 import junit.framework.TestCase;
@@ -162,64 +164,79 @@
        }
 
        public void testRequired() {
-               Constraint req = Required.instance();
+               Required req = Required.instance();
+               assertEquals("required", req.getType());
+               emptyChecks(req);
+       }
+
+       public void testPresent() {
+               Required req = Required.present();
+               assertEquals("present", req.getType());
+               emptyChecks(req);
+       }
+
+       private void emptyChecks(Required req) {
                assertFalse(req.test(""));
                assertFalse(req.test(null));
-        assertFalse(req.test(new ArrayList()));
-        assertFalse(req.test(new Object[0]));
+               assertFalse(req.test(new HashMap()));
+               assertFalse(req.test(new ArrayList()));
+               assertFalse(req.test(new Object[0]));
 
-        assertTrue(req.test(new Integer(25)));
+               assertTrue(req.test(new Integer(25)));
                assertTrue(req.test("25"));
-        assertTrue(req.test(new Object[1]));
-        assertTrue(req.test(Arrays.asList(new Object[1])));
+               assertTrue(req.test(new Object[1]));
+               Map map = new HashMap();
+               map.put("1", "1");
+               assertTrue(req.test(map));
+               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);
+       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"));
+               // 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"));
+               Person p = new Person();
 
-        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));
-    }
+               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));
@@ -264,17 +281,17 @@
                assertFalse(or.test("           "));
        }
 
-  public void testXOr() {
-    XOr xor = new XOr();
-    xor.add(new InGroup(new String[] {"123", "12345"}));
-    xor.add(new InGroup(new String[] {"1234", "12345"}));
-    assertTrue(xor.test("123"));
-    assertTrue(xor.test("1234"));
-    assertFalse(xor.test("           "));
-    assertFalse(xor.test("12345"));
-  }
+       public void testXOr() {
+               XOr xor = new XOr();
+               xor.add(new InGroup(new String[] { "123", "12345" }));
+               xor.add(new InGroup(new String[] { "1234", "12345" }));
+               assertTrue(xor.test("123"));
+               assertTrue(xor.test("1234"));
+               assertFalse(xor.test("           "));
+               assertFalse(xor.test("12345"));
+       }
 
-  public void testNot() {
+       public void testNot() {
                Number n = new Integer("25");
                Constraint p = constraints.bind(EqualTo.instance(), n);
                Not not = new Not(p);
@@ -340,8 +357,8 @@
                // test must be required, and have a length in range 3 to 25
                // or test must just equal confirmTest
                CompoundPropertyConstraint rules = new 
CompoundPropertyConstraint(constraints.or(constraints.all("test",
-                               new Constraint[] { constraints.required(), 
constraints.maxLength(25), constraints.minLength(3) }), constraints
-                               .eqProperty("test", "confirmTest")));
+                               new Constraint[] { constraints.required(), 
constraints.maxLength(25), constraints.minLength(3) }),
+                               constraints.eqProperty("test", "confirmTest")));
                r.add(rules);
                assertTrue(r.test(new TestBean()));
                TestBean b = new TestBean();
@@ -360,7 +377,7 @@
        public void testDefaultRulesSource() {
                ClassPathXmlApplicationContext ac = new 
ClassPathXmlApplicationContext(
                                "org/springframework/rules/rules-context.xml");
-               RulesSource rulesSource = 
(RulesSource)ac.getBean("rulesSource");
+               RulesSource rulesSource = (RulesSource) 
ac.getBean("rulesSource");
                Rules rules = rulesSource.getRules(Person.class);
                assertTrue(rules != null);
                Person p = new Person();
@@ -372,7 +389,6 @@
                assertFalse(rules.test(p));
        }
 
-
        public class TestBean {
 
                private String test = "testValue";


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

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
spring-rich-c-cvs mailing list
spring-rich-c-cvs@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/spring-rich-c-cvs

Reply via email to