-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
ion/src/test/java/tools/jackson/dataformat/ion/failing/IonNumberOverflow428Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package tools.jackson.dataformat.ion.failing; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.junit.Test; | ||
|
||
import tools.jackson.core.exc.InputCoercionException; | ||
import tools.jackson.dataformat.ion.*; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.fail; | ||
|
||
import java.math.BigInteger; | ||
|
||
// for [dataformats-ion#428] | ||
public class IonNumberOverflow428Test | ||
{ | ||
private final IonObjectMapper MAPPER = IonObjectMapper | ||
.builderForBinaryWriters() | ||
.build(); | ||
|
||
// Test to ensure we handle value overflow (long->int) correctly | ||
@Test | ||
public void testIntCoercionOverflow() throws Exception { | ||
_testIntCoercionFail(Long.valueOf(3L * Integer.MAX_VALUE / 2L)); | ||
} | ||
|
||
@Test | ||
public void testIntCoercionUnderflow() throws Exception { | ||
_testIntCoercionFail(Long.valueOf(3L * Integer.MIN_VALUE / 2L)); | ||
} | ||
|
||
private void _testIntCoercionFail(Long input) throws Exception | ||
{ | ||
final byte[] doc = MAPPER.writeValueAsBytes(input); | ||
|
||
// First, verify correct value decoding | ||
assertEquals(input, MAPPER.readValue(doc, Long.class)); | ||
|
||
// And then over/underflow | ||
try { | ||
Integer result = MAPPER.readValue(doc, Integer.class); | ||
fail("Should not pass; got: "+result+" (from "+input+")"); | ||
} catch (InputCoercionException e) { | ||
assertThat(e.getMessage(), Matchers.containsString("out of range of int")); | ||
} | ||
} | ||
|
||
// Test to ensure we handle value overflow (BigInteger->long) correctly | ||
@Test | ||
public void testLongCoercionOverflow() throws Exception { | ||
_testLongCoercionFail(BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.TEN)); | ||
} | ||
|
||
@Test | ||
public void testLongCoercionUnderflow() throws Exception { | ||
_testLongCoercionFail(BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.TEN)); | ||
} | ||
|
||
private void _testLongCoercionFail(BigInteger input) throws Exception | ||
{ | ||
final byte[] doc = MAPPER.writeValueAsBytes(input); | ||
|
||
// First, verify correct value decoding | ||
assertEquals(input, MAPPER.readValue(doc, BigInteger.class)); | ||
|
||
// And then over/underflow | ||
try { | ||
Long result = MAPPER.readValue(doc, Long.class); | ||
fail("Should not pass; got: "+result+" (from "+input+")"); | ||
} catch (InputCoercionException e) { | ||
assertThat(e.getMessage(), Matchers.containsString("out of range of long")); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters