Skip to content

Commit

Permalink
Improve unit testing for trees vs polymorphic
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 14, 2014
1 parent 397e457 commit 9b99cfe
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public boolean useForType(JavaType t)
TimeZone.getTimeZone("GMT"),
Base64Variants.getDefaultVariant() // 2.1
);

/*
/**********************************************************
/* Configuration settings, shared
Expand Down
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 src/test/java/com/fasterxml/jackson/failing/TestTreeWithType.java
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);
}
}

0 comments on commit 9b99cfe

Please sign in to comment.