Skip to content

Commit 823c9c1

Browse files
author
Bernhard Grünewaldt
committed
v1.0.0 done with 100% coverage
1 parent c4fd4eb commit 823c9c1

File tree

15 files changed

+153
-42
lines changed

15 files changed

+153
-42
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*.iml
22
.idea
33
target
4+
output.xml

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2017 codeclou.io
3+
Copyright (c) 2017 Bernhard Grünewaldt
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# java-junit-xml-merger
22

3-
:bangbang: WORK IN PROGRESS
3+
Merges multiple Junit XML files into a single testsuites XML file.
44

55
----
66
 
@@ -37,17 +37,22 @@ then this is for you
3737

3838
### Usage
3939

40+
With folder `reports` containing multiple `*.xml` files in junit-xml format.
41+
A combined result will be written to `junit-all.xml`
42+
4043
```
41-
java -jar java-junit-xml-merger-0.0.1-jar-with-dependencies.jar \
42-
-i=junit-report-1.xml \
43-
-i=junit-report-2.xml \
44+
# INSTALL
45+
curl -o java-junit-xml-merger-0.0.1-fatjar.jar https://????
46+
# RUN
47+
java -jar java-junit-xml-merger-0.0.1-fatjar.jar \
48+
-i=reports/ \
4449
-o=junit-all.xml \
45-
-s="My Suite"
50+
-s=My Suite
4651
```
4752

4853
-----
4954
 
5055

5156
### License
5257

53-
[MIT](https://github.com/cloutainer/k8s-jenkins-slave-deploy/blob/master/LICENSE) © [Bernhard Grünewaldt](https://github.com/clouless)
58+
[MIT](https://github.com/cloutainer/java-junit-xml-merger/blob/master/LICENSE) © [Bernhard Grünewaldt](https://github.com/clouless)

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>io.codeclou</groupId>
66
<artifactId>java-junit-xml-merger</artifactId>
7-
<version>0.0.1</version>
7+
<version>1.0.0</version>
88
<organization>
99
<name>codeclou.io</name>
1010
<url>http://codeclou.io/</url>
@@ -78,7 +78,7 @@
7878
</manifest>
7979
</archive>
8080
<descriptorRefs>
81-
<descriptorRef>jar-with-dependencies</descriptorRef>
81+
<descriptorRef>fatjar</descriptorRef>
8282
</descriptorRefs>
8383
</configuration>
8484
<executions>

src/main/java/io/codeclou/java/junit/xml/merger/JunitXmlParser.java

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* MIT License
33
*
4-
* Copyright (c) 2017 codeclou.io
4+
* Copyright (c) 2017 Bernhard Grünewaldt
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -24,6 +24,7 @@
2424
package io.codeclou.java.junit.xml.merger;
2525

2626
import io.codeclou.java.junit.xml.merger.model.TestSuite;
27+
import io.codeclou.java.junit.xml.merger.model.TestSuites;
2728
import org.apache.commons.cli.*;
2829
import org.w3c.dom.Document;
2930
import org.w3c.dom.Node;
@@ -32,13 +33,22 @@
3233
import javax.xml.parsers.DocumentBuilder;
3334
import javax.xml.parsers.DocumentBuilderFactory;
3435
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;
3542
import java.io.File;
43+
import java.io.FileOutputStream;
3644
import java.io.IOException;
3745

3846
public class JunitXmlParser {
3947

4048
private CommandLineParser parser = new DefaultParser();
4149
private Options options = new Options();
50+
private Boolean hasCmdLineParameterErrors = false;
51+
private Boolean hasFileNotFoundErrors = false;
4252

4353
protected TestSuite parseTestSuite(File filename) throws ParserConfigurationException, SAXException, IOException {
4454
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
@@ -59,21 +69,61 @@ public TestSuite transform(Node testSuite) {
5969
return t;
6070
}
6171

62-
protected void run(String[] args) throws ParseException {
63-
Option option = new Option("i", "input", true, "input files");
64-
option.setArgs(1000); // allow multiple
72+
protected void run(String[] args) throws Exception {
73+
Option option = new Option("i", "inputDir", true, "input dir that contains xml files");
6574
options.addOption(option);
66-
options.addOption("o", "output", true, "output file");
67-
CommandLine cmd = this.parser.parse( options, args);
68-
69-
System.out.println("Java Junit Xml Merger");
70-
if(cmd.hasOption("input")) {
71-
System.out.println("input ok");
72-
for (Option o : cmd.getOptions()) {
73-
System.out.println(o.getOpt() + " - " + o.getValue());
75+
options.addOption("o", "output", true, "output xml file");
76+
options.addOption("s", "suiteName", true, "suite name");
77+
CommandLine cmd = this.parser.parse(options, args);
78+
System.out.println("\033[32;1;2m+-------------------------+\033[0m");
79+
System.out.println("\033[32;1;2m| Java Junit Xml Merger |\033[0m");
80+
System.out.println("\033[32;1;2m+-------------------------+\033[0m");
81+
if (!cmd.hasOption("inputDir")) {
82+
System.out.println("\033[31;1mError >> Please specify inputDir with -i\033[0m");
83+
hasCmdLineParameterErrors = true;
84+
}
85+
if (!cmd.hasOption("output")) {
86+
System.out.println("\033[31;1mError >> Please specify output with -o\033[0m");
87+
hasCmdLineParameterErrors = true;
88+
}
89+
if (!cmd.hasOption("suiteName")) {
90+
System.out.println("\033[31;1mError >> Please specify suiteName with -s\033[0m");
91+
hasCmdLineParameterErrors = true;
92+
}
93+
if (!hasCmdLineParameterErrors) {
94+
System.out.println("\033[32;1;2mSuccess >> All input parameters ok\033[0m");
95+
File outputFile = new File(cmd.getOptionValue("output"));
96+
File inputFileDir = new File(cmd.getOptionValue("inputDir"));
97+
try {
98+
// "touch"/"overwrite" file
99+
new FileOutputStream(outputFile).close();
100+
} catch (IOException e) {
101+
hasFileNotFoundErrors = true;
102+
System.out.println("\033[31;1mError >> Outputfile not writeable\033[0m");
103+
System.out.println(outputFile.getAbsolutePath());
104+
}
105+
if (!inputFileDir.isDirectory()) {
106+
hasFileNotFoundErrors = true;
107+
System.out.println("\033[31;1mError >> Input dir not readable\033[0m");
108+
System.out.println(inputFileDir.getAbsolutePath());
109+
}
110+
if (!hasFileNotFoundErrors) {
111+
System.out.println("\033[32;1;2mSuccess >> All files and folders ok\033[0m");
112+
TestSuites suites = new TestSuites();
113+
suites.setName(cmd.getOptionValue("suiteName"));
114+
File[] filesList = inputFileDir.listFiles();
115+
for (File f : filesList) {
116+
if (f.getAbsoluteFile().toString().endsWith(".xml")) {
117+
System.out.println("\033[32;1;2mInfo >> adding " + f.getName() + " to TestSuites\033[0m");
118+
suites.getTestSuites().add(parseTestSuite(f));
119+
}
120+
}
121+
Document xml = suites.toXml();
122+
Transformer transformer = TransformerFactory.newInstance().newTransformer();
123+
Result output = new StreamResult(outputFile);
124+
Source input = new DOMSource(xml);
125+
transformer.transform(input, output);
74126
}
75-
} else {
76-
System.out.println("input fail");
77127
}
78128
}
79129
}

src/main/java/io/codeclou/java/junit/xml/merger/Merger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* MIT License
33
*
4-
* Copyright (c) 2017 codeclou.io
4+
* Copyright (c) 2017 Bernhard Grünewaldt
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal

src/main/java/io/codeclou/java/junit/xml/merger/model/TestSuite.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* MIT License
33
*
4-
* Copyright (c) 2017 codeclou.io
4+
* Copyright (c) 2017 Bernhard Grünewaldt
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal

src/main/java/io/codeclou/java/junit/xml/merger/model/TestSuites.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* MIT License
33
*
4-
* Copyright (c) 2017 codeclou.io
4+
* Copyright (c) 2017 Bernhard Grünewaldt
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -25,7 +25,6 @@
2525

2626
import org.w3c.dom.Document;
2727
import org.w3c.dom.Element;
28-
import org.w3c.dom.Node;
2928

3029
import javax.xml.parsers.DocumentBuilder;
3130
import javax.xml.parsers.DocumentBuilderFactory;

src/test/java/io/codeclou/java/junit/xml/merger/GetterSetterValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* MIT License
33
*
4-
* Copyright (c) 2017 codeclou.io
4+
* Copyright (c) 2017 Bernhard Grünewaldt
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal

src/test/java/io/codeclou/java/junit/xml/merger/JunitXmlParserTest.java

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* MIT License
33
*
4-
* Copyright (c) 2017 codeclou.io
4+
* Copyright (c) 2017 Bernhard Grünewaldt
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -25,12 +25,12 @@
2525

2626
import io.codeclou.java.junit.xml.merger.model.TestSuite;
2727
import org.junit.Test;
28+
import org.mockito.internal.util.reflection.Whitebox;
2829

2930
import java.io.File;
3031

31-
import static org.junit.Assert.assertEquals;
32-
import static org.junit.Assert.assertNotNull;
33-
import static org.junit.Assert.assertTrue;
32+
import static junit.framework.TestCase.assertFalse;
33+
import static org.junit.Assert.*;
3434

3535
public class JunitXmlParserTest {
3636

@@ -51,4 +51,65 @@ public void testParse() throws Exception {
5151
assertEquals("ut.io.codeclou.customfield.editor.model.rest.SortModelTest", t.getName());
5252
assertNotNull(t.getXml());
5353
}
54+
55+
@Test
56+
public void testRunInvalidInput1() throws Exception {
57+
String[] args = {};
58+
JunitXmlParser parser = new JunitXmlParser();
59+
parser.run(args);
60+
Boolean hasCmdLineParameterErrors = (Boolean) Whitebox.getInternalState(parser, "hasCmdLineParameterErrors");
61+
assertTrue(hasCmdLineParameterErrors);
62+
}
63+
64+
@Test
65+
public void testRunInvalidInput2() throws Exception {
66+
String[] args = {"-i=foo"};
67+
JunitXmlParser parser = new JunitXmlParser();
68+
parser.run(args);
69+
Boolean hasCmdLineParameterErrors = (Boolean) Whitebox.getInternalState(parser, "hasCmdLineParameterErrors");
70+
assertTrue(hasCmdLineParameterErrors);
71+
}
72+
73+
@Test
74+
public void testRunInvalidInput3() throws Exception {
75+
String[] args = {"-i=foo", "-o=bar.xml"};
76+
JunitXmlParser parser = new JunitXmlParser();
77+
parser.run(args);
78+
Boolean hasCmdLineParameterErrors = (Boolean) Whitebox.getInternalState(parser, "hasCmdLineParameterErrors");
79+
assertTrue(hasCmdLineParameterErrors);
80+
}
81+
82+
@Test
83+
public void testRunValidInputWithInvalidFolders() throws Exception {
84+
String[] args = {"-i=foo", "-o=?x/bar.xml", "-s=foo"};
85+
JunitXmlParser parser = new JunitXmlParser();
86+
parser.run(args);
87+
Boolean hasFileNotFoundErrors = (Boolean) Whitebox.getInternalState(parser, "hasFileNotFoundErrors");
88+
assertTrue(hasFileNotFoundErrors);
89+
}
90+
91+
@Test
92+
public void testRunValidInputWithValidFolders() throws Exception {
93+
String[] args = {"-i=src/test/resources/", "-o=output.xml", "-s=foo bar"};
94+
JunitXmlParser parser = new JunitXmlParser();
95+
parser.run(args);
96+
Boolean hasCmdLineParameterErrors = (Boolean) Whitebox.getInternalState(parser, "hasCmdLineParameterErrors");
97+
assertFalse(hasCmdLineParameterErrors);
98+
Boolean hasFileNotFoundErrors = (Boolean) Whitebox.getInternalState(parser, "hasFileNotFoundErrors");
99+
assertFalse(hasFileNotFoundErrors);
100+
}
101+
102+
@Test
103+
public void testRunValidInputWithEmptyInputFolder() throws Exception {
104+
File emptyDir = new File("src/test/resources/empty/");
105+
emptyDir.mkdir();
106+
String[] args = {"-i=src/test/resources/empty/", "-o=output.xml", "-s=foo bar"};
107+
JunitXmlParser parser = new JunitXmlParser();
108+
parser.run(args);
109+
Boolean hasCmdLineParameterErrors = (Boolean) Whitebox.getInternalState(parser, "hasCmdLineParameterErrors");
110+
assertFalse(hasCmdLineParameterErrors);
111+
Boolean hasFileNotFoundErrors = (Boolean) Whitebox.getInternalState(parser, "hasFileNotFoundErrors");
112+
assertFalse(hasFileNotFoundErrors);
113+
emptyDir.delete();
114+
}
54115
}

src/test/java/io/codeclou/java/junit/xml/merger/MergerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* MIT License
33
*
4-
* Copyright (c) 2017 codeclou.io
4+
* Copyright (c) 2017 Bernhard Grünewaldt
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal

src/test/java/io/codeclou/java/junit/xml/merger/model/TestSuiteTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* MIT License
33
*
4-
* Copyright (c) 2017 codeclou.io
4+
* Copyright (c) 2017 Bernhard Grünewaldt
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal

src/test/java/io/codeclou/java/junit/xml/merger/model/TestSuitesTest.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* MIT License
33
*
4-
* Copyright (c) 2017 codeclou.io
4+
* Copyright (c) 2017 Bernhard Grünewaldt
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -27,12 +27,6 @@
2727
import io.codeclou.java.junit.xml.merger.JunitXmlParser;
2828
import org.junit.Test;
2929
import org.w3c.dom.Document;
30-
import org.w3c.dom.Element;
31-
import org.w3c.dom.Node;
32-
33-
import javax.xml.parsers.DocumentBuilder;
34-
import javax.xml.parsers.DocumentBuilderFactory;
35-
import java.io.ByteArrayInputStream;
3630

3731
import static org.junit.Assert.assertEquals;
3832

src/test/java/io/codeclou/java/junit/xml/merger/model/XmlHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* MIT License
33
*
4-
* Copyright (c) 2017 codeclou.io
4+
* Copyright (c) 2017 Bernhard Grünewaldt
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo

0 commit comments

Comments
 (0)