Skip to content

Commit c392e42

Browse files
committed
Outsource Reporter-related things into ReporterManager
1 parent fef81ef commit c392e42

File tree

2 files changed

+132
-96
lines changed

2 files changed

+132
-96
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package org.utplsql.cli;
2+
3+
import org.utplsql.api.compatibility.CompatibilityProxy;
4+
import org.utplsql.api.reporter.CoreReporters;
5+
import org.utplsql.api.reporter.Reporter;
6+
import org.utplsql.api.reporter.ReporterFactory;
7+
import org.utplsql.cli.reporters.ReporterOptionsAware;
8+
9+
import java.io.FileNotFoundException;
10+
import java.io.FileOutputStream;
11+
import java.io.PrintStream;
12+
import java.sql.Connection;
13+
import java.sql.SQLException;
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
import java.util.concurrent.ExecutorService;
17+
18+
class ReporterManager {
19+
20+
private List<ReporterOptions> reporterOptionsList;
21+
22+
ReporterManager(List<String> reporterParams ) {
23+
initReporterOptionsList(reporterParams);
24+
}
25+
26+
private void initReporterOptionsList( List<String> reporterParams ) {
27+
reporterOptionsList = new ArrayList<>();
28+
ReporterOptions reporterOptions = null;
29+
30+
for (String p : reporterParams) {
31+
if (reporterOptions == null || !p.startsWith("-")) {
32+
reporterOptions = new ReporterOptions(p);
33+
reporterOptionsList.add(reporterOptions);
34+
}
35+
else
36+
if (p.startsWith("-o=")) {
37+
reporterOptions.setOutputFileName(p.substring(3));
38+
}
39+
else
40+
if (p.equals("-s")) {
41+
reporterOptions.forceOutputToScreen(true);
42+
}
43+
}
44+
45+
// If no reporter parameters were passed, use default reporter.
46+
if (reporterOptionsList.isEmpty()) {
47+
reporterOptionsList.add(new ReporterOptions(CoreReporters.UT_DOCUMENTATION_REPORTER.name()));
48+
}
49+
}
50+
51+
52+
/** Initializes the reporters so we can use the id to gather results
53+
*
54+
* @param conn Active Connection
55+
* @return List of Reporters
56+
* @throws SQLException
57+
*/
58+
public List<Reporter> initReporters(Connection conn, ReporterFactory reporterFactory, CompatibilityProxy compatibilityProxy) throws SQLException
59+
{
60+
final List<Reporter> reporterList = new ArrayList<>();
61+
62+
for (ReporterOptions ro : reporterOptionsList) {
63+
Reporter reporter = reporterFactory.createReporter(ro.getReporterName());
64+
65+
if ( reporter instanceof ReporterOptionsAware)
66+
((ReporterOptionsAware) reporter).setReporterOptions(ro);
67+
68+
reporter.init(conn, compatibilityProxy, reporterFactory);
69+
70+
ro.setReporterObj(reporter);
71+
reporterList.add(reporter);
72+
}
73+
74+
return reporterList;
75+
}
76+
77+
/** Starts a separate thread for each Reporter to gather its results
78+
*
79+
* @param executorService
80+
* @param ci
81+
* @param returnCode
82+
*/
83+
public void startReporterGatherers(ExecutorService executorService, final ConnectionInfo ci, final int[] returnCode)
84+
{
85+
// TODO: Implement Init-check
86+
// Gather each reporter results on a separate thread.
87+
for (ReporterOptions ro : reporterOptionsList) {
88+
executorService.submit(() -> {
89+
List<PrintStream> printStreams = new ArrayList<>();
90+
PrintStream fileOutStream = null;
91+
92+
try (Connection conn = ci.getConnection()) {
93+
if (ro.outputToScreen()) {
94+
printStreams.add(System.out);
95+
}
96+
97+
if (ro.outputToFile()) {
98+
fileOutStream = new PrintStream(new FileOutputStream(ro.getOutputFileName()));
99+
printStreams.add(fileOutStream);
100+
}
101+
102+
ro.getReporterObj().getOutputBuffer().printAvailable(conn, printStreams);
103+
} catch (SQLException | FileNotFoundException e) {
104+
System.out.println(e.getMessage());
105+
returnCode[0] = Cli.DEFAULT_ERROR_CODE;
106+
executorService.shutdownNow();
107+
} finally {
108+
if (fileOutStream != null)
109+
fileOutStream.close();
110+
}
111+
});
112+
}
113+
}
114+
115+
public List<ReporterOptions> getReporterOptionsList() {
116+
return reporterOptionsList;
117+
}
118+
}

src/main/java/org/utplsql/cli/RunCommand.java

Lines changed: 14 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,11 @@
88
import org.utplsql.api.Version;
99
import org.utplsql.api.compatibility.CompatibilityProxy;
1010
import org.utplsql.api.exception.SomeTestsFailedException;
11-
import org.utplsql.api.reporter.CoreReporters;
1211
import org.utplsql.api.reporter.Reporter;
1312
import org.utplsql.api.reporter.ReporterFactory;
1413
import org.utplsql.cli.exception.DatabaseConnectionFailed;
15-
import org.utplsql.cli.reporters.ReporterOptionsAware;
1614

1715
import java.io.File;
18-
import java.io.FileNotFoundException;
19-
import java.io.FileOutputStream;
20-
import java.io.PrintStream;
2116
import java.sql.Connection;
2217
import java.sql.SQLException;
2318
import 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

Comments
 (0)