8
8
import org .utplsql .api .Version ;
9
9
import org .utplsql .api .compatibility .CompatibilityProxy ;
10
10
import org .utplsql .api .exception .SomeTestsFailedException ;
11
- import org .utplsql .api .reporter .CoreReporters ;
12
11
import org .utplsql .api .reporter .Reporter ;
13
12
import org .utplsql .api .reporter .ReporterFactory ;
14
13
import org .utplsql .cli .exception .DatabaseConnectionFailed ;
15
- import org .utplsql .cli .reporters .ReporterOptionsAware ;
16
14
17
15
import java .io .File ;
18
- import java .io .FileNotFoundException ;
19
- import java .io .FileOutputStream ;
20
- import java .io .PrintStream ;
21
16
import java .sql .Connection ;
22
17
import java .sql .SQLException ;
23
18
import java .util .ArrayList ;
@@ -104,6 +99,7 @@ public class RunCommand {
104
99
105
100
private CompatibilityProxy compatibilityProxy ;
106
101
private ReporterFactory reporterFactory ;
102
+ private ReporterManager reporterManager ;
107
103
108
104
public ConnectionInfo getConnectionInfo () {
109
105
return connectionInfoList .get (0 );
@@ -120,7 +116,6 @@ public int run() throws Exception {
120
116
final ConnectionInfo ci = getConnectionInfo ();
121
117
122
118
final List <Reporter > reporterList ;
123
- final List <ReporterOptions > reporterOptionsList = getReporterOptionsList ();
124
119
final List <String > testPaths = getTestPaths ();
125
120
126
121
final File baseDir = new File ("" ).getAbsoluteFile ();
@@ -160,7 +155,7 @@ public int run() throws Exception {
160
155
compatibilityProxy = checkFrameworkCompatibility (conn );
161
156
reporterFactory = ReporterFactoryProvider .createReporterFactory (compatibilityProxy );
162
157
163
- reporterList = initReporters (conn , reporterOptionsList );
158
+ reporterList = getReporterManager (). initReporters (conn , reporterFactory , compatibilityProxy );
164
159
165
160
} catch (SQLException e ) {
166
161
if ( e .getErrorCode () == 1017 || e .getErrorCode () == 12514 ) {
@@ -204,76 +199,15 @@ public int run() throws Exception {
204
199
});
205
200
206
201
// Gather each reporter results on a separate thread.
207
- startReporterGatherers (reporterOptionsList , executorService , ci , returnCode );
202
+ getReporterManager (). startReporterGatherers (executorService , ci , returnCode );
208
203
209
204
executorService .shutdown ();
210
205
executorService .awaitTermination (60 , TimeUnit .MINUTES );
211
206
return returnCode [0 ];
212
207
}
213
208
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 <>();
224
209
225
- for (ReporterOptions ro : reporterOptionsList ) {
226
- Reporter reporter = reporterFactory .createReporter (ro .getReporterName ());
227
210
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
- }
277
211
278
212
/** Returns FileMapperOptions for the first item of a given param list in a baseDir
279
213
*
@@ -292,33 +226,6 @@ private FileMapperOptions getFileMapperOptionsByParamListItem(List<String> pathP
292
226
return null ;
293
227
}
294
228
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
-
322
229
/** Checks whether cli is compatible with the database framework
323
230
*
324
231
* @param conn Active Connection
@@ -396,4 +303,15 @@ public Version getDatabaseVersion() {
396
303
397
304
return null ;
398
305
}
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
+ }
399
317
}
0 commit comments