| 1 |
package wstxtest.msv; |
| 2 |
|
| 3 |
import javax.xml.stream.*; |
| 4 |
|
| 5 |
import org.codehaus.stax2.*; |
| 6 |
import org.codehaus.stax2.validation.*; |
| 7 |
|
| 8 |
import wstxtest.vstream.BaseValidationTest; |
| 9 |
|
| 10 |
/** |
| 11 |
* This is a simple base-line "smoke test" that checks that W3C Schema |
| 12 |
* validation works at least minimally. |
| 13 |
*/ |
| 14 |
public class TestW3CSchema |
| 15 |
extends BaseValidationTest |
| 16 |
{ |
| 17 |
/** |
| 18 |
* Sample schema, using sample 'personal.xsd' found from the web |
| 19 |
*/ |
| 20 |
final static String SIMPLE_NON_NS_SCHEMA = "<?xml version='1.0' encoding='UTF-8'?>\n" |
| 21 |
+ "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n" |
| 22 |
+ "<xs:element name='personnel'>\n" |
| 23 |
+ "<xs:complexType>\n" |
| 24 |
+ "<xs:sequence>\n" |
| 25 |
+ "<xs:element ref='person' minOccurs='1' maxOccurs='unbounded'/>\n" |
| 26 |
+ "</xs:sequence>\n" |
| 27 |
+ "</xs:complexType>\n" |
| 28 |
+ "<xs:unique name='unique1'>\n" |
| 29 |
+ "<xs:selector xpath='person'/>\n" |
| 30 |
+ "<xs:field xpath='name/given'/>\n" |
| 31 |
+ "<xs:field xpath='name/family'/>\n" |
| 32 |
+ "</xs:unique>\n" |
| 33 |
+ "</xs:element>\n" |
| 34 |
+ "<xs:element name='person'>\n" |
| 35 |
+ "<xs:complexType>\n" |
| 36 |
+ "<xs:sequence>\n" |
| 37 |
+ "<xs:element ref='name'/>\n" |
| 38 |
+ "<xs:element ref='email' minOccurs='0' maxOccurs='unbounded'/>\n" |
| 39 |
+ "<xs:element ref='url' minOccurs='0' maxOccurs='unbounded'/>\n" |
| 40 |
+ "<xs:element ref='link' minOccurs='0' maxOccurs='1'/>\n" |
| 41 |
+ "</xs:sequence>\n" |
| 42 |
+ "<xs:attribute name='id' type='xs:ID' use='required'/>\n" |
| 43 |
+ "<xs:attribute name='note' type='xs:string'/>\n" |
| 44 |
+ "<xs:attribute name='contr' default='false'>\n" |
| 45 |
+ "<xs:simpleType>\n" |
| 46 |
+ "<xs:restriction base = 'xs:string'>\n" |
| 47 |
+ "<xs:enumeration value='true'/>\n" |
| 48 |
+ "<xs:enumeration value='false'/>\n" |
| 49 |
+ "</xs:restriction>\n" |
| 50 |
+ "</xs:simpleType>\n" |
| 51 |
+ "</xs:attribute>\n" |
| 52 |
+ "<xs:attribute name='salary' type='xs:integer'/>\n" |
| 53 |
+ "</xs:complexType>\n" |
| 54 |
+ "</xs:element>\n" |
| 55 |
+ "<xs:element name='name'>\n" |
| 56 |
+ "<xs:complexType>\n" |
| 57 |
+ "<xs:all>\n" |
| 58 |
+ "<xs:element ref='family'/>\n" |
| 59 |
+ "<xs:element ref='given'/>\n" |
| 60 |
+ "</xs:all>\n" |
| 61 |
+ "</xs:complexType>\n" |
| 62 |
+ "</xs:element>\n" |
| 63 |
+ "<xs:element name='family' type='xs:string'/>\n" |
| 64 |
+ "<xs:element name='given' type='xs:string'/>\n" |
| 65 |
+ "<xs:element name='email' type='xs:string'/>\n" |
| 66 |
+ "<xs:element name='url'>\n" |
| 67 |
+ "<xs:complexType>\n" |
| 68 |
+ "<xs:attribute name='href' type='xs:string' default='http://'/>\n" |
| 69 |
+ "</xs:complexType>\n" |
| 70 |
+ "</xs:element>\n" |
| 71 |
|
| 72 |
+ "<xs:element name='link'>\n" |
| 73 |
+ "<xs:complexType>\n" |
| 74 |
+ "<xs:attribute name='manager' type='xs:IDREF'/>\n" |
| 75 |
+ "<xs:attribute name='subordinates' type='xs:IDREFS'/>\n" |
| 76 |
+ "</xs:complexType>\n" + "</xs:element>\n" + "</xs:schema>\n"; |
| 77 |
|
| 78 |
final static String SIMPLE_XML = "<personnel>" |
| 79 |
+ "<person id='a123' contr='true'>" + " <name>" |
| 80 |
+ "<family>Family</family><given>Fred</given>" + " </name>" |
| 81 |
+ " <url href='urn:something' />" + " </person>" |
| 82 |
+ " <person id='b12'>" |
| 83 |
+ " <name><family>Blow</family><given>Joe</given>" |
| 84 |
+ " </name>" + " <url />" + " </person>" + "</personnel>"; |
| 85 |
|
| 86 |
/** |
| 87 |
* Test validation against a simple document valid according to a very |
| 88 |
* simple W3C schema. |
| 89 |
*/ |
| 90 |
public void testSimpleNonNs() throws XMLStreamException |
| 91 |
{ |
| 92 |
XMLValidationSchema schema = parseW3CSchema(SIMPLE_NON_NS_SCHEMA); |
| 93 |
XMLStreamReader2 sr = getReader(SIMPLE_XML); |
| 94 |
sr.validateAgainst(schema); |
| 95 |
|
| 96 |
try { |
| 97 |
assertTokenType(START_ELEMENT, sr.next()); |
| 98 |
assertEquals("personnel", sr.getLocalName()); |
| 99 |
|
| 100 |
while (sr.hasNext()) { |
| 101 |
/* int type = */sr.next(); |
| 102 |
} |
| 103 |
} catch (XMLValidationException vex) { |
| 104 |
fail("Did not expect validation exception, got: " + vex); |
| 105 |
} |
| 106 |
assertTokenType(END_DOCUMENT, sr.getEventType()); |
| 107 |
} |
| 108 |
|
| 109 |
public void testSimplePartialNonNs() throws XMLStreamException |
| 110 |
{ |
| 111 |
XMLValidationSchema schema = parseW3CSchema(SIMPLE_NON_NS_SCHEMA); |
| 112 |
XMLStreamReader2 sr = getReader(SIMPLE_XML); |
| 113 |
|
| 114 |
assertTokenType(START_ELEMENT, sr.next()); |
| 115 |
assertEquals("personnel", sr.getLocalName()); |
| 116 |
sr.validateAgainst(schema); |
| 117 |
try { |
| 118 |
assertTokenType(START_ELEMENT, sr.next()); |
| 119 |
assertEquals("person", sr.getLocalName()); |
| 120 |
while (sr.hasNext()) { |
| 121 |
/* int type = */sr.next(); |
| 122 |
} |
| 123 |
} catch (XMLValidationException vex) { |
| 124 |
fail("Did not expect validation exception, got: " + vex); |
| 125 |
} |
| 126 |
assertTokenType(END_DOCUMENT, sr.getEventType()); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Test validation of a simple document that is invalid according to the |
| 131 |
* simple test schema. |
| 132 |
*/ |
| 133 |
public void testSimpleNonNsMissingId() throws XMLStreamException |
| 134 |
{ |
| 135 |
XMLValidationSchema schema = parseW3CSchema(SIMPLE_NON_NS_SCHEMA); |
| 136 |
String XML = "<personnel><person>" |
| 137 |
+ "<name><family>F</family><given>G</given>" |
| 138 |
+ "</name></person></personnel>"; |
| 139 |
verifyFailure(XML, schema, "missing id attribute", |
| 140 |
"is missing \"id\" attribute"); |
| 141 |
} |
| 142 |
|
| 143 |
public void testSimpleNonNsUndefinedId() throws XMLStreamException |
| 144 |
{ |
| 145 |
XMLValidationSchema schema = parseW3CSchema(SIMPLE_NON_NS_SCHEMA); |
| 146 |
String XML = "<personnel><person id='a1'>" |
| 147 |
+ "<name><family>F</family><given>G</given>" |
| 148 |
+ "</name><link manager='m3' /></person></personnel>"; |
| 149 |
verifyFailure(XML, schema, "undefined referenced id ('m3')", |
| 150 |
"Undefined ID 'm3'"); |
| 151 |
} |
| 152 |
|
| 153 |
public void testSimpleDataTypes() throws XMLStreamException |
| 154 |
{ |
| 155 |
// Another sample schema, from |
| 156 |
String SCHEMA = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n" |
| 157 |
+ "<xs:element name='item'>\n" |
| 158 |
+ " <xs:complexType>\n" |
| 159 |
+ " <xs:sequence>\n" |
| 160 |
+ " <xs:element name='quantity' type='xs:positiveInteger'/>" |
| 161 |
+ " <xs:element name='price' type='xs:decimal'/>" |
| 162 |
+ " </xs:sequence>" |
| 163 |
+ " </xs:complexType>" |
| 164 |
+ "</xs:element>" |
| 165 |
+ "</xs:schema>"; |
| 166 |
|
| 167 |
XMLValidationSchema schema = parseW3CSchema(SCHEMA); |
| 168 |
|
| 169 |
// First, valid doc: |
| 170 |
String XML = "<item><quantity>3 </quantity><price>\r\n4.05</price></item>"; |
| 171 |
XMLStreamReader2 sr = getReader(XML); |
| 172 |
sr.validateAgainst(schema); |
| 173 |
|
| 174 |
try { |
| 175 |
assertTokenType(START_ELEMENT, sr.next()); |
| 176 |
assertEquals("item", sr.getLocalName()); |
| 177 |
|
| 178 |
assertTokenType(START_ELEMENT, sr.next()); |
| 179 |
assertEquals("quantity", sr.getLocalName()); |
| 180 |
String str = sr.getElementText(); |
| 181 |
assertEquals("3", str.trim()); |
| 182 |
|
| 183 |
assertTokenType(START_ELEMENT, sr.next()); |
| 184 |
assertEquals("price", sr.getLocalName()); |
| 185 |
str = sr.getElementText(); |
| 186 |
assertEquals("4.05", str.trim()); |
| 187 |
|
| 188 |
assertTokenType(END_ELEMENT, sr.next()); |
| 189 |
assertTokenType(END_DOCUMENT, sr.next()); |
| 190 |
} catch (XMLValidationException vex) { |
| 191 |
fail("Did not expect validation exception, got: " + vex); |
| 192 |
} |
| 193 |
sr.close(); |
| 194 |
|
| 195 |
// Then invalid (wrong type for value) |
| 196 |
XML = "<item><quantity>34b</quantity><price>1.00</price></item>"; |
| 197 |
sr.validateAgainst(schema); |
| 198 |
verifyFailure(XML, schema, "invalid 'positive integer' datatype", |
| 199 |
"does not satisfy the \"positiveInteger\""); |
| 200 |
sr.close(); |
| 201 |
|
| 202 |
// Another invalid, empty value |
| 203 |
XML = "<item><quantity> </quantity><price>1.00</price></item>"; |
| 204 |
sr.validateAgainst(schema); |
| 205 |
// 12-Nov-2008, TSa: still having MSV bug here, need to suppress failure |
| 206 |
verifyFailure(XML, schema, "invalid (missing) positive integer value", |
| 207 |
"does not satisfy the \"positiveInteger\"", false); |
| 208 |
sr.close(); |
| 209 |
|
| 210 |
// Another invalid, missing value |
| 211 |
XML = "<item><quantity></quantity><price>1.00</price></item>"; |
| 212 |
sr.validateAgainst(schema); |
| 213 |
// 12-Nov-2008, TSa: still having MSV bug here, need to suppress failure |
| 214 |
verifyFailure(XML, schema, "invalid (missing) positive integer value", |
| 215 |
"does not satisfy the \"positiveInteger\"", false); |
| 216 |
sr.close(); |
| 217 |
} |
| 218 |
|
| 219 |
public void testSimpleText() throws XMLStreamException |
| 220 |
{ |
| 221 |
String SCHEMA = "<?xml version='1.0' encoding='utf-8' ?>\n" |
| 222 |
+ "<xs:schema elementFormDefault='qualified' xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n" |
| 223 |
+ "<xs:element name='root' type='xs:string' />" |
| 224 |
+ "</xs:schema>"; |
| 225 |
XMLValidationSchema schema = parseW3CSchema(SCHEMA); |
| 226 |
|
| 227 |
// First, 3 valid docs: |
| 228 |
String XML = "<root>xyz</root>"; |
| 229 |
XMLStreamReader2 sr = getReader(XML); |
| 230 |
sr.validateAgainst(schema); |
| 231 |
streamThrough(sr); |
| 232 |
sr.close(); |
| 233 |
|
| 234 |
XML = "<root />"; |
| 235 |
sr = getReader(XML); |
| 236 |
sr.validateAgainst(schema); |
| 237 |
streamThrough(sr); |
| 238 |
sr.close(); |
| 239 |
|
| 240 |
XML = "<root></root>"; |
| 241 |
sr = getReader(XML); |
| 242 |
sr.validateAgainst(schema); |
| 243 |
streamThrough(sr); |
| 244 |
sr.close(); |
| 245 |
|
| 246 |
// Then invalid? |
| 247 |
XML = "<foobar />"; |
| 248 |
sr = getReader(XML); |
| 249 |
sr.validateAgainst(schema); |
| 250 |
verifyFailure(XML, schema, "should warn about wrong root element", |
| 251 |
"tag name \"foobar\" is not allowed", false); |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Test for reproducing [WSTX-191] |
| 256 |
*/ |
| 257 |
public void testConstrainedText() throws XMLStreamException |
| 258 |
{ |
| 259 |
String SCHEMA = "<?xml version='1.0' encoding='UTF-8'?>\n" |
| 260 |
+ "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns='http://www.mondomaine.fr/framework'\n" |
| 261 |
+ " targetNamespace='http://www.mondomaine.fr/framework' elementFormDefault='qualified' version='1.2'>\n" |
| 262 |
+ " <xs:element name='catalog'>\n" |
| 263 |
+ " <xs:complexType>\n" |
| 264 |
+ " <xs:sequence>\n" |
| 265 |
+ " <xs:element ref='description' minOccurs='1' maxOccurs='5'/>\n" |
| 266 |
+ " </xs:sequence>\n" + " </xs:complexType>\n" |
| 267 |
+ " </xs:element>\n" |
| 268 |
+ " <xs:element name='description' nillable='true'>\n" |
| 269 |
+ " <xs:simpleType>\n" |
| 270 |
+ " <xs:restriction base='xs:string'>\n" |
| 271 |
+ " <xs:maxLength value='255'/>\n" |
| 272 |
+ " </xs:restriction>\n" + " </xs:simpleType>\n" |
| 273 |
+ " </xs:element>\n" + "</xs:schema>\n"; |
| 274 |
|
| 275 |
XMLValidationSchema schema = parseW3CSchema(SCHEMA); |
| 276 |
|
| 277 |
// first cases where there is text, and 1 to 5 descs |
| 278 |
_testValidDesc(schema, "<description>Du Texte</description>"); |
| 279 |
_testValidDesc( |
| 280 |
schema, |
| 281 |
"<description>1</description><description>2</description><description>3</description>"); |
| 282 |
_testValidDesc(schema, |
| 283 |
"<description><![CDATA[Du Texte]]></description>"); |
| 284 |
_testValidDesc(schema, |
| 285 |
"<description>??</description><description><![CDATA[...]]></description>"); |
| 286 |
_testValidDesc(schema, "<description></description>"); |
| 287 |
_testValidDesc(schema, "<description />"); |
| 288 |
_testValidDesc(schema, "<description><![CDATA[]]></description>"); |
| 289 |
} |
| 290 |
|
| 291 |
private void _testValidDesc(XMLValidationSchema schema, String descSnippet) throws XMLStreamException |
| 292 |
{ |
| 293 |
// These should all be valid according to the schema |
| 294 |
String XML = "<catalog xmlns='http://www.mondomaine.fr/framework'>" |
| 295 |
+ descSnippet + "</catalog>"; |
| 296 |
XMLStreamReader2 sr = getReader(XML); |
| 297 |
sr.validateAgainst(schema); |
| 298 |
streamThrough(sr); |
| 299 |
sr.close(); |
| 300 |
} |
| 301 |
|
| 302 |
public void testValidationHandler() throws XMLStreamException |
| 303 |
{ |
| 304 |
String SCHEMA = "<?xml version='1.0' encoding='utf-8' ?>\n" |
| 305 |
+ "<xs:schema elementFormDefault='qualified' xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n" |
| 306 |
+ "<xs:element name='root' type='xs:string' />" |
| 307 |
+ "</xs:schema>"; |
| 308 |
XMLValidationSchema schema = parseW3CSchema(SCHEMA); |
| 309 |
|
| 310 |
// Then invalid? |
| 311 |
String XML = "<foobar />"; |
| 312 |
XMLStreamReader2 sr = getReader(XML); |
| 313 |
sr.setValidationProblemHandler(new ValidationProblemHandler() { |
| 314 |
@Override |
| 315 |
public void reportProblem(XMLValidationProblem problem) |
| 316 |
throws XMLValidationException { |
| 317 |
throw new LocalValidationError(problem); |
| 318 |
} |
| 319 |
}); |
| 320 |
sr.validateAgainst(schema); |
| 321 |
boolean threw = false; |
| 322 |
try { |
| 323 |
while (sr.hasNext()) { |
| 324 |
/* int type = */sr.next(); |
| 325 |
} |
| 326 |
} catch (LocalValidationError lve) { |
| 327 |
threw = true; |
| 328 |
} |
| 329 |
assertTrue(threw); |
| 330 |
} |
| 331 |
|
| 332 |
/* |
| 333 |
/////////////////////////////////////////////////////////////////////// |
| 334 |
// Helper methods |
| 335 |
/////////////////////////////////////////////////////////////////////// |
| 336 |
*/ |
| 337 |
|
| 338 |
XMLStreamReader2 getReader(String contents) throws XMLStreamException |
| 339 |
{ |
| 340 |
XMLInputFactory2 f = getInputFactory(); |
| 341 |
setValidating(f, false); |
| 342 |
return constructStreamReader(f, contents); |
| 343 |
} |
| 344 |
|
| 345 |
/* |
| 346 |
/////////////////////////////////////////////////////////////////////// |
| 347 |
// Helper classes |
| 348 |
/////////////////////////////////////////////////////////////////////// |
| 349 |
*/ |
| 350 |
|
| 351 |
public static class LocalValidationError extends RuntimeException |
| 352 |
{ |
| 353 |
private static final long serialVersionUID = 1L; |
| 354 |
|
| 355 |
protected XMLValidationProblem problem; |
| 356 |
|
| 357 |
LocalValidationError(XMLValidationProblem problem) { |
| 358 |
this.problem = problem; |
| 359 |
} |
| 360 |
|
| 361 |
public XMLValidationProblem getProblem() { |
| 362 |
return problem; |
| 363 |
} |
| 364 |
} |
| 365 |
} |