Add unit tests to reproduce issues with hierarchich/nested @JsonUnwrapped with prefixes
diff --git a/src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrapping.java b/src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrapped.java
similarity index 71%
rename from src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrapping.java
rename to src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrapped.java
index 578087f..75b9df3 100644
--- a/src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrapping.java
+++ b/src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrapped.java
@@ -7,7 +7,7 @@
/**
* Unit tests for verifying [JACKSON-132] implementation.
*/
-public class TestUnwrapping extends BaseMapTest
+public class TestUnwrapped extends BaseMapTest
{
static class Unwrapping {
public String name;
@@ -21,6 +21,17 @@
}
}
+ static class DeepUnwrapping
+ {
+ @JsonUnwrapped
+ public Unwrapping unwrapped;
+
+ public DeepUnwrapping() { }
+ public DeepUnwrapping(String str, int x, int y) {
+ unwrapped = new Unwrapping(str, x, y);
+ }
+ }
+
static class UnwrappingWithCreator {
public String name;
@@ -58,42 +69,6 @@
public String first, last;
}
- static class DeepUnwrapping
- {
- @JsonUnwrapped
- public Unwrapping unwrapped;
-
- public DeepUnwrapping() { }
- public DeepUnwrapping(String str, int x, int y) {
- unwrapped = new Unwrapping(str, x, y);
- }
- }
-
- // Class with unwrapping using prefixes
- static class PrefixUnwrap
- {
- public String name;
- @JsonUnwrapped(prefix="_")
- public Location location;
-
- public PrefixUnwrap() { }
- public PrefixUnwrap(String str, int x, int y) {
- name = str;
- location = new Location(x, y);
- }
- }
-
- static class DeepPrefixUnwrap
- {
- @JsonUnwrapped(prefix="u.")
- public PrefixUnwrap unwrapped;
-
- public DeepPrefixUnwrap() { }
- public DeepPrefixUnwrap(String str, int x, int y) {
- unwrapped = new PrefixUnwrap(str, x, y);
- }
- }
-
/*
/**********************************************************
/* Tests, serialization
@@ -107,27 +82,12 @@
assertEquals("{\"name\":\"Tatu\",\"x\":1,\"y\":2}",
mapper.writeValueAsString(new Unwrapping("Tatu", 1, 2)));
}
-
- public void testPrefixedUnwrappingSerialize() throws Exception
- {
- assertEquals("{\"name\":\"Tatu\",\"_x\":1,\"_y\":2}",
- mapper.writeValueAsString(new PrefixUnwrap("Tatu", 1, 2)));
- }
-
public void testDeepUnwrappingSerialize() throws Exception
{
assertEquals("{\"name\":\"Tatu\",\"x\":1,\"y\":2}",
mapper.writeValueAsString(new DeepUnwrapping("Tatu", 1, 2)));
}
- // 13-Jan-2012, tatu: sorta kinda works; but not 100% sure it's like it
- // really SHOULD work?
- public void testDeepPrefixedUnwrappingSerialize() throws Exception
- {
- assertEquals("{\"u.name\":\"Bubba\",\"_x\":1,\"_y\":1}",
- mapper.writeValueAsString(new DeepPrefixUnwrap("Bubba", 1, 1)));
- }
-
/*
/**********************************************************
/* Tests, deserialization
@@ -182,14 +142,4 @@
assertEquals(1, loc.x);
assertEquals(2, loc.y);
}
-
- public void testPrefixedUnwrapping() throws Exception
- {
- PrefixUnwrap bean = mapper.readValue("{\"name\":\"Axel\",\"_x\":4,\"_y\":7}", PrefixUnwrap.class);
- assertNotNull(bean);
- assertEquals("Axel", bean.name);
- assertNotNull(bean.location);
- assertEquals(4, bean.location.x);
- assertEquals(7, bean.location.y);
- }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrappedWithPrefix.java b/src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrappedWithPrefix.java
new file mode 100644
index 0000000..e68b8a3
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrappedWithPrefix.java
@@ -0,0 +1,170 @@
+package com.fasterxml.jackson.databind.struct;
+
+import com.fasterxml.jackson.annotation.JsonUnwrapped;
+import com.fasterxml.jackson.databind.BaseMapTest;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+public class TestUnwrappedWithPrefix extends BaseMapTest
+{
+ static class Unwrapping {
+ public String name;
+ @JsonUnwrapped
+ public Location location;
+
+ public Unwrapping() { }
+ public Unwrapping(String str, int x, int y) {
+ name = str;
+ location = new Location(x, y);
+ }
+ }
+ static class DeepUnwrapping
+ {
+ @JsonUnwrapped
+ public Unwrapping unwrapped;
+
+ public DeepUnwrapping() { }
+ public DeepUnwrapping(String str, int x, int y) {
+ unwrapped = new Unwrapping(str, x, y);
+ }
+ }
+
+ static class Location {
+ public int x;
+ public int y;
+
+ public Location() { }
+ public Location(int x, int y) {
+ this.x = x;
+ this.y = y;
+ }
+ }
+
+ // Class with unwrapping using prefixes
+ static class PrefixUnwrap
+ {
+ public String name;
+ @JsonUnwrapped(prefix="_")
+ public Location location;
+
+ public PrefixUnwrap() { }
+ public PrefixUnwrap(String str, int x, int y) {
+ name = str;
+ location = new Location(x, y);
+ }
+ }
+
+ static class DeepPrefixUnwrap
+ {
+ @JsonUnwrapped(prefix="u.")
+ public PrefixUnwrap unwrapped;
+
+ public DeepPrefixUnwrap() { }
+ public DeepPrefixUnwrap(String str, int x, int y) {
+ unwrapped = new PrefixUnwrap(str, x, y);
+ }
+ }
+
+ // Let's actually test hierarchic names with unwrapping bit more:
+
+ static class ConfigRoot
+ {
+ @JsonUnwrapped(prefix="general.")
+ public ConfigGeneral general = new ConfigGeneral();
+
+ @JsonUnwrapped(prefix="misc.")
+ public ConfigMisc misc = new ConfigMisc();
+
+ public ConfigRoot() { }
+ public ConfigRoot(String name, int value)
+ {
+ general = new ConfigGeneral(name);
+ misc.value = value;
+ }
+ }
+
+ static class ConfigGeneral
+ {
+ @JsonUnwrapped(prefix="names.")
+ public ConfigNames names = new ConfigNames();
+
+ public ConfigGeneral() { }
+ public ConfigGeneral(String name) {
+ names.name = name;
+ }
+ }
+
+ static class ConfigNames {
+ public String name = "x";
+ }
+
+ static class ConfigMisc {
+ public int value;
+ }
+
+ // // // Reuse mapper to keep tests bit faster
+
+ private final ObjectMapper mapper = new ObjectMapper();
+
+ /*
+ /**********************************************************
+ /* Tests, serialization
+ /**********************************************************
+ */
+
+ public void testPrefixedUnwrappingSerialize() throws Exception
+ {
+ assertEquals("{\"name\":\"Tatu\",\"_x\":1,\"_y\":2}",
+ mapper.writeValueAsString(new PrefixUnwrap("Tatu", 1, 2)));
+ }
+
+ public void testDeepPrefixedUnwrappingSerialize() throws Exception
+ {
+ assertEquals("{\"u.name\":\"Bubba\",\"_x\":1,\"_y\":1}",
+ mapper.writeValueAsString(new DeepPrefixUnwrap("Bubba", 1, 1)));
+ }
+
+ public void testHierarchicConfigSerialize() throws Exception
+ {
+ assertEquals("{\"general.names.name\":\"Fred\",\"misc.value\":25}",
+ mapper.writeValueAsString(new ConfigRoot("Fred", 25)));
+ }
+
+ /*
+ /**********************************************************
+ /* Tests, deserialization
+ /**********************************************************
+ */
+
+ public void testPrefixedUnwrapping() throws Exception
+ {
+ PrefixUnwrap bean = mapper.readValue("{\"name\":\"Axel\",\"_x\":4,\"_y\":7}", PrefixUnwrap.class);
+ assertNotNull(bean);
+ assertEquals("Axel", bean.name);
+ assertNotNull(bean.location);
+ assertEquals(4, bean.location.x);
+ assertEquals(7, bean.location.y);
+ }
+
+ public void testDeepPrefixedUnwrappingDeserialize() throws Exception
+ {
+ DeepPrefixUnwrap bean = mapper.readValue("{\"u.name\":\"Bubba\",\"_x\":2,\"_y\":3}",
+ DeepPrefixUnwrap.class);
+ assertNotNull(bean.unwrapped);
+ assertEquals("Bubba", bean.unwrapped.name);
+ assertNotNull(bean.unwrapped.location);
+ assertEquals(2, bean.unwrapped.location.x);
+ assertEquals(3, bean.unwrapped.location.y);
+ }
+
+ public void testHierarchicConfigDeserialize() throws Exception
+ {
+ ConfigRoot root = mapper.readValue("{\"general.names.name\":\"Bob\",\"misc.value\":3}",
+ ConfigRoot.class);
+ assertNotNull(root.general);
+ assertNotNull(root.general.names);
+ assertEquals("Bob", root.general.names.name);
+ assertNotNull(root.misc);
+ assertEquals(3, root.misc.value);
+ }
+
+}