Author: sebb
Date: Sun Nov 11 13:16:22 2012
New Revision: 1407973
URL: http://svn.apache.org/viewvc?rev=1407973&view=rev
Log:
LANG-822: NumberUtils#createNumber - bad behaviour for leading "--"
Modified:
commons/proper/lang/trunk/src/changes/changes.xml
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
Modified: commons/proper/lang/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1407973&r1=1407972&r2=1407973&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/changes/changes.xml (original)
+++ commons/proper/lang/trunk/src/changes/changes.xml Sun Nov 11 13:16:22 2012
@@ -35,6 +35,7 @@
<action issue="LANG-828" type="fix">FastDateParser does not handle
non-Gregorian calendars properly</action>
<action issue="LANG-826" type="fix">FastDateParser does not handle
non-ASCII digits correctly</action>
<action issue="LANG-825" type="add">Create StrBuilder APIs similar to
String.format(String, Object...)</action>
+ <action issue="LANG-822" type="fix">NumberUtils#createNumber - bad
behaviour for leading "--"</action>
<action issue="LANG-818" type="fix">FastDateFormat's "z" pattern does not
respect timezone of Calendar instances passed to format()</action>
<action issue="LANG-817" type="fix">Add
org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS_8</action>
<action issue="LANG-813" type="fix">StringUtils.equalsIgnoreCase doesn't
check string reference equality</action>
Modified:
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java?rev=1407973&r1=1407972&r2=1407973&view=diff
==============================================================================
---
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
(original)
+++
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
Sun Nov 11 13:16:22 2012
@@ -449,13 +449,6 @@ public class NumberUtils {
if (StringUtils.isBlank(str)) {
throw new NumberFormatException("A blank string is not a valid
number");
}
- if (str.startsWith("--")) {
- // this is protection for poorness in java.lang.BigDecimal.
- // it accepts this as a legal value, but it does not appear
- // to be in specification of class. OS X Java parses it to
- // a wrong value.
- return null;
- }
if (str.startsWith("0x") || str.startsWith("-0x") ||
str.startsWith("0X") || str.startsWith("-0X")) {
int hexDigits = str.length() - 2; // drop 0x
if (str.startsWith("-")) { // drop -
@@ -721,7 +714,14 @@ public class NumberUtils {
// handle JDK1.3.1 bug where "" throws IndexOutOfBoundsException
if (StringUtils.isBlank(str)) {
throw new NumberFormatException("A blank string is not a valid
number");
- }
+ }
+ if (str.trim().startsWith("--")) {
+ // this is protection for poorness in java.lang.BigDecimal.
+ // it accepts this as a legal value, but it does not appear
+ // to be in specification of class. OS X Java parses it to
+ // a wrong value.
+ throw new NumberFormatException(str + " is not a valid number.");
+ }
return new BigDecimal(str);
}
Modified:
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java?rev=1407973&r1=1407972&r2=1407973&view=diff
==============================================================================
---
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
(original)
+++
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
Sun Nov 11 13:16:22 2012
@@ -229,6 +229,20 @@ public class NumberUtilsTest {
// LANG-693
assertEquals("createNumber(String) LANG-693 failed",
Double.valueOf(Double.MAX_VALUE), NumberUtils
.createNumber("" + Double.MAX_VALUE));
+
+ // LANG-822
+ // ensure that the underlying negative number would create a BigDecimal
+ final Number bigNum = NumberUtils.createNumber("-1.1E-700F");
+ assertEquals(BigDecimal.class,bigNum.getClass());
+ assertNotNull(bigNum);
+
+ // Check that the code fails to create a valid number when preceeded
by -- rather than -
+ try {
+ NumberUtils.createNumber("--1.1E-700F");
+ fail("Expected NumberFormatException");
+ } catch (NumberFormatException nfe) {
+ // expected
+ }
}
@Test