|
| 1 | +package com.fasterxml.jackson.dataformat.ion.failing; |
| 2 | + |
| 3 | +import org.hamcrest.Matchers; |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.core.exc.InputCoercionException; |
| 7 | +import com.fasterxml.jackson.dataformat.ion.*; |
| 8 | + |
| 9 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 10 | +import static org.junit.Assert.assertEquals; |
| 11 | +import static org.junit.Assert.fail; |
| 12 | + |
| 13 | +import java.math.BigInteger; |
| 14 | + |
| 15 | +// for [dataformats-ion#428] |
| 16 | +public class IonNumberOverflow428Test |
| 17 | +{ |
| 18 | + private final IonObjectMapper MAPPER = IonObjectMapper |
| 19 | + .builderForBinaryWriters() |
| 20 | + .build(); |
| 21 | + |
| 22 | + // Test to ensure we handle value overflow (long->int) correctly |
| 23 | + @Test |
| 24 | + public void testIntCoercionOverflow() throws Exception { |
| 25 | + _testIntCoercionFail(Long.valueOf(3L * Integer.MAX_VALUE / 2L)); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void testIntCoercionUnderflow() throws Exception { |
| 30 | + _testIntCoercionFail(Long.valueOf(3L * Integer.MIN_VALUE / 2L)); |
| 31 | + } |
| 32 | + |
| 33 | + private void _testIntCoercionFail(Long input) throws Exception |
| 34 | + { |
| 35 | + final byte[] doc = MAPPER.writeValueAsBytes(input); |
| 36 | + |
| 37 | + // First, verify correct value decoding |
| 38 | + assertEquals(input, MAPPER.readValue(doc, Long.class)); |
| 39 | + |
| 40 | + // And then over/underflow |
| 41 | + try { |
| 42 | + Integer result = MAPPER.readValue(doc, Integer.class); |
| 43 | + fail("Should not pass; got: "+result+" (from "+input+")"); |
| 44 | + } catch (InputCoercionException e) { |
| 45 | + assertThat(e.getMessage(), Matchers.containsString("out of range of int")); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // Test to ensure we handle value overflow (BigInteger->long) correctly |
| 50 | + @Test |
| 51 | + public void testLongCoercionOverflow() throws Exception { |
| 52 | + _testLongCoercionFail(BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.TEN)); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testLongCoercionUnderflow() throws Exception { |
| 57 | + _testLongCoercionFail(BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.TEN)); |
| 58 | + } |
| 59 | + |
| 60 | + private void _testLongCoercionFail(BigInteger input) throws Exception |
| 61 | + { |
| 62 | + final byte[] doc = MAPPER.writeValueAsBytes(input); |
| 63 | + |
| 64 | + // First, verify correct value decoding |
| 65 | + assertEquals(input, MAPPER.readValue(doc, BigInteger.class)); |
| 66 | + |
| 67 | + // And then over/underflow |
| 68 | + try { |
| 69 | + Long result = MAPPER.readValue(doc, Long.class); |
| 70 | + fail("Should not pass; got: "+result+" (from "+input+")"); |
| 71 | + } catch (InputCoercionException e) { |
| 72 | + assertThat(e.getMessage(), Matchers.containsString("out of range of long")); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments