-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve unit testing for trees vs polymorphic
- Loading branch information
1 parent
397e457
commit 9b99cfe
Showing
3 changed files
with
111 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/test/java/com/fasterxml/jackson/databind/node/TestTreeWithType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
81 changes: 34 additions & 47 deletions
81
src/test/java/com/fasterxml/jackson/failing/TestTreeWithType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<SavedCookie> { | ||
@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); | ||
} | ||
} |