← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2819: Improved test for numeric values in MathUtils with regex help from Bob

 

------------------------------------------------------------
revno: 2819
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2011-02-04 20:16:38 +0100
message:
  Improved test for numeric values in MathUtils with regex help from Bob
modified:
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java
  dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java	2011-01-13 15:38:43 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java	2011-02-04 19:16:38 +0000
@@ -30,7 +30,7 @@
 import java.math.BigDecimal;
 import java.math.MathContext;
 import java.util.Random;
-import java.util.Scanner;
+import java.util.regex.Pattern;
 
 import org.hisp.dhis.expression.Operator;
 import org.nfunk.jep.JEP;
@@ -45,6 +45,8 @@
     
     private static final double TOLERANCE = 0.01; 
     
+    private static final Pattern NUMERIC_PATTERN = Pattern.compile( "^[ \t]*[0-9]*[.]?[0-9]*[ \t]*$" );
+
     /**
      * Validates whether an expression is true or false.
      * 
@@ -185,7 +187,7 @@
      */
     public static boolean isNumeric( String value )
     {
-        return new Scanner( value ).hasNextDouble();
+        return value != null && !value.trim().isEmpty() && NUMERIC_PATTERN.matcher( value ).matches();
     }
     
     /**

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java'
--- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java	2011-01-13 15:38:43 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java	2011-02-04 19:16:38 +0000
@@ -41,7 +41,6 @@
  */
 public class MathUtilsTest
 {
-    
     @Test
     public void testExpressionIsTrue()
     {
@@ -78,7 +77,29 @@
 
         for (int i=0; i < numbers.length; ++i)
         {
-            assertEquals(rounded[i], MathUtils.roundToString( numbers[i], 4));
+            assertEquals( rounded[i], MathUtils.roundToString( numbers[i], 4) );
         }
     }
+    
+    @Test
+    public void testIsNumeric()
+    {
+        assertTrue( MathUtils.isNumeric( "123" ) );
+        assertTrue( MathUtils.isNumeric( "0" ) );
+        assertTrue( MathUtils.isNumeric( "1.2" ) );
+        assertTrue( MathUtils.isNumeric( "12.34" ) );
+        assertTrue( MathUtils.isNumeric( "0.0" ) );
+        assertTrue( MathUtils.isNumeric( "1.234" ) );
+        assertTrue( MathUtils.isNumeric( "1234  " ) );
+        assertTrue( MathUtils.isNumeric( "  1234" ) );
+
+        assertFalse( MathUtils.isNumeric( "Hey" ) );
+        assertFalse( MathUtils.isNumeric( "45 Perinatal Condition" ) );
+        assertFalse( MathUtils.isNumeric( "Long street 2" ) );
+        assertFalse( MathUtils.isNumeric( "1.2f" ) );
+        assertFalse( MathUtils.isNumeric( "1 234" ) );
+        assertFalse( MathUtils.isNumeric( "" ) );
+        assertFalse( MathUtils.isNumeric( " " ) );
+        assertFalse( MathUtils.isNumeric( null ) );
+    }
 }