Skip to content

Commit

Permalink
Merge branch '2.9' into 2.10
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jul 15, 2019
2 parents 6f7159c + 7750c17 commit 38775fb
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Some data-binding types overridden as well (ObjectMapper sub-classed as XmlMappe
<dependency>
<groupId>com.fasterxml.woodstox</groupId>
<artifactId>woodstox-core</artifactId>
<version>5.2.1</version>
<version>5.3.0</version>
<exclusions>
<exclusion>
<groupId>javax.xml.stream</groupId>
Expand Down
3 changes: 2 additions & 1 deletion release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Project: jackson-dataformat-xml

2.9.10 (not yet released)

#340: Incompatible woodstox-core and stax2-api dependencies
#340: Incompatible woodstox-core and stax2-api dependencies (upgrade to
`woodstox-core` 5.3.0)

2.9.9 (16-May-2019)

Expand Down
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();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fasterxml.jackson.dataformat.xml.vld;

import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;

import org.codehaus.stax2.XMLStreamReader2;
import org.codehaus.stax2.validation.XMLValidationException;
Expand All @@ -24,7 +25,7 @@ public class RelaxNGValidationTest extends ValidationTestBase
+"</element>"
;

public void testSimpleNs() throws Exception
public void testValidWithNamespace() throws Exception
{
String XML = "<root>\n"
+" <myns:leaf xmlns:myns='http://test' attr1='123' />\n"
Expand All @@ -49,4 +50,54 @@ public void testSimpleNs() throws Exception

assertTokenType(XMLStreamConstants.END_DOCUMENT, sr.getEventType());
}

public void testInvalidWithNamespace()
throws Exception
{
XMLValidationSchema schema = parseRngSchema(SIMPLE_RNG_NS_SCHEMA);

// First, wrong root element:
String XML = "<root xmlns='http://test'>\n"
+"<leaf />\n"
+"</root>";
verifyRngFailure(XML, schema, "wrong root element",
"namespace URI of tag \"root\" is wrong");

// Wrong child namespace
XML = "<root>\n"
+"<leaf xmlns='http://other' />\n"
+"</root>";
verifyRngFailure(XML, schema, "wrong child element namespace",
"namespace URI of tag \"leaf\" is wrong.");

// Wrong attribute namespace
XML = "<root>\n"
+"<ns:leaf xmlns:ns='http://test' ns:attr1='123' />\n"
+"</root>";
verifyRngFailure(XML, schema, "wrong attribute namespace",
"unexpected attribute \"attr1\"");
}

private void verifyRngFailure(String xml, XMLValidationSchema schema, String failMsg, String failPhrase)
throws Exception
{
XMLStreamReader2 sr = getXMLReader(xml);
sr.validateAgainst(schema);
try {
while (sr.hasNext()) {
/*int type =*/ sr.next();
}
fail("Expected validity exception for "+failMsg);
} catch (XMLValidationException vex) {
String origMsg = vex.getMessage();
String msg = (origMsg == null) ? "" : origMsg.toLowerCase();
if (msg.indexOf(failPhrase.toLowerCase()) < 0) {
String actualMsg = "Expected validation exception for "+failMsg+", containing phrase '"+failPhrase+"': got '"+origMsg+"'";
fail(actualMsg);
}
// should get this specific type; not basic stream exception
} catch (XMLStreamException sex) {
fail("Expected XMLValidationException for "+failMsg);
}
}
}
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();
}
}

0 comments on commit 38775fb

Please sign in to comment.