4343import java .io .File ;
4444import java .io .FileOutputStream ;
4545import java .io .IOException ;
46+ import java .util .Collection ;
4647
4748public 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 ();
0 commit comments