4343import java .io .File ;
4444import java .io .FileOutputStream ;
4545import java .io .IOException ;
46+ import java .util .Collection ;
4647
4748public class JunitXmlParser {
4849
@@ -51,23 +52,48 @@ 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 dbf = DocumentBuilderFactory .newInstance ();
5657 dbf .setFeature ("http://apache.org/xml/features/disallow-doctype-decl" , true );
5758 DocumentBuilder builder = dbf .newDocumentBuilder ();
5859 Document document = builder .parse (filename );
5960 return transform (document .getFirstChild ());
6061 }
6162
62- public TestSuite transform (Node testSuite ) {
63+ public Collection <TestSuite > transform (Node testSuite ) {
64+ System .out .println ();
65+ Collection <TestSuite > testSuites = new java .util .ArrayList <>();
66+ if (testSuite .getNodeName ().equals ("testsuites" )) {
67+ // Already a collection
68+ for (int i = 0 ; i < testSuite .getChildNodes ().getLength (); i ++) {
69+ Node child = testSuite .getChildNodes ().item (i );
70+ if (child .getNodeName ().equals ("testsuite" )) {
71+ testSuites .add (transformTestSuite (child ));
72+ }
73+ }
74+ } else {
75+ TestSuite t = transformTestSuite (testSuite );
76+ testSuites .add (t );
77+ }
78+ return testSuites ;
79+ }
80+
81+ protected TestSuite parseTestSuite (File filename ) throws ParserConfigurationException , SAXException , IOException {
82+ DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
83+ DocumentBuilder builder = factory .newDocumentBuilder ();
84+ Document document = builder .parse (filename );
85+ return transformTestSuite (document .getFirstChild ());
86+ }
87+
88+ public TestSuite transformTestSuite (Node testSuite ) {
6389 TestSuite t = new TestSuite ();
6490 NamedNodeMap attrs = testSuite .getAttributes ();
6591 t .setTests (attrs .getNamedItem ("tests" ) != null ? Long .valueOf (attrs .getNamedItem ("tests" ).getNodeValue ()) : 0L );
66- t .setErrors (attrs .getNamedItem ("errors" ) != null ? Long .valueOf (testSuite . getAttributes () .getNamedItem ("errors" ).getNodeValue ()) : 0L );
67- t .setFailures (attrs .getNamedItem ("failures" ) != null ? Long .valueOf (testSuite . getAttributes () .getNamedItem ("failures" ).getNodeValue ()) : 0L );
68- t .setSkipped (attrs .getNamedItem ("skipped" ) != null ? Long .valueOf (testSuite . getAttributes () .getNamedItem ("skipped" ).getNodeValue ()) : 0L );
69- t .setName (testSuite . getAttributes () .getNamedItem ("name" ).getNodeValue ());
70- t .setTime (attrs .getNamedItem ("time" ) != null ? Double .valueOf (testSuite . getAttributes () .getNamedItem ("time" ).getNodeValue ()) : 0.0 );
92+ t .setErrors (attrs .getNamedItem ("errors" ) != null ? Long .valueOf (attrs .getNamedItem ("errors" ).getNodeValue ()) : 0L );
93+ t .setFailures (attrs .getNamedItem ("failures" ) != null ? Long .valueOf (attrs .getNamedItem ("failures" ).getNodeValue ()) : 0L );
94+ t .setSkipped (attrs .getNamedItem ("skipped" ) != null ? Long .valueOf (attrs .getNamedItem ("skipped" ).getNodeValue ()) : 0L );
95+ t .setName (attrs .getNamedItem ("name" ).getNodeValue ());
96+ t .setTime (attrs .getNamedItem ("time" ) != null ? Double .valueOf (attrs .getNamedItem ("time" ).getNodeValue ()) : 0.0 );
7197 t .setXml (testSuite );
7298 return t ;
7399 }
@@ -119,7 +145,7 @@ protected void run(String[] args) throws Exception {
119145 if (f .getAbsoluteFile ().toString ().endsWith (".xml" )) {
120146 System .out .println ("\033 [32;1;2mInfo >> adding " + f .getName () + " to TestSuites\033 [0m" );
121147 try {
122- suites .getTestSuites ().add ( parseTestSuite (f ));
148+ suites .getTestSuites ().addAll ( parseTestSuites (f ));
123149 } catch (Exception e ) {
124150 System .out .println ("\033 [31;1mError >> the file " + f .getName () + " cannot be read: ignored\033 [0m" );
125151 e .printStackTrace ();
0 commit comments