Skip to content

Commit 5a03810

Browse files
authored
Extend XMLStreamWriter validation test coverage (#191)
1 parent 970eb80 commit 5a03810

20 files changed

+847
-463
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package failing;
2+
3+
import stax2.BaseStax2Test;
4+
5+
import java.io.StringReader;
6+
import java.io.StringWriter;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
import javax.xml.stream.*;
11+
12+
import org.codehaus.stax2.XMLStreamReader2;
13+
import org.codehaus.stax2.validation.ValidationProblemHandler;
14+
import org.codehaus.stax2.validation.XMLValidationException;
15+
import org.codehaus.stax2.validation.XMLValidationProblem;
16+
import org.codehaus.stax2.validation.XMLValidationSchema;
17+
import org.codehaus.stax2.validation.XMLValidationSchemaFactory;
18+
19+
import com.ctc.wstx.sw.RepairingNsStreamWriter;
20+
21+
public class TestInvalidAttributeValue190
22+
extends BaseStax2Test
23+
{
24+
/* A reproducer for https://github.com/FasterXML/woodstox/issues/190 */
25+
public void testInvalidAttributeValue() throws Exception
26+
{
27+
final String DOC = "<root note=\"note\" verbose=\"yes\"/>";
28+
29+
final String INPUT_DTD =
30+
"<!ELEMENT root ANY>\n"
31+
+"<!ATTLIST root note CDATA #IMPLIED>\n"
32+
;
33+
34+
XMLInputFactory f = getInputFactory();
35+
setCoalescing(f, true);
36+
37+
XMLValidationSchemaFactory schemaFactory =
38+
XMLValidationSchemaFactory.newInstance(XMLValidationSchema.SCHEMA_ID_DTD);
39+
XMLValidationSchema schema = schemaFactory.createSchema(new StringReader(INPUT_DTD));
40+
XMLStreamReader2 sr = (XMLStreamReader2)f.createXMLStreamReader(
41+
new StringReader(DOC));
42+
43+
final List<XMLValidationProblem> probs = new ArrayList<XMLValidationProblem>();
44+
45+
sr.validateAgainst(schema);
46+
sr.setValidationProblemHandler(new ValidationProblemHandler() {
47+
@Override
48+
public void reportProblem(XMLValidationProblem problem)
49+
throws XMLValidationException {
50+
probs.add(problem);
51+
}
52+
});
53+
54+
assertTokenType(START_ELEMENT, sr.next());
55+
assertEquals("root", sr.getLocalName());
56+
57+
final String verboseValue = sr.getAttributeValue(null, "verbose");
58+
59+
assertEquals("yes", verboseValue);
60+
61+
assertEquals(1, probs.size());
62+
assertEquals("Element <root> has no attribute \"verbose\"", probs.get(0).getMessage());
63+
64+
// now do the same on the writer side
65+
// and make sure that the reported problems are the same
66+
{
67+
// RepairingNsStreamWriter
68+
StringWriter writer = new StringWriter();
69+
RepairingNsStreamWriter sw = (RepairingNsStreamWriter) stax2.BaseStax2Test.constructStreamWriter(writer, true, true);
70+
validateWriter(DOC, probs, f, schema, writer, sw);
71+
}
72+
73+
}
74+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package failing;
2+
3+
import javax.xml.stream.*;
4+
5+
import org.codehaus.stax2.validation.*;
6+
7+
import wstxtest.vstream.BaseValidationTest;
8+
9+
/**
10+
* A reproducer for https://github.com/FasterXML/woodstox/issues/189
11+
* Move to {@link wstxtest.vstream.TestRelaxNG} once fixed.
12+
*/
13+
public class TestRelaxNG189
14+
extends BaseValidationTest
15+
{
16+
17+
/**
18+
* Test case for testing handling ID/IDREF/IDREF attributes, using
19+
* schema datatype library.
20+
*/
21+
public void testSimpleIdAttrsWriter()
22+
throws XMLStreamException
23+
{
24+
final String schemaDef =
25+
"<element xmlns='http://relaxng.org/ns/structure/1.0'"
26+
+" datatypeLibrary='http://www.w3.org/2001/XMLSchema-datatypes' name='root'>\n"
27+
+" <oneOrMore>\n"
28+
+" <element name='leaf'>\n"
29+
+" <attribute name='id'><data type='ID' /></attribute>\n"
30+
+" <optional>\n"
31+
+" <attribute name='ref'><data type='IDREF' /></attribute>\n"
32+
+" </optional>\n"
33+
+" <optional>\n"
34+
+" <attribute name='refs'><data type='IDREFS' /></attribute>\n"
35+
+" </optional>\n"
36+
+" </element>\n"
37+
+" </oneOrMore>\n"
38+
+"</element>"
39+
;
40+
41+
XMLValidationSchema schema = parseRngSchema(schemaDef);
42+
43+
String XML;
44+
45+
// And then invalid one, with dangling ref
46+
XML = "<root>"
47+
+" <leaf id='a' ref='second' />\n"
48+
+"</root>"
49+
;
50+
verifyFailure(XML, schema, "reference to undefined id",
51+
"Undefined ID", true, false, true);
52+
53+
// and another one with some of refs undefined
54+
XML = "<root>"
55+
+" <leaf refs='this other' id='this' />\n"
56+
+"</root>"
57+
;
58+
verifyFailure(XML, schema, "reference to undefined id",
59+
"Undefined ID", true, false, true);
60+
}
61+
62+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package failing;
2+
3+
import java.io.StringWriter;
4+
5+
import javax.xml.stream.*;
6+
7+
import org.codehaus.stax2.validation.*;
8+
9+
import com.ctc.wstx.sw.RepairingNsStreamWriter;
10+
11+
/**
12+
* A reproducer for https://github.com/FasterXML/woodstox/issues/190
13+
* Move to {@link wstxtest.vstream.TestRelaxNG} once fixed.
14+
*/
15+
public class TestRelaxNG190
16+
extends wstxtest.vstream.TestRelaxNG
17+
{
18+
19+
public void testPartialValidationOk()
20+
throws XMLStreamException
21+
{
22+
/* Hmmh... RelaxNG does define expected root. So need to
23+
* wrap the doc...
24+
*/
25+
String XML =
26+
"<dummy>\n"
27+
+"<dict>\n"
28+
+"<term type=\"name\">\n"
29+
+" <word>foobar</word>\n"
30+
+" <description>Foo Bar</description>\n"
31+
+"</term></dict>\n"
32+
+"</dummy>"
33+
;
34+
XMLValidationSchema schema = parseRngSchema(SIMPLE_RNG_SCHEMA);
35+
{
36+
StringWriter writer = new StringWriter();
37+
RepairingNsStreamWriter sw = (RepairingNsStreamWriter) constructStreamWriter(writer, true, true);
38+
_testPartialValidationOk(XML, schema, sw, writer);
39+
}
40+
}
41+
42+
43+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package failing;
2+
3+
import javax.xml.stream.XMLStreamException;
4+
5+
import org.codehaus.stax2.validation.XMLValidationSchema;
6+
7+
import wstxtest.msv.TestW3CSchema;
8+
9+
/**
10+
*/
11+
public class TestW3CSchema189
12+
extends TestW3CSchema
13+
{
14+
15+
/**
16+
* A reproducer for https://github.com/FasterXML/woodstox/issues/189
17+
* Move to {@link TestW3CSchema} once fixed.
18+
*/
19+
public void testSimpleNonNsUndefinedIdWriter189() throws XMLStreamException
20+
{
21+
XMLValidationSchema schema = parseW3CSchema(SIMPLE_NON_NS_SCHEMA);
22+
String XML = "<personnel><person id='a1'>"
23+
+ "<name><family>F</family><given>G</given>"
24+
+ "</name><link manager='m3' /></person></personnel>";
25+
verifyFailure(XML, schema, "undefined referenced id ('m3')",
26+
"Undefined ID 'm3'", true, false, true);
27+
}
28+
29+
}

0 commit comments

Comments
 (0)