source: dialogmanager/src/test/java/JacksonTest.java@ 430

Last change on this file since 430 was 430, checked in by wouter, 21 months ago

#137 added test showing jackson @JsonValue bug

File size: 2.7 KB
RevLine 
[429]1
2import static org.junit.Assert.assertEquals;
3
[430]4import java.io.IOException;
5
[429]6import org.junit.Test;
7
8import com.fasterxml.jackson.annotation.JsonAutoDetect;
9import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
10import com.fasterxml.jackson.annotation.JsonCreator;
11import com.fasterxml.jackson.annotation.JsonProperty;
12import com.fasterxml.jackson.annotation.JsonSubTypes;
13import com.fasterxml.jackson.annotation.JsonTypeInfo;
14import com.fasterxml.jackson.annotation.JsonValue;
15import com.fasterxml.jackson.databind.ObjectMapper;
16
17/**
18 * There are serious issues with some jackson versions. We need to check what we
19 * are up to to ensure our code works properly.
20 *
21 */
22public class JacksonTest {
23 final ObjectMapper jackson = new ObjectMapper();
24 final N n = new N();
25
26 @Test
[430]27 public void testN() throws IOException {
[429]28 doTest(new N());
29 }
30
31 @Test
[430]32 public void testYN() throws IOException {
[429]33 doTest(new Y(n));
34 }
35
36 @Test
[430]37 public void testYYN() throws IOException {
[429]38 doTest(new Y(new Y(n)));
39 }
40
41 @Test
[430]42 public void testZYN() throws IOException {
[429]43 doTest(new Z(new Y(n)));
44 }
45
46 @Test
[430]47 public void testZN() throws IOException {
[429]48 doTest(new Z(n));
49 }
50
[430]51 @Test
52 public void testWN() throws IOException {
53 doTest(new W(n));
54 }
55
56 private void doTest(X val) throws IOException {
[429]57 System.out.println("testing " + val);
58 String str = jackson.writeValueAsString(val);
59 System.out.println("To json: " + str);
60 X obj = jackson.readValue(str, X.class);
61 System.out.println("reparsed: " + obj);
62
63 assertEquals(val.toString(), obj.toString());
64
65 }
66}
67
68@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
69@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
70@JsonSubTypes({ @JsonSubTypes.Type(value = Y.class),
71 @JsonSubTypes.Type(value = Z.class),
[430]72 @JsonSubTypes.Type(value = W.class),
[429]73 @JsonSubTypes.Type(value = N.class) })
74interface X {
75}
76
77class N implements X {
78
79 @Override
80 public String toString() {
81 return "N";
82 }
83
84}
85
86class Y implements X {
87
88 // NOT @JsonValue - works
89 private final X value;
90
91 @JsonCreator
92 public Y(@JsonProperty("value") X val) {
93 this.value = val;
94 }
95
96 @Override
97 public String toString() {
98 return "Y(" + value + ")";
99 }
100
101}
102
103class Z implements X {
104 @JsonValue
105 private final X value;
106
107 @JsonCreator
[430]108 public Z(@JsonProperty("value") X value) {
[429]109 this.value = value;
110 }
111
112 @Override
113 public String toString() {
114 return "Z(" + value + ")";
115 }
116
[430]117}
118
119@JsonAutoDetect(fieldVisibility = Visibility.ANY)
120class W implements X {
121 private final X value;
122
123 @JsonCreator
124 public W(@JsonProperty("value") X value) {
125 this.value = value;
126 }
127
128 @JsonValue
129 public X getValue() {
130 return value;
131 }
132
133 @Override
134 public String toString() {
135 return "W(" + value + ")";
136 }
137
138}
Note: See TracBrowser for help on using the repository browser.