Commits

Anonymous committed 5df0eed

XW-558 Add Long and Short range validators

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@1591e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (9)

src/java/com/opensymphony/xwork2/validator/validators/LongRangeFieldValidator.java

+/*
+ * Copyright (c) 2002-2006 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork2.validator.validators;
+
+
+/**
+ * <!-- START SNIPPET: javadoc -->
+ * Field Validator that checks if the long specified is within a certain range.
+ * <!-- END SNIPPET: javadoc -->
+ * 
+ * 
+ * <!-- START SNIPPET: parameters -->
+ * <ul>
+ *              <li>fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required</li>
+ *              <li>min - the minimum value (if none is specified, it will not be checked) </li>
+ *              <li>max - the maximum value (if none is specified, it will not be checked) </li>
+ * </ul>
+ * <!-- END SNIPPET: parameters -->
+ * 
+ * 
+ * <pre>
+ * <!-- START SNIPPET: examples -->
+ *              &lt;validators>
+ *           &lt;!-- Plain Validator Syntax --&gt;
+ *           &lt;validator type="long">
+ *               &lt;param name="fieldName"&gt;age&lt;/param&gt;
+ *               &lt;param name="min"&gt;20&lt;/param&gt;
+ *               &lt;param name="max"&gt;50&lt;/param&gt;
+ *               &lt;message&gt;Age needs to be between ${min} and ${max}&lt;/message&gt;
+ *           &lt;/validator&gt;
+ *           
+ *           &lt;!-- Field Validator Syntax --&gt;
+ *           &lt;field name="age"&gt;
+ *               &lt;field-validator type="long"&gt;
+ *                   &lt;param name="min"&gt;20&lt;/param&gt;
+ *                   &lt;param name="max"&gt;50&lt;/param&gt;
+ *                   &lt;message&gt;Age needs to be between ${min} and ${max}&lt;/message&gt;
+ *               &lt;/field-validator&gt;
+ *           &lt;/field&gt;
+ *      &lt;/validators&gt;
+ * <!-- END SNIPPET: examples -->
+ * </pre>
+ * 
+ * 
+ * 
+ * @version $Date$
+ */
+public class LongRangeFieldValidator extends AbstractRangeValidator {
+
+    Long max = null;
+    Long min = null;
+
+
+    public void setMax(Long max) {
+        this.max = max;
+    }
+
+    public Long getMax() {
+        return max;
+    }
+
+    public Comparable getMaxComparatorValue() {
+        return max;
+    }
+
+    public void setMin(Long min) {
+        this.min = min;
+    }
+
+    public Long getMin() {
+        return min;
+    }
+
+    public Comparable getMinComparatorValue() {
+        return min;
+    }
+}

src/java/com/opensymphony/xwork2/validator/validators/ShortRangeFieldValidator.java

+/*
+ * Copyright (c) 2002-2006 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork2.validator.validators;
+
+
+/**
+ * <!-- START SNIPPET: javadoc -->
+ * Field Validator that checks if the short specified is within a certain range.
+ * <!-- END SNIPPET: javadoc -->
+ * 
+ * 
+ * <!-- START SNIPPET: parameters -->
+ * <ul>
+ *              <li>fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required</li>
+ *              <li>min - the minimum value (if none is specified, it will not be checked) </li>
+ *              <li>max - the maximum value (if none is specified, it will not be checked) </li>
+ * </ul>
+ * <!-- END SNIPPET: parameters -->
+ * 
+ * 
+ * <pre>
+ * <!-- START SNIPPET: examples -->
+ *              &lt;validators>
+ *           &lt;!-- Plain Validator Syntax --&gt;
+ *           &lt;validator type="short">
+ *               &lt;param name="fieldName"&gt;age&lt;/param&gt;
+ *               &lt;param name="min"&gt;20&lt;/param&gt;
+ *               &lt;param name="max"&gt;50&lt;/param&gt;
+ *               &lt;message&gt;Age needs to be between ${min} and ${max}&lt;/message&gt;
+ *           &lt;/validator&gt;
+ *           
+ *           &lt;!-- Field Validator Syntax --&gt;
+ *           &lt;field name="age"&gt;
+ *               &lt;field-validator type="short"&gt;
+ *                   &lt;param name="min"&gt;20&lt;/param&gt;
+ *                   &lt;param name="max"&gt;50&lt;/param&gt;
+ *                   &lt;message&gt;Age needs to be between ${min} and ${max}&lt;/message&gt;
+ *               &lt;/field-validator&gt;
+ *           &lt;/field&gt;
+ *      &lt;/validators&gt;
+ * <!-- END SNIPPET: examples -->
+ * </pre>
+ * 
+ * 
+ * 
+ * @version $Date$
+ */
+public class ShortRangeFieldValidator extends AbstractRangeValidator {
+
+    Short max = null;
+    Short min = null;
+
+
+    public void setMax(Short max) {
+        this.max = max;
+    }
+
+    public Short getMax() {
+        return max;
+    }
+
+    public Comparable getMaxComparatorValue() {
+        return max;
+    }
+
+    public void setMin(Short min) {
+        this.min = min;
+    }
+
+    public Short getMin() {
+        return min;
+    }
+
+    public Comparable getMinComparatorValue() {
+        return min;
+    }
+}

src/java/com/opensymphony/xwork2/validator/validators/default.xml

     <validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/>
     <validator name="requiredstring" class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/>
     <validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/>
+    <validator name="long" class="com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/>
+    <validator name="short" class="com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/>
     <validator name="double" class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/>
     <validator name="date" class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/>
     <validator name="expression" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/>

src/test/com/opensymphony/xwork2/SimpleAction-validation.xml

             <message key="baz.range">Could not find baz.range!</message>
         </field-validator>
     </field>
+    <field name="longFoo">
+        <field-validator type="long">
+            <param name="min">0</param>
+            <param name="max">100</param>
+            <message key="foo.range">Could not find foo.range!</message>
+        </field-validator>
+    </field>
+    <field name="shortFoo">
+        <field-validator type="short">
+            <param name="min">0</param>
+            <param name="max">100</param>
+            <message key="foo.range">Could not find foo.range!</message>
+        </field-validator>
+    </field>
     <validator type="expression">
         <param name="expression">foo > bar</param>
         <message>Foo must be greater than Bar. Foo = ${foo}, Bar = ${bar}.</message>

src/test/com/opensymphony/xwork2/SimpleAction.java

     private int bar;
     private int baz;
     private int foo;
+    private long longFoo;
+    private short shortFoo;
     private double percentage;
     private Map<Integer,String> indexedProps = new HashMap<Integer,String>();
 
     public String doInput() throws Exception {
         return INPUT;
     }
+
+
+    public long getLongFoo() {
+        return longFoo;
+    }
+
+
+    public void setLongFoo(long longFoo) {
+        this.longFoo = longFoo;
+    }
+
+
+    public short getShortFoo() {
+        return shortFoo;
+    }
+
+
+    public void setShortFoo(short shortFoo) {
+        this.shortFoo = shortFoo;
+    }
 }

src/test/com/opensymphony/xwork2/validator/DefaultActionValidatorManagerTest.java

         List validatorList = actionValidatorManager.getValidators(SimpleAction.class, alias);
 
         // 6 in the class level + 2 in the alias
-        assertEquals(9, validatorList.size());
+        assertEquals(11, validatorList.size());
     }
 
     public void testBuildsValidatorsForAliasError() {
         
         
         // 9 in the class hierarchy + 1 in the interface + 1 in interface alias
-        assertEquals(11, validatorList.size());
+        assertEquals(13, validatorList.size());
 
         // action-level validator comes first
         final Validator expressionValidator = (Validator) validatorList.get(0);
         final FieldValidator bazValidator = (FieldValidator) validatorList.get(6);
         assertEquals("baz", bazValidator.getFieldName());
         assertTrue(bazValidator instanceof IntRangeFieldValidator);
+        
+        final FieldValidator longValidator = (FieldValidator) validatorList.get(7);
+        assertEquals("longFoo", longValidator.getFieldName());
+        assertTrue(longValidator instanceof LongRangeFieldValidator);
+        
+        final FieldValidator shortValidator = (FieldValidator) validatorList.get(8);
+        assertEquals("shortFoo", shortValidator.getFieldName());
+        assertTrue(shortValidator instanceof ShortRangeFieldValidator);
 
-        final FieldValidator bazValidator1 = (FieldValidator) validatorList.get(7);
+        final FieldValidator bazValidator1 = (FieldValidator) validatorList.get(9);
         assertEquals("baz", bazValidator1.getFieldName());
         assertTrue(bazValidator1 instanceof RequiredFieldValidator);
 
-        final FieldValidator bazValidator2 = (FieldValidator) validatorList.get(8);
+        final FieldValidator bazValidator2 = (FieldValidator) validatorList.get(10);
         assertEquals("baz", bazValidator2.getFieldName());
         assertTrue(bazValidator2 instanceof IntRangeFieldValidator);
 
-        final FieldValidator dataValidator1 = (FieldValidator) validatorList.get(9);
+        final FieldValidator dataValidator1 = (FieldValidator) validatorList.get(11);
         assertEquals("data", dataValidator1.getFieldName());
         assertTrue(dataValidator1 instanceof RequiredFieldValidator);
 
-        final FieldValidator dataValidator2 = (FieldValidator) validatorList.get(10);
+        final FieldValidator dataValidator2 = (FieldValidator) validatorList.get(12);
         assertEquals("data", dataValidator2.getFieldName());
         assertTrue(dataValidator2 instanceof RequiredStringValidator);
     }

src/test/com/opensymphony/xwork2/validator/LongRangeValidatorTest.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork2.validator;
+
+import com.opensymphony.xwork2.*;
+import com.opensymphony.xwork2.config.ConfigurationManager;
+import com.opensymphony.xwork2.config.providers.MockConfigurationProvider;
+import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+
+/**
+ * LongRangeValidatorTest
+ * <p/>
+ *
+ */
+public class LongRangeValidatorTest extends XWorkTestCase {
+
+    public void testRangeValidation() {
+        HashMap params = new HashMap();
+        params.put("longFoo", "200");
+
+        HashMap extraContext = new HashMap();
+        extraContext.put(ActionContext.PARAMETERS, params);
+
+        try {
+            ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, extraContext);
+            proxy.execute();
+            assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
+
+            Map errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
+            List errorMessages = (List) errors.get("longFoo");
+            assertEquals(1, errorMessages.size());
+
+            String errorMessage = (String) errorMessages.get(0);
+            assertNotNull(errorMessage);
+        } catch (Exception e) {
+            e.printStackTrace();
+            fail();
+        }
+    }
+
+    protected void setUp() throws Exception {
+        loadConfigurationProviders(new XmlConfigurationProvider("xwork-test-beans.xml"), new MockConfigurationProvider());
+    }
+}

src/test/com/opensymphony/xwork2/validator/ShortRangeValidatorTest.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork2.validator;
+
+import com.opensymphony.xwork2.*;
+import com.opensymphony.xwork2.config.ConfigurationManager;
+import com.opensymphony.xwork2.config.providers.MockConfigurationProvider;
+import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+
+/**
+ * ShortRangeValidatorTest
+ * <p/>
+ *
+ */
+public class ShortRangeValidatorTest extends XWorkTestCase {
+
+    public void testRangeValidation() {
+        HashMap params = new HashMap();
+        params.put("shortFoo", "200");
+
+        HashMap extraContext = new HashMap();
+        extraContext.put(ActionContext.PARAMETERS, params);
+
+        try {
+            ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, extraContext);
+            proxy.execute();
+            assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
+
+            Map errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
+            List errorMessages = (List) errors.get("shortFoo");
+            assertEquals(1, errorMessages.size());
+
+            String errorMessage = (String) errorMessages.get(0);
+            assertNotNull(errorMessage);
+        } catch (Exception e) {
+            e.printStackTrace();
+            fail();
+        }
+    }
+
+    protected void setUp() throws Exception {
+        loadConfigurationProviders(new XmlConfigurationProvider("xwork-test-beans.xml"), new MockConfigurationProvider());
+    }
+}

src/test/com/opensymphony/xwork2/validator/ValidatorFactoryTest.java

             // 13 from default.xml overwritten by validators.xml
             //  1 from my-Validators.xml
             //  1 from myOther-validators.xml
-            assertEquals(15, validators.size());
+            assertEquals(17, validators.size());
         } catch (Exception e) {
             fail("We shouldn't get here...");
         }