Clover icon

java-junit-xml-merger 1.0.0

  1. Project Clover database Sa Sep 9 2017 13:37:19 MESZ
  2. Package io.codeclou.java.junit.xml.merger

File JunitXmlParser.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

14
57
3
1
129
98
11
0,19
19
3
3,67

Classes

Class Line # Actions
JunitXmlParser 46 57 0% 11 0
1.0100%
 

Contributing tests

This file is covered by 8 tests. .

Source view

1    /*
2    * MIT License
3    *
4    * Copyright (c) 2017 Bernhard Grünewaldt
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy
7    * of this software and associated documentation files (the "Software"), to deal
8    * in the Software without restriction, including without limitation the rights
9    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10    * copies of the Software, and to permit persons to whom the Software is
11    * furnished to do so, subject to the following conditions:
12    *
13    * The above copyright notice and this permission notice shall be included in all
14    * copies or substantial portions of the Software.
15    *
16    * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17    * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18    * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19    * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20    * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21    * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22    * SOFTWARE.
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.Node;
31    import org.xml.sax.SAXException;
32   
33    import javax.xml.parsers.DocumentBuilder;
34    import javax.xml.parsers.DocumentBuilderFactory;
35    import javax.xml.parsers.ParserConfigurationException;
36    import javax.xml.transform.Result;
37    import javax.xml.transform.Source;
38    import javax.xml.transform.Transformer;
39    import javax.xml.transform.TransformerFactory;
40    import javax.xml.transform.dom.DOMSource;
41    import javax.xml.transform.stream.StreamResult;
42    import java.io.File;
43    import java.io.FileOutputStream;
44    import java.io.IOException;
45   
 
46    public class JunitXmlParser {
47   
48    private CommandLineParser parser = new DefaultParser();
49    private Options options = new Options();
50    private Boolean hasCmdLineParameterErrors = false;
51    private Boolean hasFileNotFoundErrors = false;
52   
 
53  3 toggle protected TestSuite parseTestSuite(File filename) throws ParserConfigurationException, SAXException, IOException {
54  3 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
55  3 DocumentBuilder builder = factory.newDocumentBuilder();
56  3 Document document = builder.parse(filename);
57  3 return transform(document.getFirstChild());
58    }
59   
 
60  5 toggle public TestSuite transform(Node testSuite) {
61  5 TestSuite t = new TestSuite();
62  5 t.setTests(Long.valueOf(testSuite.getAttributes().getNamedItem("tests").getNodeValue()));
63  5 t.setErrors(Long.valueOf(testSuite.getAttributes().getNamedItem("errors").getNodeValue()));
64  5 t.setFailures(Long.valueOf(testSuite.getAttributes().getNamedItem("failures").getNodeValue()));
65  5 t.setSkipped(Long.valueOf(testSuite.getAttributes().getNamedItem("skipped").getNodeValue()));
66  5 t.setName(testSuite.getAttributes().getNamedItem("name").getNodeValue());
67  5 t.setTime(Double.valueOf(testSuite.getAttributes().getNamedItem("time").getNodeValue()));
68  5 t.setXml(testSuite);
69  5 return t;
70    }
71   
 
72  6 toggle protected void run(String[] args) throws Exception {
73  6 Option option = new Option("i", "inputDir", true, "input dir that contains xml files");
74  6 options.addOption(option);
75  6 options.addOption("o", "output", true, "output xml file");
76  6 options.addOption("s", "suiteName", true, "suite name");
77  6 CommandLine cmd = this.parser.parse(options, args);
78  6 System.out.println("\033[32;1;2m+-------------------------+\033[0m");
79  6 System.out.println("\033[32;1;2m| Java Junit Xml Merger |\033[0m");
80  6 System.out.println("\033[32;1;2m+-------------------------+\033[0m");
81  6 if (!cmd.hasOption("inputDir")) {
82  1 System.out.println("\033[31;1mError >> Please specify inputDir with -i\033[0m");
83  1 hasCmdLineParameterErrors = true;
84    }
85  6 if (!cmd.hasOption("output")) {
86  2 System.out.println("\033[31;1mError >> Please specify output with -o\033[0m");
87  2 hasCmdLineParameterErrors = true;
88    }
89  6 if (!cmd.hasOption("suiteName")) {
90  3 System.out.println("\033[31;1mError >> Please specify suiteName with -s\033[0m");
91  3 hasCmdLineParameterErrors = true;
92    }
93  6 if (!hasCmdLineParameterErrors) {
94  3 System.out.println("\033[32;1;2mSuccess >> All input parameters ok\033[0m");
95  3 File outputFile = new File(cmd.getOptionValue("output"));
96  3 File inputFileDir = new File(cmd.getOptionValue("inputDir"));
97  3 try {
98    // "touch"/"overwrite" file
99  3 new FileOutputStream(outputFile).close();
100    } catch (IOException e) {
101  1 hasFileNotFoundErrors = true;
102  1 System.out.println("\033[31;1mError >> Outputfile not writeable\033[0m");
103  1 System.out.println(outputFile.getAbsolutePath());
104    }
105  3 if (!inputFileDir.isDirectory()) {
106  1 hasFileNotFoundErrors = true;
107  1 System.out.println("\033[31;1mError >> Input dir not readable\033[0m");
108  1 System.out.println(inputFileDir.getAbsolutePath());
109    }
110  3 if (!hasFileNotFoundErrors) {
111  2 System.out.println("\033[32;1;2mSuccess >> All files and folders ok\033[0m");
112  2 TestSuites suites = new TestSuites();
113  2 suites.setName(cmd.getOptionValue("suiteName"));
114  2 File[] filesList = inputFileDir.listFiles();
115  2 for (File f : filesList) {
116  3 if (f.getAbsoluteFile().toString().endsWith(".xml")) {
117  2 System.out.println("\033[32;1;2mInfo >> adding " + f.getName() + " to TestSuites\033[0m");
118  2 suites.getTestSuites().add(parseTestSuite(f));
119    }
120    }
121  2 Document xml = suites.toXml();
122  2 Transformer transformer = TransformerFactory.newInstance().newTransformer();
123  2 Result output = new StreamResult(outputFile);
124  2 Source input = new DOMSource(xml);
125  2 transformer.transform(input, output);
126    }
127    }
128    }
129    }