From 4920a1da320677108c98bc191bdbffe7169983ba Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Sun, 8 Jun 2003 14:14:01 +0000 Subject: [PATCH] Fix bug in NumberUtils where '12a' was treated as a number. bug 20538, reported/fix by Eric Pugh/Phil Steitz git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137359 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang/NumberUtils.java | 6 +++- .../apache/commons/lang/NumberUtilsTest.java | 28 +++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/java/org/apache/commons/lang/NumberUtils.java b/src/java/org/apache/commons/lang/NumberUtils.java index a2d3d15dd..047d08268 100644 --- a/src/java/org/apache/commons/lang/NumberUtils.java +++ b/src/java/org/apache/commons/lang/NumberUtils.java @@ -62,8 +62,10 @@ import java.math.BigDecimal; * @author Rand McNeely * @author Stephen Colebourne * @author Steve Downey + * @author Eric Pugh + * @author Phil Steitz * @since 1.0 - * @version $Id: NumberUtils.java,v 1.9 2003/06/08 14:09:36 scolebourne Exp $ + * @version $Id: NumberUtils.java,v 1.10 2003/06/08 14:14:01 scolebourne Exp $ */ public final class NumberUtils { @@ -763,6 +765,8 @@ public final class NumberUtils { // not allowing L with an exponoent return foundDigit && !hasExp; } + // last character is illegal + return false; } // allowSigns is true iff the val ends in 'E' // found digit it to make sure weird stuff like '.' and '1E-' doesn't pass diff --git a/src/test/org/apache/commons/lang/NumberUtilsTest.java b/src/test/org/apache/commons/lang/NumberUtilsTest.java index e0401bfef..daf17aee3 100644 --- a/src/test/org/apache/commons/lang/NumberUtilsTest.java +++ b/src/test/org/apache/commons/lang/NumberUtilsTest.java @@ -65,7 +65,10 @@ import junit.framework.TestSuite; * * @author Rand McNeely * @author Ringo De Smet - * @version $Id: NumberUtilsTest.java,v 1.6 2003/05/21 23:49:15 scolebourne Exp $ + * @author Eric Pugh + * @author Phil Steitz + * @author Stephen Colebourne + * @version $Id: NumberUtilsTest.java,v 1.7 2003/06/08 14:14:01 scolebourne Exp $ */ public class NumberUtilsTest extends TestCase { @@ -493,13 +496,34 @@ public class NumberUtilsTest extends TestCase { val = "1234E5l"; assertTrue("isNumber(String) 14 Neg failed", !NumberUtils.isNumber(val)); assertTrue("isNumber(String)/createNumber(String) 14 Neg failed", !checkCreateNumber(val)); + val = "11a"; + assertTrue("isNumber(String) 15 Neg failed", !NumberUtils.isNumber(val)); + assertTrue("isNumber(String)/createNumber(String) 15 Neg failed", !checkCreateNumber(val)); + val = "1a"; + assertTrue("isNumber(String) 16 Neg failed", !NumberUtils.isNumber(val)); + assertTrue("isNumber(String)/createNumber(String) 16 Neg failed", !checkCreateNumber(val)); + val = "a"; + assertTrue("isNumber(String) 17 Neg failed", !NumberUtils.isNumber(val)); + assertTrue("isNumber(String)/createNumber(String) 17 Neg failed", !checkCreateNumber(val)); + val = "11g"; + assertTrue("isNumber(String) 18 Neg failed", !NumberUtils.isNumber(val)); + assertTrue("isNumber(String)/createNumber(String) 18 Neg failed", !checkCreateNumber(val)); + val = "11z"; + assertTrue("isNumber(String) 19 Neg failed", !NumberUtils.isNumber(val)); + assertTrue("isNumber(String)/createNumber(String) 19 Neg failed", !checkCreateNumber(val)); + val = "11def"; + assertTrue("isNumber(String) 20 Neg failed", !NumberUtils.isNumber(val)); + assertTrue("isNumber(String)/createNumber(String) 20 Neg failed", !checkCreateNumber(val)); + val = "11d11"; + assertTrue("isNumber(String) 21 Neg failed", !NumberUtils.isNumber(val)); + assertTrue("isNumber(String)/createNumber(String) 21 Neg failed", !checkCreateNumber(val)); } private boolean checkCreateNumber(String val) { try { Object obj = NumberUtils.createNumber(val); - if(obj == null) { + if (obj == null) { return false; } return true;