Skip to content

Commit 88f3b36

Browse files
committed
Extend XMLStreamWriter validation test coverage by checking the writer
validation wherever we test the reader validation
1 parent f348b5f commit 88f3b36

11 files changed

+500
-319
lines changed

src/main/java/com/ctc/wstx/sw/SimpleOutputElement.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,11 @@ public String getNamespaceURI() {
246246
}
247247

248248
public QName getName() {
249-
return QNameCreator.create(mURI, mLocalName, mPrefix);
249+
if (mPrefix != null) {
250+
return QNameCreator.create(mURI, mLocalName, mPrefix);
251+
} else {
252+
return new QName(mURI, mLocalName);
253+
}
250254
}
251255

252256
/*

src/test/java/stax2/BaseStax2Test.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package stax2;
22

33
import java.io.*;
4+
import java.util.ArrayList;
45
import java.util.HashMap;
6+
import java.util.List;
57

68
import junit.framework.TestCase;
79

@@ -12,6 +14,12 @@
1214
import org.codehaus.stax2.evt.*;
1315

1416
import org.codehaus.stax2.ri.Stax2ReaderAdapter;
17+
import org.codehaus.stax2.validation.ValidationProblemHandler;
18+
import org.codehaus.stax2.validation.XMLValidationException;
19+
import org.codehaus.stax2.validation.XMLValidationProblem;
20+
import org.codehaus.stax2.validation.XMLValidationSchema;
21+
22+
import com.ctc.wstx.stax.WstxOutputFactory;
1523

1624
/**
1725
* Base unit test class to be inherited by all unit tests that test
@@ -186,6 +194,14 @@ protected XMLStreamReader2 constructNonNsStreamReader(String content, boolean co
186194
return (XMLStreamReader2) f.createXMLStreamReader(new StringReader(content));
187195
}
188196

197+
protected static XMLStreamWriter2 constructStreamWriter(Writer writer, boolean nsSupported, boolean repairing) throws XMLStreamException {
198+
WstxOutputFactory f = new WstxOutputFactory();
199+
f.getConfig().doSupportNamespaces(nsSupported);
200+
f.getConfig().enableAutomaticNamespaces(repairing);
201+
return (XMLStreamWriter2) f.createXMLStreamWriter(writer);
202+
}
203+
204+
189205
/**
190206
* Method to force constructing a wrapper for given stream reader.
191207
* Have to use this method to work around natural resistance by
@@ -328,7 +344,7 @@ protected static boolean isNamespaceAware(XMLOutputFactory f)
328344
* @return Dummy value calculated on contents; used to make sure
329345
* no dead code is eliminated
330346
*/
331-
protected int streamThrough(XMLStreamReader sr)
347+
protected static int streamThrough(XMLStreamReader sr)
332348
throws XMLStreamException
333349
{
334350
int result = 0;
@@ -578,6 +594,40 @@ protected void verifyException(Throwable e, String match)
578594
}
579595
}
580596

597+
protected static void validateWriter(final String DOC, final List<XMLValidationProblem> probs, XMLInputFactory f,
598+
XMLValidationSchema schema, StringWriter writer, XMLStreamWriter2 sw) throws XMLStreamException {
599+
sw.validateAgainst(schema);
600+
final List<XMLValidationProblem> writerProbs = new ArrayList<XMLValidationProblem>();
601+
sw.setValidationProblemHandler(new ValidationProblemHandler() {
602+
603+
@Override
604+
public void reportProblem(XMLValidationProblem problem) throws XMLValidationException {
605+
writerProbs.add(problem);
606+
}
607+
});
608+
609+
XMLStreamReader2 sr = (XMLStreamReader2)f.createXMLStreamReader(
610+
new StringReader(DOC));
611+
612+
sw.copyEventFromReader(sr, false);
613+
while (sr.hasNext()) {
614+
/* int type = */sr.next();
615+
sw.copyEventFromReader(sr, false);
616+
}
617+
sr.close();
618+
sw.close();
619+
assertEquals(DOC, writer.toString());
620+
621+
assertEquals(probs.size(), writerProbs.size());
622+
for (int i = 0; i < probs.size(); i++) {
623+
XMLValidationProblem expected = probs.get(i);
624+
XMLValidationProblem actual = writerProbs.get(i);
625+
assertEquals(expected.getMessage(), actual.getMessage());
626+
assertEquals(expected.getSeverity(), actual.getSeverity());
627+
}
628+
}
629+
630+
581631
/*
582632
///////////////////////////////////////////////////////////
583633
// Debug/output helpers

src/test/java/wstxtest/BaseWstxTest.java

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -234,38 +234,6 @@ protected static void setFixContent(XMLOutputFactory f, boolean state)
234234
//////////////////////////////////////////////////
235235
*/
236236

237-
/**
238-
* Method that will iterate through contents of an XML document
239-
* using specified stream reader; will also access some of data
240-
* to make sure reader reads most of lazy-loadable data.
241-
* Method is usually called to try to get an exception for invalid
242-
* content.
243-
*
244-
* @return Dummy value calculated on contents; used to make sure
245-
* no dead code is eliminated
246-
*/
247-
@Override
248-
protected int streamThrough(XMLStreamReader sr)
249-
throws XMLStreamException
250-
{
251-
int result = 0;
252-
253-
while (sr.hasNext()) {
254-
int type = sr.next();
255-
result += type;
256-
if (sr.hasText()) {
257-
// will also do basic verification for text content, to
258-
// see that all text accessor methods return same content
259-
result += getAndVerifyText(sr).hashCode();
260-
}
261-
if (sr.hasName()) {
262-
result += sr.getName().hashCode();
263-
}
264-
}
265-
266-
return result;
267-
}
268-
269237
@Override
270238
protected int streamThroughFailing(XMLInputFactory f, String contents,
271239
String msg)

src/test/java/wstxtest/msv/TestW3CSchema.java

Lines changed: 71 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
package wstxtest.msv;
22

3+
import java.io.StringReader;
4+
import java.io.StringWriter;
5+
import java.io.Writer;
6+
37
import javax.xml.stream.*;
48

59
import org.codehaus.stax2.*;
610
import org.codehaus.stax2.validation.*;
711

12+
import com.ctc.wstx.stax.WstxInputFactory;
13+
import com.ctc.wstx.stax.WstxOutputFactory;
14+
815
import wstxtest.vstream.BaseValidationTest;
916

1017
/**
@@ -78,10 +85,10 @@ public class TestW3CSchema
7885
final static String SIMPLE_XML = "<personnel>"
7986
+ "<person id='a123' contr='true'>" + " <name>"
8087
+ "<family>Family</family><given>Fred</given>" + " </name>"
81-
+ " <url href='urn:something' />" + " </person>"
88+
+ " <url href='urn:something'/>" + " </person>"
8289
+ " <person id='b12'>"
8390
+ " <name><family>Blow</family><given>Joe</given>"
84-
+ " </name>" + " <url />" + " </person>" + "</personnel>";
91+
+ " </name>" + " <url/>" + " </person>" + "</personnel>";
8592

8693
/**
8794
* Test validation against a simple document valid according to a very
@@ -90,40 +97,59 @@ public class TestW3CSchema
9097
public void testSimpleNonNs() throws XMLStreamException
9198
{
9299
XMLValidationSchema schema = parseW3CSchema(SIMPLE_NON_NS_SCHEMA);
93-
XMLStreamReader2 sr = getReader(SIMPLE_XML);
100+
String XML = SIMPLE_XML;
101+
XMLStreamReader2 sr = getReader(XML);
94102
sr.validateAgainst(schema);
95103

104+
StringWriter writer = new StringWriter();
105+
XMLStreamWriter2 sw = (XMLStreamWriter2) getOutputFactory().createXMLStreamWriter(writer);
106+
sw.validateAgainst(schema);
107+
sw.copyEventFromReader(sr, false);
108+
96109
try {
97110
assertTokenType(START_ELEMENT, sr.next());
98111
assertEquals("personnel", sr.getLocalName());
112+
sw.copyEventFromReader(sr, false);
99113

100114
while (sr.hasNext()) {
101115
/* int type = */sr.next();
116+
sw.copyEventFromReader(sr, false);
102117
}
103118
} catch (XMLValidationException vex) {
104119
fail("Did not expect validation exception, got: " + vex);
105120
}
106121
assertTokenType(END_DOCUMENT, sr.getEventType());
122+
assertEquals(XML.replace("'", "\""), writer.toString());
107123
}
108124

109125
public void testSimplePartialNonNs() throws XMLStreamException
110126
{
111127
XMLValidationSchema schema = parseW3CSchema(SIMPLE_NON_NS_SCHEMA);
112-
XMLStreamReader2 sr = getReader(SIMPLE_XML);
128+
String XML = SIMPLE_XML;
129+
XMLStreamReader2 sr = getReader(XML);
113130

131+
StringWriter writer = new StringWriter();
132+
XMLStreamWriter2 sw = (XMLStreamWriter2) getOutputFactory().createXMLStreamWriter(writer);
133+
sw.copyEventFromReader(sr, false);
134+
114135
assertTokenType(START_ELEMENT, sr.next());
115136
assertEquals("personnel", sr.getLocalName());
137+
sw.copyEventFromReader(sr, false);
116138
sr.validateAgainst(schema);
139+
sw.validateAgainst(schema);
117140
try {
118141
assertTokenType(START_ELEMENT, sr.next());
119142
assertEquals("person", sr.getLocalName());
143+
sw.copyEventFromReader(sr, false);
120144
while (sr.hasNext()) {
121145
/* int type = */sr.next();
146+
sw.copyEventFromReader(sr, false);
122147
}
123148
} catch (XMLValidationException vex) {
124149
fail("Did not expect validation exception, got: " + vex);
125150
}
126151
assertTokenType(END_DOCUMENT, sr.getEventType());
152+
assertEquals(XML.replace("'", "\""), writer.toString());
127153
}
128154

129155
/**
@@ -192,6 +218,18 @@ public void testSimpleDataTypes() throws XMLStreamException
192218
}
193219
sr.close();
194220

221+
// validate the same document on the writer side
222+
sr = getReader(XML);
223+
StringWriter writer = new StringWriter();
224+
XMLStreamWriter2 sw = (XMLStreamWriter2) getOutputFactory().createXMLStreamWriter(writer);
225+
sw.validateAgainst(schema);
226+
sw.copyEventFromReader(sr, true);
227+
while (sr.hasNext()) {
228+
/* int type = */sr.next();
229+
sw.copyEventFromReader(sr, true);
230+
}
231+
assertEquals(XML.replace("\r", ""), writer.toString());
232+
195233
// Then invalid (wrong type for value)
196234
XML = "<item><quantity>34b</quantity><price>1.00</price></item>";
197235
sr.validateAgainst(schema);
@@ -224,33 +262,28 @@ public void testSimpleText() throws XMLStreamException
224262
+ "</xs:schema>";
225263
XMLValidationSchema schema = parseW3CSchema(SCHEMA);
226264

227-
// First, 3 valid docs:
228-
String XML = "<root>xyz</root>";
229-
XMLStreamReader2 sr = getReader(XML);
230-
sr.validateAgainst(schema);
231-
streamThrough(sr);
232-
sr.close();
233-
234-
XML = "<root />";
235-
sr = getReader(XML);
236-
sr.validateAgainst(schema);
237-
streamThrough(sr);
238-
sr.close();
265+
String XML = null;
266+
for (ValidationMode mode : ValidationMode.values()) {
267+
// First, 3 valid docs:
268+
XML = "<root>xyz</root>";
269+
mode.validate(schema, XML);
270+
271+
XML = "<root />";
272+
mode.validate(schema, XML, "<root/>");
273+
274+
XML = "<root></root>";
275+
mode.validate(schema, XML, "<root/>");
276+
}
239277

240-
XML = "<root></root>";
241-
sr = getReader(XML);
242-
sr.validateAgainst(schema);
243-
streamThrough(sr);
244-
sr.close();
245278

246279
// Then invalid?
247280
XML = "<foobar />";
248-
sr = getReader(XML);
281+
XMLStreamReader2 sr = getReader(XML);
249282
sr.validateAgainst(schema);
250283
verifyFailure(XML, schema, "should warn about wrong root element",
251284
"tag name \"foobar\" is not allowed", false);
252285
}
253-
286+
254287
/**
255288
* Test for reproducing [WSTX-191]
256289
*/
@@ -283,20 +316,24 @@ public void testConstrainedText() throws XMLStreamException
283316
"<description><![CDATA[Du Texte]]></description>");
284317
_testValidDesc(schema,
285318
"<description>??</description><description><![CDATA[...]]></description>");
286-
_testValidDesc(schema, "<description></description>");
287-
_testValidDesc(schema, "<description />");
319+
_testValidDesc(schema, "<description></description>", "<description/>");
320+
_testValidDesc(schema, "<description />", "<description/>");
288321
_testValidDesc(schema, "<description><![CDATA[]]></description>");
289322
}
290323

291-
private void _testValidDesc(XMLValidationSchema schema, String descSnippet) throws XMLStreamException
324+
private void _testValidDesc(XMLValidationSchema schema, String descSnippet) throws XMLStreamException {
325+
_testValidDesc(schema, descSnippet, descSnippet);
326+
}
327+
private void _testValidDesc(XMLValidationSchema schema, String descSnippet, String expectedSnippet) throws XMLStreamException
292328
{
293-
// These should all be valid according to the schema
294-
String XML = "<catalog xmlns='http://www.mondomaine.fr/framework'>"
295-
+ descSnippet + "</catalog>";
296-
XMLStreamReader2 sr = getReader(XML);
297-
sr.validateAgainst(schema);
298-
streamThrough(sr);
299-
sr.close();
329+
for (ValidationMode mode : ValidationMode.values()) {
330+
// These should all be valid according to the schema
331+
String XML = "<catalog xmlns='http://www.mondomaine.fr/framework'>"
332+
+ descSnippet + "</catalog>";
333+
String expectedXML = "<catalog xmlns='http://www.mondomaine.fr/framework'>"
334+
+ expectedSnippet + "</catalog>";
335+
mode.validate(schema, XML, expectedXML.replace("'", "\""));
336+
}
300337
}
301338

302339
public void testValidationHandler() throws XMLStreamException
@@ -361,4 +398,5 @@ public XMLValidationProblem getProblem() {
361398
return problem;
362399
}
363400
}
401+
364402
}

src/test/java/wstxtest/msv/TestW3CSchemaTypes.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.codehaus.stax2.validation.*;
77

88
import wstxtest.vstream.BaseValidationTest;
9+
import wstxtest.vstream.BaseValidationTest.ValidationMode;
910

1011
import java.io.StringWriter;
1112

@@ -45,9 +46,9 @@ public class TestW3CSchemaTypes
4546
public void testSimpleValidInt() throws Exception
4647
{
4748
XMLValidationSchema schema = parseW3CSchema(SCHEMA_INT);
48-
XMLStreamReader2 sr = getReader("<price>129</price>");
49-
sr.validateAgainst(schema);
50-
streamThrough(sr);
49+
for (ValidationMode mode : ValidationMode.values()) {
50+
mode.validate(schema, "<price>129</price>");
51+
}
5152
}
5253

5354
public void testSimpleInvalidInt() throws Exception
@@ -62,9 +63,9 @@ public void testSimpleInvalidInt() throws Exception
6263
public void testSimpleValidFloat() throws Exception
6364
{
6465
XMLValidationSchema schema = parseW3CSchema(SCHEMA_FLOAT);
65-
XMLStreamReader2 sr = getReader("<price>1.00</price>");
66-
sr.validateAgainst(schema);
67-
streamThrough(sr);
66+
for (ValidationMode mode : ValidationMode.values()) {
67+
mode.validate(schema, "<price>1.00</price>");
68+
}
6869
}
6970

7071
public void testSimpleInvalidFloat() throws Exception

0 commit comments

Comments
 (0)