22
33import static java .lang .String .format ;
44
5- import java .io .File ;
6- import java .io .FileOutputStream ;
7- import java .io .IOException ;
8- import java .io .PrintStream ;
95import java .sql .Connection ;
106import java .sql .DriverManager ;
117import java .sql .SQLException ;
128import java .util .ArrayList ;
13- import java .util .HashMap ;
149import java .util .List ;
15- import java .util .Map ;
1610
1711import org .apache .commons .lang3 .StringUtils ;
1812import org .apache .maven .model .Resource ;
2317import org .apache .maven .plugins .annotations .Mojo ;
2418import org .apache .maven .plugins .annotations .Parameter ;
2519import org .utplsql .api .FileMapperOptions ;
26- import org .utplsql .api .OutputBuffer ;
2720import org .utplsql .api .TestRunner ;
2821import org .utplsql .api .exception .SomeTestsFailedException ;
2922import org .utplsql .api .reporter .Reporter ;
3023import org .utplsql .api .reporter .ReporterFactory ;
3124import org .utplsql .helper .PluginDefault ;
3225import org .utplsql .helper .SQLScannerHelper ;
33- import org .utplsql .model .ReporterConfiguration ;
3426import org .utplsql .model .ReporterParameter ;
27+ import org .utplsql .reporter .ReporterWriter ;
3528
3629/**
3730 * This class expose the {@link TestRunner} interface to Maven.
4235@ Mojo (name = "test" , defaultPhase = LifecyclePhase .TEST )
4336public class UtPLSQLMojo extends AbstractMojo
4437{
45- @ Parameter (defaultValue = "jdbc:oracle:thin:@localhost:1521:ut3 " )
38+ @ Parameter (defaultValue = "${dbUrl} " )
4639 private String url ;
4740
48- @ Parameter (defaultValue = "ut3 " )
41+ @ Parameter (defaultValue = "${dbUser} " )
4942 private String user ;
5043
51- @ Parameter (defaultValue = "XNtxj8eEgA6X6b6f " )
44+ @ Parameter (defaultValue = "${dbPass} " )
5245 private String password ;
5346
5447 @ Parameter
@@ -57,18 +50,11 @@ public class UtPLSQLMojo extends AbstractMojo
5750 @ Parameter
5851 private String excludeObject ;
5952
60- @ Parameter (defaultValue = "${maven.test.failure.ignore}" )
61- private boolean ignoreFailure ;
62-
63- @ Parameter (defaultValue = "false" )
64- private boolean colorConsole ;
65-
6653 @ Parameter (defaultValue = "false" )
6754 private boolean skipCompatibilityCheck ;
6855
6956 @ Parameter
7057 private List <ReporterParameter > reporters ;
71- private Map <String , ReporterParameter > mapReporters = new HashMap <>();
7258
7359 @ Parameter (defaultValue = "" )
7460 private List <String > paths ;
@@ -82,6 +68,15 @@ public class UtPLSQLMojo extends AbstractMojo
8268 @ Parameter (defaultValue = "${project.build.directory}" , readonly = true )
8369 private String targetDir ;
8470
71+ @ Parameter (defaultValue = "${maven.test.failure.ignore}" )
72+ private boolean ignoreFailure ;
73+
74+ // Color in the console, loaded by environment variables
75+ private boolean colorConsole = PluginDefault .resolveColor ();
76+
77+ // Reporter Writer
78+ private ReporterWriter reporterWriter ;
79+
8580 /**
8681 *
8782 * Execute the plugin
@@ -90,21 +85,17 @@ public class UtPLSQLMojo extends AbstractMojo
9085 @ Override
9186 public void execute () throws MojoExecutionException
9287 {
93- FileMapperOptions sourceMappingOptions = buildOptions (sources , PluginDefault .buildDefaultSource (), "sources" );
94- ;
95- FileMapperOptions testMappingOptions = buildOptions (tests , PluginDefault .buildDefaultTest (), "test" );
96-
9788 Connection connection = null ;
98- List <Reporter > reporterList = null ;
9989 try
10090 {
91+ FileMapperOptions sourceMappingOptions = buildOptions (sources , PluginDefault .buildDefaultSource (), "sources" );
92+ FileMapperOptions testMappingOptions = buildOptions (tests , PluginDefault .buildDefaultTest (), "test" );
93+
94+ // Create the Connection to the Database
10195 connection = DriverManager .getConnection (url , user , password );
102- reporterList = initReporters (connection );
96+ List < Reporter > reporterList = initReporters (connection );
10397
104- if (getLog ().isDebugEnabled ())
105- {
106- dumpParameters (sourceMappingOptions , testMappingOptions , reporterList );
107- }
98+ logParameters (sourceMappingOptions , testMappingOptions , reporterList );
10899
109100 TestRunner runner = new TestRunner ()
110101 .addPathList (paths )
@@ -116,11 +107,11 @@ public void execute() throws MojoExecutionException
116107 .failOnErrors (!ignoreFailure );
117108
118109 // Setting Optional Parameters
119- if (StringUtils .isNotEmpty (excludeObject ))
110+ if (StringUtils .isNotBlank (excludeObject ))
120111 {
121112 runner .excludeObject (excludeObject );
122113 }
123- if (StringUtils .isNotEmpty (includeObject ))
114+ if (StringUtils .isNotBlank (includeObject ))
124115 {
125116 runner .includeObject (includeObject );
126117 }
@@ -143,10 +134,8 @@ public void execute() throws MojoExecutionException
143134 try
144135 {
145136 // Write Reporters
146- for (Reporter reporter : reporterList )
147- {
148- writeReporter (connection , reporter );
149- }
137+ reporterWriter .writeReporters (connection );
138+
150139 if (connection != null )
151140 {
152141 connection .close ();
@@ -159,41 +148,6 @@ public void execute() throws MojoExecutionException
159148 }
160149 }
161150
162- /**
163- *
164- * @param connection
165- * @param reporter
166- * @throws SQLException
167- * @throws IOException
168- */
169- private void writeReporter (Connection connection , Reporter reporter ) throws SQLException , IOException
170- {
171- String outputFile = mapReporters .get (reporter .getReporterId ()).getConfiguration ().getOutputFile ();
172- if (StringUtils .isNotBlank (outputFile ) && !StringUtils .equals ("-" , outputFile ))
173- {
174- File file = new File (outputFile );
175- if (!file .isAbsolute ())
176- {
177- file = new File (targetDir , outputFile );
178- }
179- if (!file .getParentFile ().exists ())
180- {
181- file .getParentFile ().mkdirs ();
182- }
183- try (FileOutputStream fout = new FileOutputStream (file ))
184- {
185-
186- getLog ().info (format ("Writing report %s to %s" , reporter .getSelfType (), file .getAbsolutePath ()));
187- new OutputBuffer (reporter ).printAvailable (connection , new PrintStream (fout ));
188- }
189- }
190- else
191- {
192- getLog ().info (format ("Reporter %s:" , reporter .getSelfType ()));
193- new OutputBuffer (reporter ).printAvailable (connection , System .out );
194- }
195- }
196-
197151 /**
198152 *
199153 * @param resources
@@ -217,12 +171,11 @@ private FileMapperOptions buildOptions(List<Resource> resources, Resource defaul
217171 catch (Exception e )
218172 {
219173 throw new MojoExecutionException (format ("Invalid <%s> in your pom.xml: %s" , msg , e .getMessage ()));
220-
221174 }
222175 }
223176
224177 /**
225- * Init all the reports
178+ * Init all the reporters
226179 *
227180 * @param connection
228181 * @return
@@ -232,19 +185,20 @@ private List<Reporter> initReporters(Connection connection) throws SQLException
232185 {
233186 List <Reporter > reporterList = new ArrayList <>();
234187
188+ // Initializate Reporters
189+ reporterWriter = new ReporterWriter (targetDir );
190+
235191 for (ReporterParameter reporterParameter : reporters )
236192 {
237- String reporterName = reporterParameter .getId ().name ();
238- Reporter reporter = ReporterFactory .createReporter (reporterName );
193+ Reporter reporter = ReporterFactory .createReporter (reporterParameter .getName ());
239194 reporter .init (connection );
240195 reporterList .add (reporter );
241- if (reporterParameter .getConfiguration () == null
242- || StringUtils .isEmpty (reporterParameter .getConfiguration ().getOutputFile ()))
196+
197+ // Only added the reporter if at least one of the output is required
198+ if (StringUtils .isNotBlank (reporterParameter .getFileOutput ()) || reporterParameter .isConsoleOutput ())
243199 {
244- reporterParameter
245- .setConfiguration (new ReporterConfiguration (reporterParameter .getId ().getOutputFile ()));
200+ reporterWriter .addReporter (reporterParameter , reporter );
246201 }
247- mapReporters .put (reporter .getReporterId (), reporterParameter );
248202 }
249203
250204 return reporterList ;
@@ -256,10 +210,18 @@ private List<Reporter> initReporters(Connection connection) throws SQLException
256210 * @param testMappingOptions
257211 * @param reporterList
258212 */
259- private void dumpParameters (FileMapperOptions sourceMappingOptions , FileMapperOptions testMappingOptions ,
213+ private void logParameters (FileMapperOptions sourceMappingOptions , FileMapperOptions testMappingOptions ,
260214 List <Reporter > reporterList )
261215 {
262216 Log log = getLog ();
217+ log .info ("Invoking TestRunner with: " + targetDir );
218+
219+ // Do nothing when the debug is disabled
220+ if (!log .isDebugEnabled ())
221+ {
222+ return ;
223+ }
224+
263225 log .debug ("Invoking TestRunner with: " );
264226 log .debug ("reporters=" );
265227 reporterList .forEach ((Reporter r ) -> log .debug (r .getSelfType ()));
0 commit comments