-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathXmlTestUtil.java
308 lines (256 loc) · 9.24 KB
/
XmlTestUtil.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package tools.jackson.dataformat.xml;
import java.io.*;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import tools.jackson.core.JsonParser;
import tools.jackson.core.testutil.JacksonTestUtilBase;
import tools.jackson.databind.AnnotationIntrospector;
import tools.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import tools.jackson.module.jakarta.xmlbind.JakartaXmlBindAnnotationIntrospector;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
public abstract class XmlTestUtil
extends JacksonTestUtilBase
{
protected static final String DEFAULT_NEW_LINE;
static {
String newLine = System.getProperty("line.separator");
DEFAULT_NEW_LINE = newLine == null ? "\n" : newLine;
}
@JsonPropertyOrder({ "first", "last", "id" })
protected static class NameBean {
@JacksonXmlProperty(isAttribute=true)
public int age;
public String last, first;
public NameBean() { }
public NameBean(int age, String f, String l) {
this.age = age;
first = f;
last = l;
}
}
/**
* Sample class from Jackson tutorial ("JacksonInFiveMinutes")
*/
public static class FiveMinuteUser {
public enum Gender { MALE, FEMALE };
public static class Name
{
private String _first, _last;
public Name() { }
public Name(String f, String l) {
_first = f;
_last = l;
}
public String getFirst() { return _first; }
public String getLast() { return _last; }
public void setFirst(String s) { _first = s; }
public void setLast(String s) { _last = s; }
@Override
public boolean equals(Object o)
{
if (o == this) return true;
if (o == null || o.getClass() != getClass()) return false;
Name other = (Name) o;
return _first.equals(other._first) && _last.equals(other._last);
}
}
private Gender _gender;
private Name _name;
private boolean _isVerified;
private byte[] _userImage;
public FiveMinuteUser() { }
public FiveMinuteUser(String first, String last, boolean verified, Gender g, byte[] data)
{
_name = new Name(first, last);
_isVerified = verified;
_gender = g;
_userImage = data;
}
public Name getName() { return _name; }
public boolean isVerified() { return _isVerified; }
public Gender getGender() { return _gender; }
public byte[] getUserImage() { return _userImage; }
public void setName(Name n) { _name = n; }
public void setVerified(boolean b) { _isVerified = b; }
public void setGender(Gender g) { _gender = g; }
public void setUserImage(byte[] b) { _userImage = b; }
@Override
public boolean equals(Object o)
{
if (o == this) return true;
if (o == null || o.getClass() != getClass()) return false;
FiveMinuteUser other = (FiveMinuteUser) o;
if (_isVerified != other._isVerified) return false;
if (_gender != other._gender) return false;
if (!_name.equals(other._name)) return false;
byte[] otherImage = other._userImage;
if (otherImage.length != _userImage.length) return false;
for (int i = 0, len = _userImage.length; i < len; ++i) {
if (_userImage[i] != otherImage[i]) {
return false;
}
}
return true;
}
}
protected static class StringBean
{
public String text;
public StringBean() { this("foobar"); }
public StringBean(String s) { text = s; }
@Override
public String toString() {
if (text == null) return "NULL";
return "\""+text+"\"";
}
}
/**
* Simple wrapper around String type, usually to test value
* conversions or wrapping
*/
protected static class StringWrapper {
public String str;
public StringWrapper() { }
public StringWrapper(String value) {
str = value;
}
}
protected static class IntWrapper {
public int i;
public IntWrapper() { }
public IntWrapper(int value) {
i = value;
}
}
public static class Point {
public int x, y;
protected Point() { } // for deser
public Point(int x0, int y0) {
x = x0;
y = y0;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Point)) {
return false;
}
Point other = (Point) o;
return (other.x == x) && (other.y == y);
}
@Override
public String toString() {
return String.format("[x=%d, y=%d]", x, y);
}
}
/*
/**********************************************************
/* Some sample documents:
/**********************************************************
*/
protected final static int SAMPLE_SPEC_VALUE_WIDTH = 800;
protected final static int SAMPLE_SPEC_VALUE_HEIGHT = 600;
protected final static String SAMPLE_SPEC_VALUE_TITLE = "View from 15th Floor";
protected final static String SAMPLE_SPEC_VALUE_TN_URL = "http://www.example.com/image/481989943";
protected final static int SAMPLE_SPEC_VALUE_TN_HEIGHT = 125;
protected final static String SAMPLE_SPEC_VALUE_TN_WIDTH = "100";
protected final static int SAMPLE_SPEC_VALUE_TN_ID1 = 116;
protected final static int SAMPLE_SPEC_VALUE_TN_ID2 = 943;
protected final static int SAMPLE_SPEC_VALUE_TN_ID3 = 234;
protected final static int SAMPLE_SPEC_VALUE_TN_ID4 = 38793;
protected final static String SAMPLE_DOC_JSON_SPEC =
"{\n"
+" \"Image\" : {\n"
+" \"Width\" : "+SAMPLE_SPEC_VALUE_WIDTH+",\n"
+" \"Height\" : "+SAMPLE_SPEC_VALUE_HEIGHT+","
+"\"Title\" : \""+SAMPLE_SPEC_VALUE_TITLE+"\",\n"
+" \"Thumbnail\" : {\n"
+" \"Url\" : \""+SAMPLE_SPEC_VALUE_TN_URL+"\",\n"
+"\"Height\" : "+SAMPLE_SPEC_VALUE_TN_HEIGHT+",\n"
+" \"Width\" : \""+SAMPLE_SPEC_VALUE_TN_WIDTH+"\"\n"
+" },\n"
+" \"IDs\" : ["+SAMPLE_SPEC_VALUE_TN_ID1+","+SAMPLE_SPEC_VALUE_TN_ID2+","+SAMPLE_SPEC_VALUE_TN_ID3+","+SAMPLE_SPEC_VALUE_TN_ID4+"]\n"
+" }"
+"}"
;
/*
/**********************************************************
/* Construction, factory methods
/**********************************************************
*/
protected XmlTestUtil() {
super();
}
protected XmlFactoryBuilder streamFactoryBuilder() {
return XmlFactory.builder();
}
protected static XmlMapper newMapper() {
return new XmlMapper();
}
protected static XmlMapper.Builder mapperBuilder() {
return XmlMapper.builder();
}
protected static XmlMapper.Builder mapperBuilder(XmlFactory f) {
return XmlMapper.builder(f);
}
protected XmlMapper xmlMapper(boolean useListWrapping)
{
return XmlMapper.builder()
.defaultUseWrapper(useListWrapping)
.build();
}
protected AnnotationIntrospector jakartaXMLBindAnnotationIntrospector() {
return new JakartaXmlBindAnnotationIntrospector();
}
/*
/**********************************************************
/* Additional assertion methods
/**********************************************************
*/
protected void verifyFieldName(JsonParser jp, String expName)
throws IOException
{
assertEquals(expName, jp.getString());
assertEquals(expName, jp.currentName());
}
/*
/**********************************************************
/* Helper methods, other
/**********************************************************
*/
/**
* Helper method that tries to remove unnecessary namespace
* declaration that default JDK XML parser (SJSXP) sees fit
* to add.
*/
protected static String removeSjsxpNamespace(String xml)
{
final String match = " xmlns=\"\"";
int ix = xml.indexOf(match);
if (ix > 0) {
xml = xml.substring(0, ix) + xml.substring(ix+match.length());
}
return xml;
}
protected String readAll(File f) throws IOException
{
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
br.close();
return sb.toString();
}
public String jaxbSerialized(Object ob, Class<?>... classes) throws Exception
{
StringWriter sw = new StringWriter();
if (classes.length == 0) {
jakarta.xml.bind.JAXB.marshal(ob, sw);
} else {
jakarta.xml.bind.JAXBContext.newInstance(classes).createMarshaller().marshal(ob, sw);
}
sw.close();
return sw.toString();
}
}