Skip to content

Commit

Permalink
Prune control characters from Xml CDATA
Browse files Browse the repository at this point in the history
Closes #1566
  • Loading branch information
krmahadevan committed Oct 5, 2017
1 parent 412a3ba commit b5ff38f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Current
Fixed: GITHUB-1566: Invalid XML characters in Params in testng-results.xml (Krishnan Mahadevan)
Fixed: GITHUB-1554: @Parameters and parameter injection not wroking when used on the same method (Krishnan Mahadevan)
Fixed: GITHUB-990: NullPointerExceptions after a superclass configuration method fails with configfailurepolicy="continue" (Krishnan Mahadevan)
Fixed: GITHUB-461 : Annotate annotations with @Documented (Krishnan Mahadevan)
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/testng/reporters/XMLStringBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ public void setDefaultComment(String defaultComment) {
* Add a CDATA tag.
*/
public void addCDATA(String content) {
if (content != null) {
content = content.replaceAll("\\p{Cc}", "");
}
m_buffer.append(m_currentIndent);
if (content == null) {
m_buffer.append("<![CDATA[null]]>");
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/test/reports/Issue1566Sample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package test.reports;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class Issue1566Sample {
@DataProvider
public Object[][] dataProvider() {
return new Object[][]{{
"test \u000C"
}};
}

@Test(dataProvider = "dataProvider")
public void test(String argument) {
}
}
37 changes: 37 additions & 0 deletions src/test/java/test/reports/XmlReporterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package test.reports;

import org.testng.Assert;
import org.testng.TestNG;
import org.testng.annotations.Test;
import org.testng.reporters.XMLReporter;
import test.SimpleBaseTest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.UUID;
import java.util.regex.Pattern;

public class XmlReporterTest extends SimpleBaseTest {
@Test(description = "GITHUB-1566")
public void testMethod() throws IOException {
String suiteName = UUID.randomUUID().toString();
File fileLocation = createDirInTempDir(suiteName);
TestNG testng = create(fileLocation.toPath(), Issue1566Sample.class);
testng.setUseDefaultListeners(true);
testng.run();
File file = new File(fileLocation, XMLReporter.FILE_NAME);
boolean flag = false;
Pattern pattern = Pattern.compile("\\p{Cc}");
try(BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
if (pattern.matcher(line).find()) {
flag = true;
}
}
}
Assert.assertFalse(flag, "Should not have found a control character");
}
}
1 change: 1 addition & 0 deletions src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
-->
<class name="test.simple.IncludedExcludedTest" />
<class name="test.reports.ReportTest" />
<class name="test.reports.XmlReporterTest"/>
<class name="test.annotationtransformer.AnnotationTransformerTest" />
<!--
<class name="test.jar.JarTest" />
Expand Down

0 comments on commit b5ff38f

Please sign in to comment.