Skip to content

Commit

Permalink
Extend XMLStreamWriter validation test coverage (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga authored Feb 5, 2024
1 parent 970eb80 commit 5a03810
Show file tree
Hide file tree
Showing 20 changed files with 847 additions and 463 deletions.
74 changes: 74 additions & 0 deletions src/test/java/failing/TestInvalidAttributeValue190.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package failing;

import stax2.BaseStax2Test;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import javax.xml.stream.*;

import org.codehaus.stax2.XMLStreamReader2;
import org.codehaus.stax2.validation.ValidationProblemHandler;
import org.codehaus.stax2.validation.XMLValidationException;
import org.codehaus.stax2.validation.XMLValidationProblem;
import org.codehaus.stax2.validation.XMLValidationSchema;
import org.codehaus.stax2.validation.XMLValidationSchemaFactory;

import com.ctc.wstx.sw.RepairingNsStreamWriter;

public class TestInvalidAttributeValue190
extends BaseStax2Test
{
/* A reproducer for https://github.com/FasterXML/woodstox/issues/190 */
public void testInvalidAttributeValue() throws Exception
{
final String DOC = "<root note=\"note\" verbose=\"yes\"/>";

final String INPUT_DTD =
"<!ELEMENT root ANY>\n"
+"<!ATTLIST root note CDATA #IMPLIED>\n"
;

XMLInputFactory f = getInputFactory();
setCoalescing(f, true);

XMLValidationSchemaFactory schemaFactory =
XMLValidationSchemaFactory.newInstance(XMLValidationSchema.SCHEMA_ID_DTD);
XMLValidationSchema schema = schemaFactory.createSchema(new StringReader(INPUT_DTD));
XMLStreamReader2 sr = (XMLStreamReader2)f.createXMLStreamReader(
new StringReader(DOC));

final List<XMLValidationProblem> probs = new ArrayList<XMLValidationProblem>();

sr.validateAgainst(schema);
sr.setValidationProblemHandler(new ValidationProblemHandler() {
@Override
public void reportProblem(XMLValidationProblem problem)
throws XMLValidationException {
probs.add(problem);
}
});

assertTokenType(START_ELEMENT, sr.next());
assertEquals("root", sr.getLocalName());

final String verboseValue = sr.getAttributeValue(null, "verbose");

assertEquals("yes", verboseValue);

assertEquals(1, probs.size());
assertEquals("Element <root> has no attribute \"verbose\"", probs.get(0).getMessage());

// now do the same on the writer side
// and make sure that the reported problems are the same
{
// RepairingNsStreamWriter
StringWriter writer = new StringWriter();
RepairingNsStreamWriter sw = (RepairingNsStreamWriter) stax2.BaseStax2Test.constructStreamWriter(writer, true, true);
validateWriter(DOC, probs, f, schema, writer, sw);
}

}
}
62 changes: 62 additions & 0 deletions src/test/java/failing/TestRelaxNG189.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package failing;

import javax.xml.stream.*;

import org.codehaus.stax2.validation.*;

import wstxtest.vstream.BaseValidationTest;

/**
* A reproducer for https://github.com/FasterXML/woodstox/issues/189
* Move to {@link wstxtest.vstream.TestRelaxNG} once fixed.
*/
public class TestRelaxNG189
extends BaseValidationTest
{

/**
* Test case for testing handling ID/IDREF/IDREF attributes, using
* schema datatype library.
*/
public void testSimpleIdAttrsWriter()
throws XMLStreamException
{
final String schemaDef =
"<element xmlns='http://relaxng.org/ns/structure/1.0'"
+" datatypeLibrary='http://www.w3.org/2001/XMLSchema-datatypes' name='root'>\n"
+" <oneOrMore>\n"
+" <element name='leaf'>\n"
+" <attribute name='id'><data type='ID' /></attribute>\n"
+" <optional>\n"
+" <attribute name='ref'><data type='IDREF' /></attribute>\n"
+" </optional>\n"
+" <optional>\n"
+" <attribute name='refs'><data type='IDREFS' /></attribute>\n"
+" </optional>\n"
+" </element>\n"
+" </oneOrMore>\n"
+"</element>"
;

XMLValidationSchema schema = parseRngSchema(schemaDef);

String XML;

// And then invalid one, with dangling ref
XML = "<root>"
+" <leaf id='a' ref='second' />\n"
+"</root>"
;
verifyFailure(XML, schema, "reference to undefined id",
"Undefined ID", true, false, true);

// and another one with some of refs undefined
XML = "<root>"
+" <leaf refs='this other' id='this' />\n"
+"</root>"
;
verifyFailure(XML, schema, "reference to undefined id",
"Undefined ID", true, false, true);
}

}
43 changes: 43 additions & 0 deletions src/test/java/failing/TestRelaxNG190.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package failing;

import java.io.StringWriter;

import javax.xml.stream.*;

import org.codehaus.stax2.validation.*;

import com.ctc.wstx.sw.RepairingNsStreamWriter;

/**
* A reproducer for https://github.com/FasterXML/woodstox/issues/190
* Move to {@link wstxtest.vstream.TestRelaxNG} once fixed.
*/
public class TestRelaxNG190
extends wstxtest.vstream.TestRelaxNG
{

public void testPartialValidationOk()
throws XMLStreamException
{
/* Hmmh... RelaxNG does define expected root. So need to
* wrap the doc...
*/
String XML =
"<dummy>\n"
+"<dict>\n"
+"<term type=\"name\">\n"
+" <word>foobar</word>\n"
+" <description>Foo Bar</description>\n"
+"</term></dict>\n"
+"</dummy>"
;
XMLValidationSchema schema = parseRngSchema(SIMPLE_RNG_SCHEMA);
{
StringWriter writer = new StringWriter();
RepairingNsStreamWriter sw = (RepairingNsStreamWriter) constructStreamWriter(writer, true, true);
_testPartialValidationOk(XML, schema, sw, writer);
}
}


}
29 changes: 29 additions & 0 deletions src/test/java/failing/TestW3CSchema189.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package failing;

import javax.xml.stream.XMLStreamException;

import org.codehaus.stax2.validation.XMLValidationSchema;

import wstxtest.msv.TestW3CSchema;

/**
*/
public class TestW3CSchema189
extends TestW3CSchema
{

/**
* A reproducer for https://github.com/FasterXML/woodstox/issues/189
* Move to {@link TestW3CSchema} once fixed.
*/
public void testSimpleNonNsUndefinedIdWriter189() 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'", true, false, true);
}

}
Loading

0 comments on commit 5a03810

Please sign in to comment.