← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 10637: Fixed bug with numeric regex pattern - didn't allow negative values.

 

------------------------------------------------------------
revno: 10637
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2013-04-19 15:52:46 +0200
message:
  Fixed bug with numeric regex pattern - didn't allow negative values.
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	2013-02-19 09:59:26 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java	2013-04-19 13:52:46 +0000
@@ -47,7 +47,7 @@
     
     private static final double TOLERANCE = 0.01; 
     
-    private static final Pattern NUMERIC_PATTERN = Pattern.compile( "^[ \\t]*[\\.]?[0-9]+[ \\t]*$|^[ \\t]*[0-9]+[\\.][0-9]+[ \\t]*$" );
+    private static final Pattern NUMERIC_PATTERN = Pattern.compile( "^-?(0|[1-9]\\d*)(\\.\\d+)?$" );
 
     /**
      * Validates whether an expression is true or false.

=== 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	2012-12-21 12:59:39 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java	2013-04-19 13:52:46 +0000
@@ -96,10 +96,9 @@
         assertTrue( MathUtils.isNumeric( "1.2" ) );
         assertTrue( MathUtils.isNumeric( "12.34" ) );
         assertTrue( MathUtils.isNumeric( "0.0" ) );
-        assertTrue( MathUtils.isNumeric( ".1" ) );
         assertTrue( MathUtils.isNumeric( "1.234" ) );
-        assertTrue( MathUtils.isNumeric( "1234  " ) );
-        assertTrue( MathUtils.isNumeric( "  1234" ) );
+        assertTrue( MathUtils.isNumeric( "-1234" ) );
+        assertTrue( MathUtils.isNumeric( "-12.34" ) );
 
         assertFalse( MathUtils.isNumeric( "Hey" ) );
         assertFalse( MathUtils.isNumeric( "45 Perinatal Condition" ) );
@@ -108,8 +107,16 @@
         assertFalse( MathUtils.isNumeric( "1 234" ) );
         assertFalse( MathUtils.isNumeric( "." ) );
         assertFalse( MathUtils.isNumeric( "1." ) );
+        assertFalse( MathUtils.isNumeric( ".1" ) );
         assertFalse( MathUtils.isNumeric( "" ) );
         assertFalse( MathUtils.isNumeric( " " ) );
+        assertFalse( MathUtils.isNumeric( "+1234  " ) );
+        assertFalse( MathUtils.isNumeric( "1234  " ) );
+        assertFalse( MathUtils.isNumeric( "  1234" ) );
+        assertFalse( MathUtils.isNumeric( "1,234" ) );
+        assertFalse( MathUtils.isNumeric( "0,1" ) );
+        assertFalse( MathUtils.isNumeric( "0," ) );
+        assertFalse( MathUtils.isNumeric( "0." ) );
         assertFalse( MathUtils.isNumeric( null ) );
     }