Skip to content

Commit

Permalink
Revert "Revert "Merge pull request #7 from gurock/encode_somechars_on…
Browse files Browse the repository at this point in the history
…_value_attributes""

This reverts commit 31ffd1f.
  • Loading branch information
bitcoder committed Mar 5, 2024
1 parent 31ffd1f commit eb6d57b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 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 @@
<groupId>com.testrail</groupId>
<artifactId>testrail-junit-extensions</artifactId>
<packaging>jar</packaging>
<version>0.1.1</version>
<version>0.1.2</version>
<name>testrail-junit-extensions</name>
<description>Improvements for JUnit that allow you to take better advantage of JUnit 5 (jupiter engine)</description>
<url>https://github.com/gurock/testrail-junit-extensions</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ private void writeXmlReport(TestIdentifier testIdentifier, Map<TestIdentifier, A
Writer out) throws XMLStreamException {

XMLOutputFactory factory = XMLOutputFactory.newInstance();
factory.setProperty("escapeCharacters", false);
XMLStreamWriter xmlWriter = factory.createXMLStreamWriter(out);
xmlWriter.writeStartDocument("UTF-8", "1.0");
newLine(xmlWriter);
Expand Down Expand Up @@ -299,7 +300,7 @@ private void writeTestcase(TestIdentifier testIdentifier, AggregatedTestResult t
List<ReportEntry> entries = this.reportData.getReportEntries(testIdentifier);
Map<String, String> testrunProperties = getTestRunProperties(entries);
for (Map.Entry<String, String> property : testrunProperties.entrySet()) {
addProperty(writer, property.getKey(), property.getValue());
addPropertyAndEscapeValue(writer, property.getKey(), property.getValue());
}

writer.writeEndElement(); // properties
Expand All @@ -316,6 +317,12 @@ private void addProperty(XMLStreamWriter writer, String name, String value) thro
newLine(writer);
}


private void addPropertyAndEscapeValue(XMLStreamWriter writer, String name, String value) throws XMLStreamException {
writer.writeCharacters(String.format("<property name=\"%s\" value=\"%s\"/>", name, escapeIllegalCharsForAttributes(value)));
newLine(writer);
}

private void addPropertyWithInnerContent(XMLStreamWriter writer, String name, String value)
throws XMLStreamException {
writer.writeStartElement("property");
Expand Down Expand Up @@ -502,6 +509,11 @@ private void writeAttributeSafely(XMLStreamWriter writer, String name, String va
writer.writeAttribute(name, escapeIllegalChars(value));
}


private void writeAttributeSafelyEncodingSomeChars(XMLStreamWriter writer, String name, String value) throws XMLStreamException {
writer.writeAttribute(name, escapeIllegalCharsForAttributes(value));
}

private void writeCDataSafely(XMLStreamWriter writer, String data) throws XMLStreamException {
for (String safeDataPart : CDATA_SPLIT_PATTERN.split(escapeIllegalChars(data))) {
writer.writeCData(safeDataPart);
Expand Down Expand Up @@ -533,6 +545,28 @@ private static boolean isAllowedXmlCharacter(int codePoint) {
|| (codePoint >= 0x10000 && codePoint <= 0x10FFFF);
}

static String escapeIllegalCharsForAttributes(String text) {
if (text.codePoints().allMatch(XmlReportWriter::isAllowedXmlCharacterForAttributes)) {
return text;
}
StringBuilder result = new StringBuilder(text.length() * 2);
text.codePoints().forEach(codePoint -> {
if (isAllowedXmlCharacterForAttributes(codePoint)) {
result.appendCodePoint(codePoint);
} else { // use a Character Reference (cf. https://www.w3.org/TR/xml/#NT-CharRef)
result.append("&#").append(codePoint).append(';');
}
});
return result.toString();
}

private static boolean isAllowedXmlCharacterForAttributes(int codePoint) {
// source: https://www.w3.org/TR/xml/#charsets with a workaround for enconding some characters, such as tab, newline, carriage return
return (codePoint >= 0x20 && codePoint <= 0xD7FF) //
|| (codePoint >= 0xE000 && codePoint <= 0xFFFD) //
|| (codePoint >= 0x10000 && codePoint <= 0x10FFFF);
}

private void newLine(XMLStreamWriter xmlWriter) throws XMLStreamException {
xmlWriter.writeCharacters("\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.joox.JOOX.$;
import static org.joox.JOOX.attr;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod;
Expand Down Expand Up @@ -378,6 +379,16 @@ public void shouldStoreTestRunPropertiesMultiple() throws Exception {
assertThat(testcase.child("properties").children("property").matchAttr("name", "my_property2").attr("value")).isEqualTo("world");
}

@Test
public void shouldStoreMultilineTestRunProperties() throws Exception {
String testMethodName = "testWithTestRunPropertyMultiline";
executeTestMethodWithParams(TEST_EXAMPLES_CLASS, testMethodName, "com.testrail.junit.customjunitxml.TestRailTestReporter");
Match testsuite = readValidXmlFile(tempDirectory.resolve(REPORT_NAME));
Match testcase = testsuite.child("testcase");
assertThat(testcase.attr("name", String.class)).isEqualTo(testMethodName);
assertThat(testcase.child("properties").children("property").matchAttr("name", "testrail_case_field").attr("value")).isEqualTo("custom_steps:1. First step\n2. Second step\n3. Third step");
}

private Match readValidXmlFile(Path xmlFile) throws Exception {
assertTrue(Files.exists(xmlFile), () -> "File does not exist: " + xmlFile);
try (BufferedReader reader = Files.newBufferedReader(xmlFile)) {
Expand All @@ -398,6 +409,15 @@ static void assertValidAccordingToJenkinsSchema(Document document) throws Except
}
}

private void dumpJunitXMLReport(Path reportPath) {
System.out.println("Junit XML report: " + reportPath);
try {
Files.lines(reportPath).forEach(System.out::println);
} catch (Exception e) {
e.printStackTrace();
}
}

private enum CachedSchema {

JENKINS("/enhanced-jenkins-junit.xsd");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public void testWithMultipleTestRunProperties(TestRailTestReporter customReporte
customReporter.setProperty("my_property2", "world");
}

@Test
public void testWithTestRunPropertyMultiline(TestRailTestReporter customReporter) {
customReporter.setProperty("testrail_case_field", "custom_steps:1. First step\n2. Second step\n3. Third step");
}


@Test
@TestRail(id = "myCustomId")
public void annotatedTestWithCustomId() {
Expand Down

0 comments on commit eb6d57b

Please sign in to comment.