Author: tv
Date: Sun Feb 11 17:11:55 2018
New Revision: 1823857

URL: http://svn.apache.org/viewvc?rev=1823857&view=rev
Log:
TRB-94: Align validation of numbers with ValueParser behavior

Modified:
    turbine/fulcrum/trunk/intake/src/changes/changes.xml
    
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/BigDecimalValidator.java
    
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/DoubleValidator.java
    
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/FloatValidator.java
    
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/NumberValidator.java
    
turbine/fulcrum/trunk/intake/src/test/org/apache/fulcrum/intake/IntakeValidatonTest.java

Modified: turbine/fulcrum/trunk/intake/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/intake/src/changes/changes.xml?rev=1823857&r1=1823856&r2=1823857&view=diff
==============================================================================
--- turbine/fulcrum/trunk/intake/src/changes/changes.xml (original)
+++ turbine/fulcrum/trunk/intake/src/changes/changes.xml Sun Feb 11 17:11:55 
2018
@@ -28,8 +28,11 @@
      <release version="1.2.4" date="in Subversion">
      </release>
      <release version="1.2.3" date="2018-02-11">
+      <action type="fix" dev="tv" issue="TRB-94" due-to="Tilo Villwock">
+        Intake's Validator and the BaseValueParser don't parse inputs the same 
way
+      </action>
       <action dev="tv" type="update">
-        Update dependencies commons-lang3, commons-pool2
+        Update dependencies commons-lang3 3.5, commons-pool2 2.5.0
       </action>
       <action dev="painter" type="fix">
         Fix FileNotFoundException during parallel deployment with Tomcat

Modified: 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/BigDecimalValidator.java
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/BigDecimalValidator.java?rev=1823857&r1=1823856&r2=1823857&view=diff
==============================================================================
--- 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/BigDecimalValidator.java
 (original)
+++ 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/BigDecimalValidator.java
 Sun Feb 11 17:11:55 2018
@@ -20,8 +20,6 @@ package org.apache.fulcrum.intake.valida
  */
 
 import java.math.BigDecimal;
-import java.text.NumberFormat;
-import java.text.ParseException;
 import java.util.Locale;
 
 
@@ -64,16 +62,7 @@ public class BigDecimalValidator
     @Override
     protected BigDecimal parseNumber(String stringValue, Locale locale) throws 
NumberFormatException
     {
-        NumberFormat nf = NumberFormat.getInstance(locale);
-
-        try
-        {
-            Number number = nf.parse(stringValue);
-            return new BigDecimal(number.doubleValue());
-               }
-        catch (ParseException e)
-        {
-               throw new NumberFormatException(e.getMessage());
-               }
+        Number number = parseIntoNumber(stringValue, locale);
+        return new BigDecimal(number.doubleValue());
     }
 }

Modified: 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/DoubleValidator.java
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/DoubleValidator.java?rev=1823857&r1=1823856&r2=1823857&view=diff
==============================================================================
--- 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/DoubleValidator.java
 (original)
+++ 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/DoubleValidator.java
 Sun Feb 11 17:11:55 2018
@@ -1,26 +1,5 @@
 package org.apache.fulcrum.intake.validator;
 
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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 java.text.NumberFormat;
-import java.text.ParseException;
 import java.util.Locale;
 
 /**
@@ -62,15 +41,7 @@ public class DoubleValidator
     @Override
     protected Double parseNumber(String stringValue, Locale locale) throws 
NumberFormatException
     {
-        NumberFormat nf = NumberFormat.getInstance(locale);
-
-        try
-        {
-                       return 
Double.valueOf(nf.parse(stringValue).doubleValue());
-               }
-        catch (ParseException e)
-        {
-               throw new NumberFormatException(e.getMessage());
-               }
+        Number number = parseIntoNumber(stringValue, locale);
+               return Double.valueOf(number.doubleValue());
     }
 }

Modified: 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/FloatValidator.java
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/FloatValidator.java?rev=1823857&r1=1823856&r2=1823857&view=diff
==============================================================================
--- 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/FloatValidator.java
 (original)
+++ 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/FloatValidator.java
 Sun Feb 11 17:11:55 2018
@@ -1,26 +1,5 @@
 package org.apache.fulcrum.intake.validator;
 
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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 java.text.NumberFormat;
-import java.text.ParseException;
 import java.util.Locale;
 
 /**
@@ -62,15 +41,7 @@ public class FloatValidator
     @Override
     protected Float parseNumber(String stringValue, Locale locale) throws 
NumberFormatException
     {
-        NumberFormat nf = NumberFormat.getInstance(locale);
-
-        try
-        {
-            return Float.valueOf(nf.parse(stringValue).floatValue());
-               }
-        catch (ParseException e)
-        {
-               throw new NumberFormatException(e.getMessage());
-               }
+        Number number = parseIntoNumber(stringValue, locale);
+        return Float.valueOf(number.floatValue());
     }
 }

Modified: 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/NumberValidator.java
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/NumberValidator.java?rev=1823857&r1=1823856&r2=1823857&view=diff
==============================================================================
--- 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/NumberValidator.java
 (original)
+++ 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/NumberValidator.java
 Sun Feb 11 17:11:55 2018
@@ -1,5 +1,9 @@
 package org.apache.fulcrum.intake.validator;
 
+import java.text.NumberFormat;
+import java.text.ParseException;
+import java.text.ParsePosition;
+
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -133,6 +137,38 @@ public abstract class NumberValidator<T
     protected abstract T parseNumber(String stringValue, Locale locale) throws 
NumberFormatException;
 
     /**
+     * Helper method to parse a number object out of a string
+     *
+     * @param stringValue the string value
+     * @param locale the locale to use while parsing
+     *
+     * @return the Number
+     *
+     * @throws NumberFormatException if the value could not be parsed
+     */
+    protected Number parseIntoNumber(String stringValue, Locale locale) throws 
NumberFormatException
+    {
+        NumberFormat nf = NumberFormat.getInstance(locale);
+
+        try
+        {
+            ParsePosition pos = new ParsePosition(0);
+            Number number = nf.parse(stringValue, pos);
+
+            if (pos.getIndex() != stringValue.length())
+            {
+                throw new ParseException("Could not parse string completely", 
pos.getErrorIndex());
+            }
+
+            return number;
+        }
+        catch (ParseException e)
+        {
+            throw new NumberFormatException(e.getMessage());
+        }
+    }
+
+    /**
      * Determine whether a field meets the criteria specified
      * in the constraints defined for this validator
      *

Modified: 
turbine/fulcrum/trunk/intake/src/test/org/apache/fulcrum/intake/IntakeValidatonTest.java
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/intake/src/test/org/apache/fulcrum/intake/IntakeValidatonTest.java?rev=1823857&r1=1823856&r2=1823857&view=diff
==============================================================================
--- 
turbine/fulcrum/trunk/intake/src/test/org/apache/fulcrum/intake/IntakeValidatonTest.java
 (original)
+++ 
turbine/fulcrum/trunk/intake/src/test/org/apache/fulcrum/intake/IntakeValidatonTest.java
 Sun Feb 11 17:11:55 2018
@@ -224,6 +224,17 @@ public class IntakeValidatonTest extends
         {
             fail("Validator should not throw ValidationException");
         }
+
+        // TRB-94
+        try
+        {
+            v.assertValidity("13,3m", Locale.GERMANY);
+            fail("Validator should throw ValidationException");
+        }
+        catch (ValidationException ve)
+        {
+            assertEquals("Not a number", ve.getMessage());
+        }
     }
 
     @Test
@@ -283,6 +294,17 @@ public class IntakeValidatonTest extends
         {
             fail("Validator should not throw ValidationException");
         }
+
+        // TRB-94
+        try
+        {
+            v.assertValidity("13m", Locale.GERMANY);
+            fail("Validator should throw ValidationException");
+        }
+        catch (ValidationException ve)
+        {
+            assertEquals("Not a number", ve.getMessage());
+        }
     }
 
     @Test
@@ -408,6 +430,17 @@ public class IntakeValidatonTest extends
         {
             fail("Validator should not throw ValidationException");
         }
+
+        // TRB-94
+        try
+        {
+            v.assertValidity("13,3m", Locale.GERMANY);
+            fail("Validator should throw ValidationException");
+        }
+        catch (ValidationException ve)
+        {
+            assertEquals("Not a number", ve.getMessage());
+        }
     }
 
     @Test
@@ -601,6 +634,17 @@ public class IntakeValidatonTest extends
         {
             fail("Validator should not throw ValidationException");
         }
+
+        // TRB-94
+        try
+        {
+            v.assertValidity("13,3m", Locale.GERMANY);
+            fail("Validator should throw ValidationException");
+        }
+        catch (ValidationException ve)
+        {
+            assertEquals("Not a number", ve.getMessage());
+        }
     }
 
     @Test
@@ -660,6 +704,17 @@ public class IntakeValidatonTest extends
         {
             fail("Validator should not throw ValidationException");
         }
+
+        // TRB-94
+        try
+        {
+            v.assertValidity("13m", Locale.GERMANY);
+            fail("Validator should throw ValidationException");
+        }
+        catch (ValidationException ve)
+        {
+            assertEquals("Not a number", ve.getMessage());
+        }
     }
 
     @Test
@@ -719,6 +774,17 @@ public class IntakeValidatonTest extends
         {
             fail("Validator should not throw ValidationException");
         }
+
+        // TRB-94
+        try
+        {
+            v.assertValidity("13m", Locale.GERMANY);
+            fail("Validator should throw ValidationException");
+        }
+        catch (ValidationException ve)
+        {
+            assertEquals("Not a number", ve.getMessage());
+        }
     }
 
 }


Reply via email to