Skip to content

Commit

Permalink
Add a reproducer for FasterXML#190 'Element <root> has no attribute "…
Browse files Browse the repository at this point in the history
…verbose"' not thrown from RepairingNsStreamWriter when validating against a DTD schema
  • Loading branch information
ppalaga committed Jan 13, 2024
1 parent 7c10488 commit 50db34c
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 10 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);
}

}
}
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);
}
}


}
8 changes: 0 additions & 8 deletions src/test/java/wstxtest/vstream/TestInvalidAttributeValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.codehaus.stax2.validation.XMLValidationSchema;
import org.codehaus.stax2.validation.XMLValidationSchemaFactory;

import com.ctc.wstx.sw.RepairingNsStreamWriter;
import com.ctc.wstx.sw.SimpleNsStreamWriter;

public class TestInvalidAttributeValue
Expand Down Expand Up @@ -69,12 +68,5 @@ public void reportProblem(XMLValidationProblem problem)
SimpleNsStreamWriter sw = (SimpleNsStreamWriter) stax2.BaseStax2Test.constructStreamWriter(writer, true, false);
validateWriter(DOC, probs, f, schema, writer, sw);
}
{
// RepairingNsStreamWriter
StringWriter writer = new StringWriter();
RepairingNsStreamWriter sw = (RepairingNsStreamWriter) stax2.BaseStax2Test.constructStreamWriter(writer, true, true);
validateWriter(DOC, probs, f, schema, writer, sw);
}

}
}
4 changes: 2 additions & 2 deletions src/test/java/wstxtest/vstream/TestRelaxNG.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public class TestRelaxNG
extends BaseValidationTest
{
final static String SIMPLE_RNG_SCHEMA =
protected final static String SIMPLE_RNG_SCHEMA =
"<element name='dict' xmlns='http://relaxng.org/ns/structure/1.0'>\n"
+" <oneOrMore>\n"
+" <element name='term'>\n"
Expand Down Expand Up @@ -500,7 +500,7 @@ public void testPartialValidationOk()
}
}

private void _testPartialValidationOk(String XML, XMLValidationSchema schema, XMLStreamWriter2 sw, StringWriter writer) throws XMLStreamException {
protected void _testPartialValidationOk(String XML, XMLValidationSchema schema, XMLStreamWriter2 sw, StringWriter writer) throws XMLStreamException {
XMLStreamReader2 sr = getReader(XML);
assertTokenType(START_ELEMENT, sr.next());
sw.copyEventFromReader(sr, false);
Expand Down

0 comments on commit 50db34c

Please sign in to comment.