1. opensymphony
  2. xwork

Commits

rainerh  committed 36fb323

Create a DoubleRangeFieldValidator
Issue number: XW-216
Obtained from:
Submitted by:
Reviewed by:

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

  • Participants
  • Parent commits a184bee
  • Branches master

Comments (0)

Files changed (9)

File src/java/com/opensymphony/xwork/validator/validators/DoubleRangeFieldValidator.java

View file
  • Ignore whitespace
+/*
+ * Copyright (c) 2002-2006 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork.validator.validators;
+
+/**
+ * <!-- START SNIPPET: javadoc -->
+ * Field Validator that checks if the double 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="double">
+ *               &lt;param name="fieldName"&gt;percentage&lt;/param&gt;
+ *               &lt;param name="min"&gt;20.1&lt;/param&gt;
+ *               &lt;param name="max"&gt;50.1&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="percentage"&gt;
+ *               &lt;field-validator type="double"&gt;
+ *                   &lt;param name="min"&gt;0.123&lt;/param&gt;
+ *                   &lt;param name="max"&gt;99.98&lt;/param&gt;
+ *                   &lt;message&gt;Percentage needs to be between ${min} and ${max}&lt;/message&gt;
+ *               &lt;/field-validator&gt;
+ *           &lt;/field&gt;
+ *      &lt;/validators&gt;
+ * <!-- END SNIPPET: examples -->
+ * </pre>
+ *
+ * @author Rainer Hermanns
+ * @version $Id$
+ */
+public class DoubleRangeFieldValidator extends AbstractRangeValidator {
+
+    Double max = null;
+    Double min = null;
+
+
+    public void setMax(Double max) {
+        this.max = max;
+    }
+
+    public Double getMax() {
+        return max;
+    }
+
+    public Comparable getMaxComparatorValue() {
+        return max;
+    }
+
+    public void setMin(Double min) {
+        this.min = min;
+    }
+
+    public Double getMin() {
+        return min;
+    }
+
+    public Comparable getMinComparatorValue() {
+        return min;
+    }
+}

File src/java/com/opensymphony/xwork/validator/validators/default.xml

View file
  • Ignore whitespace
     <validator name="required" class="com.opensymphony.xwork.validator.validators.RequiredFieldValidator"/>
     <validator name="requiredstring" class="com.opensymphony.xwork.validator.validators.RequiredStringValidator"/>
     <validator name="int" class="com.opensymphony.xwork.validator.validators.IntRangeFieldValidator"/>
+    <validator name="double" class="com.opensymphony.xwork.validator.validators.DoubleRangeFieldValidator"/>
     <validator name="date" class="com.opensymphony.xwork.validator.validators.DateRangeFieldValidator"/>
     <validator name="expression" class="com.opensymphony.xwork.validator.validators.ExpressionValidator"/>
     <validator name="fieldexpression" class="com.opensymphony.xwork.validator.validators.FieldExpressionValidator"/>

File src/test/com/opensymphony/xwork/SimpleAction-validation.xml

View file
  • Ignore whitespace
             <message>bar must be between ${min} and ${max}, current value is ${bar}.</message>
         </field-validator>
     </field>
+    <field name="percentage">
+        <field-validator type="double">
+            <param name="min">0.1</param>
+            <param name="max">10.1</param>
+            <message>percentage must be between ${min} and ${max}, current value is ${percentage}.</message>
+        </field-validator>
+    </field>
     <field name="date">
         <field-validator type="date">
             <param name="min">12/22/2002</param>

File src/test/com/opensymphony/xwork/SimpleAction.java

View file
  • Ignore whitespace
     private int bar;
     private int baz;
     private int foo;
+    private double percentage;
 
     private String aliasSource;
     private String aliasDest;
         return bar;
     }
 
+    public double getPercentage() {
+        return percentage;
+    }
+
+    public void setPercentage(double percentage) {
+        this.percentage = percentage;
+    }
+
     public void setBaz(int baz) {
         this.baz = baz;
     }

File src/test/com/opensymphony/xwork/config/providers/MockConfigurationProvider.java

View file
  • Ignore whitespace
         params = new HashMap();
         params.put("date", new java.util.Date(2002 - 1900, 11, 20));
 
+        //Explicitly set an out-of-range double for DoubleRangeValidatorTest
+        params.put("percentage", new Double(100.0123));
+
         ActionConfig validationActionConfig = new ActionConfig(null, SimpleAction.class, params, results, interceptors);
         defaultPackageContext.addActionConfig(VALIDATION_ACTION_NAME, validationActionConfig);
         defaultPackageContext.addActionConfig(VALIDATION_ALIAS_NAME, validationActionConfig);
         defaultPackageContext.addActionConfig(VALIDATION_SUBPROPERTY_NAME, validationActionConfig);
 
+
+        params = new HashMap();
+        params.put("percentage", new Double(1.234567));
+        ActionConfig percentageActionConfig = new ActionConfig(null, SimpleAction.class, params, results, interceptors);
+        defaultPackageContext.addActionConfig("percentage", percentageActionConfig);
+
         // We need this actionconfig to be the final destination for action chaining
         ActionConfig barActionConfig = new ActionConfig(null, SimpleAction.class, null, null, null);
         defaultPackageContext.addActionConfig("bar", barActionConfig);
     /**
      * Tells whether the ConfigurationProvider should reload its configuration
      *
-     * @return
+     * @return false
      */
     public boolean needsReload() {
         return false;

File src/test/com/opensymphony/xwork/validator/DefaultActionValidatorManagerTest.java

View file
  • Ignore whitespace
         List validatorList = actionValidatorManager.getValidators(SimpleAction.class, alias);
 
         // 6 in the class level + 2 in the alias
-        assertEquals(8, validatorList.size());
+        assertEquals(9, validatorList.size());
     }
 
     public void testDefaultMessageInterpolation() {
        
         
         
-        // 8 in the class hierarchy + 1 in the interface + 1 in interface alias
-        assertEquals(10, validatorList.size());
+        // 9 in the class hierarchy + 1 in the interface + 1 in interface alias
+        assertEquals(11, validatorList.size());
 
         // action-level validator comes first
         final Validator expressionValidator = (Validator) validatorList.get(0);
         assertEquals("bar", barValidator2.getFieldName());
         assertTrue(barValidator2 instanceof IntRangeFieldValidator);
 
-        final FieldValidator dateValidator = (FieldValidator) validatorList.get(3);
+        final FieldValidator doubleValidator = (FieldValidator) validatorList.get(3);
+        assertEquals("percentage", doubleValidator.getFieldName());
+        assertTrue(doubleValidator instanceof DoubleRangeFieldValidator);
+
+        final FieldValidator dateValidator = (FieldValidator) validatorList.get(4);
         assertEquals("date", dateValidator.getFieldName());
         assertTrue(dateValidator instanceof DateRangeFieldValidator);
 
-        final FieldValidator fooValidator = (FieldValidator) validatorList.get(4);
+        final FieldValidator fooValidator = (FieldValidator) validatorList.get(5);
         assertEquals("foo", fooValidator.getFieldName());
         assertTrue(fooValidator instanceof IntRangeFieldValidator);
 
-        final FieldValidator bazValidator = (FieldValidator) validatorList.get(5);
+        final FieldValidator bazValidator = (FieldValidator) validatorList.get(6);
         assertEquals("baz", bazValidator.getFieldName());
         assertTrue(bazValidator instanceof IntRangeFieldValidator);
 
-        final FieldValidator bazValidator1 = (FieldValidator) validatorList.get(6);
+        final FieldValidator bazValidator1 = (FieldValidator) validatorList.get(7);
         assertEquals("baz", bazValidator1.getFieldName());
         assertTrue(bazValidator1 instanceof RequiredFieldValidator);
 
-        final FieldValidator bazValidator2 = (FieldValidator) validatorList.get(7);
+        final FieldValidator bazValidator2 = (FieldValidator) validatorList.get(8);
         assertEquals("baz", bazValidator2.getFieldName());
         assertTrue(bazValidator2 instanceof IntRangeFieldValidator);
 
-        final FieldValidator dataValidator1 = (FieldValidator) validatorList.get(8);
+        final FieldValidator dataValidator1 = (FieldValidator) validatorList.get(9);
         assertEquals("data", dataValidator1.getFieldName());
         assertTrue(dataValidator1 instanceof RequiredFieldValidator);
 
-        final FieldValidator dataValidator2 = (FieldValidator) validatorList.get(9);
+        final FieldValidator dataValidator2 = (FieldValidator) validatorList.get(10);
         assertEquals("data", dataValidator2.getFieldName());
         assertTrue(dataValidator2 instanceof RequiredStringValidator);
     }

File src/test/com/opensymphony/xwork/validator/DoubleRangeValidatorTest.java

View file
  • Ignore whitespace
+package com.opensymphony.xwork.validator;
+
+import junit.framework.TestCase;
+
+import java.util.Map;
+import java.util.List;
+import java.util.Iterator;
+import java.util.HashMap;
+
+import com.opensymphony.xwork.ActionProxy;
+import com.opensymphony.xwork.ActionProxyFactory;
+import com.opensymphony.xwork.ValidationAware;
+import com.opensymphony.xwork.config.providers.MockConfigurationProvider;
+import com.opensymphony.xwork.config.ConfigurationManager;
+
+/**
+ * <code>DoubleRangeValidatorTest</code>
+ *
+ * @author <a href="mailto:hermanns@aixcept.de">Rainer Hermanns</a>
+ * @version $Id$
+ */
+public class DoubleRangeValidatorTest extends TestCase {
+
+    public void testRangeValidationWithError() throws Exception {
+        ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, null);
+        proxy.execute();
+        assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
+
+        Map errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
+        Iterator it = errors.entrySet().iterator();
+
+        List errorMessages = (List) errors.get("percentage");
+        assertNotNull("Expected double range validation error message.", errorMessages);
+        assertEquals(1, errorMessages.size());
+
+        String errorMessage = (String) errorMessages.get(0);
+        assertNotNull("Expecting: percentage must be between 0.1 and 10.1, current value is 100.0123.", errorMessage);
+        assertEquals("percentage must be between 0.1 and 10.1, current value is 100.0123.", errorMessage);
+    }
+
+    public void testRangeValidationNoError() throws Exception {
+
+        ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy("", "percentage", null);
+        proxy.execute();
+        assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
+
+        Map errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
+        Iterator it = errors.entrySet().iterator();
+
+        List errorMessages = (List) errors.get("percentage");
+        assertNull("Expected no double range validation error message.", errorMessages);
+
+    }
+
+    protected void setUp() throws Exception {
+        ConfigurationManager.clearConfigurationProviders();
+        ConfigurationManager.addConfigurationProvider(new MockConfigurationProvider());
+        ConfigurationManager.getConfiguration().reload();
+    }
+}

File src/test/com/opensymphony/xwork/validator/SimpleActionValidationTest.java

View file
  • Ignore whitespace
         //valid values
         params.put("bar", "7");
         params.put("date", "12/23/2002");
+        params.put("percentage", "1.23456789");
 
         HashMap extraContext = new HashMap();
         extraContext.put(ActionContext.PARAMETERS, params);

File src/test/validators.xml

View file
  • Ignore whitespace
     <validator name="required" class="com.opensymphony.xwork.validator.validators.RequiredFieldValidator"/>
     <validator name="requiredstring" class="com.opensymphony.xwork.validator.validators.RequiredStringValidator"/>
     <validator name="int" class="com.opensymphony.xwork.validator.validators.IntRangeFieldValidator"/>
+    <validator name="double" class="com.opensymphony.xwork.validator.validators.DoubleRangeFieldValidator"/>
     <validator name="date" class="com.opensymphony.xwork.validator.validators.DateRangeFieldValidator"/>
     <validator name="expression" class="com.opensymphony.xwork.validator.validators.ExpressionValidator"/>
     <validator name="fieldexpression" class="com.opensymphony.xwork.validator.validators.FieldExpressionValidator"/>