Clover icon

java-junit-xml-merger 1.0.1

  1. Project Clover database Mi Okt 4 2017 15:38:24 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

24
58
3
1
131
100
16
0,28
19,33
3
5,33

Classes

Class Line # Actions
JunitXmlParser 47 58 0% 16 0
1.0100%
 

Contributing tests

This file is covered by 9 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.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   
 
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   
 
54  5 toggle 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   
 
61  7 toggle 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   
 
74  6 toggle 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    // "touch"/"overwrite" file
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    }