Author: jleroux
Date: Sun Sep 6 10:16:08 2009 New Revision: 811793 URL: http://svn.apache.org/viewvc?rev=811793&view=rev Log: A patch from Philipp Hoppen "Two bugs in UtilValidate.isDouble() and UtilValidate.isFloat()" (https://issues.apache.org/jira/browse/OFBIZ-2828) - OFBIZ-2828 I tested the Junits with and without the modifications : OK Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/test/BaseUnitTests.java ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilValidate.java Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/test/BaseUnitTests.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/test/BaseUnitTests.java?rev=811793&r1=811792&r2=811793&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/test/BaseUnitTests.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/test/BaseUnitTests.java Sun Sep 6 10:16:08 2009 @@ -22,6 +22,7 @@ import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.UtilFormatOut; +import org.ofbiz.base.util.UtilValidate; public class BaseUnitTests extends TestCase { @@ -57,4 +58,16 @@ null, UtilFormatOut.formatPrintableCreditCard(null)); } + public void testIsDouble_1() { + assertFalse(UtilValidate.isDouble("10.0", true, true, 2, 2)); + } + public void testIsFloat_1() { + assertFalse(UtilValidate.isFloat("10.0", true, true, 2, 2)); + } + public void testIsDouble_2() { + assertTrue(UtilValidate.isDouble("10.000", true, true, 3, 3)); + } + public void testIsFloat_2() { + assertTrue(UtilValidate.isFloat("10.000", true, true, 3, 3)); + } } Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilValidate.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilValidate.java?rev=811793&r1=811792&r2=811793&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilValidate.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilValidate.java Sun Sep 6 10:16:08 2009 @@ -486,14 +486,13 @@ float temp = Float.parseFloat(s); if (!allowNegative && temp < 0) return false; if (!allowPositive && temp > 0) return false; - String floatString = Float.toString(temp); - int decimalPoint = floatString.indexOf("."); + int decimalPoint = s.indexOf("."); if (decimalPoint == -1) { if (minDecimal > 0) return false; return true; } // 1.2345; length=6; point=1; num=4 - int numDecimals = floatString.length() - decimalPoint; + int numDecimals = s.length() - decimalPoint - 1; if (minDecimal >= 0 && numDecimals < minDecimal) return false; if (maxDecimal >= 0 && numDecimals > maxDecimal) return false; return true; @@ -511,14 +510,13 @@ double temp = Double.parseDouble(s); if (!allowNegative && temp < 0) return false; if (!allowPositive && temp > 0) return false; - String doubleString = Double.toString(temp); - int decimalPoint = doubleString.indexOf("."); + int decimalPoint = s.indexOf("."); if (decimalPoint == -1) { if (minDecimal > 0) return false; return true; } // 1.2345; length=6; point=1; num=4 - int numDecimals = doubleString.length() - decimalPoint; + int numDecimals = s.length() - decimalPoint - 1; if (minDecimal >= 0 && numDecimals < minDecimal) return false; if (maxDecimal >= 0 && numDecimals > maxDecimal) return false; return true; |
Free forum by Nabble | Edit this page |