From 80d47767966cc0cd1605aed55e254a699a7d16ef Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Sat, 25 Jun 2022 19:25:21 +0100 Subject: [PATCH] Negative number issue (2.13 branch - tests only) (#781) --- .../jackson/core/read/NumberParsingTest.java | 19 ++++++++ .../FailingNonStandardNumberParsingTest.java | 45 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/test/java/com/fasterxml/jackson/failing/FailingNonStandardNumberParsingTest.java diff --git a/src/test/java/com/fasterxml/jackson/core/read/NumberParsingTest.java b/src/test/java/com/fasterxml/jackson/core/read/NumberParsingTest.java index 898dec8969..e5b83c2344 100644 --- a/src/test/java/com/fasterxml/jackson/core/read/NumberParsingTest.java +++ b/src/test/java/com/fasterxml/jackson/core/read/NumberParsingTest.java @@ -177,6 +177,7 @@ public void testIntParsing() throws Exception public void testIntParsingWithStrings() throws Exception { assertEquals(3, NumberInput.parseInt("3")); + assertEquals(3, NumberInput.parseInt("+3")); assertEquals(0, NumberInput.parseInt("0")); assertEquals(-3, NumberInput.parseInt("-3")); assertEquals(27, NumberInput.parseInt("27")); @@ -189,6 +190,24 @@ public void testIntParsingWithStrings() throws Exception assertEquals(Integer.MAX_VALUE, NumberInput.parseInt(""+Integer.MAX_VALUE)); } + public void testLongParsingWithStrings() throws Exception + { + assertEquals(3, NumberInput.parseLong("3")); + assertEquals(3, NumberInput.parseLong("+3")); + assertEquals(0, NumberInput.parseLong("0")); + assertEquals(-3, NumberInput.parseLong("-3")); + assertEquals(27, NumberInput.parseLong("27")); + assertEquals(-31, NumberInput.parseLong("-31")); + assertEquals(271, NumberInput.parseLong("271")); + assertEquals(-131, NumberInput.parseLong("-131")); + assertEquals(2709, NumberInput.parseLong("2709")); + assertEquals(-9999, NumberInput.parseLong("-9999")); + assertEquals(Long.MIN_VALUE, NumberInput.parseLong(""+Long.MIN_VALUE)); + assertEquals(Integer.MIN_VALUE-1, NumberInput.parseLong(""+(Integer.MIN_VALUE-1))); + assertEquals(Long.MAX_VALUE, NumberInput.parseLong(""+Long.MAX_VALUE)); + assertEquals(Integer.MAX_VALUE+1, NumberInput.parseLong(""+(Integer.MAX_VALUE+1))); + } + /* /********************************************************************** /* Tests, Long diff --git a/src/test/java/com/fasterxml/jackson/failing/FailingNonStandardNumberParsingTest.java b/src/test/java/com/fasterxml/jackson/failing/FailingNonStandardNumberParsingTest.java new file mode 100644 index 0000000000..c80b9f02ba --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/failing/FailingNonStandardNumberParsingTest.java @@ -0,0 +1,45 @@ +package com.fasterxml.jackson.failing; + +import com.fasterxml.jackson.core.BaseTest; +import com.fasterxml.jackson.core.JsonFactory; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; +import com.fasterxml.jackson.core.json.JsonReadFeature; + +public class FailingNonStandardNumberParsingTest + extends BaseTest +{ + private final JsonFactory JSON_F = JsonFactory.builder() + .enable(JsonReadFeature.ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS) + .build(); + + protected JsonFactory jsonFactory() { + return JSON_F; + } + + public void testLeadingDotInNegativeDecimalAllowedAsync() throws Exception { + _testLeadingDotInNegativeDecimalAllowed(jsonFactory(), MODE_DATA_INPUT); + } + + public void testLeadingDotInNegativeDecimalAllowedBytes() throws Exception { + _testLeadingDotInNegativeDecimalAllowed(jsonFactory(), MODE_INPUT_STREAM); + _testLeadingDotInNegativeDecimalAllowed(jsonFactory(), MODE_INPUT_STREAM_THROTTLED); + } + + public void testLeadingDotInNegativeDecimalAllowedReader() throws Exception { + _testLeadingDotInNegativeDecimalAllowed(jsonFactory(), MODE_READER); + } + + private void _testLeadingDotInNegativeDecimalAllowed(JsonFactory f, int mode) throws Exception + { + JsonParser p = createParser(f, mode, " -.125 "); + try { + assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken()); + assertEquals(-0.125, p.getValueAsDouble()); + assertEquals("-0.125", p.getDecimalValue().toString()); + assertEquals("-.125", p.getText()); + } finally { + p.close(); + } + } +}