-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathTestW3CSchema.java
365 lines (324 loc) · 14.2 KB
/
TestW3CSchema.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package wstxtest.msv;
import javax.xml.stream.*;
import org.codehaus.stax2.*;
import org.codehaus.stax2.validation.*;
import wstxtest.vstream.BaseValidationTest;
/**
* This is a simple base-line "smoke test" that checks that W3C Schema
* validation works at least minimally.
*/
public class TestW3CSchema
extends BaseValidationTest
{
/**
* Sample schema, using sample 'personal.xsd' found from the web
*/
final static String SIMPLE_NON_NS_SCHEMA = "<?xml version='1.0' encoding='UTF-8'?>\n"
+ "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n"
+ "<xs:element name='personnel'>\n"
+ "<xs:complexType>\n"
+ "<xs:sequence>\n"
+ "<xs:element ref='person' minOccurs='1' maxOccurs='unbounded'/>\n"
+ "</xs:sequence>\n"
+ "</xs:complexType>\n"
+ "<xs:unique name='unique1'>\n"
+ "<xs:selector xpath='person'/>\n"
+ "<xs:field xpath='name/given'/>\n"
+ "<xs:field xpath='name/family'/>\n"
+ "</xs:unique>\n"
+ "</xs:element>\n"
+ "<xs:element name='person'>\n"
+ "<xs:complexType>\n"
+ "<xs:sequence>\n"
+ "<xs:element ref='name'/>\n"
+ "<xs:element ref='email' minOccurs='0' maxOccurs='unbounded'/>\n"
+ "<xs:element ref='url' minOccurs='0' maxOccurs='unbounded'/>\n"
+ "<xs:element ref='link' minOccurs='0' maxOccurs='1'/>\n"
+ "</xs:sequence>\n"
+ "<xs:attribute name='id' type='xs:ID' use='required'/>\n"
+ "<xs:attribute name='note' type='xs:string'/>\n"
+ "<xs:attribute name='contr' default='false'>\n"
+ "<xs:simpleType>\n"
+ "<xs:restriction base = 'xs:string'>\n"
+ "<xs:enumeration value='true'/>\n"
+ "<xs:enumeration value='false'/>\n"
+ "</xs:restriction>\n"
+ "</xs:simpleType>\n"
+ "</xs:attribute>\n"
+ "<xs:attribute name='salary' type='xs:integer'/>\n"
+ "</xs:complexType>\n"
+ "</xs:element>\n"
+ "<xs:element name='name'>\n"
+ "<xs:complexType>\n"
+ "<xs:all>\n"
+ "<xs:element ref='family'/>\n"
+ "<xs:element ref='given'/>\n"
+ "</xs:all>\n"
+ "</xs:complexType>\n"
+ "</xs:element>\n"
+ "<xs:element name='family' type='xs:string'/>\n"
+ "<xs:element name='given' type='xs:string'/>\n"
+ "<xs:element name='email' type='xs:string'/>\n"
+ "<xs:element name='url'>\n"
+ "<xs:complexType>\n"
+ "<xs:attribute name='href' type='xs:string' default='http://'/>\n"
+ "</xs:complexType>\n"
+ "</xs:element>\n"
+ "<xs:element name='link'>\n"
+ "<xs:complexType>\n"
+ "<xs:attribute name='manager' type='xs:IDREF'/>\n"
+ "<xs:attribute name='subordinates' type='xs:IDREFS'/>\n"
+ "</xs:complexType>\n" + "</xs:element>\n" + "</xs:schema>\n";
final static String SIMPLE_XML = "<personnel>"
+ "<person id='a123' contr='true'>" + " <name>"
+ "<family>Family</family><given>Fred</given>" + " </name>"
+ " <url href='urn:something' />" + " </person>"
+ " <person id='b12'>"
+ " <name><family>Blow</family><given>Joe</given>"
+ " </name>" + " <url />" + " </person>" + "</personnel>";
/**
* Test validation against a simple document valid according to a very
* simple W3C schema.
*/
public void testSimpleNonNs() throws XMLStreamException
{
XMLValidationSchema schema = parseW3CSchema(SIMPLE_NON_NS_SCHEMA);
XMLStreamReader2 sr = getReader(SIMPLE_XML);
sr.validateAgainst(schema);
try {
assertTokenType(START_ELEMENT, sr.next());
assertEquals("personnel", sr.getLocalName());
while (sr.hasNext()) {
/* int type = */sr.next();
}
} catch (XMLValidationException vex) {
fail("Did not expect validation exception, got: " + vex);
}
assertTokenType(END_DOCUMENT, sr.getEventType());
}
public void testSimplePartialNonNs() throws XMLStreamException
{
XMLValidationSchema schema = parseW3CSchema(SIMPLE_NON_NS_SCHEMA);
XMLStreamReader2 sr = getReader(SIMPLE_XML);
assertTokenType(START_ELEMENT, sr.next());
assertEquals("personnel", sr.getLocalName());
sr.validateAgainst(schema);
try {
assertTokenType(START_ELEMENT, sr.next());
assertEquals("person", sr.getLocalName());
while (sr.hasNext()) {
/* int type = */sr.next();
}
} catch (XMLValidationException vex) {
fail("Did not expect validation exception, got: " + vex);
}
assertTokenType(END_DOCUMENT, sr.getEventType());
}
/**
* Test validation of a simple document that is invalid according to the
* simple test schema.
*/
public void testSimpleNonNsMissingId() throws XMLStreamException
{
XMLValidationSchema schema = parseW3CSchema(SIMPLE_NON_NS_SCHEMA);
String XML = "<personnel><person>"
+ "<name><family>F</family><given>G</given>"
+ "</name></person></personnel>";
verifyFailure(XML, schema, "missing id attribute",
"is missing \"id\" attribute");
}
public void testSimpleNonNsUndefinedId() throws XMLStreamException
{
XMLValidationSchema schema = parseW3CSchema(SIMPLE_NON_NS_SCHEMA);
String XML = "<personnel><person id='a1'>"
+ "<name><family>F</family><given>G</given>"
+ "</name><link manager='m3' /></person></personnel>";
verifyFailure(XML, schema, "undefined referenced id ('m3')",
"Undefined ID 'm3'");
}
public void testSimpleDataTypes() throws XMLStreamException
{
// Another sample schema, from
String SCHEMA = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n"
+ "<xs:element name='item'>\n"
+ " <xs:complexType>\n"
+ " <xs:sequence>\n"
+ " <xs:element name='quantity' type='xs:positiveInteger'/>"
+ " <xs:element name='price' type='xs:decimal'/>"
+ " </xs:sequence>"
+ " </xs:complexType>"
+ "</xs:element>"
+ "</xs:schema>";
XMLValidationSchema schema = parseW3CSchema(SCHEMA);
// First, valid doc:
String XML = "<item><quantity>3 </quantity><price>\r\n4.05</price></item>";
XMLStreamReader2 sr = getReader(XML);
sr.validateAgainst(schema);
try {
assertTokenType(START_ELEMENT, sr.next());
assertEquals("item", sr.getLocalName());
assertTokenType(START_ELEMENT, sr.next());
assertEquals("quantity", sr.getLocalName());
String str = sr.getElementText();
assertEquals("3", str.trim());
assertTokenType(START_ELEMENT, sr.next());
assertEquals("price", sr.getLocalName());
str = sr.getElementText();
assertEquals("4.05", str.trim());
assertTokenType(END_ELEMENT, sr.next());
assertTokenType(END_DOCUMENT, sr.next());
} catch (XMLValidationException vex) {
fail("Did not expect validation exception, got: " + vex);
}
sr.close();
// Then invalid (wrong type for value)
XML = "<item><quantity>34b</quantity><price>1.00</price></item>";
sr.validateAgainst(schema);
verifyFailure(XML, schema, "invalid 'positive integer' datatype",
"does not satisfy the \"positiveInteger\"");
sr.close();
// Another invalid, empty value
XML = "<item><quantity> </quantity><price>1.00</price></item>";
sr.validateAgainst(schema);
// 12-Nov-2008, TSa: still having MSV bug here, need to suppress failure
verifyFailure(XML, schema, "invalid (missing) positive integer value",
"does not satisfy the \"positiveInteger\"", false);
sr.close();
// Another invalid, missing value
XML = "<item><quantity></quantity><price>1.00</price></item>";
sr.validateAgainst(schema);
// 12-Nov-2008, TSa: still having MSV bug here, need to suppress failure
verifyFailure(XML, schema, "invalid (missing) positive integer value",
"does not satisfy the \"positiveInteger\"", false);
sr.close();
}
public void testSimpleText() throws XMLStreamException
{
String SCHEMA = "<?xml version='1.0' encoding='utf-8' ?>\n"
+ "<xs:schema elementFormDefault='qualified' xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n"
+ "<xs:element name='root' type='xs:string' />"
+ "</xs:schema>";
XMLValidationSchema schema = parseW3CSchema(SCHEMA);
// First, 3 valid docs:
String XML = "<root>xyz</root>";
XMLStreamReader2 sr = getReader(XML);
sr.validateAgainst(schema);
streamThrough(sr);
sr.close();
XML = "<root />";
sr = getReader(XML);
sr.validateAgainst(schema);
streamThrough(sr);
sr.close();
XML = "<root></root>";
sr = getReader(XML);
sr.validateAgainst(schema);
streamThrough(sr);
sr.close();
// Then invalid?
XML = "<foobar />";
sr = getReader(XML);
sr.validateAgainst(schema);
verifyFailure(XML, schema, "should warn about wrong root element",
"tag name \"foobar\" is not allowed", false);
}
/**
* Test for reproducing [WSTX-191]
*/
public void testConstrainedText() throws XMLStreamException
{
String SCHEMA = "<?xml version='1.0' encoding='UTF-8'?>\n"
+ "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns='http://www.mondomaine.fr/framework'\n"
+ " targetNamespace='http://www.mondomaine.fr/framework' elementFormDefault='qualified' version='1.2'>\n"
+ " <xs:element name='catalog'>\n"
+ " <xs:complexType>\n"
+ " <xs:sequence>\n"
+ " <xs:element ref='description' minOccurs='1' maxOccurs='5'/>\n"
+ " </xs:sequence>\n" + " </xs:complexType>\n"
+ " </xs:element>\n"
+ " <xs:element name='description' nillable='true'>\n"
+ " <xs:simpleType>\n"
+ " <xs:restriction base='xs:string'>\n"
+ " <xs:maxLength value='255'/>\n"
+ " </xs:restriction>\n" + " </xs:simpleType>\n"
+ " </xs:element>\n" + "</xs:schema>\n";
XMLValidationSchema schema = parseW3CSchema(SCHEMA);
// first cases where there is text, and 1 to 5 descs
_testValidDesc(schema, "<description>Du Texte</description>");
_testValidDesc(
schema,
"<description>1</description><description>2</description><description>3</description>");
_testValidDesc(schema,
"<description><![CDATA[Du Texte]]></description>");
_testValidDesc(schema,
"<description>??</description><description><![CDATA[...]]></description>");
_testValidDesc(schema, "<description></description>");
_testValidDesc(schema, "<description />");
_testValidDesc(schema, "<description><![CDATA[]]></description>");
}
private void _testValidDesc(XMLValidationSchema schema, String descSnippet) throws XMLStreamException
{
// These should all be valid according to the schema
String XML = "<catalog xmlns='http://www.mondomaine.fr/framework'>"
+ descSnippet + "</catalog>";
XMLStreamReader2 sr = getReader(XML);
sr.validateAgainst(schema);
streamThrough(sr);
sr.close();
}
public void testValidationHandler() throws XMLStreamException
{
String SCHEMA = "<?xml version='1.0' encoding='utf-8' ?>\n"
+ "<xs:schema elementFormDefault='qualified' xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n"
+ "<xs:element name='root' type='xs:string' />"
+ "</xs:schema>";
XMLValidationSchema schema = parseW3CSchema(SCHEMA);
// Then invalid?
String XML = "<foobar />";
XMLStreamReader2 sr = getReader(XML);
sr.setValidationProblemHandler(new ValidationProblemHandler() {
@Override
public void reportProblem(XMLValidationProblem problem)
throws XMLValidationException {
throw new LocalValidationError(problem);
}
});
sr.validateAgainst(schema);
boolean threw = false;
try {
while (sr.hasNext()) {
/* int type = */sr.next();
}
} catch (LocalValidationError lve) {
threw = true;
}
assertTrue(threw);
}
/*
///////////////////////////////////////////////////////////////////////
// Helper methods
///////////////////////////////////////////////////////////////////////
*/
XMLStreamReader2 getReader(String contents) throws XMLStreamException
{
XMLInputFactory2 f = getInputFactory();
setValidating(f, false);
return constructStreamReader(f, contents);
}
/*
///////////////////////////////////////////////////////////////////////
// Helper classes
///////////////////////////////////////////////////////////////////////
*/
public static class LocalValidationError extends RuntimeException
{
private static final long serialVersionUID = 1L;
protected XMLValidationProblem problem;
LocalValidationError(XMLValidationProblem problem) {
this.problem = problem;
}
public XMLValidationProblem getProblem() {
return problem;
}
}
}