-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
31 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 30 additions & 31 deletions
61
java/test/com/github/bazel_contrib/contrib_rules_jvm/junit5/SafeXmlTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,47 @@ | ||
package com.github.bazel_contrib.contrib_rules_jvm.junit5; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Node; | ||
import org.xml.sax.InputSource; | ||
import org.xml.sax.SAXException; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.io.StringReader; | ||
import java.io.StringWriter; | ||
import java.io.Writer; | ||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
import javax.xml.stream.XMLOutputFactory; | ||
import javax.xml.stream.XMLStreamException; | ||
import javax.xml.stream.XMLStreamWriter; | ||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.io.StringReader; | ||
import java.io.StringWriter; | ||
import java.io.Writer; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import org.junit.jupiter.api.Test; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Node; | ||
import org.xml.sax.InputSource; | ||
import org.xml.sax.SAXException; | ||
|
||
public class SafeXmlTest { | ||
|
||
@Test | ||
public void properlyEscapesCDataSection() | ||
throws XMLStreamException, ParserConfigurationException, IOException, SAXException { | ||
try (Writer writer = new StringWriter()) { | ||
XMLStreamWriter xml = XMLOutputFactory.newDefaultFactory().createXMLStreamWriter(writer); | ||
@Test | ||
public void properlyEscapesCDataSection() | ||
throws XMLStreamException, ParserConfigurationException, IOException, SAXException { | ||
try (Writer writer = new StringWriter()) { | ||
XMLStreamWriter xml = XMLOutputFactory.newDefaultFactory().createXMLStreamWriter(writer); | ||
|
||
xml.writeStartDocument("UTF-8", "1.0"); | ||
// Output the "end of cdata" marker | ||
SafeXml.writeTextElement(xml, "container", "]]>"); | ||
xml.writeEndDocument(); | ||
xml.writeStartDocument("UTF-8", "1.0"); | ||
// Output the "end of cdata" marker | ||
SafeXml.writeTextElement(xml, "container", "]]>"); | ||
xml.writeEndDocument(); | ||
|
||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | ||
DocumentBuilder builder; | ||
try (Reader reader = new StringReader(writer.toString())) { | ||
builder = factory.newDocumentBuilder(); | ||
Document parsed = builder.parse(new InputSource(reader)); | ||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | ||
DocumentBuilder builder; | ||
try (Reader reader = new StringReader(writer.toString())) { | ||
builder = factory.newDocumentBuilder(); | ||
Document parsed = builder.parse(new InputSource(reader)); | ||
|
||
Node container = parsed.getElementsByTagName("container").item(0); | ||
Node container = parsed.getElementsByTagName("container").item(0); | ||
|
||
assertEquals("]]>", container.getTextContent()); | ||
} | ||
} | ||
assertEquals("]]>", container.getTextContent()); | ||
} | ||
} | ||
} | ||
} | ||
} |