forked from codeclou/java-junit-xml-merger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJunitXmlParser.java
More file actions
162 lines (153 loc) · 7.78 KB
/
Copy pathJunitXmlParser.java
File metadata and controls
162 lines (153 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* MIT License
*
* Copyright (c) 2017 Bernhard Grünewaldt
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.codeclou.java.junit.xml.merger;
import io.codeclou.java.junit.xml.merger.model.TestSuite;
import io.codeclou.java.junit.xml.merger.model.TestSuites;
import org.apache.commons.cli.*;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
public class JunitXmlParser {
private CommandLineParser parser = new DefaultParser();
private Options options = new Options();
private Boolean hasCmdLineParameterErrors = false;
private Boolean hasFileNotFoundErrors = false;
protected Collection<TestSuite> parseTestSuites(File filename) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(filename);
return transform(document.getFirstChild());
}
public Collection<TestSuite> transform(Node testSuite) {
System.out.println();
Collection<TestSuite> testSuites = new java.util.ArrayList<>();
if (testSuite.getNodeName().equals("testsuites")) {
// Already a collection
for (int i = 0; i < testSuite.getChildNodes().getLength(); i++) {
Node child = testSuite.getChildNodes().item(i);
if (child.getNodeName().equals("testsuite")) {
testSuites.add(transformTestSuite(child));
}
}
} else {
TestSuite t = transformTestSuite(testSuite);
testSuites.add(t);
}
return testSuites;
}
protected TestSuite parseTestSuite(File filename) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(filename);
return transformTestSuite(document.getFirstChild());
}
public TestSuite transformTestSuite(Node testSuite) {
TestSuite t = new TestSuite();
NamedNodeMap attrs = testSuite.getAttributes();
t.setTests(attrs.getNamedItem("tests") != null ? Long.valueOf(attrs.getNamedItem("tests").getNodeValue()) : 0L);
t.setErrors(attrs.getNamedItem("errors") != null ? Long.valueOf(attrs.getNamedItem("errors").getNodeValue()) : 0L);
t.setFailures(attrs.getNamedItem("failures") != null ? Long.valueOf(attrs.getNamedItem("failures").getNodeValue()) : 0L);
t.setSkipped(attrs.getNamedItem("skipped") != null ? Long.valueOf(attrs.getNamedItem("skipped").getNodeValue()) : 0L);
t.setName(attrs.getNamedItem("name").getNodeValue());
t.setTime(attrs.getNamedItem("time") != null ? Double.valueOf(attrs.getNamedItem("time").getNodeValue()) : 0.0);
t.setXml(testSuite);
return t;
}
protected void run(String[] args) throws Exception {
Option option = new Option("i", "inputDir", true, "input dir that contains xml files");
options.addOption(option);
options.addOption("o", "output", true, "output xml file");
options.addOption("s", "suiteName", true, "suite name");
CommandLine cmd = this.parser.parse(options, args);
System.out.println("\033[32;1;2m+-------------------------+\033[0m");
System.out.println("\033[32;1;2m| Java Junit Xml Merger |\033[0m");
System.out.println("\033[32;1;2m+-------------------------+\033[0m");
if (!cmd.hasOption("inputDir")) {
System.out.println("\033[31;1mError >> Please specify inputDir with -i\033[0m");
hasCmdLineParameterErrors = true;
}
if (!cmd.hasOption("output")) {
System.out.println("\033[31;1mError >> Please specify output with -o\033[0m");
hasCmdLineParameterErrors = true;
}
if (!cmd.hasOption("suiteName")) {
System.out.println("\033[31;1mError >> Please specify suiteName with -s\033[0m");
hasCmdLineParameterErrors = true;
}
if (!hasCmdLineParameterErrors) {
System.out.println("\033[32;1;2mSuccess >> All input parameters ok\033[0m");
File outputFile = new File(cmd.getOptionValue("output"));
File inputFileDir = new File(cmd.getOptionValue("inputDir"));
try {
// "touch"/"overwrite" file
new FileOutputStream(outputFile).close();
} catch (IOException e) {
hasFileNotFoundErrors = true;
System.out.println("\033[31;1mError >> Outputfile not writeable\033[0m");
System.out.println(outputFile.getAbsolutePath());
}
if (!inputFileDir.isDirectory()) {
hasFileNotFoundErrors = true;
System.out.println("\033[31;1mError >> Input dir not readable\033[0m");
System.out.println(inputFileDir.getAbsolutePath());
}
if (!hasFileNotFoundErrors) {
System.out.println("\033[32;1;2mSuccess >> All files and folders ok\033[0m");
TestSuites suites = new TestSuites();
suites.setName(cmd.getOptionValue("suiteName"));
File[] filesList = inputFileDir.listFiles();
for (File f : filesList) {
if (f.getAbsoluteFile().toString().endsWith(".xml")) {
System.out.println("\033[32;1;2mInfo >> adding " + f.getName() + " to TestSuites\033[0m");
try {
suites.getTestSuites().addAll(parseTestSuites(f));
} catch (Exception e) {
System.out.println("\033[31;1mError >> the file " + f.getName() + " cannot be read: ignored\033[0m");
e.printStackTrace();
}
}
}
Document xml = suites.toXml();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
Result output = new StreamResult(outputFile);
Source input = new DOMSource(xml);
transformer.transform(input, output);
}
}
}
}