Skip to content

Commit

Permalink
Merge branch '6.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 22, 2024
2 parents f98fec4 + ef10fdc commit eea9a78
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 123 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.fasterxml</groupId>
<artifactId>oss-parent</artifactId>
<version>56</version>
<version>58</version>
</parent>

<groupId>com.fasterxml.woodstox</groupId>
Expand Down
237 changes: 115 additions & 122 deletions src/test/java/wstxtest/wstream/TestInvalidChars.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,50 +26,54 @@ public class TestInvalidChars

public void testInvalidCatchingCharacters() throws XMLStreamException
{
doTestInvalid(CHARACTERS);
doTestInvalid(CHARACTERS);
}

public void testInvalidCatchingCData() throws XMLStreamException
{
doTestInvalid(CDATA);
doTestInvalid(CDATA);
}

public void testInvalidCatchingComment() throws XMLStreamException
{
doTestInvalid(COMMENT);
doTestInvalid(COMMENT);
}

public void testInvalidCatchingPI() throws XMLStreamException
{
doTestInvalid(PROCESSING_INSTRUCTION);
doTestInvalid(PROCESSING_INSTRUCTION);
}

public void testInvalidCatchingAttribute() throws XMLStreamException
{
doTestInvalid(ATTRIBUTE);
doTestInvalid(ATTRIBUTE);
}

// // And then also that we can fix problems

public void testValidReplacingCharacters() throws Exception
{
doTestValid(CHARACTERS);
doTestValid(CHARACTERS);
}

public void testValidReplacingCData() throws Exception
{
doTestValid(CDATA);
doTestValid(CDATA);
}

public void testValidReplacingComment() throws Exception
{
doTestValid(COMMENT);
doTestValid(COMMENT);
}

public void testValidReplacingPI() throws Exception
{
doTestValid(PROCESSING_INSTRUCTION);
doTestValid(PROCESSING_INSTRUCTION);
}

public void testValidReplacingAttribute() throws Exception
{
doTestValid(ATTRIBUTE);
doTestValid(ATTRIBUTE);
}

/*
Expand All @@ -79,124 +83,116 @@ public void testValidReplacingAttribute() throws Exception
*/

private void doTestInvalid(int evtType)
throws XMLStreamException
throws XMLStreamException
{
XMLOutputFactory2 f = getFactory(null);
doTestInvalid(evtType, f.createXMLStreamWriter(new ByteArrayOutputStream(), "ISO-8859-1"), true);
doTestInvalid(evtType, f.createXMLStreamWriter(new ByteArrayOutputStream(), "US-ASCII"), true);
// [WSTX-173] affects backends that do not do their own encoding:
doTestInvalid(evtType, f.createXMLStreamWriter(new StringWriter()), false);
doTestInvalid(evtType, f.createXMLStreamWriter(new ByteArrayOutputStream(), "UTF-8"), false);
doTestInvalid(evtType, f.createXMLStreamWriter(new ByteArrayOutputStream(), "ISO-8859-1"), true);
doTestInvalid(evtType, f.createXMLStreamWriter(new ByteArrayOutputStream(), "US-ASCII"), true);
// [WSTX-173] affects backends that do not do their own encoding:
doTestInvalid(evtType, f.createXMLStreamWriter(new StringWriter()), false);
doTestInvalid(evtType, f.createXMLStreamWriter(new ByteArrayOutputStream(), "UTF-8"), false);
}

/**
* @param strictChecks Due to [WSTX-173], may need to relax some checks
* to pass for now. Not needed once bug is fixed.
* @param strictChecks Due to [WSTX-173], may need to relax some checks to pass
* for now. Not needed once bug is fixed.
*/
private void doTestInvalid(int evtType, XMLStreamWriter sw1,
boolean strictChecks)
throws XMLStreamException
{
XMLStreamWriter2 sw = (XMLStreamWriter2) sw1;
sw.writeStartDocument();
sw.writeStartElement("root");
try {
switch (evtType) {
case ATTRIBUTE:
sw.writeAttribute("attr", INVALID_TEXT);
// always strict for attributes and characters
handleFailure(sw, "Expected an exception for ATTRIBUTE", true);
break;
case CHARACTERS:
sw.writeCharacters(INVALID_TEXT);
handleFailure(sw, "Expected an exception for CHARACTERS", true);
break;
case CDATA:
sw.writeCData(INVALID_TEXT);
handleFailure(sw, "Expected an exception for CDATA", strictChecks);
break;
case COMMENT:
sw.writeComment(INVALID_TEXT);
handleFailure(sw, "Expected an exception for COMMENT", strictChecks);
break;
case PROCESSING_INSTRUCTION:
sw.writeProcessingInstruction("pi", INVALID_TEXT);
handleFailure(sw, "Expected an exception for PROCESSING_INSTRUCTION", strictChecks);
break;
}
} catch (XMLStreamException xse) {
sw.closeCompletely();
}
}

private void doTestValid(int evtType)
throws IOException, XMLStreamException
{
private void doTestInvalid(int evtType, XMLStreamWriter sw1, boolean strictChecks) throws XMLStreamException {
XMLStreamWriter2 sw = (XMLStreamWriter2) sw1;
sw.writeStartDocument();
sw.writeStartElement("root");
try {
switch (evtType) {
case ATTRIBUTE:
sw.writeAttribute("attr", INVALID_TEXT);
// always strict for attributes and characters
handleFailure(sw, "Expected an exception for ATTRIBUTE", true);
break;
case CHARACTERS:
sw.writeCharacters(INVALID_TEXT);
handleFailure(sw, "Expected an exception for CHARACTERS", true);
break;
case CDATA:
sw.writeCData(INVALID_TEXT);
handleFailure(sw, "Expected an exception for CDATA", strictChecks);
break;
case COMMENT:
sw.writeComment(INVALID_TEXT);
handleFailure(sw, "Expected an exception for COMMENT", strictChecks);
break;
case PROCESSING_INSTRUCTION:
sw.writeProcessingInstruction("pi", INVALID_TEXT);
handleFailure(sw, "Expected an exception for PROCESSING_INSTRUCTION", strictChecks);
break;
}
} catch (XMLStreamException xse) {
sw.closeCompletely();
}
}

private void doTestValid(int evtType) throws IOException, XMLStreamException {
XMLOutputFactory2 f = getFactory(REPL_CHAR);
doTestValid(f, evtType, "ISO-8859-1", true);
doTestValid(f, evtType, "US-ASCII", true);
doTestValid(f, evtType, "ISO-8859-1", true);
doTestValid(f, evtType, "US-ASCII", true);

// [WSTX-173] affects backends that do not do their own encoding:
doTestValid(f, evtType, "UTF-8", false);
// [WSTX-173] affects backends that do not do their own encoding:
doTestValid(f, evtType, "UTF-8", false);

StringWriter strw = new StringWriter();
XMLStreamWriter sw = f.createXMLStreamWriter(strw);
buildValid(evtType, sw);
verifyValidReplacement(evtType, sw, strw.toString(), false);
StringWriter strw = new StringWriter();
XMLStreamWriter sw = f.createXMLStreamWriter(strw);
buildValid(evtType, sw);
verifyValidReplacement(evtType, sw, strw.toString(), false);
}

private void doTestValid(XMLOutputFactory2 f, int evtType, String enc, boolean strict)
throws IOException, XMLStreamException
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLStreamWriter sw = f.createXMLStreamWriter(out, enc);
buildValid(evtType, sw);
verifyValidReplacement(evtType, sw, out.toString(enc), strict);
}

private void verifyValidReplacement(int evtType, XMLStreamWriter sw, String doc, boolean strict)
{
if (doc.indexOf(REPL_CHAR.charValue()) < 0) { // no replacement...
handleFailure(sw, "Failed to replace invalid char, event "+tokenTypeDesc(evtType)+", xml = '"+doc+"'", strict);
}
}

private void buildValid(int evtType, XMLStreamWriter sw1)
throws XMLStreamException
{
XMLStreamWriter2 sw = (XMLStreamWriter2) sw1;
sw.writeStartDocument();
sw.writeStartElement("root");

switch (evtType) {
case ATTRIBUTE:
sw.writeAttribute("attr", INVALID_TEXT);
break;
case CHARACTERS:
sw.writeCharacters(INVALID_TEXT);
break;
case CDATA:
sw.writeCData(INVALID_TEXT);
break;
case COMMENT:
sw.writeComment(INVALID_TEXT);
break;
case PROCESSING_INSTRUCTION:
sw.writeProcessingInstruction("pi", INVALID_TEXT);
break;
}
sw.writeEndElement();
sw.writeEndDocument();
sw.closeCompletely();
}

private void handleFailure(XMLStreamWriter sw, String msg, boolean doFail)
{
if (doFail) {
fail(msg+" (stream writer: "+sw+")");
} else {
warn("suppressing failure '"+msg+"' (stream writer: "+sw+")");
}
throws IOException, XMLStreamException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLStreamWriter sw = f.createXMLStreamWriter(out, enc);
buildValid(evtType, sw);
verifyValidReplacement(evtType, sw, out.toString(enc), strict);
}

private void verifyValidReplacement(int evtType, XMLStreamWriter sw, String doc, boolean strict) {
if (doc.indexOf(REPL_CHAR.charValue()) < 0) { // no replacement...
handleFailure(sw,
"Failed to replace invalid char, event " + tokenTypeDesc(evtType) + ", xml = '" + doc + "'",
strict);
}
}

private void buildValid(int evtType, XMLStreamWriter sw1) throws XMLStreamException {
XMLStreamWriter2 sw = (XMLStreamWriter2) sw1;
sw.writeStartDocument();
sw.writeStartElement("root");

switch (evtType) {
case ATTRIBUTE:
sw.writeAttribute("attr", INVALID_TEXT);
break;
case CHARACTERS:
sw.writeCharacters(INVALID_TEXT);
break;
case CDATA:
sw.writeCData(INVALID_TEXT);
break;
case COMMENT:
sw.writeComment(INVALID_TEXT);
break;
case PROCESSING_INSTRUCTION:
sw.writeProcessingInstruction("pi", INVALID_TEXT);
break;
}
sw.writeEndElement();
sw.writeEndDocument();
sw.closeCompletely();
}

private void handleFailure(XMLStreamWriter sw, String msg, boolean doFail) {
if (doFail) {
fail(msg + " (stream writer: " + sw + ")");
} else {
warn("suppressing failure '" + msg + "' (stream writer: " + sw + ")");
}
}

/*
Expand All @@ -205,15 +201,12 @@ private void handleFailure(XMLStreamWriter sw, String msg, boolean doFail)
//////////////////////////////////////////////
*/

private XMLOutputFactory2 getFactory(Character replChar)
throws XMLStreamException
{
private XMLOutputFactory2 getFactory(Character replChar) throws XMLStreamException {
XMLOutputFactory2 f = getOutputFactory();
setRepairing(f, false);
setValidateContent(f, true);
f.setProperty(WstxOutputProperties.P_OUTPUT_INVALID_CHAR_HANDLER,
(replChar == null) ? null : new InvalidCharHandler.ReplacingHandler(replChar.charValue()));
f.setProperty(WstxOutputProperties.P_OUTPUT_INVALID_CHAR_HANDLER,
(replChar == null) ? null : new InvalidCharHandler.ReplacingHandler(replChar.charValue()));
return f;
}
}

0 comments on commit eea9a78

Please sign in to comment.