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

Last change on this file since 1108 was 614, checked in by wouter, 15 months ago

#137 for now disabled these tests, they show some issue in jackson that we probably can work around

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