diff options
3 files changed, 129 insertions, 120 deletions
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/TestGenerics.java b/src/test/java/com/fasterxml/jackson/databind/deser/TestGenerics.java index 967a6e7ef..62571a2cb 100644 --- a/src/test/java/com/fasterxml/jackson/databind/deser/TestGenerics.java +++ b/src/test/java/com/fasterxml/jackson/databind/deser/TestGenerics.java @@ -94,11 +94,8 @@ public class TestGenerics assertEquals(13, bean.x); } - /** - * Unit test for verifying that we can use different - * type bindings for individual generic types; - * problem with [JACKSON-190] - */ + // Test for verifying that we can use different + // type bindings for individual generic types. public void testMultipleWrappers() throws Exception { ObjectMapper mapper = new ObjectMapper(); @@ -119,7 +116,7 @@ public class TestGenerics assertEquals(new Wrapper<Long>(7L), result3); } - //[Issue#381] + //[databind#381] public void testMultipleWrappersSingleValueArray() throws Exception { ObjectMapper mapper = new ObjectMapper(); diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKScalarsTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKScalarsTest.java index 115163cd1..48070ed40 100644 --- a/src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKScalarsTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKScalarsTest.java @@ -48,7 +48,7 @@ public class JDKScalarsTest long _v; void setV(long v) { _v = v; } } - + final static class DoubleBean { double _v; void setV(double v) { _v = v; } @@ -280,40 +280,6 @@ public class JDKScalarsTest assertNotNull(array); assertEquals(1, array.length); assertEquals(0, array[0]); - - // [databind#381] - final ObjectMapper mapper = new ObjectMapper(); - mapper.disable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS); - try { - mapper.readValue("{\"v\":[3]}", IntBean.class); - fail("Did not throw exception when reading a value from a single value array with the UNWRAP_SINGLE_VALUE_ARRAYS feature disabled"); - } catch (MismatchedInputException exp) { - //Correctly threw exception - } - - mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS); - - result = mapper.readValue("{\"v\":[3]}", IntBean.class); - assertEquals(3, result._v); - - result = mapper.readValue("[{\"v\":[3]}]", IntBean.class); - assertEquals(3, result._v); - - try { - mapper.readValue("[{\"v\":[3,3]}]", IntBean.class); - fail("Did not throw exception while reading a value from a multi value array with UNWRAP_SINGLE_VALUE_ARRAY feature enabled"); - } catch (MismatchedInputException exp) { - //threw exception as required - } - - result = mapper.readValue("{\"v\":[null]}", IntBean.class); - assertNotNull(result); - assertEquals(0, result._v); - - array = mapper.readValue("[ [ null ] ]", int[].class); - assertNotNull(array); - assertEquals(1, array.length); - assertEquals(0, array[0]); } public void testLongWrapper() throws Exception @@ -343,40 +309,6 @@ public class JDKScalarsTest assertNotNull(array); assertEquals(1, array.length); assertEquals(0, array[0]); - - // [databind#381] - final ObjectMapper mapper = new ObjectMapper(); - mapper.disable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS); - try { - mapper.readValue("{\"v\":[3]}", LongBean.class); - fail("Did not throw exception when reading a value from a single value array with the UNWRAP_SINGLE_VALUE_ARRAYS feature disabled"); - } catch (MismatchedInputException exp) { - //Correctly threw exception - } - - mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS); - - result = mapper.readValue("{\"v\":[3]}", LongBean.class); - assertEquals(3, result._v); - - result = mapper.readValue("[{\"v\":[3]}]", LongBean.class); - assertEquals(3, result._v); - - try { - mapper.readValue("[{\"v\":[3,3]}]", LongBean.class); - fail("Did not throw exception while reading a value from a multi value array with UNWRAP_SINGLE_VALUE_ARRAY feature enabled"); - } catch (MismatchedInputException exp) { - //threw exception as required - } - - result = mapper.readValue("{\"v\":[null]}", LongBean.class); - assertNotNull(result); - assertEquals(0, result._v); - - array = mapper.readValue("[ [ null ] ]", long[].class); - assertNotNull(array); - assertEquals(1, array.length); - assertEquals(0, array[0]); } /** @@ -464,44 +396,6 @@ public class JDKScalarsTest } } - public void testDoubleAsArray() throws Exception - { - final ObjectMapper mapper = new ObjectMapper(); - mapper.disable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS); - final double value = 0.016; - try { - mapper.readValue("{\"v\":[" + value + "]}", DoubleBean.class); - fail("Did not throw exception when reading a value from a single value array with the UNWRAP_SINGLE_VALUE_ARRAYS feature disabled"); - } catch (JsonMappingException exp) { - //Correctly threw exception - } - - mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS); - - DoubleBean result = mapper.readValue("{\"v\":[" + value + "]}", - DoubleBean.class); - assertEquals(value, result._v); - - result = mapper.readValue("[{\"v\":[" + value + "]}]", DoubleBean.class); - assertEquals(value, result._v); - - try { - mapper.readValue("[{\"v\":[" + value + "," + value + "]}]", DoubleBean.class); - fail("Did not throw exception while reading a value from a multi value array with UNWRAP_SINGLE_VALUE_ARRAY feature enabled"); - } catch (MismatchedInputException exp) { - //threw exception as required - } - - result = mapper.readValue("{\"v\":[null]}", DoubleBean.class); - assertNotNull(result); - assertEquals(0d, result._v); - - double[] array = mapper.readValue("[ [ null ] ]", double[].class); - assertNotNull(array); - assertEquals(1, array.length); - assertEquals(0d, array[0]); - } - public void testDoublePrimitiveNonNumeric() throws Exception { // first, simple case: diff --git a/src/test/java/com/fasterxml/jackson/databind/struct/UnwrapSingleArrayScalarsTest.java b/src/test/java/com/fasterxml/jackson/databind/struct/UnwrapSingleArrayScalarsTest.java index c2220f9b3..a13988e90 100644 --- a/src/test/java/com/fasterxml/jackson/databind/struct/UnwrapSingleArrayScalarsTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/struct/UnwrapSingleArrayScalarsTest.java @@ -16,8 +16,19 @@ import com.fasterxml.jackson.databind.exc.MismatchedInputException; public class UnwrapSingleArrayScalarsTest extends BaseMapTest { static class BooleanBean { - boolean _v; - void setV(boolean v) { _v = v; } + public boolean v; + } + + static class IntBean { + public int v; + } + + static class LongBean { + public long v; + } + + static class DoubleBean { + public double v; } private final ObjectMapper MAPPER = new ObjectMapper(); @@ -37,17 +48,17 @@ public class UnwrapSingleArrayScalarsTest extends BaseMapTest // [databind#381] final ObjectReader r = UNWRAPPING_READER.forType(BooleanBean.class); BooleanBean result = r.readValue(new StringReader("{\"v\":[true]}")); - assertTrue(result._v); + assertTrue(result.v); _verifyMultiValueArrayFail("[{\"v\":[true,true]}]", BooleanBean.class); result = r.readValue("{\"v\":[null]}"); assertNotNull(result); - assertFalse(result._v); + assertFalse(result.v); result = r.readValue("[{\"v\":[null]}]"); assertNotNull(result); - assertFalse(result._v); + assertFalse(result.v); boolean[] array = UNWRAPPING_READER.forType(boolean[].class) .readValue(new StringReader("[ [ null ] ]")); @@ -61,8 +72,115 @@ public class UnwrapSingleArrayScalarsTest extends BaseMapTest /* Single-element as array tests, numbers /********************************************************** */ - - // [databind#381] + + public void testIntPrimitiveArrayUnwrap() throws Exception + { + try { + NO_UNWRAPPING_READER.forType(IntBean.class) + .readValue("{\"v\":[3]}"); + fail("Did not throw exception when reading a value from a single value array with the UNWRAP_SINGLE_VALUE_ARRAYS feature disabled"); + } catch (MismatchedInputException e) { + verifyException(e, "Cannot deserialize instance of `int`"); + } + + ObjectReader r = UNWRAPPING_READER.forType(IntBean.class); + IntBean result = r.readValue("{\"v\":[3]}"); + assertEquals(3, result.v); + + result = r.readValue("[{\"v\":[3]}]"); + assertEquals(3, result.v); + + try { + r.readValue("[{\"v\":[3,3]}]"); + fail("Did not throw exception while reading a value from a multi value array with UNWRAP_SINGLE_VALUE_ARRAY feature enabled"); + } catch (MismatchedInputException e) { + verifyException(e, "more than one value"); + } + + result = r.readValue("{\"v\":[null]}"); + assertNotNull(result); + assertEquals(0, result.v); + + int[] array = UNWRAPPING_READER.forType(int[].class).readValue("[ [ null ] ]"); + assertNotNull(array); + assertEquals(1, array.length); + assertEquals(0, array[0]); + + } + + public void testLongPrimitiveArrayUnwrap() throws Exception + { + final ObjectReader unwrapR = UNWRAPPING_READER.forType(LongBean.class); + final ObjectReader noUnwrapR = NO_UNWRAPPING_READER.forType(LongBean.class); + + try { + noUnwrapR.readValue("{\"v\":[3]}"); + fail("Did not throw exception when reading a value from a single value array"); + } catch (MismatchedInputException e) { + verifyException(e, "Cannot deserialize instance of `long`"); + } + + LongBean result = unwrapR.readValue("{\"v\":[3]}"); + assertEquals(3, result.v); + + result = unwrapR.readValue("[{\"v\":[3]}]"); + assertEquals(3, result.v); + + try { + unwrapR.readValue("[{\"v\":[3,3]}]"); + fail("Did not throw exception while reading a value from a multi value array"); + } catch (MismatchedInputException e) { + verifyException(e, "more than one value"); + } + + result = unwrapR.readValue("{\"v\":[null]}"); + assertNotNull(result); + assertEquals(0, result.v); + + long[] array = unwrapR.forType(long[].class) + .readValue("[ [ null ] ]"); + assertNotNull(array); + assertEquals(1, array.length); + assertEquals(0, array[0]); + } + + public void testDoubleAsArray() throws Exception + { + final ObjectReader unwrapR = UNWRAPPING_READER.forType(DoubleBean.class); + final ObjectReader noUnwrapR = NO_UNWRAPPING_READER.forType(DoubleBean.class); + + final double value = 0.016; + try { + noUnwrapR.readValue("{\"v\":[" + value + "]}"); + fail("Did not throw exception when reading a value from a single value array"); + } catch (MismatchedInputException e) { + verifyException(e, "Cannot deserialize instance of `double`"); + } + + DoubleBean result = unwrapR.readValue("{\"v\":[" + value + "]}"); + assertEquals(value, result.v); + + result = unwrapR.readValue("[{\"v\":[" + value + "]}]"); + assertEquals(value, result.v); + + try { + unwrapR.readValue("[{\"v\":[" + value + "," + value + "]}]"); + fail("Did not throw exception while reading a value from a multi value array"); + } catch (MismatchedInputException e) { + verifyException(e, "more than one value"); + } + + result = unwrapR.readValue("{\"v\":[null]}"); + assertNotNull(result); + assertEquals(0d, result.v); + + double[] array = unwrapR.forType(double[].class) + .readValue("[ [ null ] ]"); + assertNotNull(array); + assertEquals(1, array.length); + assertEquals(0d, array[0]); + } + public void testSingleElementScalarArrays() throws Exception { final int intTest = 932832; final double doubleTest = 32.3234; |