From 9faa03befa00226ddfc05a5a9e990e2c9970385d Mon Sep 17 00:00:00 2001 From: "Kim, Joo Hyuk" Date: Sun, 7 Jan 2024 11:57:13 +0900 Subject: [PATCH 1/2] Migrate all tests --- .../convert/CoerceBoolToStringTest.java | 14 +++- .../convert/CoerceContainersTest.java | 29 ++++++- .../convert/CoerceEmptyArrayTest.java | 17 +++- .../convert/CoerceEmptyToInt3234Test.java | 15 +++- .../databind/convert/CoerceEnumTest.java | 20 ++++- .../convert/CoerceFloatToIntTest.java | 17 +++- .../convert/CoerceFloatToStringTest.java | 15 +++- .../convert/CoerceIntToFloatTest.java | 18 ++++- .../convert/CoerceIntToStringTest.java | 15 +++- .../convert/CoerceJDKScalarsTest.java | 14 +++- .../convert/CoerceMiscScalarsTest.java | 18 ++++- .../convert/CoerceNaNStringToNumberTest.java | 15 +++- .../databind/convert/CoercePojosTest.java | 16 +++- .../convert/CoerceStringToIntsTest.java | 16 +++- .../databind/convert/CoerceToBooleanTest.java | 21 ++++- .../ConvertingAbstractSerializer795Test.java | 12 ++- .../convert/DisableCoercions3690Test.java | 14 +++- .../convert/EmptyStringAsSingleValueTest.java | 52 ++++++++---- .../convert/ScalarConversionTest.java | 12 ++- .../convert/TestArrayConversions.java | 21 +++-- .../databind/convert/TestBeanConversions.java | 19 ++++- .../convert/TestConvertingDeserializer.java | 23 +++++- .../convert/TestConvertingSerializer.java | 18 ++++- .../databind/convert/TestMapConversions.java | 10 ++- .../convert/TestPolymorphicUpdateValue.java | 12 ++- .../convert/TestStringConversions.java | 11 ++- .../convert/TestUpdateViaObjectReader.java | 21 ++++- .../databind/convert/UpdateValueTest.java | 17 +++- .../databind/testutil/DatabindTestUtil.java | 79 ++++++++++++++++++- 29 files changed, 502 insertions(+), 79 deletions(-) diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceBoolToStringTest.java b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceBoolToStringTest.java index dd7a73f3da..b0265cec9a 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceBoolToStringTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceBoolToStringTest.java @@ -1,7 +1,8 @@ package com.fasterxml.jackson.databind.convert; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.BaseMapTest; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.databind.cfg.CoercionAction; @@ -9,7 +10,11 @@ import com.fasterxml.jackson.databind.exc.MismatchedInputException; import com.fasterxml.jackson.databind.type.LogicalType; -public class CoerceBoolToStringTest extends BaseMapTest +import static org.junit.jupiter.api.Assertions.*; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*; + +public class CoerceBoolToStringTest { private final ObjectMapper DEFAULT_MAPPER = newJsonMapper(); @@ -33,16 +38,19 @@ public class CoerceBoolToStringTest extends BaseMapTest cfg.setCoercion(CoercionInputShape.Boolean, CoercionAction.AsEmpty)) .build(); + @Test public void testDefaultBooleanToStringCoercion() throws JsonProcessingException { assertSuccessfulBooleanToStringCoercionWith(DEFAULT_MAPPER); } + @Test public void testCoerceConfigToConvert() throws JsonProcessingException { assertSuccessfulBooleanToStringCoercionWith(MAPPER_TRY_CONVERT); } + @Test public void testCoerceConfigToNull() throws JsonProcessingException { assertNull(MAPPER_TO_NULL.readValue("true", String.class)); @@ -53,6 +61,7 @@ public void testCoerceConfigToNull() throws JsonProcessingException assertNull(arr[0]); } + @Test public void testCoerceConfigToEmpty() throws JsonProcessingException { assertEquals("", MAPPER_TO_EMPTY.readValue("true", String.class)); @@ -63,6 +72,7 @@ public void testCoerceConfigToEmpty() throws JsonProcessingException assertEquals("", arr[0]); } + @Test public void testCoerceConfigToFail() throws JsonProcessingException { _verifyCoerceFail(MAPPER_TO_FAIL, String.class, "true"); diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceContainersTest.java b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceContainersTest.java index 4ad6d91516..3b4b2288f5 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceContainersTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceContainersTest.java @@ -1,14 +1,23 @@ package com.fasterxml.jackson.databind.convert; -import java.util.*; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.*; +import org.junit.jupiter.api.Test; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.DatabindException; +import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.cfg.CoercionAction; import com.fasterxml.jackson.databind.cfg.CoercionInputShape; -public class CoerceContainersTest extends BaseMapTest +import static org.junit.jupiter.api.Assertions.*; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*; + +public class CoerceContainersTest { private final String JSON_EMPTY = q(""); @@ -25,6 +34,7 @@ public class CoerceContainersTest extends BaseMapTest /******************************************************** */ + @Test public void testScalarCollections() throws Exception { final JavaType listType = VANILLA_MAPPER.getTypeFactory() @@ -48,6 +58,7 @@ public void testScalarCollections() throws Exception assertEquals(0, result.size()); } + @Test public void testStringCollections() throws Exception { final JavaType listType = VANILLA_MAPPER.getTypeFactory() @@ -64,6 +75,7 @@ public void testStringCollections() throws Exception /******************************************************** */ + @Test public void testScalarMap() throws Exception { final JavaType mapType = VANILLA_MAPPER.getTypeFactory() @@ -74,6 +86,7 @@ public void testScalarMap() throws Exception assertEquals(0, result.size()); } + @Test public void testEnumMap() throws Exception { final JavaType mapType = VANILLA_MAPPER.getTypeFactory() @@ -90,6 +103,7 @@ public void testEnumMap() throws Exception /******************************************************** */ + @Test public void testObjectArray() throws Exception { final JavaType arrayType = VANILLA_MAPPER.getTypeFactory() @@ -100,6 +114,7 @@ public void testObjectArray() throws Exception assertEquals(0, result.length); } + @Test public void testStringArray() throws Exception { final JavaType arrayType = VANILLA_MAPPER.getTypeFactory() @@ -110,6 +125,7 @@ public void testStringArray() throws Exception assertEquals(0, result.length); } + @Test public void testBooleanArray() throws Exception { _verifyNoCoercion(boolean[].class); @@ -118,6 +134,7 @@ public void testBooleanArray() throws Exception assertEquals(0, result.length); } + @Test public void testIntArray() throws Exception { _verifyNoCoercion(int[].class); @@ -126,6 +143,7 @@ public void testIntArray() throws Exception assertEquals(0, result.length); } + @Test public void testLongArray() throws Exception { _verifyNoCoercion(long[].class); @@ -134,6 +152,7 @@ public void testLongArray() throws Exception assertEquals(0, result.length); } + @Test public void testFloatArray() throws Exception { _verifyNoCoercion(float[].class); @@ -142,6 +161,7 @@ public void testFloatArray() throws Exception assertEquals(0, result.length); } + @Test public void testDoubleArray() throws Exception { _verifyNoCoercion(double[].class); @@ -150,6 +170,7 @@ public void testDoubleArray() throws Exception assertEquals(0, result.length); } + @Test public void testPOJOArray() throws Exception { _verifyNoCoercion(StringWrapper[].class); diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceEmptyArrayTest.java b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceEmptyArrayTest.java index 6eef027c83..72b3a971d5 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceEmptyArrayTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceEmptyArrayTest.java @@ -6,20 +6,27 @@ import java.net.URL; import java.util.*; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.cfg.CoercionAction; import com.fasterxml.jackson.databind.cfg.CoercionInputShape; import com.fasterxml.jackson.databind.exc.MismatchedInputException; +import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; import com.fasterxml.jackson.databind.type.LogicalType; +import static org.junit.jupiter.api.Assertions.*; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*; + /** * Tests to verify implementation of [databind#540]; also for * follow up work of: * * - [databind#994] */ -public class CoerceEmptyArrayTest extends BaseMapTest +public class CoerceEmptyArrayTest { private final ObjectMapper DEFAULT_MAPPER = sharedMapper(); private final ObjectReader DEFAULT_READER = DEFAULT_MAPPER.reader(); @@ -64,6 +71,7 @@ public boolean equals(Object o) { /********************************************************** */ + @Test public void testSettings() { assertFalse(DEFAULT_MAPPER.isEnabled(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT)); assertFalse(DEFAULT_READER.isEnabled(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT)); @@ -77,6 +85,7 @@ public void testSettings() { */ // [databind#540] + @Test public void testPOJOFromEmptyArray() throws Exception { final Class targetType = Bean.class; @@ -113,6 +122,7 @@ public void testPOJOFromEmptyArray() throws Exception /********************************************************** */ + @Test public void testMapFromEmptyArray() throws Exception { final Class targetType = Map.class; @@ -129,10 +139,11 @@ public void testMapFromEmptyArray() throws Exception // assume overrides work ok since POJOs test it } + @Test public void testEnumMapFromEmptyArray() throws Exception { final JavaType targetType = DEFAULT_READER.getTypeFactory() - .constructType(new TypeReference>() { }); + .constructType(new TypeReference>() { }); assertNull(MAPPER_TO_NULL.readerFor(targetType).readValue(EMPTY_ARRAY)); @@ -147,6 +158,7 @@ public void testEnumMapFromEmptyArray() throws Exception /********************************************************** */ + @Test public void testNumbersFromEmptyArray() throws Exception { for (Class targetType : new Class[] { @@ -180,6 +192,7 @@ public void testNumbersFromEmptyArray() throws Exception _verifyToEmptyCoercion(MAPPER_TO_EMPTY, BigDecimal.class, new BigDecimal(BigInteger.ZERO)); } + @Test public void testOtherScalarsFromEmptyArray() throws Exception { for (Class targetType : new Class[] { diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceEmptyToInt3234Test.java b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceEmptyToInt3234Test.java index 5c71b1ddfb..b3c039c00c 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceEmptyToInt3234Test.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceEmptyToInt3234Test.java @@ -1,9 +1,16 @@ package com.fasterxml.jackson.databind.convert; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.*; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.a2q; +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.newJsonMapper; + // [databind#3234] -public class CoerceEmptyToInt3234Test extends BaseMapTest +public class CoerceEmptyToInt3234Test { static class BasicIntWrapper { public int value = 13; @@ -24,12 +31,14 @@ static class BasicDoubleWrapper { // // // Ints + @Test public void testSimpleIntFromEmpty() throws Exception { BasicIntWrapper w = READER_INT_BASIC.readValue(a2q("{'value':''}")); assertEquals(0, w.value); } + @Test public void testSimpleIntFromBlank() throws Exception { BasicIntWrapper w = READER_INT_BASIC.readValue(a2q("{'value':' '}")); @@ -38,12 +47,14 @@ public void testSimpleIntFromBlank() throws Exception // // // Long + @Test public void testSimpleLongFromEmpty() throws Exception { BasicLongWrapper w = READER_LONG_BASIC.readValue(a2q("{'value':''}")); assertEquals(0L, w.value); } + @Test public void testSimpleLongFromBlank() throws Exception { BasicLongWrapper w = READER_LONG_BASIC.readValue(a2q("{'value':' '}")); @@ -52,12 +63,14 @@ public void testSimpleLongFromBlank() throws Exception // // // Double + @Test public void testSimpleDoublegFromEmpty() throws Exception { BasicDoubleWrapper w = READER_DOUBLE_BASIC.readValue(a2q("{'value':''}")); assertEquals((double) 0, w.value); } + @Test public void testSimpleDoubleFromBlank() throws Exception { BasicDoubleWrapper w = READER_DOUBLE_BASIC.readValue(a2q("{'value':' '}")); diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceEnumTest.java b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceEnumTest.java index 409d91cac3..92aadbdf54 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceEnumTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceEnumTest.java @@ -1,5 +1,7 @@ package com.fasterxml.jackson.databind.convert; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.JsonEnumDefaultValue; import com.fasterxml.jackson.databind.*; @@ -8,7 +10,11 @@ import com.fasterxml.jackson.databind.exc.MismatchedInputException; import com.fasterxml.jackson.databind.type.LogicalType; -public class CoerceEnumTest extends BaseMapTest +import static org.junit.jupiter.api.Assertions.*; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*; + +public class CoerceEnumTest { protected enum EnumCoerce { A, B, C, @@ -33,20 +39,24 @@ protected enum EnumCoerce { /******************************************************** */ + @Test public void testLegacyDefaults() throws Exception { // first, verify default settings which do not accept empty String: assertFalse(MAPPER.isEnabled(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS)); } + @Test public void testEnumFromEmptyGlobalConfig() throws Exception { _testEnumFromEmptyGlobalConfig(CoercionInputShape.EmptyString, JSON_EMPTY, null); } + @Test public void testEnumFromEmptyLogicalTypeConfig() throws Exception { _testEnumFromEmptyLogicalTypeConfig(CoercionInputShape.EmptyString, JSON_EMPTY, null); } + @Test public void testEnumFromEmptyPhysicalTypeConfig() throws Exception { _testEnumFromEmptyPhysicalTypeConfig(CoercionInputShape.EmptyString, JSON_EMPTY, null); } @@ -57,14 +67,17 @@ public void testEnumFromEmptyPhysicalTypeConfig() throws Exception { /******************************************************** */ + @Test public void testEnumFromBlankGlobalConfig() throws Exception { _testEnumFromEmptyGlobalConfig(CoercionInputShape.EmptyString, JSON_BLANK, Boolean.TRUE); } + @Test public void testEnumFromBlankLogicalTypeConfig() throws Exception { _testEnumFromEmptyLogicalTypeConfig(CoercionInputShape.EmptyString, JSON_BLANK, Boolean.TRUE); } + @Test public void testEnumFromBlankPhysicalTypeConfig() throws Exception { _testEnumFromEmptyPhysicalTypeConfig(CoercionInputShape.EmptyString, JSON_BLANK, Boolean.TRUE); } @@ -75,6 +88,7 @@ public void testEnumFromBlankPhysicalTypeConfig() throws Exception { /******************************************************** */ + @Test public void testEnumFromIntFailLegacy() throws Exception { final ObjectReader r = MAPPER.readerFor(EnumCoerce.class); @@ -92,6 +106,7 @@ public void testEnumFromIntFailLegacy() throws Exception } } + @Test public void testEnumFromIntAsNull() throws Exception { final String json = "1"; @@ -107,6 +122,7 @@ public void testEnumFromIntAsNull() throws Exception assertNull(_readEnumPass(mapper, json)); } + @Test public void testEnumFromIntAsEmpty() throws Exception { final String json = "1"; @@ -122,6 +138,7 @@ public void testEnumFromIntAsEmpty() throws Exception assertEquals(ENUM_DEFAULT, _readEnumPass(mapper, json)); } + @Test public void testEnumFromIntCoerce() throws Exception { final String json = "1"; @@ -138,6 +155,7 @@ public void testEnumFromIntCoerce() throws Exception assertEquals(exp, _readEnumPass(mapper, json)); } + @Test public void testEnumFromIntFailCoercionConfig() throws Exception { final String json = "1"; diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceFloatToIntTest.java b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceFloatToIntTest.java index 30ab689ee5..10c3b96dd1 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceFloatToIntTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceFloatToIntTest.java @@ -4,6 +4,8 @@ import java.util.*; import java.util.concurrent.atomic.AtomicLong; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.cfg.CoercionAction; @@ -12,7 +14,11 @@ import com.fasterxml.jackson.databind.exc.MismatchedInputException; import com.fasterxml.jackson.databind.type.LogicalType; -public class CoerceFloatToIntTest extends BaseMapTest +import static org.junit.jupiter.api.Assertions.*; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*; + +public class CoerceFloatToIntTest { private final ObjectMapper DEFAULT_MAPPER = newJsonMapper(); private final ObjectReader READER_LEGACY_FAIL = DEFAULT_MAPPER.reader() @@ -44,6 +50,7 @@ public class CoerceFloatToIntTest extends BaseMapTest /******************************************************** */ + @Test public void testLegacyDoubleToIntCoercion() throws Exception { // by default, should be ok @@ -72,6 +79,7 @@ public void testLegacyDoubleToIntCoercion() throws Exception assertEquals(95L, biggie.longValue()); } + @Test public void testLegacyFailDoubleToInt() throws Exception { _verifyCoerceFail(READER_LEGACY_FAIL, Integer.class, "1.5", "java.lang.Integer"); @@ -80,6 +88,7 @@ public void testLegacyFailDoubleToInt() throws Exception _verifyCoerceFail(READER_LEGACY_FAIL, int[].class, "[ 2.5 ]", "to `int` value"); } + @Test public void testLegacyFailDoubleToLong() throws Exception { _verifyCoerceFail(READER_LEGACY_FAIL, Long.class, "0.5"); @@ -88,6 +97,7 @@ public void testLegacyFailDoubleToLong() throws Exception _verifyCoerceFail(READER_LEGACY_FAIL, long[].class, "[ -1.35 ]", "to `long` value"); } + @Test public void testLegacyFailDoubleToOther() throws Exception { _verifyCoerceFail(READER_LEGACY_FAIL, Short.class, "0.5"); @@ -110,6 +120,7 @@ public void testLegacyFailDoubleToOther() throws Exception */ // [databind#2804] + @Test public void testLegacyFail2804() throws Exception { _testLegacyFail2804("5.5", Integer.class); @@ -148,6 +159,7 @@ private void _testLegacyFail2804(String doc, JavaType targetType, /******************************************************** */ + @Test public void testCoerceConfigFloatToNull() throws Exception { assertNull(MAPPER_TO_NULL.readValue("1.5", Integer.class)); @@ -201,6 +213,7 @@ public void testCoerceConfigFloatToNull() throws Exception /******************************************************** */ + @Test public void testCoerceConfigFloatToEmpty() throws Exception { assertEquals(Integer.valueOf(0), MAPPER_TO_EMPTY.readValue("1.2", Integer.class)); @@ -238,6 +251,7 @@ public void testCoerceConfigFloatToEmpty() throws Exception /******************************************************** */ + @Test public void testCoerceConfigFloatSuccess() throws Exception { assertEquals(Integer.valueOf(1), MAPPER_TRY_CONVERT.readValue("1.2", Integer.class)); @@ -275,6 +289,7 @@ public void testCoerceConfigFloatSuccess() throws Exception /******************************************************** */ + @Test public void testCoerceConfigFailFromFloat() throws Exception { _verifyCoerceFail(MAPPER_TO_FAIL, Integer.class, "1.5"); diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceFloatToStringTest.java b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceFloatToStringTest.java index 8f492eea5a..6353c12d7c 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceFloatToStringTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceFloatToStringTest.java @@ -1,7 +1,8 @@ package com.fasterxml.jackson.databind.convert; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.BaseMapTest; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.databind.cfg.CoercionAction; @@ -9,7 +10,11 @@ import com.fasterxml.jackson.databind.exc.MismatchedInputException; import com.fasterxml.jackson.databind.type.LogicalType; -public class CoerceFloatToStringTest extends BaseMapTest +import static org.junit.jupiter.api.Assertions.*; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*; + +public class CoerceFloatToStringTest { private final ObjectMapper DEFAULT_MAPPER = newJsonMapper(); @@ -33,16 +38,19 @@ public class CoerceFloatToStringTest extends BaseMapTest cfg.setCoercion(CoercionInputShape.Float, CoercionAction.AsEmpty)) .build(); + @Test public void testDefaultFloatToStringCoercion() throws JsonProcessingException { assertSuccessfulFloatToStringCoercionWith(DEFAULT_MAPPER); } + @Test public void testCoerceConfigToConvert() throws JsonProcessingException { assertSuccessfulFloatToStringCoercionWith(MAPPER_TRY_CONVERT); } + @Test public void testCoerceConfigToNull() throws JsonProcessingException { assertNull(MAPPER_TO_NULL.readValue("1.2", String.class)); @@ -53,6 +61,7 @@ public void testCoerceConfigToNull() throws JsonProcessingException assertNull(arr[0]); } + @Test public void testCoerceConfigToEmpty() throws JsonProcessingException { assertEquals("", MAPPER_TO_EMPTY.readValue("3.5", String.class)); @@ -63,7 +72,7 @@ public void testCoerceConfigToEmpty() throws JsonProcessingException assertEquals("", arr[0]); } - + @Test public void testCoerceConfigToFail() throws JsonProcessingException { _verifyCoerceFail(MAPPER_TO_FAIL, String.class, "3.5"); diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceIntToFloatTest.java b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceIntToFloatTest.java index c1f1c23085..4349fc1ad8 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceIntToFloatTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceIntToFloatTest.java @@ -1,7 +1,10 @@ package com.fasterxml.jackson.databind.convert; +import java.math.BigDecimal; + +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.BaseMapTest; import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectReader; @@ -9,9 +12,12 @@ import com.fasterxml.jackson.databind.cfg.CoercionInputShape; import com.fasterxml.jackson.databind.exc.MismatchedInputException; import com.fasterxml.jackson.databind.type.LogicalType; -import java.math.BigDecimal; -public class CoerceIntToFloatTest extends BaseMapTest +import static org.junit.jupiter.api.Assertions.*; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*; + +public class CoerceIntToFloatTest { private final ObjectMapper DEFAULT_MAPPER = newJsonMapper(); @@ -39,16 +45,19 @@ public class CoerceIntToFloatTest extends BaseMapTest .disable(MapperFeature.ALLOW_COERCION_OF_SCALARS) .build(); + @Test public void testDefaultIntToFloatCoercion() throws JsonProcessingException { assertSuccessfulIntToFloatConversionsWith(DEFAULT_MAPPER); } + @Test public void testCoerceConfigToConvert() throws JsonProcessingException { assertSuccessfulIntToFloatConversionsWith(MAPPER_TRY_CONVERT); } + @Test public void testCoerceConfigToNull() throws JsonProcessingException { assertNull(MAPPER_TO_NULL.readValue("1", Float.class)); @@ -80,6 +89,7 @@ public void testCoerceConfigToNull() throws JsonProcessingException } } + @Test public void testCoerceConfigToEmpty() throws JsonProcessingException { assertEquals(0.0f, MAPPER_TO_EMPTY.readValue("3", Float.class)); @@ -105,6 +115,7 @@ public void testCoerceConfigToEmpty() throws JsonProcessingException assertEquals(BigDecimal.valueOf(0), MAPPER_TO_EMPTY.readValue("3643", BigDecimal.class)); } + @Test public void testCoerceConfigToFail() throws JsonProcessingException { _verifyCoerceFail(MAPPER_TO_FAIL, Float.class, "3"); @@ -120,6 +131,7 @@ public void testCoerceConfigToFail() throws JsonProcessingException _verifyCoerceFail(MAPPER_TO_FAIL, BigDecimal.class, "73455342"); } + @Test public void testLegacyConfiguration() throws JsonProcessingException { assertSuccessfulIntToFloatConversionsWith(LEGACY_SCALAR_COERCION_FAIL); diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceIntToStringTest.java b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceIntToStringTest.java index 1ee22b4e7f..8bc258995f 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceIntToStringTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceIntToStringTest.java @@ -1,7 +1,9 @@ package com.fasterxml.jackson.databind.convert; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.BaseMapTest; + import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.databind.cfg.CoercionAction; @@ -9,8 +11,12 @@ import com.fasterxml.jackson.databind.exc.MismatchedInputException; import com.fasterxml.jackson.databind.type.LogicalType; +import static org.junit.jupiter.api.Assertions.*; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*; + // [databind#3013] / PR #3608 -public class CoerceIntToStringTest extends BaseMapTest +public class CoerceIntToStringTest { private final ObjectMapper DEFAULT_MAPPER = newJsonMapper(); @@ -34,16 +40,19 @@ public class CoerceIntToStringTest extends BaseMapTest cfg.setCoercion(CoercionInputShape.Integer, CoercionAction.AsEmpty)) .build(); + @Test public void testDefaultIntToStringCoercion() throws JsonProcessingException { assertSuccessfulIntToStringCoercionWith(DEFAULT_MAPPER); } + @Test public void testCoerceConfigToConvert() throws JsonProcessingException { assertSuccessfulIntToStringCoercionWith(MAPPER_TRY_CONVERT); } + @Test public void testCoerceConfigToNull() throws JsonProcessingException { assertNull(MAPPER_TO_NULL.readValue("1", String.class)); @@ -54,6 +63,7 @@ public void testCoerceConfigToNull() throws JsonProcessingException assertNull(arr[0]); } + @Test public void testCoerceConfigToEmpty() throws JsonProcessingException { assertEquals("", MAPPER_TO_EMPTY.readValue("3", String.class)); @@ -64,6 +74,7 @@ public void testCoerceConfigToEmpty() throws JsonProcessingException assertEquals("", arr[0]); } + @Test public void testCoerceConfigToFail() throws JsonProcessingException { _verifyCoerceFail(MAPPER_TO_FAIL, String.class, "3"); diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceJDKScalarsTest.java b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceJDKScalarsTest.java index 15f9344a9a..2e549ebebd 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceJDKScalarsTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceJDKScalarsTest.java @@ -7,6 +7,8 @@ import java.math.BigInteger; import java.util.concurrent.atomic.AtomicBoolean; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.JsonParser; @@ -14,8 +16,12 @@ import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.exc.MismatchedInputException; +import static org.junit.jupiter.api.Assertions.*; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*; + // Tests for "old" coercions (pre-2.12), with `MapperFeature.ALLOW_COERCION_OF_SCALARS` -public class CoerceJDKScalarsTest extends BaseMapTest +public class CoerceJDKScalarsTest { static class BooleanPOJO { public Boolean value; @@ -47,6 +53,7 @@ public BooleanWrapper(@JsonProperty("ctor") Boolean foo) { /********************************************************** */ + @Test public void testNullValueFromEmpty() throws Exception { // wrappers accept `null` fine @@ -86,6 +93,7 @@ private void _verifyNullOkFromEmpty(Class type, Object exp) throws IOExceptio } } + @Test public void testNullFailFromEmpty() throws Exception { _verifyNullFail(Boolean.class); @@ -127,6 +135,7 @@ private void _verifyNullFail(Class type) throws IOException /********************************************************** */ + @Test public void testStringToNumbersCoercionOk() throws Exception { _verifyCoerceSuccess(q("123"), Byte.TYPE, Byte.valueOf((byte) 123)); @@ -150,6 +159,7 @@ public void testStringToNumbersCoercionOk() throws Exception assertTrue(ab.get()); } + @Test public void testStringCoercionFailInteger() throws Exception { _verifyRootStringCoerceFail("123", Byte.TYPE); @@ -162,6 +172,7 @@ public void testStringCoercionFailInteger() throws Exception _verifyRootStringCoerceFail("123", Long.class); } + @Test public void testStringCoercionFailFloat() throws Exception { _verifyRootStringCoerceFail("123.5", Float.TYPE); @@ -173,6 +184,7 @@ public void testStringCoercionFailFloat() throws Exception _verifyRootStringCoerceFail("123.0", BigDecimal.class); } + @Test public void testMiscCoercionFail() throws Exception { // And then we have coercions from more esoteric types too diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceMiscScalarsTest.java b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceMiscScalarsTest.java index 8e66469cf9..adf9b8094f 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceMiscScalarsTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceMiscScalarsTest.java @@ -15,14 +15,19 @@ import java.util.UUID; import java.util.regex.Pattern; -import com.fasterxml.jackson.databind.BaseMapTest; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.cfg.CoercionAction; import com.fasterxml.jackson.databind.cfg.CoercionInputShape; import com.fasterxml.jackson.databind.exc.MismatchedInputException; -public class CoerceMiscScalarsTest extends BaseMapTest +import static org.junit.jupiter.api.Assertions.*; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*; + +public class CoerceMiscScalarsTest { private final ObjectMapper DEFAULT_MAPPER = sharedMapper(); @@ -54,6 +59,7 @@ public class CoerceMiscScalarsTest extends BaseMapTest /********************************************************************** */ + @Test public void testScalarDefaultsFromEmpty() throws Exception { // mostly as null, with some exceptions @@ -84,6 +90,7 @@ public void testScalarDefaultsFromEmpty() throws Exception /********************************************************************** */ + @Test public void testScalarEmptyToNull() throws Exception { _testScalarEmptyToNull(MAPPER_EMPTY_TO_NULL, File.class); @@ -100,6 +107,7 @@ public void testScalarEmptyToNull() throws Exception _testScalarEmptyToNull(MAPPER_EMPTY_TO_NULL, InetSocketAddress.class); } + @Test public void testScalarEmptyToEmpty() throws Exception { _testScalarEmptyToNull(MAPPER_EMPTY_TO_EMPTY, File.class); @@ -122,6 +130,7 @@ public void testScalarEmptyToEmpty() throws Exception _testScalarEmptyToNull(MAPPER_EMPTY_TO_EMPTY, InetSocketAddress.class); } + @Test public void testScalarEmptyToTryConvert() throws Exception { // Should be same as `AsNull` for most but not all @@ -151,6 +160,7 @@ public void testScalarEmptyToTryConvert() throws Exception /********************************************************************** */ + @Test public void testScalarsFailFromEmpty() throws Exception { _verifyScalarToFail(MAPPER_EMPTY_TO_FAIL, File.class); @@ -176,6 +186,7 @@ public void testScalarsFailFromEmpty() throws Exception // UUID is quite compatible, but not exactly due to historical reasons; // also uses custom subtype, so test separately + @Test public void testUUIDCoercions() throws Exception { // Coerce to `null` both by default, "TryConvert" and explicit @@ -200,6 +211,7 @@ public void testUUIDCoercions() throws Exception // StringBuilder is its own special type, since it naturally maps // from String values, hence separate testing + @Test public void testStringBuilderCoercions() throws Exception { // should result in an "empty" StringBuilder for all valid settings @@ -217,7 +229,7 @@ private void _checkEmptyStringBuilder(StringBuilder sb) { } // Date, Calendar also included here for convenience - + @Test public void testLegacyDateTimeCoercions() throws Exception { // Coerce to `null` both by default, "TryConvert" and explicit diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceNaNStringToNumberTest.java b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceNaNStringToNumberTest.java index 67783daf4a..ad0747e590 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceNaNStringToNumberTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceNaNStringToNumberTest.java @@ -1,10 +1,15 @@ package com.fasterxml.jackson.databind.convert; -import com.fasterxml.jackson.databind.BaseMapTest; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.ObjectMapper; -public class CoerceNaNStringToNumberTest extends BaseMapTest +import static org.junit.jupiter.api.Assertions.*; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*; + +public class CoerceNaNStringToNumberTest { static class DoubleBean { double _v; @@ -32,6 +37,7 @@ static class FloatBean { /********************************************************************** */ + @Test public void testDoublePrimitiveNonNumeric() throws Exception { // first, simple case: @@ -47,6 +53,7 @@ public void testDoublePrimitiveNonNumeric() throws Exception assertEquals(Double.POSITIVE_INFINITY, array[0]); } + @Test public void testDoublePrimFromNaNCoercionDisabled() throws Exception { // first, simple case: @@ -61,6 +68,7 @@ public void testDoublePrimFromNaNCoercionDisabled() throws Exception assertEquals(Double.POSITIVE_INFINITY, array[0]); } + @Test public void testDoubleWrapperFromNaNCoercionDisabled() throws Exception { double value = Double.POSITIVE_INFINITY; @@ -68,6 +76,7 @@ public void testDoubleWrapperFromNaNCoercionDisabled() throws Exception assertTrue(dv.isInfinite()); } + @Test public void testFloatPrimitiveNonNumeric() throws Exception { // bit tricky with binary fps but... @@ -82,6 +91,7 @@ public void testFloatPrimitiveNonNumeric() throws Exception assertEquals(Float.POSITIVE_INFINITY, array[0]); } + @Test public void testFloatPriFromNaNCoercionDisabled() throws Exception { // first, simple case: @@ -96,6 +106,7 @@ public void testFloatPriFromNaNCoercionDisabled() throws Exception assertEquals(Float.POSITIVE_INFINITY, array[0]); } + @Test public void testFloatWrapperFromNaNCoercionDisabled() throws Exception { float value = Float.POSITIVE_INFINITY; diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/CoercePojosTest.java b/src/test/java/com/fasterxml/jackson/databind/convert/CoercePojosTest.java index 4c7acf3197..e275ca3a3e 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/CoercePojosTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/CoercePojosTest.java @@ -2,6 +2,8 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.JacksonException; import com.fasterxml.jackson.databind.*; @@ -10,7 +12,11 @@ import com.fasterxml.jackson.databind.exc.MismatchedInputException; import com.fasterxml.jackson.databind.type.LogicalType; -public class CoercePojosTest extends BaseMapTest +import static org.junit.jupiter.api.Assertions.*; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*; + +public class CoercePojosTest { static class Bean { public String a; @@ -36,6 +42,7 @@ public BeanWithProp3676(@JsonProperty("a") String a) { /******************************************************** */ + @Test public void testPOJOFromEmptyStringLegacy() throws Exception { // first, verify default settings which do not accept empty String: @@ -48,16 +55,19 @@ public void testPOJOFromEmptyStringLegacy() throws Exception } + @Test public void testPOJOFromEmptyGlobalConfig() throws Exception { _testPOJOFromEmptyGlobalConfig(CoercionInputShape.EmptyString, JSON_EMPTY, null); } + @Test public void testPOJOFromEmptyLogicalTypeConfig() throws Exception { _testPOJOFromEmptyLogicalTypeConfig(CoercionInputShape.EmptyString, JSON_EMPTY, null); } + @Test public void testPOJOFromEmptyPhysicalTypeConfig() throws Exception { _testPOJOFromEmptyPhysicalTypeConfig(CoercionInputShape.EmptyString, JSON_EMPTY, null); @@ -65,6 +75,7 @@ public void testPOJOFromEmptyPhysicalTypeConfig() throws Exception // [databind#3676] Alternative test for "Mode.PROPERTIES" variant where we // have no "default" constructor + @Test public void testPOJOFromEmptyWithProperties() throws Exception { // Then coerce as empty @@ -85,16 +96,19 @@ public void testPOJOFromEmptyWithProperties() throws Exception /******************************************************** */ + @Test public void testPOJOFromBlankGlobalConfig() throws Exception { _testPOJOFromEmptyGlobalConfig(CoercionInputShape.EmptyString, JSON_BLANK, Boolean.TRUE); } + @Test public void testPOJOFromBlankLogicalTypeConfig() throws Exception { _testPOJOFromEmptyLogicalTypeConfig(CoercionInputShape.EmptyString, JSON_BLANK, Boolean.TRUE); } + @Test public void testPOJOFromBlankPhysicalTypeConfig() throws Exception { _testPOJOFromEmptyPhysicalTypeConfig(CoercionInputShape.EmptyString, JSON_BLANK, Boolean.TRUE); diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceStringToIntsTest.java b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceStringToIntsTest.java index b001a78917..b4afa43c10 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceStringToIntsTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceStringToIntsTest.java @@ -3,13 +3,19 @@ import java.math.BigInteger; import java.util.concurrent.atomic.AtomicLong; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.cfg.CoercionAction; import com.fasterxml.jackson.databind.cfg.CoercionInputShape; import com.fasterxml.jackson.databind.exc.MismatchedInputException; import com.fasterxml.jackson.databind.type.LogicalType; -public class CoerceStringToIntsTest extends BaseMapTest +import static org.junit.jupiter.api.Assertions.*; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*; + +public class CoerceStringToIntsTest { private final ObjectMapper DEFAULT_MAPPER = newJsonMapper(); private final ObjectReader READER_LEGACY_FAIL = jsonMapperBuilder() @@ -43,6 +49,7 @@ public class CoerceStringToIntsTest extends BaseMapTest /******************************************************** */ + @Test public void testLegacyStringToIntCoercion() throws Exception { // by default, should be ok @@ -71,6 +78,7 @@ public void testLegacyStringToIntCoercion() throws Exception assertEquals(95007, biggie.intValue()); } + @Test public void testLegacyFailStringToInt() throws Exception { _verifyCoerceFail(READER_LEGACY_FAIL, Integer.class, q("52"), "java.lang.Integer"); @@ -79,6 +87,7 @@ public void testLegacyFailStringToInt() throws Exception _verifyCoerceFail(READER_LEGACY_FAIL, int[].class, "[ \"-128\" ]", "element of `int[]`"); } + @Test public void testLegacyFailStringToLong() throws Exception { _verifyCoerceFail(READER_LEGACY_FAIL, Long.class, q("55")); @@ -87,6 +96,7 @@ public void testLegacyFailStringToLong() throws Exception _verifyCoerceFail(READER_LEGACY_FAIL, long[].class, "[ \"136\" ]", "element of `long[]`"); } + @Test public void testLegacyFailStringToOther() throws Exception { _verifyCoerceFail(READER_LEGACY_FAIL, Short.class, q("50")); @@ -108,6 +118,7 @@ public void testLegacyFailStringToOther() throws Exception /******************************************************** */ + @Test public void testCoerceConfigStringToNull() throws Exception { assertNull(MAPPER_TO_NULL.readValue(q("155"), Integer.class)); @@ -161,6 +172,7 @@ public void testCoerceConfigStringToNull() throws Exception /******************************************************** */ + @Test public void testCoerceConfigStringToEmpty() throws Exception { assertEquals(Integer.valueOf(0), MAPPER_TO_EMPTY.readValue(q("12"), Integer.class)); @@ -198,6 +210,7 @@ public void testCoerceConfigStringToEmpty() throws Exception /******************************************************** */ + @Test public void testCoerceConfigStringConvert() throws Exception { assertEquals(Integer.valueOf(12), MAPPER_TRY_CONVERT.readValue(q("12"), Integer.class)); @@ -235,6 +248,7 @@ public void testCoerceConfigStringConvert() throws Exception /******************************************************** */ + @Test public void testCoerceConfigFailFromString() throws Exception { _verifyCoerceFail(MAPPER_TO_FAIL, Integer.class, q("15")); diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceToBooleanTest.java b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceToBooleanTest.java index 36dc810b70..64ef0e6244 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceToBooleanTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceToBooleanTest.java @@ -5,18 +5,23 @@ import java.io.StringReader; import java.util.concurrent.atomic.AtomicBoolean; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; - import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.cfg.CoercionAction; import com.fasterxml.jackson.databind.cfg.CoercionInputShape; import com.fasterxml.jackson.databind.exc.MismatchedInputException; import com.fasterxml.jackson.databind.type.LogicalType; -public class CoerceToBooleanTest extends BaseMapTest +import static org.junit.jupiter.api.Assertions.*; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*; + +public class CoerceToBooleanTest { static class BooleanPOJO { public boolean value; @@ -82,6 +87,7 @@ public BooleanWrapper(@JsonProperty("ctor") Boolean foo) { */ // for [databind#403] + @Test public void testEmptyStringFailForBooleanPrimitive() throws IOException { final ObjectReader reader = DEFAULT_MAPPER @@ -96,6 +102,7 @@ public void testEmptyStringFailForBooleanPrimitive() throws IOException } } + @Test public void testStringToBooleanCoercionOk() throws Exception { // first successful coercions. Boolean has a ton... @@ -125,6 +132,7 @@ private void _verifyCoerceSuccess(ObjectMapper mapper, assertEquals(exp, result); } + @Test public void testStringToBooleanCoercionFail() throws Exception { _verifyRootStringCoerceFail(LEGACY_NONCOERCING_MAPPER, "true", Boolean.TYPE); @@ -179,6 +187,7 @@ private void _verifyStringCoerceFail(ObjectMapper nonCoercingMapper, /********************************************************** */ + @Test public void testIntToBooleanCoercionSuccessPojo() throws Exception { BooleanPOJO p; @@ -195,6 +204,7 @@ public void testIntToBooleanCoercionSuccessPojo() throws Exception assertEquals(true, p.value); } + @Test public void testIntToBooleanCoercionSuccessRoot() throws Exception { final ObjectReader br = DEFAULT_MAPPER.readerFor(Boolean.class); @@ -219,6 +229,7 @@ public void testIntToBooleanCoercionSuccessRoot() throws Exception } // Test for verifying that Long values are coerced to boolean correctly as well + @Test public void testLongToBooleanCoercionOk() throws Exception { long value = 1L + Integer.MAX_VALUE; @@ -246,6 +257,7 @@ public void testLongToBooleanCoercionOk() throws Exception } // [databind#2635], [databind#2770] + @Test public void testIntToBooleanCoercionFailBytes() throws Exception { _verifyBooleanCoerceFail(a2q("{'value':1}"), true, JsonToken.VALUE_NUMBER_INT, "1", BooleanPOJO.class); @@ -258,6 +270,7 @@ public void testIntToBooleanCoercionFailBytes() throws Exception } // [databind#2635], [databind#2770] + @Test public void testIntToBooleanCoercionFailChars() throws Exception { _verifyBooleanCoerceFail(a2q("{'value':1}"), false, JsonToken.VALUE_NUMBER_INT, "1", BooleanPOJO.class); @@ -275,6 +288,7 @@ public void testIntToBooleanCoercionFailChars() throws Exception /********************************************************** */ + @Test public void testIntToNullCoercion() throws Exception { assertNull(MAPPER_INT_TO_NULL.readValue("0", Boolean.class)); @@ -296,6 +310,7 @@ public void testIntToNullCoercion() throws Exception assertFalse(p.value); } + @Test public void testIntToEmptyCoercion() throws Exception { // "empty" value for Boolean/boolean is False/false @@ -319,6 +334,7 @@ public void testIntToEmptyCoercion() throws Exception assertFalse(p.value); } + @Test public void testIntToTryCoercion() throws Exception { // And "TryCoerce" should do what would be typically expected @@ -348,6 +364,7 @@ public void testIntToTryCoercion() throws Exception /********************************************************** */ + @Test public void testFailFromInteger() throws Exception { _verifyFailFromInteger(MAPPER_TO_FAIL, BooleanPOJO.class, DOC_WITH_0, Boolean.TYPE); diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/ConvertingAbstractSerializer795Test.java b/src/test/java/com/fasterxml/jackson/databind/convert/ConvertingAbstractSerializer795Test.java index f25159949d..54571edde5 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/ConvertingAbstractSerializer795Test.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/ConvertingAbstractSerializer795Test.java @@ -1,12 +1,18 @@ package com.fasterxml.jackson.databind.convert; import com.fasterxml.jackson.annotation.*; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.annotation.*; import com.fasterxml.jackson.databind.util.StdConverter; +import static org.junit.jupiter.api.Assertions.*; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*; + // for [databind#795] -public class ConvertingAbstractSerializer795Test extends BaseMapTest +public class ConvertingAbstractSerializer795Test { public static abstract class AbstractCustomType { final String value; @@ -71,14 +77,16 @@ public static class NonAbstractCustomTypeUser { /********************************************************** */ - private static final ObjectMapper JSON_MAPPER = new ObjectMapper(); + private static final ObjectMapper JSON_MAPPER = newJsonMapper(); + @Test public void testAbstractTypeDeserialization() throws Exception { String test="{\"customField\": \"customString\"}"; AbstractCustomTypeUser cu = JSON_MAPPER.readValue(test, AbstractCustomTypeUser.class); assertNotNull(cu); } + @Test public void testNonAbstractDeserialization() throws Exception { String test="{\"customField\": \"customString\"}"; NonAbstractCustomTypeUser cu = JSON_MAPPER.readValue(test, NonAbstractCustomTypeUser.class); diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/DisableCoercions3690Test.java b/src/test/java/com/fasterxml/jackson/databind/convert/DisableCoercions3690Test.java index a214806b07..4cf2a77bd8 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/DisableCoercions3690Test.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/DisableCoercions3690Test.java @@ -1,6 +1,9 @@ package com.fasterxml.jackson.databind.convert; -import com.fasterxml.jackson.databind.BaseMapTest; +import java.util.List; + +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.cfg.CoercionAction; @@ -8,9 +11,12 @@ import com.fasterxml.jackson.databind.exc.InvalidFormatException; import com.fasterxml.jackson.databind.exc.MismatchedInputException; import com.fasterxml.jackson.databind.type.TypeFactory; -import java.util.List; -public class DisableCoercions3690Test extends BaseMapTest +import static org.junit.jupiter.api.Assertions.*; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*; + +public class DisableCoercions3690Test { // [databind#3690] static class Input3690 { @@ -30,6 +36,7 @@ public void setField(T field) { } // [databind#3690] + @Test public void testCoercionFail3690() throws Exception { ObjectMapper mapper = jsonMapperBuilder() @@ -55,6 +62,7 @@ public void testCoercionFail3690() throws Exception } // [databind#3924] + @Test public void testFailMessage3924() throws Exception { // Arrange : Building a strict ObjectMapper. ObjectMapper mapper = jsonMapperBuilder() diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/EmptyStringAsSingleValueTest.java b/src/test/java/com/fasterxml/jackson/databind/convert/EmptyStringAsSingleValueTest.java index 5e67441ce4..7e647b0c12 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/EmptyStringAsSingleValueTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/EmptyStringAsSingleValueTest.java @@ -3,20 +3,22 @@ import java.util.Collections; import java.util.List; -import com.fasterxml.jackson.annotation.JsonCreator; +import org.junit.jupiter.api.Test; +import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.core.type.TypeReference; - -import com.fasterxml.jackson.databind.BaseMapTest; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.cfg.CoercionAction; import com.fasterxml.jackson.databind.cfg.CoercionInputShape; -import org.junit.Assert; + +import static org.junit.jupiter.api.Assertions.*; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*; // [databind#3418]: Coercion from empty String to Collection, with // `DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY` -public class EmptyStringAsSingleValueTest extends BaseMapTest +public class EmptyStringAsSingleValueTest { static final class StringWrapper { private final String s; @@ -55,123 +57,143 @@ public int hashCode() { }) .build(); + @Test public void testEmptyToList() throws Exception { // NO coercion + empty string input + StringCollectionDeserializer assertEquals(Collections.singletonList(""), NORMAL_MAPPER.readValue("\"\"", new TypeReference>() {})); } + @Test public void testEmptyToListWrapper() throws Exception { // NO coercion + empty string input + normal CollectionDeserializer assertEquals(Collections.singletonList(new StringWrapper("")), NORMAL_MAPPER.readValue("\"\"", new TypeReference>() {})); } + @Test public void testCoercedEmptyToList() throws Exception { // YES coercion + empty string input + StringCollectionDeserializer assertEquals(Collections.emptyList(), COERCION_MAPPER.readValue("\"\"", new TypeReference>() {})); } + @Test public void testCoercedEmptyToListWrapper() throws Exception { // YES coercion + empty string input + normal CollectionDeserializer assertEquals(Collections.emptyList(), COERCION_MAPPER.readValue("\"\"", new TypeReference>() {})); } + @Test public void testCoercedListToList() throws Exception { // YES coercion + empty LIST input + StringCollectionDeserializer assertEquals(Collections.emptyList(), COERCION_MAPPER.readValue("[]", new TypeReference>() {})); } + @Test public void testCoercedListToListWrapper() throws Exception { // YES coercion + empty LIST input + normal CollectionDeserializer assertEquals(Collections.emptyList(), COERCION_MAPPER.readValue("[]", new TypeReference>() {})); } + @Test public void testBlankToList() throws Exception { // NO coercion + empty string input + StringCollectionDeserializer assertEquals(Collections.singletonList(" "), NORMAL_MAPPER.readValue("\" \"", new TypeReference>() {})); } + @Test public void testBlankToListWrapper() throws Exception { // NO coercion + empty string input + normal CollectionDeserializer assertEquals(Collections.singletonList(new StringWrapper(" ")), NORMAL_MAPPER.readValue("\" \"", new TypeReference>() {})); } + @Test public void testCoercedBlankToList() throws Exception { // YES coercion + empty string input + StringCollectionDeserializer assertEquals(Collections.emptyList(), COERCION_MAPPER.readValue("\" \"", new TypeReference>() {})); } + @Test public void testCoercedBlankToListWrapper() throws Exception { // YES coercion + empty string input + normal CollectionDeserializer assertEquals(Collections.emptyList(), COERCION_MAPPER.readValue("\" \"", new TypeReference>() {})); } + @Test public void testEmptyToArray() throws Exception { // NO coercion + empty string input + StringCollectionDeserializer - Assert.assertArrayEquals(new String[]{""}, + assertArrayEquals(new String[]{""}, NORMAL_MAPPER.readValue("\"\"", new TypeReference() {})); } + @Test public void testEmptyToArrayWrapper() throws Exception { // NO coercion + empty string input + normal CollectionDeserializer - Assert.assertArrayEquals(new StringWrapper[]{new StringWrapper("")}, + assertArrayEquals(new StringWrapper[]{new StringWrapper("")}, NORMAL_MAPPER.readValue("\"\"", new TypeReference() {})); } + @Test public void testCoercedEmptyToArray() throws Exception { // YES coercion + empty string input + StringCollectionDeserializer - Assert.assertArrayEquals(new String[0], COERCION_MAPPER.readValue("\"\"", + assertArrayEquals(new String[0], COERCION_MAPPER.readValue("\"\"", new TypeReference() {})); } + @Test public void testCoercedEmptyToArrayWrapper() throws Exception { // YES coercion + empty string input + normal CollectionDeserializer - Assert.assertArrayEquals(new StringWrapper[0], + assertArrayEquals(new StringWrapper[0], COERCION_MAPPER.readValue("\"\"", new TypeReference() {})); } + @Test public void testCoercedListToArray() throws Exception { // YES coercion + empty LIST input + StringCollectionDeserializer - Assert.assertArrayEquals(new String[0], + assertArrayEquals(new String[0], COERCION_MAPPER.readValue("[]", new TypeReference() {})); } + @Test public void testCoercedListToArrayWrapper() throws Exception { // YES coercion + empty LIST input + normal CollectionDeserializer - Assert.assertArrayEquals(new StringWrapper[0], + assertArrayEquals(new StringWrapper[0], COERCION_MAPPER.readValue("[]", new TypeReference() {})); } + @Test public void testBlankToArray() throws Exception { // NO coercion + empty string input + StringCollectionDeserializer - Assert.assertArrayEquals(new String[]{" "}, + assertArrayEquals(new String[]{" "}, NORMAL_MAPPER.readValue("\" \"", new TypeReference() {})); } + @Test public void testBlankToArrayWrapper() throws Exception { // NO coercion + empty string input + normal CollectionDeserializer - Assert.assertArrayEquals(new StringWrapper[]{new StringWrapper(" ")}, + assertArrayEquals(new StringWrapper[]{new StringWrapper(" ")}, NORMAL_MAPPER.readValue("\" \"", new TypeReference() {})); } + @Test public void testCoercedBlankToArray() throws Exception { // YES coercion + empty string input + StringCollectionDeserializer - Assert.assertArrayEquals(new String[0], + assertArrayEquals(new String[0], COERCION_MAPPER.readValue("\" \"", new TypeReference() {})); } + @Test public void testCoercedBlankToArrayWrapper() throws Exception { // YES coercion + empty string input + normal CollectionDeserializer - Assert.assertArrayEquals(new StringWrapper[0], + assertArrayEquals(new StringWrapper[0], COERCION_MAPPER.readValue("\" \"", new TypeReference() {})); } } diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/ScalarConversionTest.java b/src/test/java/com/fasterxml/jackson/databind/convert/ScalarConversionTest.java index 12d320414e..86621255da 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/ScalarConversionTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/ScalarConversionTest.java @@ -1,12 +1,19 @@ package com.fasterxml.jackson.databind.convert; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.*; -public class ScalarConversionTest extends BaseMapTest +import static org.junit.jupiter.api.Assertions.*; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*; + +public class ScalarConversionTest { - private final ObjectMapper MAPPER = new ObjectMapper(); + private final ObjectMapper MAPPER = newJsonMapper(); // [databind#1433] + @Test public void testConvertValueNullPrimitive() throws Exception { assertEquals(Byte.valueOf((byte) 0), MAPPER.convertValue(null, Byte.TYPE)); @@ -20,6 +27,7 @@ public void testConvertValueNullPrimitive() throws Exception } // [databind#1433] + @Test public void testConvertValueNullBoxed() throws Exception { assertNull(MAPPER.convertValue(null, Byte.class)); diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/TestArrayConversions.java b/src/test/java/com/fasterxml/jackson/databind/convert/TestArrayConversions.java index a466a56a3c..9128247f90 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/TestArrayConversions.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/TestArrayConversions.java @@ -4,12 +4,16 @@ import java.util.*; import java.lang.reflect.Array; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.*; +import static org.junit.jupiter.api.Assertions.*; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*; + public class TestArrayConversions - extends com.fasterxml.jackson.databind.BaseMapTest { final static String OVERFLOW_MSG_BYTE = "out of range of Java byte"; final static String OVERFLOW_MSG_SHORT = "out of range of Java short"; @@ -17,8 +21,9 @@ public class TestArrayConversions final static String OVERFLOW_MSG_INT = "out of range of int"; final static String OVERFLOW_MSG_LONG = "out of range of long"; - final ObjectMapper MAPPER = new ObjectMapper(); + final ObjectMapper MAPPER = newJsonMapper(); + @Test public void testNullXform() throws Exception { /* when given null, null should be returned without conversion @@ -34,6 +39,7 @@ public void testNullXform() throws Exception * correctly, i.e. type -> type gives equal (although * not necessarily same) output */ + @Test public void testArrayIdentityTransforms() throws Exception { // first integral types @@ -47,6 +53,7 @@ public void testArrayIdentityTransforms() throws Exception verifyDoubleArrayConversion(doubles(), float[].class); } + @Test public void testByteArrayFrom() throws Exception { /* Note: byte arrays are tricky, since they are considered @@ -58,6 +65,7 @@ public void testByteArrayFrom() throws Exception verifyIntegralArrays(exp, data, exp.length); } + @Test public void testShortArrayToX() throws Exception { short[] data = shorts(); @@ -66,6 +74,7 @@ public void testShortArrayToX() throws Exception verifyShortArrayConversion(data, long[].class); } + @Test public void testIntArrayToX() throws Exception { int[] data = ints(); @@ -79,6 +88,7 @@ public void testIntArrayToX() throws Exception assertEquals(expNums, actNums); } + @Test public void testLongArrayToX() throws Exception { long[] data = longs(); @@ -91,6 +101,7 @@ public void testLongArrayToX() throws Exception assertEquals(expNums, actNums); } + @Test public void testOverflows() { // Byte overflow @@ -115,7 +126,7 @@ public void testOverflows() verifyException(e, OVERFLOW_MSG_INT); } // Longs need help of BigInteger... - BigInteger biggie = BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE);; + BigInteger biggie = BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE); List l = new ArrayList(); l.add(biggie); try { @@ -203,7 +214,7 @@ private void verifyIntegralArrays(Object inputArray, Object outputArray, int siz Number n2 = (Number) Array.get(outputArray, i); double value1 = n1.longValue(); double value2 = n2.longValue(); - assertEquals("Entry #"+i+"/"+size+" not equal", value1, value2); + assertEquals(value1, value2, "Entry #"+i+"/"+size+" not equal"); } } @@ -214,7 +225,7 @@ private void verifyDoubleArrays(Object inputArray, Object outputArray, int size) Number n2 = (Number) Array.get(outputArray, i); double value1 = n1.doubleValue(); double value2 = n2.doubleValue(); - assertEquals("Entry #"+i+"/"+size+" not equal", value1, value2); + assertEquals(value1, value2, "Entry #"+i+"/"+size+" not equal"); } } } diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/TestBeanConversions.java b/src/test/java/com/fasterxml/jackson/databind/convert/TestBeanConversions.java index 6e4ff0e15c..82ade0af5a 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/TestBeanConversions.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/TestBeanConversions.java @@ -3,23 +3,26 @@ import java.util.LinkedHashMap; import java.util.Map; -import com.fasterxml.jackson.core.JsonParser; +import org.junit.jupiter.api.Test; +import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.exc.InvalidFormatException; import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; import com.fasterxml.jackson.databind.util.StdConverter; - import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import static org.junit.jupiter.api.Assertions.*; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*; + /** * Tests for various conversions, especially ones using * {@link ObjectMapper#convertValue(Object, Class)}. */ public class TestBeanConversions - extends com.fasterxml.jackson.databind.BaseMapTest { static class PointZ { public int x, y; @@ -121,8 +124,9 @@ public NullBean deserialize(final JsonParser parser, final DeserializationContex /********************************************************** */ - private final ObjectMapper MAPPER = new ObjectMapper(); + private final ObjectMapper MAPPER = newJsonMapper(); + @Test public void testBeanConvert() { // should have no problems convert between compatible beans... @@ -136,6 +140,7 @@ public void testBeanConvert() // For [JACKSON-371]; verify that we know property that caused issue... // (note: not optimal place for test, but will have to do for now) + @Test public void testErrorReporting() throws Exception { //String json = "{\"boolProp\":\"oops\"}"; @@ -154,6 +159,7 @@ public void testErrorReporting() throws Exception } } + @Test public void testIssue458() throws Exception { ObjectWrapper a = new ObjectWrapper("foo"); @@ -164,6 +170,7 @@ public void testIssue458() throws Exception } // should work regardless of wrapping... + @Test public void testWrapping() throws Exception { ObjectMapper wrappingMapper = new ObjectMapper(); @@ -187,6 +194,7 @@ public void testWrapping() throws Exception } // [Issue-11]: simple cast, for POJOs etc + @Test public void testConvertUsingCast() throws Exception { String str = new String("foo"); @@ -208,6 +216,7 @@ private void _convertAndVerifyPoint(ObjectMapper m) /** * Need to test "shortcuts" introduced by [databind#11] */ + @Test public void testIssue11() throws Exception { // then some other no-op conversions @@ -254,6 +263,7 @@ public void testIssue11() throws Exception assertEquals(0, m.size()); } + @Test public void testConversionIssue288() throws Exception { String json = MAPPER.writeValueAsString(new ConvertingBean(1, 2)); @@ -262,6 +272,7 @@ public void testConversionIssue288() throws Exception } // Test null conversions from [databind#1433] + @Test public void testConversionIssue1433() throws Exception { assertNull(MAPPER.convertValue(null, Object.class)); diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/TestConvertingDeserializer.java b/src/test/java/com/fasterxml/jackson/databind/convert/TestConvertingDeserializer.java index e7af0ead43..e9d90f4640 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/TestConvertingDeserializer.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/TestConvertingDeserializer.java @@ -3,11 +3,17 @@ import java.math.BigDecimal; import java.util.*; +import org.junit.jupiter.api.Test; + +import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.util.StdConverter; +import static org.junit.jupiter.api.Assertions.*; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*; + public class TestConvertingDeserializer -extends com.fasterxml.jackson.databind.BaseMapTest { @JsonDeserialize(converter=ConvertingBeanConverter.class) static class ConvertingBean @@ -121,6 +127,9 @@ static class Issue795Bean /********************************************************** */ + private final ObjectMapper MAPPER = newJsonMapper(); + + @Test public void testClassAnnotationSimple() throws Exception { ConvertingBean bean = objectReader(ConvertingBean.class).readValue("[1,2]"); @@ -129,6 +138,7 @@ public void testClassAnnotationSimple() throws Exception assertEquals(2, bean.y); } + @Test public void testClassAnnotationForLists() throws Exception { ConvertingBeanContainer container = objectReader(ConvertingBeanContainer.class) @@ -139,6 +149,7 @@ public void testClassAnnotationForLists() throws Exception assertEquals(4, container.values.get(1).y); } + @Test public void testPropertyAnnotationSimple() throws Exception { PointWrapper wrapper = objectReader(PointWrapper.class).readValue("{\"value\":[3,4]}"); @@ -148,6 +159,7 @@ public void testPropertyAnnotationSimple() throws Exception assertEquals(4, wrapper.value.y); } + @Test public void testPropertyAnnotationLowerCasing() throws Exception { LowerCaseText text = objectReader(LowerCaseText.class).readValue("{\"text\":\"Yay!\"}"); @@ -156,6 +168,7 @@ public void testPropertyAnnotationLowerCasing() throws Exception assertEquals("yay!", text.text); } + @Test public void testPropertyAnnotationArrayLC() throws Exception { LowerCaseTextArray texts = objectReader(LowerCaseTextArray.class).readValue("{\"texts\":[\"ABC\"]}"); @@ -165,6 +178,7 @@ public void testPropertyAnnotationArrayLC() throws Exception assertEquals("abc", texts.texts[0]); } + @Test public void testPropertyAnnotationForArrays() throws Exception { PointListWrapperArray array = objectReader(PointListWrapperArray.class) @@ -175,6 +189,7 @@ public void testPropertyAnnotationForArrays() throws Exception assertEquals(5, array.values[1].x); } + @Test public void testPropertyAnnotationForLists() throws Exception { PointListWrapperList array = objectReader(PointListWrapperList.class) @@ -185,6 +200,7 @@ public void testPropertyAnnotationForLists() throws Exception assertEquals(7, array.values.get(0).x); } + @Test public void testPropertyAnnotationForMaps() throws Exception { PointListWrapperMap map = objectReader(PointListWrapperMap.class) @@ -199,13 +215,14 @@ public void testPropertyAnnotationForMaps() throws Exception } // [databind#795] + @Test public void testConvertToAbstract() throws Exception { Issue795Bean bean = objectReader(Issue795Bean.class) .readValue("{\"value\":\"1.25\"}"); assertNotNull(bean.value); - assertTrue("Type not BigDecimal but "+bean.value.getClass(), - bean.value instanceof BigDecimal); + assertTrue(bean.value instanceof BigDecimal, + "Type not BigDecimal but "+bean.value.getClass()); assertEquals(new BigDecimal("1.25"), bean.value); } } diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/TestConvertingSerializer.java b/src/test/java/com/fasterxml/jackson/databind/convert/TestConvertingSerializer.java index dea85ede3f..da7452e500 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/TestConvertingSerializer.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/TestConvertingSerializer.java @@ -3,17 +3,20 @@ import java.io.IOException; import java.util.*; -import com.fasterxml.jackson.core.JsonGenerator; +import org.junit.jupiter.api.Test; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.util.StdConverter; -import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import static org.junit.jupiter.api.Assertions.*; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*; public class TestConvertingSerializer - extends com.fasterxml.jackson.databind.BaseMapTest { @JsonSerialize(converter=ConvertingBeanConverter.class) static class ConvertingBean @@ -174,12 +177,14 @@ public Object convert(ConvertingBeanWithUntypedConverter cb) { /********************************************************** */ + @Test public void testClassAnnotationSimple() throws Exception { String json = objectWriter().writeValueAsString(new ConvertingBean(1, 2)); assertEquals("[1,2]", json); } + @Test public void testClassAnnotationForLists() throws Exception { String json = objectWriter().writeValueAsString(new ConvertingBeanContainer( @@ -187,40 +192,47 @@ public void testClassAnnotationForLists() throws Exception assertEquals("{\"values\":[[1,2],[3,4]]}", json); } + @Test public void testPropertyAnnotationSimple() throws Exception { String json = objectWriter().writeValueAsString(new PointWrapper(3, 4)); assertEquals("{\"value\":[3,4]}", json); } + @Test public void testPropertyAnnotationForArrays() throws Exception { String json = objectWriter().writeValueAsString(new PointListWrapperArray(4, 5)); assertEquals("{\"values\":[[4,5],[5,4]]}", json); } + @Test public void testPropertyAnnotationForLists() throws Exception { String json = objectWriter().writeValueAsString(new PointListWrapperList(7, 8)); assertEquals("{\"values\":[[7,8],[8,7]]}", json); } + @Test public void testPropertyAnnotationForMaps() throws Exception { String json = objectWriter().writeValueAsString(new PointListWrapperMap("a", 1, 2)); assertEquals("{\"values\":{\"a\":[1,2]}}", json); } // [databind#357] + @Test public void testConverterForList357() throws Exception { String json = objectWriter().writeValueAsString(new ListWrapper()); assertEquals("{\"list\":[[\"Hello world!\"]]}", json); } // [databind#359] + @Test public void testIssue359() throws Exception { String json = objectWriter().writeValueAsString(new Bean359()); assertEquals("{\"stuff\":[\"Target\"]}", json); } // [databind#731]: Problems converting from java.lang.Object ("unknown") + @Test public void testIssue731() throws Exception { String json = objectWriter().writeValueAsString(new ConvertingBeanWithUntypedConverter(1, 2)); diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/TestMapConversions.java b/src/test/java/com/fasterxml/jackson/databind/convert/TestMapConversions.java index 0208e5a36c..c137c6cf1b 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/TestMapConversions.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/TestMapConversions.java @@ -2,13 +2,16 @@ import java.util.*; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.util.StdConverter; +import static org.junit.jupiter.api.Assertions.*; + public class TestMapConversions - extends com.fasterxml.jackson.databind.BaseMapTest { final ObjectMapper MAPPER = new ObjectMapper(); @@ -48,6 +51,7 @@ public Map convert(final Request value) { /** * Test that verifies that we can go between couple of types of Maps... */ + @Test public void testMapToMap() { Map input = new LinkedHashMap(); @@ -67,6 +71,7 @@ public void testMapToMap() assertEquals(Integer.valueOf(-4), roundtrip.get("B")); } + @Test public void testMapToBean() { EnumMap map = new EnumMap(AB.class); @@ -77,6 +82,7 @@ public void testMapToBean() assertEquals("-1", bean.B); } + @Test public void testBeanToMap() { Bean bean = new Bean(); @@ -89,6 +95,7 @@ public void testBeanToMap() } // [Issue#287]: Odd problems with `Object` type, static typing + @Test public void testIssue287() throws Exception { // use local instance to ensure no caching affects it: @@ -99,6 +106,7 @@ public void testIssue287() throws Exception } // [databind#810] + @Test public void testMapToProperties() throws Exception { Bean bean = new Bean(); diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/TestPolymorphicUpdateValue.java b/src/test/java/com/fasterxml/jackson/databind/convert/TestPolymorphicUpdateValue.java index c7f8df329d..5c57f41365 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/TestPolymorphicUpdateValue.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/TestPolymorphicUpdateValue.java @@ -1,14 +1,19 @@ package com.fasterxml.jackson.databind.convert; -import com.fasterxml.jackson.annotation.*; +import org.junit.jupiter.api.Test; +import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.databind.*; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*; + /** * Unit tests for verifying handling of update value on polymorphic * objects. */ -public class TestPolymorphicUpdateValue extends BaseMapTest +public class TestPolymorphicUpdateValue { @JsonTypeInfo(include=JsonTypeInfo.As.WRAPPER_ARRAY //PROPERTY ,use=JsonTypeInfo.Id.NAME, property="type") @@ -30,8 +35,9 @@ public static class Child extends Parent { /******************************************************** */ - private final ObjectMapper MAPPER = new ObjectMapper(); + private final ObjectMapper MAPPER = newJsonMapper(); + @Test public void testPolymorphicTest() throws Exception { Child c = new Child(); diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/TestStringConversions.java b/src/test/java/com/fasterxml/jackson/databind/convert/TestStringConversions.java index 2db21e96ff..55cd18b906 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/TestStringConversions.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/TestStringConversions.java @@ -2,7 +2,7 @@ import java.util.*; -import static org.junit.Assert.*; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.core.Base64Variants; import com.fasterxml.jackson.databind.*; @@ -10,8 +10,9 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.util.StdConverter; +import static org.junit.jupiter.api.Assertions.*; + public class TestStringConversions - extends com.fasterxml.jackson.databind.BaseMapTest { static class LCConverter extends StdConverter { @@ -32,6 +33,7 @@ protected StringWrapperWithConvert() { } private final ObjectMapper MAPPER = new ObjectMapper(); + @Test public void testSimple() { assertEquals(Boolean.TRUE, MAPPER.convertValue("true", Boolean.class)); @@ -47,6 +49,7 @@ public void testSimple() assertArrayEquals(ints, MAPPER.convertValue(Ints, int[].class)); } + @Test public void testStringsToInts() { // let's verify our "neat trick" actually works... @@ -54,6 +57,7 @@ public void testStringsToInts() MAPPER.convertValue("1 2 3 4 -1 0".split("\\s+"), int[].class)); } + @Test public void testBytesToBase64AndBack() throws Exception { byte[] input = new byte[] { 1, 2, 3, 4, 5, 6, 7 }; @@ -69,6 +73,7 @@ public void testBytesToBase64AndBack() throws Exception assertArrayEquals(input, result); } + @Test public void testBytestoCharArray() throws Exception { byte[] input = new byte[] { 1, 2, 3, 4, 5, 6, 7 }; @@ -79,11 +84,13 @@ public void testBytestoCharArray() throws Exception assertArrayEquals(expEncoded, actEncoded); } + @Test public void testLowerCasingSerializer() throws Exception { assertEquals("{\"value\":\"abc\"}", MAPPER.writeValueAsString(new StringWrapperWithConvert("ABC"))); } + @Test public void testLowerCasingDeserializer() throws Exception { StringWrapperWithConvert value = MAPPER.readValue("{\"value\":\"XyZ\"}", StringWrapperWithConvert.class); diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/TestUpdateViaObjectReader.java b/src/test/java/com/fasterxml/jackson/databind/convert/TestUpdateViaObjectReader.java index b945546e8b..04196064c4 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/TestUpdateViaObjectReader.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/TestUpdateViaObjectReader.java @@ -3,6 +3,8 @@ import java.io.IOException; import java.util.*; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.annotation.JsonTypeInfo.Id; import com.fasterxml.jackson.core.*; @@ -12,14 +14,17 @@ import com.fasterxml.jackson.databind.deser.std.StdNodeBasedDeserializer; import com.fasterxml.jackson.databind.module.SimpleModule; -import static org.junit.Assert.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.*; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.a2q; +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.newJsonMapper; /** * Unit tests for verifying that "updating reader" works as * expected. */ @SuppressWarnings("serial") -public class TestUpdateViaObjectReader extends BaseMapTest +public class TestUpdateViaObjectReader { static class Bean { public String a = "a"; @@ -177,8 +182,9 @@ public Bean3814B convert(JsonNode root, DeserializationContext ctxt) throws IOEx /******************************************************** */ - private final ObjectMapper MAPPER = new ObjectMapper(); + private final ObjectMapper MAPPER = newJsonMapper(); + @Test public void testBeanUpdate() throws Exception { Bean bean = new Bean(); @@ -216,6 +222,7 @@ public void testBeanUpdate() throws Exception assertEquals("xyz", b3.b); } + @Test public void testListUpdate() throws Exception { List strs = new ArrayList(); @@ -230,6 +237,7 @@ public void testListUpdate() throws Exception assertEquals("d", strs.get(3)); } + @Test public void testMapUpdate() throws Exception { Map strs = new HashMap(); @@ -246,6 +254,7 @@ public void testMapUpdate() throws Exception // Test for [JACKSON-717] -- ensure 'readValues' also does update @SuppressWarnings("resource") + @Test public void testUpdateSequence() throws Exception { XYBean toUpdate = new XYBean(); @@ -274,6 +283,7 @@ public void testUpdateSequence() throws Exception } // [JACKSON-824] + @Test public void testUpdatingWithViews() throws Exception { Updateable bean = new Updateable(); @@ -289,6 +299,7 @@ public void testUpdatingWithViews() throws Exception } // [databind#744] + @Test public void testIssue744() throws Exception { ObjectMapper mapper = new ObjectMapper(); @@ -330,6 +341,7 @@ public void testIssue744() throws Exception } // [databind#1831] + @Test public void test1831UsingNode() throws Exception { String catJson = MAPPER.writeValueAsString(new Cat()); JsonNode jsonNode = MAPPER.readTree(catJson); @@ -339,6 +351,7 @@ public void test1831UsingNode() throws Exception { assertSame(optionalCat, result); } + @Test public void test1831UsingString() throws Exception { String catJson = MAPPER.writeValueAsString(new Cat()); AnimalWrapper optionalCat = new AnimalWrapper(); @@ -347,6 +360,7 @@ public void test1831UsingString() throws Exception { } // [databind#3814] + @Test public void testReaderForUpdating3814() throws Exception { // Arrange JsonNode root = MAPPER.readTree(a2q("{'age': 30 }")); @@ -361,6 +375,7 @@ public void testReaderForUpdating3814() throws Exception { } // [databind#3814] + @Test public void testReaderForUpdating3814DoesNotOverride() throws Exception { // Arrange JsonNode root = MAPPER.readTree(a2q("{'age': 30 }")); diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/UpdateValueTest.java b/src/test/java/com/fasterxml/jackson/databind/convert/UpdateValueTest.java index f935c5248a..06713ddee7 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/UpdateValueTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/UpdateValueTest.java @@ -2,12 +2,19 @@ import java.util.*; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.*; +import static org.junit.jupiter.api.Assertions.*; + +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.Point; +import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.newJsonMapper; + /** * Tests for {@link ObjectMapper#updateValue}. */ -public class UpdateValueTest extends BaseMapTest +public class UpdateValueTest { /* /******************************************************** @@ -15,8 +22,9 @@ public class UpdateValueTest extends BaseMapTest /******************************************************** */ - private final ObjectMapper MAPPER = new ObjectMapper(); + private final ObjectMapper MAPPER = newJsonMapper(); + @Test public void testMapUpdate() throws Exception { Map base = new LinkedHashMap<>(); @@ -34,6 +42,7 @@ public void testMapUpdate() throws Exception assertEquals(Boolean.TRUE, ob.get("xyz")); } + @Test public void testListUpdate() throws Exception { List base = new ArrayList<>(); @@ -51,6 +60,7 @@ public void testListUpdate() throws Exception assertEquals(overrides[1], ob.get(3)); } + @Test public void testArrayUpdate() throws Exception { // Since Arrays are immutable, not sure what "right answer" ought to be @@ -71,6 +81,7 @@ public void testArrayUpdate() throws Exception /******************************************************** */ + @Test public void testPOJO() throws Exception { Point base = new Point(42, 28); @@ -88,6 +99,7 @@ public void testPOJO() throws Exception /******************************************************** */ + @Test public void testMisc() throws Exception { // if either is `null`, should return first arg @@ -95,5 +107,4 @@ public void testMisc() throws Exception List input = new ArrayList<>(); assertSame(input, MAPPER.updateValue(input, null)); } - } diff --git a/src/test/java/com/fasterxml/jackson/databind/testutil/DatabindTestUtil.java b/src/test/java/com/fasterxml/jackson/databind/testutil/DatabindTestUtil.java index 8d8648b4e4..2acac3efdd 100644 --- a/src/test/java/com/fasterxml/jackson/databind/testutil/DatabindTestUtil.java +++ b/src/test/java/com/fasterxml/jackson/databind/testutil/DatabindTestUtil.java @@ -4,11 +4,14 @@ import java.nio.charset.StandardCharsets; import java.util.*; +import com.fasterxml.jackson.core.JsonLocation; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.databind.type.TypeFactory; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.*; /** * Class containing test utility methods. @@ -25,6 +28,47 @@ public class DatabindTestUtil /********************************************************** */ + public static class IntWrapper { + public int i; + + public IntWrapper() { } + public IntWrapper(int value) { i = value; } + } + + public static class LongWrapper { + public long l; + + public LongWrapper() { } + public LongWrapper(long value) { l = value; } + } + + public static class FloatWrapper { + public float f; + + public FloatWrapper() { } + public FloatWrapper(float value) { f = value; } + } + + public static class DoubleWrapper { + public double d; + + public DoubleWrapper() { } + public DoubleWrapper(double value) { d = value; } + } + + /** + * Simple wrapper around String type, usually to test value + * conversions or wrapping + */ + public static class StringWrapper { + public String str; + + public StringWrapper() { } + public StringWrapper(String value) { + str = value; + } + } + public static enum ABC { A, B, C; } public static class Point { @@ -66,6 +110,22 @@ public static ObjectMapper sharedMapper() { return SHARED_MAPPER; } + public static ObjectMapper objectMapper() { + return sharedMapper(); + } + + public static ObjectWriter objectWriter() { + return sharedMapper().writer(); + } + + public static ObjectReader objectReader() { + return sharedMapper().reader(); + } + + public static ObjectReader objectReader(Class cls) { + return sharedMapper().readerFor(cls); + } + public static TypeFactory newTypeFactory() { // this is a work-around; no null modifier added return TypeFactory.defaultInstance().withModifier(null); @@ -123,6 +183,18 @@ public static byte[] utf8Bytes(String str) { /********************************************************** */ + public static void assertToken(JsonToken expToken, JsonToken actToken) + { + if (actToken != expToken) { + fail("Expected token "+expToken+", current token "+actToken); + } + } + + public static void assertToken(JsonToken expToken, JsonParser jp) + { + assertToken(expToken, jp.currentToken()); + } + /** * @param e Exception to check * @param anyMatches Array of Strings of which AT LEAST ONE ("any") has to be included @@ -142,4 +214,9 @@ public static void verifyException(Throwable e, String... anyMatches) + Arrays.asList(anyMatches)+"): got one (of type "+e.getClass().getName() +") with message \""+msg+"\""); } + + public static void assertValidLocation(JsonLocation location) { + assertNotNull(location, "Should have non-null location"); + assertTrue(location.getLineNr() > 0, "Should have positive line number"); + } } From 466a8f083fc87fef69ca6c665771aac3ce52f2cb Mon Sep 17 00:00:00 2001 From: "Kim, Joo Hyuk" Date: Sun, 7 Jan 2024 12:00:18 +0900 Subject: [PATCH 2/2] Organize imports --- .../jackson/databind/convert/CoerceEnumTest.java | 1 - .../databind/convert/CoerceIntToStringTest.java | 1 - .../databind/convert/CoerceMiscScalarsTest.java | 13 ++----------- .../jackson/databind/convert/CoercePojosTest.java | 5 ++--- .../ConvertingAbstractSerializer795Test.java | 2 +- 5 files changed, 5 insertions(+), 17 deletions(-) diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceEnumTest.java b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceEnumTest.java index 92aadbdf54..a3b49b06c8 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceEnumTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceEnumTest.java @@ -3,7 +3,6 @@ import org.junit.jupiter.api.Test; import com.fasterxml.jackson.annotation.JsonEnumDefaultValue; - import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.cfg.CoercionAction; import com.fasterxml.jackson.databind.cfg.CoercionInputShape; diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceIntToStringTest.java b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceIntToStringTest.java index 8bc258995f..01a9f846a1 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceIntToStringTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceIntToStringTest.java @@ -3,7 +3,6 @@ import org.junit.jupiter.api.Test; import com.fasterxml.jackson.core.JsonProcessingException; - import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.databind.cfg.CoercionAction; diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceMiscScalarsTest.java b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceMiscScalarsTest.java index adf9b8094f..f863fc8a30 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceMiscScalarsTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceMiscScalarsTest.java @@ -1,18 +1,9 @@ package com.fasterxml.jackson.databind.convert; import java.io.File; -import java.net.InetAddress; -import java.net.InetSocketAddress; -import java.net.URI; -import java.net.URL; +import java.net.*; import java.nio.charset.Charset; -import java.util.Calendar; -import java.util.Currency; -import java.util.Date; -import java.util.GregorianCalendar; -import java.util.Locale; -import java.util.TimeZone; -import java.util.UUID; +import java.util.*; import java.util.regex.Pattern; import org.junit.jupiter.api.Test; diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/CoercePojosTest.java b/src/test/java/com/fasterxml/jackson/databind/convert/CoercePojosTest.java index e275ca3a3e..b39f5a1099 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/CoercePojosTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/CoercePojosTest.java @@ -1,11 +1,10 @@ package com.fasterxml.jackson.databind.convert; -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonProperty; import org.junit.jupiter.api.Test; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.JacksonException; - import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.cfg.CoercionAction; import com.fasterxml.jackson.databind.cfg.CoercionInputShape; diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/ConvertingAbstractSerializer795Test.java b/src/test/java/com/fasterxml/jackson/databind/convert/ConvertingAbstractSerializer795Test.java index 54571edde5..8589f04e05 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/ConvertingAbstractSerializer795Test.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/ConvertingAbstractSerializer795Test.java @@ -1,8 +1,8 @@ package com.fasterxml.jackson.databind.convert; -import com.fasterxml.jackson.annotation.*; import org.junit.jupiter.api.Test; +import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.annotation.*; import com.fasterxml.jackson.databind.util.StdConverter;