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

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

#137 added test for jackson

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