Skip to content

Commit d6d7985

Browse files
committed
feat: support files containing testsuites
1 parent 832005b commit d6d7985

4 files changed

Lines changed: 57 additions & 10 deletions

File tree

src/main/java/io/codeclou/java/junit/xml/merger/JunitXmlParser.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.io.File;
4444
import java.io.FileOutputStream;
4545
import java.io.IOException;
46+
import java.util.Collection;
4647

4748
public class JunitXmlParser {
4849

@@ -51,22 +52,47 @@ public class JunitXmlParser {
5152
private Boolean hasCmdLineParameterErrors = false;
5253
private Boolean hasFileNotFoundErrors = false;
5354

54-
protected TestSuite parseTestSuite(File filename) throws ParserConfigurationException, SAXException, IOException {
55+
protected Collection<TestSuite> parseTestSuites(File filename) throws ParserConfigurationException, SAXException, IOException {
5556
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
5657
DocumentBuilder builder = factory.newDocumentBuilder();
5758
Document document = builder.parse(filename);
5859
return transform(document.getFirstChild());
5960
}
6061

61-
public TestSuite transform(Node testSuite) {
62+
public Collection<TestSuite> transform(Node testSuite) {
63+
System.out.println();
64+
Collection<TestSuite> testSuites = new java.util.ArrayList<>();
65+
if (testSuite.getNodeName().equals("testsuites")) {
66+
// Already a collection
67+
for (int i = 0; i < testSuite.getChildNodes().getLength(); i++) {
68+
Node child = testSuite.getChildNodes().item(i);
69+
if (child.getNodeName().equals("testsuite")) {
70+
testSuites.add(transformTestSuite(child));
71+
}
72+
}
73+
} else {
74+
TestSuite t = transformTestSuite(testSuite);
75+
testSuites.add(t);
76+
}
77+
return testSuites;
78+
}
79+
80+
protected TestSuite parseTestSuite(File filename) throws ParserConfigurationException, SAXException, IOException {
81+
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
82+
DocumentBuilder builder = factory.newDocumentBuilder();
83+
Document document = builder.parse(filename);
84+
return transformTestSuite(document.getFirstChild());
85+
}
86+
87+
public TestSuite transformTestSuite(Node testSuite) {
6288
TestSuite t = new TestSuite();
6389
NamedNodeMap attrs = testSuite.getAttributes();
6490
t.setTests(attrs.getNamedItem("tests") != null ? Long.valueOf(attrs.getNamedItem("tests").getNodeValue()) : 0L);
65-
t.setErrors(attrs.getNamedItem("errors") != null ? Long.valueOf(testSuite.getAttributes().getNamedItem("errors").getNodeValue()) : 0L);
66-
t.setFailures(attrs.getNamedItem("failures") != null ? Long.valueOf(testSuite.getAttributes().getNamedItem("failures").getNodeValue()) : 0L);
67-
t.setSkipped(attrs.getNamedItem("skipped") != null ? Long.valueOf(testSuite.getAttributes().getNamedItem("skipped").getNodeValue()) : 0L);
68-
t.setName(testSuite.getAttributes().getNamedItem("name").getNodeValue());
69-
t.setTime(attrs.getNamedItem("time") != null ? Double.valueOf(testSuite.getAttributes().getNamedItem("time").getNodeValue()) : 0.0);
91+
t.setErrors(attrs.getNamedItem("errors") != null ? Long.valueOf(attrs.getNamedItem("errors").getNodeValue()) : 0L);
92+
t.setFailures(attrs.getNamedItem("failures") != null ? Long.valueOf(attrs.getNamedItem("failures").getNodeValue()) : 0L);
93+
t.setSkipped(attrs.getNamedItem("skipped") != null ? Long.valueOf(attrs.getNamedItem("skipped").getNodeValue()) : 0L);
94+
t.setName(attrs.getNamedItem("name").getNodeValue());
95+
t.setTime(attrs.getNamedItem("time") != null ? Double.valueOf(attrs.getNamedItem("time").getNodeValue()) : 0.0);
7096
t.setXml(testSuite);
7197
return t;
7298
}
@@ -118,7 +144,7 @@ protected void run(String[] args) throws Exception {
118144
if (f.getAbsoluteFile().toString().endsWith(".xml")) {
119145
System.out.println("\033[32;1;2mInfo >> adding " + f.getName() + " to TestSuites\033[0m");
120146
try {
121-
suites.getTestSuites().add(parseTestSuite(f));
147+
suites.getTestSuites().addAll(parseTestSuites(f));
122148
} catch (Exception e) {
123149
System.out.println("\033[31;1mError >> the file " + f.getName() + " cannot be read: ignored\033[0m");
124150
e.printStackTrace();

src/test/java/io/codeclou/java/junit/xml/merger/JunitXmlParserTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.mockito.internal.util.reflection.Whitebox;
2929

3030
import java.io.File;
31+
import java.util.Collection;
3132

3233
import static junit.framework.TestCase.assertFalse;
3334
import static org.junit.Assert.*;
@@ -39,6 +40,13 @@ private File getTestFile(String filename) {
3940
return new File(classLoader.getResource(filename).getFile());
4041
}
4142

43+
@Test
44+
public void testParseSuites() throws Exception {
45+
JunitXmlParser parser = new JunitXmlParser();
46+
Collection<TestSuite> c = parser.parseTestSuites(getTestFile("testsuites.xml"));
47+
assertFalse(c.isEmpty());
48+
}
49+
4250
@Test
4351
public void testParse() throws Exception {
4452
JunitXmlParser parser = new JunitXmlParser();

src/test/java/io/codeclou/java/junit/xml/merger/model/TestSuitesTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public void testPojoGetterSetter() {
3939
@Test
4040
public void testToXml() throws Exception {
4141
JunitXmlParser jxml = new JunitXmlParser();
42-
TestSuite suite1 = jxml.transform(XmlHelper.xmlFromString("<testsuite name='foo' time='4.20' errors='1' tests='3' failures='5' skipped='2'></testsuite>".replaceAll("'", "\"")));
43-
TestSuite suite2 = jxml.transform(XmlHelper.xmlFromString("<testsuite name='bar' time='21.01' errors='2' tests='4' failures='3' skipped='6'></testsuite>".replaceAll("'", "\"")));
42+
TestSuite suite1 = jxml.transformTestSuite(XmlHelper.xmlFromString("<testsuite name='foo' time='4.20' errors='1' tests='3' failures='5' skipped='2'></testsuite>".replaceAll("'", "\"")));
43+
TestSuite suite2 = jxml.transformTestSuite(XmlHelper.xmlFromString("<testsuite name='bar' time='21.01' errors='2' tests='4' failures='3' skipped='6'></testsuite>".replaceAll("'", "\"")));
4444
TestSuites suites = new TestSuites();
4545
suites.setName("foobar23");
4646
suites.getTestSuites().add(suite1);

src/test/resources/testsuites.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<testsuites>
3+
<testsuite tests="3" failures="0" name="ut.io.codeclou.customfield.editor.model.rest.SortModelTest" time="0.029" errors="0" skipped="0">
4+
<testcase classname="ut.io.codeclou.customfield.editor.model.rest.SortModelTest" name="testIsValid" time="0"/>
5+
<testcase classname="ut.io.codeclou.customfield.editor.model.rest.SortModelTest" name="testToSortCollator" time="0.029"/>
6+
<testcase classname="ut.io.codeclou.customfield.editor.model.rest.SortModelTest" name="testIsDescending" time="0"/>
7+
</testsuite>
8+
<testsuite tests="3" failures="0" name="ut.io.codeclou.customfield.editor.model.rest.SortModelTest" time="0.029" errors="0" skipped="0">
9+
<testcase classname="ut.io.codeclou.customfield.editor.model.rest.SortModelTest" name="testIsValid" time="0"/>
10+
<testcase classname="ut.io.codeclou.customfield.editor.model.rest.SortModelTest" name="testToSortCollator" time="0.029"/>
11+
<testcase classname="ut.io.codeclou.customfield.editor.model.rest.SortModelTest" name="testIsDescending" time="0"/>
12+
</testsuite>
13+
</testsuites>

0 commit comments

Comments
 (0)