Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.codeclou</groupId>
<artifactId>java-junit-xml-merger</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>
<organization>
<name>codeclou.io</name>
<url>http://codeclou.io/</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;

public class JunitXmlParser {

Expand All @@ -51,23 +52,48 @@ public class JunitXmlParser {
private Boolean hasCmdLineParameterErrors = false;
private Boolean hasFileNotFoundErrors = false;

protected TestSuite parseTestSuite(File filename) throws ParserConfigurationException, SAXException, IOException {
protected Collection<TestSuite> parseTestSuites(File filename) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
DocumentBuilder builder = dbf.newDocumentBuilder();
Document document = builder.parse(filename);
return transform(document.getFirstChild());
}

public TestSuite transform(Node testSuite) {
public Collection<TestSuite> transform(Node testSuite) {
System.out.println();
Collection<TestSuite> testSuites = new java.util.ArrayList<>();
if (testSuite.getNodeName().equals("testsuites")) {
// Already a collection
for (int i = 0; i < testSuite.getChildNodes().getLength(); i++) {
Node child = testSuite.getChildNodes().item(i);
if (child.getNodeName().equals("testsuite")) {
testSuites.add(transformTestSuite(child));
}
}
} else {
TestSuite t = transformTestSuite(testSuite);
testSuites.add(t);
}
return testSuites;
}

protected TestSuite parseTestSuite(File filename) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(filename);
return transformTestSuite(document.getFirstChild());
}

public TestSuite transformTestSuite(Node testSuite) {
TestSuite t = new TestSuite();
NamedNodeMap attrs = testSuite.getAttributes();
t.setTests(attrs.getNamedItem("tests") != null ? Long.valueOf(attrs.getNamedItem("tests").getNodeValue()) : 0L);
t.setErrors(attrs.getNamedItem("errors") != null ? Long.valueOf(testSuite.getAttributes().getNamedItem("errors").getNodeValue()) : 0L);
t.setFailures(attrs.getNamedItem("failures") != null ? Long.valueOf(testSuite.getAttributes().getNamedItem("failures").getNodeValue()) : 0L);
t.setSkipped(attrs.getNamedItem("skipped") != null ? Long.valueOf(testSuite.getAttributes().getNamedItem("skipped").getNodeValue()) : 0L);
t.setName(testSuite.getAttributes().getNamedItem("name").getNodeValue());
t.setTime(attrs.getNamedItem("time") != null ? Double.valueOf(testSuite.getAttributes().getNamedItem("time").getNodeValue()) : 0.0);
t.setErrors(attrs.getNamedItem("errors") != null ? Long.valueOf(attrs.getNamedItem("errors").getNodeValue()) : 0L);
t.setFailures(attrs.getNamedItem("failures") != null ? Long.valueOf(attrs.getNamedItem("failures").getNodeValue()) : 0L);
t.setSkipped(attrs.getNamedItem("skipped") != null ? Long.valueOf(attrs.getNamedItem("skipped").getNodeValue()) : 0L);
t.setName(attrs.getNamedItem("name").getNodeValue());
t.setTime(attrs.getNamedItem("time") != null ? Double.valueOf(attrs.getNamedItem("time").getNodeValue()) : 0.0);
t.setXml(testSuite);
return t;
}
Expand Down Expand Up @@ -119,7 +145,7 @@ protected void run(String[] args) throws Exception {
if (f.getAbsoluteFile().toString().endsWith(".xml")) {
System.out.println("\033[32;1;2mInfo >> adding " + f.getName() + " to TestSuites\033[0m");
try {
suites.getTestSuites().add(parseTestSuite(f));
suites.getTestSuites().addAll(parseTestSuites(f));
} catch (Exception e) {
System.out.println("\033[31;1mError >> the file " + f.getName() + " cannot be read: ignored\033[0m");
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import org.mockito.internal.util.reflection.Whitebox;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static junit.framework.TestCase.assertFalse;
import static org.junit.Assert.*;
Expand All @@ -39,6 +42,20 @@ private File getTestFile(String filename) {
return new File(classLoader.getResource(filename).getFile());
}

@Test
public void testParseSuites() throws Exception {
// GIVEN
JunitXmlParser parser = new JunitXmlParser();
// WHEN
Collection<TestSuite> c = parser.parseTestSuites(getTestFile("testsuites.xml"));
List<TestSuite> l = new ArrayList<>(c);
// THEN
assertFalse(c.isEmpty());
assertEquals(2, c.size());
assertEquals(l.get(0).getName(), "ut.io.codeclou.customfield.editor.model.rest.SortModelTestOne");
assertEquals(l.get(1). getName(), "ut.io.codeclou.customfield.editor.model.rest.SortModelTestTwo");
}

@Test
public void testParse() throws Exception {
JunitXmlParser parser = new JunitXmlParser();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public void testPojoGetterSetter() {
@Test
public void testToXml() throws Exception {
JunitXmlParser jxml = new JunitXmlParser();
TestSuite suite1 = jxml.transform(XmlHelper.xmlFromString("<testsuite name='foo' time='4.20' errors='1' tests='3' failures='5' skipped='2'></testsuite>".replaceAll("'", "\"")));
TestSuite suite2 = jxml.transform(XmlHelper.xmlFromString("<testsuite name='bar' time='21.01' errors='2' tests='4' failures='3' skipped='6'></testsuite>".replaceAll("'", "\"")));
TestSuite suite1 = jxml.transformTestSuite(XmlHelper.xmlFromString("<testsuite name='foo' time='4.20' errors='1' tests='3' failures='5' skipped='2'></testsuite>".replaceAll("'", "\"")));
TestSuite suite2 = jxml.transformTestSuite(XmlHelper.xmlFromString("<testsuite name='bar' time='21.01' errors='2' tests='4' failures='3' skipped='6'></testsuite>".replaceAll("'", "\"")));
TestSuites suites = new TestSuites();
suites.setName("foobar23");
suites.getTestSuites().add(suite1);
Expand Down
13 changes: 13 additions & 0 deletions src/test/resources/testsuites.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<testsuites>
<testsuite tests="3" failures="0" name="ut.io.codeclou.customfield.editor.model.rest.SortModelTestOne" time="0.029" errors="0" skipped="0">
<testcase classname="ut.io.codeclou.customfield.editor.model.rest.SortModelTest" name="testIsValid" time="0"/>
<testcase classname="ut.io.codeclou.customfield.editor.model.rest.SortModelTest" name="testToSortCollator" time="0.029"/>
<testcase classname="ut.io.codeclou.customfield.editor.model.rest.SortModelTest" name="testIsDescending" time="0"/>
</testsuite>
<testsuite tests="3" failures="0" name="ut.io.codeclou.customfield.editor.model.rest.SortModelTestTwo" time="0.029" errors="0" skipped="0">
<testcase classname="ut.io.codeclou.customfield.editor.model.rest.SortModelTest" name="testIsValid" time="0"/>
<testcase classname="ut.io.codeclou.customfield.editor.model.rest.SortModelTest" name="testToSortCollator" time="0.029"/>
<testcase classname="ut.io.codeclou.customfield.editor.model.rest.SortModelTest" name="testIsDescending" time="0"/>
</testsuite>
</testsuites>