2
2
3
3
import com .beust .jcommander .Parameter ;
4
4
import 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 ;
6
9
import org .utplsql .api .compatibility .CompatibilityProxy ;
7
10
import org .utplsql .api .exception .SomeTestsFailedException ;
8
- import org .utplsql .api .reporter .CoverageHTMLReporter ;
9
11
import org .utplsql .api .reporter .Reporter ;
10
12
import org .utplsql .api .reporter .ReporterFactory ;
11
13
import org .utplsql .cli .exception .DatabaseConnectionFailed ;
12
14
13
15
import java .io .File ;
14
- import java .io .FileNotFoundException ;
15
- import java .io .FileOutputStream ;
16
- import java .io .PrintStream ;
17
- import java .nio .file .Paths ;
18
16
import java .sql .Connection ;
19
17
import java .sql .SQLException ;
20
18
import java .util .ArrayList ;
@@ -100,6 +98,8 @@ public class RunCommand {
100
98
101
99
102
100
private CompatibilityProxy compatibilityProxy ;
101
+ private ReporterFactory reporterFactory ;
102
+ private ReporterManager reporterManager ;
103
103
104
104
public ConnectionInfo getConnectionInfo () {
105
105
return connectionInfoList .get (0 );
@@ -116,7 +116,6 @@ public int run() throws Exception {
116
116
final ConnectionInfo ci = getConnectionInfo ();
117
117
118
118
final List <Reporter > reporterList ;
119
- final List <ReporterOptions > reporterOptionsList = getReporterOptionsList ();
120
119
final List <String > testPaths = getTestPaths ();
121
120
122
121
final File baseDir = new File ("" ).getAbsoluteFile ();
@@ -154,8 +153,9 @@ public int run() throws Exception {
154
153
155
154
// First of all do a compatibility check and fail-fast
156
155
compatibilityProxy = checkFrameworkCompatibility (conn );
156
+ reporterFactory = ReporterFactoryProvider .createReporterFactory (compatibilityProxy );
157
157
158
- reporterList = initReporters (conn , reporterOptionsList );
158
+ reporterList = getReporterManager (). initReporters (conn , reporterFactory , compatibilityProxy );
159
159
160
160
} catch (SQLException e ) {
161
161
if ( e .getErrorCode () == 1017 || e .getErrorCode () == 12514 ) {
@@ -199,78 +199,15 @@ public int run() throws Exception {
199
199
});
200
200
201
201
// Gather each reporter results on a separate thread.
202
- startReporterGatherers (reporterOptionsList , executorService , ci , returnCode );
202
+ getReporterManager (). startReporterGatherers (executorService , ci , returnCode );
203
203
204
204
executorService .shutdown ();
205
205
executorService .awaitTermination (60 , TimeUnit .MINUTES );
206
206
return returnCode [0 ];
207
207
}
208
208
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 <>();
219
209
220
- for (ReporterOptions ro : reporterOptionsList ) {
221
- Reporter reporter = ReporterFactory .createReporter (ro .getReporterName ());
222
210
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
- }
274
211
275
212
/** Returns FileMapperOptions for the first item of a given param list in a baseDir
276
213
*
@@ -289,33 +226,6 @@ private FileMapperOptions getFileMapperOptionsByParamListItem(List<String> pathP
289
226
return null ;
290
227
}
291
228
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
-
319
229
/** Checks whether cli is compatible with the database framework
320
230
*
321
231
* @param conn Active Connection
@@ -393,4 +303,15 @@ public Version getDatabaseVersion() {
393
303
394
304
return null ;
395
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
+ }
396
317
}
0 commit comments