From 9b99cfebd36494b43fc71d821a9b7874ab636776 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Fri, 14 Mar 2014 12:44:37 -0700 Subject: [PATCH] Improve unit testing for trees vs polymorphic --- .../jackson/databind/ObjectMapper.java | 2 +- .../databind/node/TestTreeWithType.java | 76 +++++++++++++++++ .../jackson/failing/TestTreeWithType.java | 81 ++++++++----------- 3 files changed, 111 insertions(+), 48 deletions(-) create mode 100644 src/test/java/com/fasterxml/jackson/databind/node/TestTreeWithType.java diff --git a/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java b/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java index 63ec3a9f34..257380488b 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java +++ b/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java @@ -222,7 +222,7 @@ public boolean useForType(JavaType t) TimeZone.getTimeZone("GMT"), Base64Variants.getDefaultVariant() // 2.1 ); - + /* /********************************************************** /* Configuration settings, shared diff --git a/src/test/java/com/fasterxml/jackson/databind/node/TestTreeWithType.java b/src/test/java/com/fasterxml/jackson/databind/node/TestTreeWithType.java new file mode 100644 index 0000000000..c6a60369a1 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/databind/node/TestTreeWithType.java @@ -0,0 +1,76 @@ +package com.fasterxml.jackson.databind.node; + +import java.io.IOException; + +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.core.*; +import com.fasterxml.jackson.databind.*; + +public class TestTreeWithType extends BaseMapTest +{ + public static class Foo { + public String bar; + + public Foo() { } + + public Foo(String bar) { + this.bar = bar; + } + } + + /* + /********************************************************** + /* Unit tests + /********************************************************** + */ + + private final ObjectMapper MAPPER = new ObjectMapper(); + + public void testValueAsStringWithoutDefaultTyping() throws Exception { + + Foo foo = new Foo("baz"); + String json = MAPPER.writeValueAsString(foo); + + JsonNode jsonNode = MAPPER.readTree(json); + assertEquals(jsonNode.get("bar").textValue(), foo.bar); + } + + public void testValueAsStringWithDefaultTyping() throws Exception { + final ObjectMapper mapper = new ObjectMapper(); + mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); + + Foo foo = new Foo("baz"); + String json = mapper.writeValueAsString(foo); + + JsonNode jsonNode = mapper.readTree(json); + assertEquals(jsonNode.get("bar").textValue(), foo.bar); + } + + public void testReadTreeWithDefaultTyping() throws Exception + { + final String CLASS = Foo.class.getName(); + + final ObjectMapper mapper = new ObjectMapper(); + mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, + JsonTypeInfo.As.PROPERTY); + String json = "{\"@class\":\""+CLASS+"\",\"bar\":\"baz\"}"; + JsonNode jsonNode = mapper.readTree(json); + assertEquals(jsonNode.get("bar").textValue(), "baz"); + } + + public void testValueToTreeWithoutDefaultTyping() throws Exception { + + Foo foo = new Foo("baz"); + JsonNode jsonNode = MAPPER.valueToTree(foo); + assertEquals(jsonNode.get("bar").textValue(), foo.bar); + } + + public void testValueToTreeWithDefaultTyping() throws Exception { + final ObjectMapper mapper = new ObjectMapper(); + mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); + + Foo foo = new Foo("baz"); + JsonNode jsonNode = mapper.valueToTree(foo); + assertEquals(jsonNode.get("bar").textValue(), foo.bar); + } +} diff --git a/src/test/java/com/fasterxml/jackson/failing/TestTreeWithType.java b/src/test/java/com/fasterxml/jackson/failing/TestTreeWithType.java index 58427922cc..5b91470a1a 100644 --- a/src/test/java/com/fasterxml/jackson/failing/TestTreeWithType.java +++ b/src/test/java/com/fasterxml/jackson/failing/TestTreeWithType.java @@ -1,70 +1,57 @@ package com.fasterxml.jackson.failing; -import com.fasterxml.jackson.annotation.JsonTypeInfo; +import java.io.IOException; + +import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.databind.module.SimpleModule; public class TestTreeWithType extends BaseMapTest { - public static class Foo { - public String bar; - public Foo() { } + // [Issue#353] + public class SavedCookie { + public String name, value; - public Foo(String bar) { - this.bar = bar; + public SavedCookie() { } + public SavedCookie(String n, String v) { + name = n; + value = v; } } - + + public class SavedCookieDeserializer extends JsonDeserializer { + @Override + public SavedCookie deserialize(JsonParser jsonParser, DeserializationContext ctxt) + throws IOException { + ObjectCodec oc = jsonParser.getCodec(); + JsonNode node = oc.readTree(jsonParser); + return new SavedCookie(node.path("name").textValue(), + node.path("value").textValue()); + } + } /* /********************************************************** /* Unit tests /********************************************************** */ - private final ObjectMapper mapper = new ObjectMapper(); - - public void testValueAsStringWithoutDefaultTyping() throws Exception { - - Foo foo = new Foo("baz"); - String json = mapper.writeValueAsString(foo); - - JsonNode jsonNode = mapper.readTree(json); - assertEquals(jsonNode.get("bar").textValue(), foo.bar); - } - - public void testValueAsStringWithDefaultTyping() throws Exception { - mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); - - Foo foo = new Foo("baz"); - String json = mapper.writeValueAsString(foo); - - JsonNode jsonNode = mapper.readTree(json); - assertEquals(jsonNode.get("bar").textValue(), foo.bar); - } - - public void testReadTreeWithDefaultTyping() throws Exception + public void testIssue353() throws Exception { - final String CLASS = Foo.class.getName(); + ObjectMapper mapper = new ObjectMapper(); - mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, - JsonTypeInfo.As.PROPERTY); - String json = "{\"@class\":\""+CLASS+"\",\"bar\":\"baz\"}"; - JsonNode jsonNode = mapper.readTree(json); - assertEquals(jsonNode.get("bar").textValue(), "baz"); - } - - public void testValueToTreeWithoutDefaultTyping() throws Exception { + mapper.enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.NON_FINAL, "@class"); - Foo foo = new Foo("baz"); - JsonNode jsonNode = mapper.valueToTree(foo); - assertEquals(jsonNode.get("bar").textValue(), foo.bar); - } + SimpleModule testModule = new SimpleModule("MyModule", new Version(1, 0, 0, null, "TEST", "TEST")); + testModule.addDeserializer(SavedCookie.class, new SavedCookieDeserializer()); + mapper.registerModule(testModule); - public void testValueToTreeWithDefaultTyping() throws Exception { - mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); + SavedCookie savedCookie = new SavedCookie("key", "v"); + String json = mapper.writeValueAsString(savedCookie); + + SavedCookie out = mapper.reader(SavedCookie.class).readValue(json); - Foo foo = new Foo("baz"); - JsonNode jsonNode = mapper.valueToTree(foo); - assertEquals(jsonNode.get("bar").textValue(), foo.bar); + assertEquals("key", out.name); + assertEquals("v", out.value); } }