-
Notifications
You must be signed in to change notification settings - Fork 100
/
JSONDataWriterTest.java
183 lines (148 loc) · 5.24 KB
/
JSONDataWriterTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package org.kohsuke.stapler.export;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;
import net.sf.json.JSONObject;
import org.junit.Ignore;
import org.junit.Test;
public class JSONDataWriterTest {
private ExportConfig config =
new ExportConfig().withFlavor(Flavor.JSON).withClassAttribute(ClassAttributeBehaviour.IF_NEEDED.simple());
private <T> String serialize(T bean, Class<T> clazz) throws IOException {
StringWriter w = new StringWriter();
Model<T> model = new ModelBuilder().get(clazz);
model.writeTo(bean, Flavor.JSON.createDataWriter(bean, w, config));
return w.toString();
}
@ExportedBean
public static class X {
@Exported
public String a = "aval";
public String b = "bval";
@Exported
public String getC() {
return "cval";
}
public String getD() {
return "dval";
}
}
@Test
public void testSimpleUsage() throws Exception {
assertEquals("{\"_class\":\"X\",\"a\":\"aval\",\"c\":\"cval\"}", serialize(new X(), X.class));
}
@ExportedBean(defaultVisibility = 2)
public abstract static class Super {
@Exported
public String basic = "super";
@Exported
public abstract String generic();
}
public static class Sub extends Super {
@Override
public String generic() {
return "sub";
}
@Exported
public String specific() {
return "sub";
}
}
@ExportedBean
public static class Container {
@Exported
public Super polymorph = new Sub();
}
@Test
public void testInheritance() throws Exception {
assertEquals(
"{\"_class\":\"Container\",\"polymorph\":{\"_class\":\"Sub\",\"basic\":\"super\",\"generic\":\"sub\",\"specific\":\"sub\"}}",
serialize(new Container(), Container.class));
}
public static class Sub2 extends Super {
@Exported
@Override
public String generic() {
return "sub2";
}
}
@Test
public void testEncodedChars() throws Exception {
assertEquals(
"{\"_class\":\"Encoded\",\"bar\":\"\\ud834\\udd1e\",\"foo\":\"\\u0000\"}",
serialize(new Encoded(), Encoded.class));
}
@Test
public void testInheritance2() throws Exception { // JENKINS-13336
assertEquals(
"{\"_class\":\"Sub2\",\"basic\":\"super\",\"generic\":\"sub2\"}", serialize(new Sub2(), Sub2.class));
}
@Test
public void exceptionHandling() throws IOException {
assertEquals(
"{\"_class\":\"Supers\",\"elements\":[{\"_class\":\"Sub\",\"basic\":\"super\",\"generic\":\"sub\",\"specific\":\"sub\"},{\"_class\":\"Sub2\",\"basic\":\"super\",\"generic\":\"sub2\"}]}",
serialize(new Supers(new Sub(), new Broken(), new Sub2()), Supers.class));
}
public static class Broken extends Super {
@Exported
@Override
public String generic() {
throw new RuntimeException("oops");
}
}
@ExportedBean
public static class Supers {
@Exported
public final List<? extends Super> elements;
public Supers(Super... elements) {
this.elements = Arrays.asList(elements);
}
}
@Test
public void JSONObjectWithoutNullValue() throws IOException {
String json = "{'key':'value', 'optional':'present'}";
StringWriter w = new StringWriter();
ModelWithJsonField jsonModel = new ModelWithJsonField(json);
DataWriter writer = Flavor.JSON.createDataWriter(jsonModel, w);
Model<ModelWithJsonField> model = new ModelBuilder().get(ModelWithJsonField.class);
model.writeTo(jsonModel, writer);
assertThat("not a correct representation of " + json, w.toString(), containsString("present"));
}
@Test
@Ignore
public void JSONObjectWithNullValue() throws IOException {
String json = "{'key':'value', 'optional':null}";
StringWriter w = new StringWriter();
ModelWithJsonField jsonModel = new ModelWithJsonField(json);
DataWriter writer = Flavor.JSON.createDataWriter(jsonModel, w);
Model<ModelWithJsonField> model = new ModelBuilder().get(ModelWithJsonField.class);
model.writeTo(jsonModel, writer);
assertThat("not a correct representation of " + json, w.toString(), containsString("null"));
}
@ExportedBean
public static class ModelWithJsonField {
private final JSONObject json;
public ModelWithJsonField(String jsonAsString) {
json = JSONObject.fromObject(jsonAsString.replace('\'', '"'));
}
@Exported(visibility = 2)
public JSONObject getJson() {
return json;
}
}
@ExportedBean
public static class Encoded {
@Exported
public String getFoo() {
return "\u0000";
}
@Exported
public String getBar() {
return "\uD834\uDD1E";
}
}
}