Skip to content

Commit 50db34c

Browse files
committed
Add a reproducer for FasterXML#190 'Element <root> has no attribute "verbose"' not thrown from RepairingNsStreamWriter when validating against a DTD schema
1 parent 7c10488 commit 50db34c

File tree

4 files changed

+119
-10
lines changed

4 files changed

+119
-10
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: 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+
}

src/test/java/wstxtest/vstream/TestInvalidAttributeValue.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import org.codehaus.stax2.validation.XMLValidationSchema;
1717
import org.codehaus.stax2.validation.XMLValidationSchemaFactory;
1818

19-
import com.ctc.wstx.sw.RepairingNsStreamWriter;
2019
import com.ctc.wstx.sw.SimpleNsStreamWriter;
2120

2221
public class TestInvalidAttributeValue
@@ -69,12 +68,5 @@ public void reportProblem(XMLValidationProblem problem)
6968
SimpleNsStreamWriter sw = (SimpleNsStreamWriter) stax2.BaseStax2Test.constructStreamWriter(writer, true, false);
7069
validateWriter(DOC, probs, f, schema, writer, sw);
7170
}
72-
{
73-
// RepairingNsStreamWriter
74-
StringWriter writer = new StringWriter();
75-
RepairingNsStreamWriter sw = (RepairingNsStreamWriter) stax2.BaseStax2Test.constructStreamWriter(writer, true, true);
76-
validateWriter(DOC, probs, f, schema, writer, sw);
77-
}
78-
7971
}
8072
}

src/test/java/wstxtest/vstream/TestRelaxNG.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
public class TestRelaxNG
1919
extends BaseValidationTest
2020
{
21-
final static String SIMPLE_RNG_SCHEMA =
21+
protected final static String SIMPLE_RNG_SCHEMA =
2222
"<element name='dict' xmlns='http://relaxng.org/ns/structure/1.0'>\n"
2323
+" <oneOrMore>\n"
2424
+" <element name='term'>\n"
@@ -500,7 +500,7 @@ public void testPartialValidationOk()
500500
}
501501
}
502502

503-
private void _testPartialValidationOk(String XML, XMLValidationSchema schema, XMLStreamWriter2 sw, StringWriter writer) throws XMLStreamException {
503+
protected void _testPartialValidationOk(String XML, XMLValidationSchema schema, XMLStreamWriter2 sw, StringWriter writer) throws XMLStreamException {
504504
XMLStreamReader2 sr = getReader(XML);
505505
assertTokenType(START_ELEMENT, sr.next());
506506
sw.copyEventFromReader(sr, false);

0 commit comments

Comments
 (0)