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
2424package io .codeclou .java .junit .xml .merger ;
2525
2626import io .codeclou .java .junit .xml .merger .model .TestSuite ;
27+ import io .codeclou .java .junit .xml .merger .model .TestSuites ;
2728import org .apache .commons .cli .*;
2829import org .w3c .dom .Document ;
2930import org .w3c .dom .Node ;
3233import javax .xml .parsers .DocumentBuilder ;
3334import javax .xml .parsers .DocumentBuilderFactory ;
3435import 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 ;
3542import java .io .File ;
43+ import java .io .FileOutputStream ;
3644import java .io .IOException ;
3745
3846public 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}
0 commit comments