88import org .utplsql .api .Version ;
99import org .utplsql .api .compatibility .CompatibilityProxy ;
1010import org .utplsql .api .exception .SomeTestsFailedException ;
11- import org .utplsql .api .reporter .CoreReporters ;
1211import org .utplsql .api .reporter .Reporter ;
1312import org .utplsql .api .reporter .ReporterFactory ;
1413import org .utplsql .cli .exception .DatabaseConnectionFailed ;
15- import org .utplsql .cli .reporters .ReporterOptionsAware ;
1614
1715import java .io .File ;
18- import java .io .FileNotFoundException ;
19- import java .io .FileOutputStream ;
20- import java .io .PrintStream ;
2116import java .sql .Connection ;
2217import java .sql .SQLException ;
2318import java .util .ArrayList ;
@@ -104,6 +99,7 @@ public class RunCommand {
10499
105100 private CompatibilityProxy compatibilityProxy ;
106101 private ReporterFactory reporterFactory ;
102+ private ReporterManager reporterManager ;
107103
108104 public ConnectionInfo getConnectionInfo () {
109105 return connectionInfoList .get (0 );
@@ -120,7 +116,6 @@ public int run() throws Exception {
120116 final ConnectionInfo ci = getConnectionInfo ();
121117
122118 final List <Reporter > reporterList ;
123- final List <ReporterOptions > reporterOptionsList = getReporterOptionsList ();
124119 final List <String > testPaths = getTestPaths ();
125120
126121 final File baseDir = new File ("" ).getAbsoluteFile ();
@@ -160,7 +155,7 @@ public int run() throws Exception {
160155 compatibilityProxy = checkFrameworkCompatibility (conn );
161156 reporterFactory = ReporterFactoryProvider .createReporterFactory (compatibilityProxy );
162157
163- reporterList = initReporters (conn , reporterOptionsList );
158+ reporterList = getReporterManager (). initReporters (conn , reporterFactory , compatibilityProxy );
164159
165160 } catch (SQLException e ) {
166161 if ( e .getErrorCode () == 1017 || e .getErrorCode () == 12514 ) {
@@ -204,76 +199,15 @@ public int run() throws Exception {
204199 });
205200
206201 // Gather each reporter results on a separate thread.
207- startReporterGatherers (reporterOptionsList , executorService , ci , returnCode );
202+ getReporterManager (). startReporterGatherers (executorService , ci , returnCode );
208203
209204 executorService .shutdown ();
210205 executorService .awaitTermination (60 , TimeUnit .MINUTES );
211206 return returnCode [0 ];
212207 }
213208
214- /** Initializes the reporters so we can use the id to gather results
215- *
216- * @param conn Active Connection
217- * @param reporterOptionsList
218- * @return List of Reporters
219- * @throws SQLException
220- */
221- private List <Reporter > initReporters ( Connection conn , List <ReporterOptions > reporterOptionsList ) throws SQLException
222- {
223- final List <Reporter > reporterList = new ArrayList <>();
224209
225- for (ReporterOptions ro : reporterOptionsList ) {
226- Reporter reporter = reporterFactory .createReporter (ro .getReporterName ());
227210
228- if ( reporter instanceof ReporterOptionsAware )
229- ((ReporterOptionsAware ) reporter ).setReporterOptions (ro );
230-
231- reporter .init (conn , compatibilityProxy , reporterFactory );
232-
233- ro .setReporterObj (reporter );
234- reporterList .add (reporter );
235- }
236-
237- return reporterList ;
238- }
239-
240- /** Starts a separate thread for each Reporter to gather its results
241- *
242- * @param reporterOptionsList
243- * @param executorService
244- * @param ci
245- * @param returnCode
246- */
247- private void startReporterGatherers (List <ReporterOptions > reporterOptionsList , ExecutorService executorService , final ConnectionInfo ci , final int [] returnCode )
248- {
249- // Gather each reporter results on a separate thread.
250- for (ReporterOptions ro : reporterOptionsList ) {
251- executorService .submit (() -> {
252- List <PrintStream > printStreams = new ArrayList <>();
253- PrintStream fileOutStream = null ;
254-
255- try (Connection conn = ci .getConnection ()) {
256- if (ro .outputToScreen ()) {
257- printStreams .add (System .out );
258- }
259-
260- if (ro .outputToFile ()) {
261- fileOutStream = new PrintStream (new FileOutputStream (ro .getOutputFileName ()));
262- printStreams .add (fileOutStream );
263- }
264-
265- ro .getReporterObj ().getOutputBuffer ().printAvailable (conn , printStreams );
266- } catch (SQLException | FileNotFoundException e ) {
267- System .out .println (e .getMessage ());
268- returnCode [0 ] = Cli .DEFAULT_ERROR_CODE ;
269- executorService .shutdownNow ();
270- } finally {
271- if (fileOutStream != null )
272- fileOutStream .close ();
273- }
274- });
275- }
276- }
277211
278212 /** Returns FileMapperOptions for the first item of a given param list in a baseDir
279213 *
@@ -292,33 +226,6 @@ private FileMapperOptions getFileMapperOptionsByParamListItem(List<String> pathP
292226 return null ;
293227 }
294228
295- public List <ReporterOptions > getReporterOptionsList () {
296- List <ReporterOptions > reporterOptionsList = new ArrayList <>();
297- ReporterOptions reporterOptions = null ;
298-
299- for (String p : reporterParams ) {
300- if (reporterOptions == null || !p .startsWith ("-" )) {
301- reporterOptions = new ReporterOptions (p );
302- reporterOptionsList .add (reporterOptions );
303- }
304- else
305- if (p .startsWith ("-o=" )) {
306- reporterOptions .setOutputFileName (p .substring (3 ));
307- }
308- else
309- if (p .equals ("-s" )) {
310- reporterOptions .forceOutputToScreen (true );
311- }
312- }
313-
314- // If no reporter parameters were passed, use default reporter.
315- if (reporterOptionsList .isEmpty ()) {
316- reporterOptionsList .add (new ReporterOptions (CoreReporters .UT_DOCUMENTATION_REPORTER .name ()));
317- }
318-
319- return reporterOptionsList ;
320- }
321-
322229 /** Checks whether cli is compatible with the database framework
323230 *
324231 * @param conn Active Connection
@@ -396,4 +303,15 @@ public Version getDatabaseVersion() {
396303
397304 return null ;
398305 }
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+ }
399317}
0 commit comments