| 1 | 
   | 
 | 
 
| 2 | 
   | 
 | 
 
| 3 | 
   | 
 | 
 
| 4 | 
   | 
 | 
 
| 5 | 
   | 
 | 
 
| 6 | 
   | 
 | 
 
| 7 | 
   | 
 | 
 
| 8 | 
   | 
 | 
 
| 9 | 
   | 
 | 
 
| 10 | 
   | 
 | 
 
| 11 | 
   | 
 | 
 
| 12 | 
   | 
 | 
 
| 13 | 
   | 
 | 
 
| 14 | 
   | 
 | 
 
| 15 | 
   | 
 | 
 
| 16 | 
   | 
 | 
 
| 17 | 
   | 
 | 
 
| 18 | 
   | 
 | 
 
| 19 | 
   | 
 | 
 
| 20 | 
   | 
 | 
 
| 21 | 
   | 
 | 
 
| 22 | 
   | 
 | 
 
| 23 | 
   | 
 | 
 
| 24 | 
   | 
package io.codeclou.java.junit.xml.merger; | 
 
| 25 | 
   | 
 | 
 
| 26 | 
   | 
import io.codeclou.java.junit.xml.merger.model.TestSuite; | 
 
| 27 | 
   | 
import io.codeclou.java.junit.xml.merger.model.TestSuites; | 
 
| 28 | 
   | 
import org.apache.commons.cli.*; | 
 
| 29 | 
   | 
import org.w3c.dom.Document; | 
 
| 30 | 
   | 
import org.w3c.dom.NamedNodeMap; | 
 
| 31 | 
   | 
import org.w3c.dom.Node; | 
 
| 32 | 
   | 
import org.xml.sax.SAXException; | 
 
| 33 | 
   | 
 | 
 
| 34 | 
   | 
import javax.xml.parsers.DocumentBuilder; | 
 
| 35 | 
   | 
import javax.xml.parsers.DocumentBuilderFactory; | 
 
| 36 | 
   | 
import javax.xml.parsers.ParserConfigurationException; | 
 
| 37 | 
   | 
import javax.xml.transform.Result; | 
 
| 38 | 
   | 
import javax.xml.transform.Source; | 
 
| 39 | 
   | 
import javax.xml.transform.Transformer; | 
 
| 40 | 
   | 
import javax.xml.transform.TransformerFactory; | 
 
| 41 | 
   | 
import javax.xml.transform.dom.DOMSource; | 
 
| 42 | 
   | 
import javax.xml.transform.stream.StreamResult; | 
 
| 43 | 
   | 
import java.io.File; | 
 
| 44 | 
   | 
import java.io.FileOutputStream; | 
 
| 45 | 
   | 
import java.io.IOException; | 
 
| 46 | 
   | 
 | 
|   | 
     | 
    
        
            
                 | 100% | 
                Uncovered Elements: 0 (85) | 
                Complexity: 16 | 
                Complexity Density: 0,28 | 
                             
         
     | 
| 47 | 
   | 
public class JunitXmlParser { | 
 
| 48 | 
   | 
 | 
 
| 49 | 
   | 
    private CommandLineParser parser = new DefaultParser(); | 
 
| 50 | 
   | 
    private Options options = new Options(); | 
 
| 51 | 
   | 
    private Boolean hasCmdLineParameterErrors = false; | 
 
| 52 | 
   | 
    private Boolean hasFileNotFoundErrors = false; | 
 
| 53 | 
   | 
 | 
|   | 
     | 
    
        
            
                 | 100% | 
                Uncovered Elements: 0 (4) | 
                Complexity: 1 | 
                Complexity Density: 0,25 | 
                             
         
     | 
| 54 | 
 5 | 
     protected TestSuite parseTestSuite(File filename) throws ParserConfigurationException, SAXException, IOException {... | 
 
| 55 | 
 5 | 
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | 
 
| 56 | 
 5 | 
        DocumentBuilder builder = factory.newDocumentBuilder(); | 
 
| 57 | 
 5 | 
        Document document = builder.parse(filename); | 
 
| 58 | 
 5 | 
        return transform(document.getFirstChild()); | 
 
| 59 | 
   | 
    } | 
 
| 60 | 
   | 
 | 
|   | 
     | 
    
        
            
                 | 100% | 
                Uncovered Elements: 0 (20) | 
                Complexity: 6 | 
                Complexity Density: 0,6 | 
                             
         
     | 
| 61 | 
 7 | 
     public TestSuite transform(Node testSuite) {... | 
 
| 62 | 
 7 | 
        TestSuite t = new TestSuite(); | 
 
| 63 | 
 7 | 
        NamedNodeMap attrs = testSuite.getAttributes(); | 
 
| 64 | 
 7 | 
        t.setTests(attrs.getNamedItem("tests") != null ? Long.valueOf(attrs.getNamedItem("tests").getNodeValue()) : 0L); | 
 
| 65 | 
 7 | 
        t.setErrors(attrs.getNamedItem("errors") != null ? Long.valueOf(testSuite.getAttributes().getNamedItem("errors").getNodeValue()) : 0L); | 
 
| 66 | 
 7 | 
        t.setFailures(attrs.getNamedItem("failures") != null ? Long.valueOf(testSuite.getAttributes().getNamedItem("failures").getNodeValue()) : 0L); | 
 
| 67 | 
 7 | 
        t.setSkipped(attrs.getNamedItem("skipped") != null ? Long.valueOf(testSuite.getAttributes().getNamedItem("skipped").getNodeValue()) : 0L); | 
 
| 68 | 
 7 | 
        t.setName(testSuite.getAttributes().getNamedItem("name").getNodeValue()); | 
 
| 69 | 
 7 | 
        t.setTime(attrs.getNamedItem("time") != null ? Double.valueOf(testSuite.getAttributes().getNamedItem("time").getNodeValue()) : 0.0); | 
 
| 70 | 
 7 | 
        t.setXml(testSuite); | 
 
| 71 | 
 7 | 
        return t; | 
 
| 72 | 
   | 
    } | 
 
| 73 | 
   | 
 | 
|   | 
     | 
    
        
            
                 | 100% | 
                Uncovered Elements: 0 (58) | 
                Complexity: 9 | 
                Complexity Density: 0,2 | 
                             
         
     | 
| 74 | 
 6 | 
     protected void run(String[] args) throws Exception {... | 
 
| 75 | 
 6 | 
        Option option = new Option("i", "inputDir", true, "input dir that contains xml files"); | 
 
| 76 | 
 6 | 
        options.addOption(option); | 
 
| 77 | 
 6 | 
        options.addOption("o", "output", true, "output xml file"); | 
 
| 78 | 
 6 | 
        options.addOption("s", "suiteName", true, "suite name"); | 
 
| 79 | 
 6 | 
        CommandLine cmd = this.parser.parse(options, args); | 
 
| 80 | 
 6 | 
        System.out.println("\033[32;1;2m+-------------------------+\033[0m"); | 
 
| 81 | 
 6 | 
        System.out.println("\033[32;1;2m|  Java Junit Xml Merger  |\033[0m"); | 
 
| 82 | 
 6 | 
        System.out.println("\033[32;1;2m+-------------------------+\033[0m"); | 
 
| 83 | 
 6 | 
        if (!cmd.hasOption("inputDir")) { | 
 
| 84 | 
 1 | 
            System.out.println("\033[31;1mError >> Please specify inputDir with -i\033[0m"); | 
 
| 85 | 
 1 | 
            hasCmdLineParameterErrors = true; | 
 
| 86 | 
   | 
        } | 
 
| 87 | 
 6 | 
        if (!cmd.hasOption("output")) { | 
 
| 88 | 
 2 | 
            System.out.println("\033[31;1mError >> Please specify output with -o\033[0m"); | 
 
| 89 | 
 2 | 
            hasCmdLineParameterErrors = true; | 
 
| 90 | 
   | 
        } | 
 
| 91 | 
 6 | 
        if (!cmd.hasOption("suiteName")) { | 
 
| 92 | 
 3 | 
            System.out.println("\033[31;1mError >> Please specify suiteName with -s\033[0m"); | 
 
| 93 | 
 3 | 
            hasCmdLineParameterErrors = true; | 
 
| 94 | 
   | 
        } | 
 
| 95 | 
 6 | 
        if (!hasCmdLineParameterErrors) { | 
 
| 96 | 
 3 | 
            System.out.println("\033[32;1;2mSuccess >> All input parameters ok\033[0m"); | 
 
| 97 | 
 3 | 
            File outputFile = new File(cmd.getOptionValue("output")); | 
 
| 98 | 
 3 | 
            File inputFileDir = new File(cmd.getOptionValue("inputDir")); | 
 
| 99 | 
 3 | 
            try { | 
 
| 100 | 
   | 
                 | 
 
| 101 | 
 3 | 
                new FileOutputStream(outputFile).close(); | 
 
| 102 | 
   | 
            } catch (IOException e) { | 
 
| 103 | 
 1 | 
                hasFileNotFoundErrors = true; | 
 
| 104 | 
 1 | 
                System.out.println("\033[31;1mError >> Outputfile not writeable\033[0m"); | 
 
| 105 | 
 1 | 
                System.out.println(outputFile.getAbsolutePath()); | 
 
| 106 | 
   | 
            } | 
 
| 107 | 
 3 | 
            if (!inputFileDir.isDirectory()) { | 
 
| 108 | 
 1 | 
                hasFileNotFoundErrors = true; | 
 
| 109 | 
 1 | 
                System.out.println("\033[31;1mError >> Input dir not readable\033[0m"); | 
 
| 110 | 
 1 | 
                System.out.println(inputFileDir.getAbsolutePath()); | 
 
| 111 | 
   | 
            } | 
 
| 112 | 
 3 | 
            if (!hasFileNotFoundErrors) { | 
 
| 113 | 
 2 | 
                System.out.println("\033[32;1;2mSuccess >> All files and folders ok\033[0m"); | 
 
| 114 | 
 2 | 
                TestSuites suites = new TestSuites(); | 
 
| 115 | 
 2 | 
                suites.setName(cmd.getOptionValue("suiteName")); | 
 
| 116 | 
 2 | 
                File[] filesList = inputFileDir.listFiles(); | 
 
| 117 | 
 2 | 
                for (File f : filesList) { | 
 
| 118 | 
 4 | 
                    if (f.getAbsoluteFile().toString().endsWith(".xml")) { | 
 
| 119 | 
 3 | 
                        System.out.println("\033[32;1;2mInfo >> adding " + f.getName() +  " to TestSuites\033[0m"); | 
 
| 120 | 
 3 | 
                        suites.getTestSuites().add(parseTestSuite(f)); | 
 
| 121 | 
   | 
                    } | 
 
| 122 | 
   | 
                } | 
 
| 123 | 
 2 | 
                Document xml = suites.toXml(); | 
 
| 124 | 
 2 | 
                Transformer transformer = TransformerFactory.newInstance().newTransformer(); | 
 
| 125 | 
 2 | 
                Result output = new StreamResult(outputFile); | 
 
| 126 | 
 2 | 
                Source input = new DOMSource(xml); | 
 
| 127 | 
 2 | 
                transformer.transform(input, output); | 
 
| 128 | 
   | 
            } | 
 
| 129 | 
   | 
        } | 
 
| 130 | 
   | 
    } | 
 
| 131 | 
   | 
} |