22
33import com .beust .jcommander .Parameter ;
44import com .beust .jcommander .Parameters ;
5- import org .utplsql .api .*;
5+ import org .utplsql .api .FileMapperOptions ;
6+ import org .utplsql .api .KeyValuePair ;
7+ import org .utplsql .api .TestRunner ;
8+ import org .utplsql .api .Version ;
69import org .utplsql .api .compatibility .CompatibilityProxy ;
710import org .utplsql .api .exception .SomeTestsFailedException ;
8- import org .utplsql .api .reporter .CoverageHTMLReporter ;
911import org .utplsql .api .reporter .Reporter ;
1012import org .utplsql .api .reporter .ReporterFactory ;
1113import org .utplsql .cli .exception .DatabaseConnectionFailed ;
1214
1315import java .io .File ;
14- import java .io .FileNotFoundException ;
15- import java .io .FileOutputStream ;
16- import java .io .PrintStream ;
17- import java .nio .file .Paths ;
1816import java .sql .Connection ;
1917import java .sql .SQLException ;
2018import java .util .ArrayList ;
@@ -100,6 +98,8 @@ public class RunCommand {
10098
10199
102100 private CompatibilityProxy compatibilityProxy ;
101+ private ReporterFactory reporterFactory ;
102+ private ReporterManager reporterManager ;
103103
104104 public ConnectionInfo getConnectionInfo () {
105105 return connectionInfoList .get (0 );
@@ -116,7 +116,6 @@ public int run() throws Exception {
116116 final ConnectionInfo ci = getConnectionInfo ();
117117
118118 final List <Reporter > reporterList ;
119- final List <ReporterOptions > reporterOptionsList = getReporterOptionsList ();
120119 final List <String > testPaths = getTestPaths ();
121120
122121 final File baseDir = new File ("" ).getAbsoluteFile ();
@@ -154,8 +153,9 @@ public int run() throws Exception {
154153
155154 // First of all do a compatibility check and fail-fast
156155 compatibilityProxy = checkFrameworkCompatibility (conn );
156+ reporterFactory = ReporterFactoryProvider .createReporterFactory (compatibilityProxy );
157157
158- reporterList = initReporters (conn , reporterOptionsList );
158+ reporterList = getReporterManager (). initReporters (conn , reporterFactory , compatibilityProxy );
159159
160160 } catch (SQLException e ) {
161161 if ( e .getErrorCode () == 1017 || e .getErrorCode () == 12514 ) {
@@ -199,78 +199,15 @@ public int run() throws Exception {
199199 });
200200
201201 // Gather each reporter results on a separate thread.
202- startReporterGatherers (reporterOptionsList , executorService , ci , returnCode );
202+ getReporterManager (). startReporterGatherers (executorService , ci , returnCode );
203203
204204 executorService .shutdown ();
205205 executorService .awaitTermination (60 , TimeUnit .MINUTES );
206206 return returnCode [0 ];
207207 }
208208
209- /** Initializes the reporters so we can use the id to gather results
210- *
211- * @param conn Active Connection
212- * @param reporterOptionsList
213- * @return List of Reporters
214- * @throws SQLException
215- */
216- private List <Reporter > initReporters ( Connection conn , List <ReporterOptions > reporterOptionsList ) throws SQLException
217- {
218- final List <Reporter > reporterList = new ArrayList <>();
219209
220- for (ReporterOptions ro : reporterOptionsList ) {
221- Reporter reporter = ReporterFactory .createReporter (ro .getReporterName ());
222210
223- // Quick-hack for CoverageHTML Reporter
224- if ( reporter instanceof CoverageHTMLReporter && ro .outputToFile () ) {
225- ((CoverageHTMLReporter )reporter ).setAssetsPath (ro .getOutputFileName ()+"_assets/" );
226- CoverageHTMLReporter .writeReportAssetsTo (Paths .get (ro .getOutputFileName ()+"_assets/" ));
227- }
228-
229- reporter .init (conn );
230- ro .setReporterObj (reporter );
231- reporterList .add (reporter );
232- }
233-
234- return reporterList ;
235- }
236-
237- /** Starts a separate thread for each Reporter to gather its results
238- *
239- * @param reporterOptionsList
240- * @param executorService
241- * @param ci
242- * @param returnCode
243- */
244- private void startReporterGatherers (List <ReporterOptions > reporterOptionsList , ExecutorService executorService , final ConnectionInfo ci , final int [] returnCode )
245- {
246- // Gather each reporter results on a separate thread.
247- for (ReporterOptions ro : reporterOptionsList ) {
248- executorService .submit (() -> {
249- List <PrintStream > printStreams = new ArrayList <>();
250- PrintStream fileOutStream = null ;
251-
252- try (Connection conn = ci .getConnection ()) {
253- if (ro .outputToScreen ()) {
254- printStreams .add (System .out );
255- }
256-
257- if (ro .outputToFile ()) {
258- fileOutStream = new PrintStream (new FileOutputStream (ro .getOutputFileName ()));
259- printStreams .add (fileOutStream );
260- }
261-
262- new OutputBuffer (ro .getReporterObj ()).printAvailable (conn , printStreams );
263- } catch (SQLException | FileNotFoundException e ) {
264- System .out .println (e .getMessage ());
265- returnCode [0 ] = Cli .DEFAULT_ERROR_CODE ;
266- executorService .shutdownNow ();
267- } finally {
268- if (fileOutStream != null )
269- fileOutStream .close ();
270- }
271- });
272- }
273- }
274211
275212 /** Returns FileMapperOptions for the first item of a given param list in a baseDir
276213 *
@@ -289,33 +226,6 @@ private FileMapperOptions getFileMapperOptionsByParamListItem(List<String> pathP
289226 return null ;
290227 }
291228
292- public List <ReporterOptions > getReporterOptionsList () {
293- List <ReporterOptions > reporterOptionsList = new ArrayList <>();
294- ReporterOptions reporterOptions = null ;
295-
296- for (String p : reporterParams ) {
297- if (reporterOptions == null || !p .startsWith ("-" )) {
298- reporterOptions = new ReporterOptions (p );
299- reporterOptionsList .add (reporterOptions );
300- }
301- else
302- if (p .startsWith ("-o=" )) {
303- reporterOptions .setOutputFileName (p .substring (3 ));
304- }
305- else
306- if (p .equals ("-s" )) {
307- reporterOptions .forceOutputToScreen (true );
308- }
309- }
310-
311- // If no reporter parameters were passed, use default reporter.
312- if (reporterOptionsList .isEmpty ()) {
313- reporterOptionsList .add (new ReporterOptions (CustomTypes .UT_DOCUMENTATION_REPORTER ));
314- }
315-
316- return reporterOptionsList ;
317- }
318-
319229 /** Checks whether cli is compatible with the database framework
320230 *
321231 * @param conn Active Connection
@@ -393,4 +303,15 @@ public Version getDatabaseVersion() {
393303
394304 return null ;
395305 }
306+
307+ private ReporterManager getReporterManager () {
308+ if ( reporterManager == null )
309+ reporterManager = new ReporterManager (reporterParams );
310+
311+ return reporterManager ;
312+ }
313+
314+ public List <ReporterOptions > getReporterOptionsList () {
315+ return getReporterManager ().getReporterOptionsList ();
316+ }
396317}
0 commit comments