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