-
-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
144 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 37 additions & 2 deletions
39
src/test/java/com/fasterxml/jackson/dataformat/xml/vld/DTDValidationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,44 @@ | ||
package com.fasterxml.jackson.dataformat.xml.vld; | ||
|
||
import org.codehaus.stax2.XMLStreamReader2; | ||
import org.codehaus.stax2.validation.XMLValidationSchema; | ||
|
||
import static javax.xml.stream.XMLStreamConstants.*; | ||
|
||
import javax.xml.stream.XMLStreamException; | ||
|
||
// Should test validation failure too but... | ||
public class DTDValidationTest extends ValidationTestBase | ||
{ | ||
public void testSimpleNs() throws Exception | ||
final static String SIMPLE_DTD = | ||
"<!ELEMENT root (leaf+)>\n" | ||
+"<!ATTLIST root attr CDATA #REQUIRED>\n" | ||
+"<!ELEMENT leaf EMPTY>\n" | ||
; | ||
|
||
public void testFullValidationOk() throws Exception | ||
{ | ||
|
||
String XML = "<root attr='123'><leaf /></root>"; | ||
XMLValidationSchema schema = parseDTDSchema(SIMPLE_DTD); | ||
XMLStreamReader2 sr = getXMLReader(XML); | ||
sr.validateAgainst(schema); | ||
while (sr.next() != END_DOCUMENT) { } | ||
sr.close(); | ||
} | ||
|
||
public void testValidationFail() throws Exception | ||
{ | ||
String XML = "<root><leaf /></root>"; | ||
XMLValidationSchema schema = parseDTDSchema(SIMPLE_DTD); | ||
XMLStreamReader2 sr = getXMLReader(XML); | ||
sr.validateAgainst(schema); | ||
|
||
try { | ||
sr.next(); | ||
fail("Should not pass"); | ||
} catch (XMLStreamException e) { | ||
verifyException(e, "Required attribute \"attr\" missing"); | ||
} | ||
sr.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 52 additions & 1 deletion
53
src/test/java/com/fasterxml/jackson/dataformat/xml/vld/W3CSchemaValidationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,61 @@ | ||
package com.fasterxml.jackson.dataformat.xml.vld; | ||
|
||
import org.codehaus.stax2.XMLStreamReader2; | ||
import org.codehaus.stax2.validation.XMLValidationException; | ||
import org.codehaus.stax2.validation.XMLValidationSchema; | ||
|
||
import static javax.xml.stream.XMLStreamConstants.*; | ||
|
||
// Basic verification that W3C Schema validation is available | ||
public class W3CSchemaValidationTest extends ValidationTestBase | ||
{ | ||
public void testSimpleNs() throws Exception | ||
public void testSimpleDataTypes() throws Exception | ||
{ | ||
// 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 = getXMLReader(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(); | ||
} | ||
} |