From a1736a0d3788a0bc23f97f15969fab7688dc8038 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Thu, 10 Oct 2019 12:42:13 -0700 Subject: [PATCH] Test refactoring --- .../databind/ser/TestKeySerializers.java | 255 ------------------ .../ser/jdk/MapKeySerializationTest.java | 255 +++++++++++++++++- 2 files changed, 247 insertions(+), 263 deletions(-) delete mode 100644 src/test/java/com/fasterxml/jackson/databind/ser/TestKeySerializers.java diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/TestKeySerializers.java b/src/test/java/com/fasterxml/jackson/databind/ser/TestKeySerializers.java deleted file mode 100644 index 8b1df46b39..0000000000 --- a/src/test/java/com/fasterxml/jackson/databind/ser/TestKeySerializers.java +++ /dev/null @@ -1,255 +0,0 @@ -package com.fasterxml.jackson.databind.ser; - -import java.io.IOException; -import java.util.*; - -import com.fasterxml.jackson.annotation.*; -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.*; -import com.fasterxml.jackson.databind.annotation.JsonSerialize; -import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder; -import com.fasterxml.jackson.databind.module.SimpleModule; - -public class TestKeySerializers extends BaseMapTest -{ - public static class KarlSerializer extends JsonSerializer - { - @Override - public void serialize(String value, JsonGenerator gen, SerializerProvider provider) throws IOException { - gen.writeFieldName("Karl"); - } - } - - public static class NullKeySerializer extends JsonSerializer - { - private String _null; - public NullKeySerializer(String s) { _null = s; } - @Override - public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException { - gen.writeFieldName(_null); - } - } - - public static class NullValueSerializer extends JsonSerializer - { - private String _null; - public NullValueSerializer(String s) { _null = s; } - @Override - public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException { - gen.writeString(_null); - } - } - - public static class NotKarlBean - { - public Map map = new HashMap(); - { - map.put("Not Karl", 1); - } - } - - public static class KarlBean - { - @JsonSerialize(keyUsing = KarlSerializer.class) - public Map map = new HashMap(); - { - map.put("Not Karl", 1); - } - } - - enum ABC { - A, B, C - } - - enum AbcLC { - A, B, C; - - @JsonValue - public String toLC() { - return name().toLowerCase(); - } - } - - static class ABCKeySerializer extends JsonSerializer { - @Override - public void serialize(ABC value, JsonGenerator gen, - SerializerProvider provider) throws IOException { - gen.writeFieldName("xxx"+value); - } - } - - @JsonSerialize(keyUsing = ABCKeySerializer.class) - public static enum ABCMixin { } - - public static enum Outer { - inner; - } - - static class ABCMapWrapper { - public Map stuff = new HashMap(); - public ABCMapWrapper() { - stuff.put(ABC.B, "bar"); - } - } - - static class BAR{ - T value; - - public BAR(T value) { - this.value = value; - } - - @JsonValue - public T getValue() { - return value; - } - - @Override - public String toString() { - return this.getClass().getSimpleName() - + ", value:" + value - ; - } - } - - static class UCString { - private String value; - - public UCString(String v) { - value = v.toUpperCase(); - } - - @JsonValue - public String asString() { - return value; - } - } - - /* - /********************************************************** - /* Unit tests - /********************************************************** - */ - - private final ObjectMapper MAPPER = new ObjectMapper(); - - public void testNotKarl() throws IOException { - final String serialized = MAPPER.writeValueAsString(new NotKarlBean()); - assertEquals("{\"map\":{\"Not Karl\":1}}", serialized); - } - - public void testKarl() throws IOException { - final String serialized = MAPPER.writeValueAsString(new KarlBean()); - assertEquals("{\"map\":{\"Karl\":1}}", serialized); - } - - // [databind#75]: caching of KeySerializers - public void testBoth() throws IOException - { - // Let's NOT use shared one, to ensure caching starts from clean slate - final ObjectMapper mapper = new ObjectMapper(); - final String value1 = mapper.writeValueAsString(new NotKarlBean()); - assertEquals("{\"map\":{\"Not Karl\":1}}", value1); - final String value2 = mapper.writeValueAsString(new KarlBean()); - assertEquals("{\"map\":{\"Karl\":1}}", value2); - } - - // Test custom key serializer for enum - public void testCustomForEnum() throws IOException - { - // cannot use shared mapper as we are registering a module - final ObjectMapper mapper = new ObjectMapper(); - SimpleModule mod = new SimpleModule("test"); - mod.addKeySerializer(ABC.class, new ABCKeySerializer()); - mapper.registerModule(mod); - - String json = mapper.writeValueAsString(new ABCMapWrapper()); - assertEquals("{\"stuff\":{\"xxxB\":\"bar\"}}", json); - } - - public void testCustomNullSerializers() throws IOException - { - final ObjectMapper mapper = new ObjectMapper(); - mapper.getSerializerProvider().setNullKeySerializer(new NullKeySerializer("NULL-KEY")); - mapper.getSerializerProvider().setNullValueSerializer(new NullValueSerializer("NULL")); - Map input = new HashMap<>(); - input.put(null, 3); - String json = mapper.writeValueAsString(input); - assertEquals("{\"NULL-KEY\":3}", json); - json = mapper.writeValueAsString(new Object[] { 1, null, true }); - assertEquals("[1,\"NULL\",true]", json); - } - - public void testCustomEnumInnerMapKey() throws Exception { - Map outerMap = new HashMap(); - Map> map = new EnumMap>(ABC.class); - Map innerMap = new HashMap(); - innerMap.put("one", "1"); - map.put(ABC.A, innerMap); - outerMap.put(Outer.inner, map); - final ObjectMapper mapper = new ObjectMapper(); - SimpleModule mod = new SimpleModule("test"); - mod.setMixInAnnotation(ABC.class, ABCMixin.class); - mod.addKeySerializer(ABC.class, new ABCKeySerializer()); - mapper.registerModule(mod); - - JsonNode tree = mapper.convertValue(outerMap, JsonNode.class); - - JsonNode innerNode = tree.get("inner"); - String key = innerNode.fieldNames().next(); - assertEquals("xxxA", key); - } - - // [databind#838] - public void testUnWrappedMapWithDefaultType() throws Exception{ - final ObjectMapper mapper = new ObjectMapper(); - SimpleModule mod = new SimpleModule("test"); - mod.addKeySerializer(ABC.class, new ABCKeySerializer()); - mapper.registerModule(mod); - - TypeResolverBuilder typer = ObjectMapper.DefaultTypeResolverBuilder.construct( - ObjectMapper.DefaultTyping.NON_FINAL, mapper.getPolymorphicTypeValidator()); - typer = typer.init(JsonTypeInfo.Id.NAME, null); - typer = typer.inclusion(JsonTypeInfo.As.PROPERTY); - //typer = typer.typeProperty(TYPE_FIELD); - typer = typer.typeIdVisibility(true); - mapper.setDefaultTyping(typer); - - Map stuff = new HashMap(); - stuff.put(ABC.B, "bar"); - String json = mapper.writerFor(new TypeReference>() {}) - .writeValueAsString(stuff); - assertEquals("{\"@type\":\"HashMap\",\"xxxB\":\"bar\"}", json); - } - - // [databind#838] - @SuppressWarnings("deprecation") - public void testUnWrappedMapWithKeySerializer() throws Exception{ - SimpleModule mod = new SimpleModule("test"); - mod.addKeySerializer(ABC.class, new ABCKeySerializer()); - final ObjectMapper mapper = new ObjectMapper() - .registerModule(mod) - .enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT) - .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) - .disable(SerializationFeature.WRITE_NULL_MAP_VALUES) - .setSerializationInclusion(JsonInclude.Include.NON_EMPTY) - ; - - Map> stuff = new HashMap>(); - stuff.put(ABC.B, new BAR("bar")); - String json = mapper.writerFor(new TypeReference>>() {}) - .writeValueAsString(stuff); - assertEquals("{\"xxxB\":\"bar\"}", json); - } - - // [databind#943] - public void testDynamicMapKeys() throws Exception - { - Map stuff = new LinkedHashMap(); - stuff.put(AbcLC.B, Integer.valueOf(3)); - stuff.put(new UCString("foo"), Integer.valueOf(4)); - String json = MAPPER.writeValueAsString(stuff); - assertEquals(aposToQuotes("{'b':3,'FOO':4}"), json); - } -} diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/jdk/MapKeySerializationTest.java b/src/test/java/com/fasterxml/jackson/databind/ser/jdk/MapKeySerializationTest.java index 8731deb605..661f0b4695 100644 --- a/src/test/java/com/fasterxml/jackson/databind/ser/jdk/MapKeySerializationTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/ser/jdk/MapKeySerializationTest.java @@ -1,20 +1,57 @@ package com.fasterxml.jackson.databind.ser.jdk; import java.io.IOException; +import java.util.EnumMap; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.JsonValue; + import com.fasterxml.jackson.core.Base64Variants; import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.type.TypeReference; + import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder; +import com.fasterxml.jackson.databind.module.SimpleModule; @SuppressWarnings("serial") public class MapKeySerializationTest extends BaseMapTest { - // for [databind#47] + public static class KarlSerializer extends JsonSerializer + { + @Override + public void serialize(String value, JsonGenerator gen, SerializerProvider provider) throws IOException { + gen.writeFieldName("Karl"); + } + } + + public static class NotKarlBean + { + public Map map = new HashMap(); + { + map.put("Not Karl", 1); + } + } + + public static class KarlBean + { + @JsonSerialize(keyUsing = KarlSerializer.class) + public Map map = new HashMap(); + { + map.put("Not Karl", 1); + } + } + + public static enum Outer { + inner; + } + public static class Wat { private final String wat; @@ -37,6 +74,90 @@ public String toString() { static class WatMap extends HashMap { } + enum ABC { + A, B, C + } + + enum AbcLC { + A, B, C; + + @JsonValue + public String toLC() { + return name().toLowerCase(); + } + } + + static class ABCMapWrapper { + public Map stuff = new HashMap(); + public ABCMapWrapper() { + stuff.put(ABC.B, "bar"); + } + } + + @JsonSerialize(keyUsing = ABCKeySerializer.class) + public static enum ABCMixin { } + + static class BAR{ + T value; + + public BAR(T value) { + this.value = value; + } + + @JsonValue + public T getValue() { + return value; + } + + @Override + public String toString() { + return this.getClass().getSimpleName() + + ", value:" + value + ; + } + } + + static class UCString { + private String value; + + public UCString(String v) { + value = v.toUpperCase(); + } + + @JsonValue + public String asString() { + return value; + } + } + + static class ABCKeySerializer extends JsonSerializer { + @Override + public void serialize(ABC value, JsonGenerator gen, + SerializerProvider provider) throws IOException { + gen.writeFieldName("xxx"+value); + } + } + + public static class NullKeySerializer extends JsonSerializer + { + private String _null; + public NullKeySerializer(String s) { _null = s; } + @Override + public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException { + gen.writeFieldName(_null); + } + } + + public static class NullValueSerializer extends JsonSerializer + { + private String _null; + public NullValueSerializer(String s) { _null = s; } + @Override + public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException { + gen.writeString(_null); + } + } + static class DefaultKeySerializer extends JsonSerializer { @Override @@ -54,6 +175,82 @@ public void serialize(Object value, JsonGenerator g, SerializerProvider provider final private ObjectMapper MAPPER = objectMapper(); + public void testNotKarl() throws IOException { + final String serialized = MAPPER.writeValueAsString(new NotKarlBean()); + assertEquals("{\"map\":{\"Not Karl\":1}}", serialized); + } + + public void testKarl() throws IOException { + final String serialized = MAPPER.writeValueAsString(new KarlBean()); + assertEquals("{\"map\":{\"Karl\":1}}", serialized); + } + + // [databind#75]: caching of KeySerializers + public void testBoth() throws IOException + { + // Let's NOT use shared one, to ensure caching starts from clean slate + final ObjectMapper mapper = new ObjectMapper(); + final String value1 = mapper.writeValueAsString(new NotKarlBean()); + assertEquals("{\"map\":{\"Not Karl\":1}}", value1); + final String value2 = mapper.writeValueAsString(new KarlBean()); + assertEquals("{\"map\":{\"Karl\":1}}", value2); + } + + // Test custom key serializer for enum + public void testCustomForEnum() throws IOException + { + // cannot use shared mapper as we are registering a module + final ObjectMapper mapper = new ObjectMapper(); + SimpleModule mod = new SimpleModule("test"); + mod.addKeySerializer(ABC.class, new ABCKeySerializer()); + mapper.registerModule(mod); + + String json = mapper.writeValueAsString(new ABCMapWrapper()); + assertEquals("{\"stuff\":{\"xxxB\":\"bar\"}}", json); + } + + public void testCustomNullSerializers() throws IOException + { + final ObjectMapper mapper = new ObjectMapper(); + mapper.getSerializerProvider().setNullKeySerializer(new NullKeySerializer("NULL-KEY")); + mapper.getSerializerProvider().setNullValueSerializer(new NullValueSerializer("NULL")); + Map input = new HashMap<>(); + input.put(null, 3); + String json = mapper.writeValueAsString(input); + assertEquals("{\"NULL-KEY\":3}", json); + json = mapper.writeValueAsString(new Object[] { 1, null, true }); + assertEquals("[1,\"NULL\",true]", json); + } + + public void testCustomEnumInnerMapKey() throws Exception { + Map outerMap = new HashMap(); + Map> map = new EnumMap>(ABC.class); + Map innerMap = new HashMap(); + innerMap.put("one", "1"); + map.put(ABC.A, innerMap); + outerMap.put(Outer.inner, map); + final ObjectMapper mapper = new ObjectMapper(); + SimpleModule mod = new SimpleModule("test"); + mod.setMixInAnnotation(ABC.class, ABCMixin.class); + mod.addKeySerializer(ABC.class, new ABCKeySerializer()); + mapper.registerModule(mod); + + JsonNode tree = mapper.convertValue(outerMap, JsonNode.class); + + JsonNode innerNode = tree.get("inner"); + String key = innerNode.fieldNames().next(); + assertEquals("xxxA", key); + } + + public void testDefaultKeySerializer() throws IOException + { + ObjectMapper m = new ObjectMapper(); + m.getSerializerProvider().setDefaultKeySerializer(new DefaultKeySerializer()); + Map map = new HashMap(); + map.put("a", "b"); + assertEquals("{\"DEFAULT:a\":\"b\"}", m.writeValueAsString(map)); + } + // [databind#47] public void testMapJsonValueKey47() throws Exception { @@ -73,15 +270,57 @@ public void testClassKey() throws IOException assertEquals(aposToQuotes("{'java.lang.String':2}"), json); } - public void testDefaultKeySerializer() throws IOException - { - ObjectMapper m = new ObjectMapper(); - m.getSerializerProvider().setDefaultKeySerializer(new DefaultKeySerializer()); - Map map = new HashMap(); - map.put("a", "b"); - assertEquals("{\"DEFAULT:a\":\"b\"}", m.writeValueAsString(map)); + // [databind#838] + @SuppressWarnings("deprecation") + public void testUnWrappedMapWithKeySerializer() throws Exception{ + SimpleModule mod = new SimpleModule("test"); + mod.addKeySerializer(ABC.class, new ABCKeySerializer()); + final ObjectMapper mapper = new ObjectMapper() + .registerModule(mod) + .enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT) + .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .disable(SerializationFeature.WRITE_NULL_MAP_VALUES) + .setSerializationInclusion(JsonInclude.Include.NON_EMPTY) + ; + + Map> stuff = new HashMap>(); + stuff.put(ABC.B, new BAR("bar")); + String json = mapper.writerFor(new TypeReference>>() {}) + .writeValueAsString(stuff); + assertEquals("{\"xxxB\":\"bar\"}", json); } + // [databind#838] + public void testUnWrappedMapWithDefaultType() throws Exception{ + final ObjectMapper mapper = new ObjectMapper(); + SimpleModule mod = new SimpleModule("test"); + mod.addKeySerializer(ABC.class, new ABCKeySerializer()); + mapper.registerModule(mod); + + TypeResolverBuilder typer = ObjectMapper.DefaultTypeResolverBuilder.construct( + ObjectMapper.DefaultTyping.NON_FINAL, mapper.getPolymorphicTypeValidator()); + typer = typer.init(JsonTypeInfo.Id.NAME, null); + typer = typer.inclusion(JsonTypeInfo.As.PROPERTY); + //typer = typer.typeProperty(TYPE_FIELD); + typer = typer.typeIdVisibility(true); + mapper.setDefaultTyping(typer); + + Map stuff = new HashMap(); + stuff.put(ABC.B, "bar"); + String json = mapper.writerFor(new TypeReference>() {}) + .writeValueAsString(stuff); + assertEquals("{\"@type\":\"HashMap\",\"xxxB\":\"bar\"}", json); + } + + // [databind#943] + public void testDynamicMapKeys() throws Exception + { + Map stuff = new LinkedHashMap(); + stuff.put(AbcLC.B, Integer.valueOf(3)); + stuff.put(new UCString("foo"), Integer.valueOf(4)); + String json = MAPPER.writeValueAsString(stuff); + assertEquals(aposToQuotes("{'b':3,'FOO':4}"), json); + } // [databind#1552] public void testMapsWithBinaryKeys() throws Exception {