Skip to content

Commit 187da24

Browse files
authored
Merge pull request #65 from utPLSQL/feature/refactor_reporter_api
Make cli compatible with java-api 3.1.0 (new reporter API)
2 parents db9be5d + c392e42 commit 187da24

File tree

8 files changed

+231
-107
lines changed

8 files changed

+231
-107
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>org.utplsql</groupId>
66
<artifactId>cli</artifactId>
7-
<version>3.0.5-SNAPSHOT</version>
7+
<version>3.1.0-SNAPSHOT</version>
88
<packaging>jar</packaging>
99

1010
<name>cli</name>
@@ -22,7 +22,7 @@
2222
<dependency>
2323
<groupId>org.utplsql</groupId>
2424
<artifactId>java-api</artifactId>
25-
<version>3.0.5-SNAPSHOT</version>
25+
<version>3.1.0-SNAPSHOT</version>
2626
<scope>compile</scope>
2727
<exclusions>
2828
<exclusion>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public static void main(String[] args) {
1919
LocaleInitializer.initLocale();
2020

2121
JCommander jc = new JCommander();
22+
jc.setProgramName("utplsql");
2223
// jc.addCommand(HELP_CMD, new HelpCommand());
2324
RunCommand runCmd = new RunCommand();
2425
jc.addCommand(RUN_CMD, runCmd);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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.ReporterFactory;
6+
import org.utplsql.cli.reporters.LocalAssetsCoverageHTMLReporter;
7+
8+
/** A simple class to provide a ReporterFactory for the RunCommand
9+
*
10+
* @author pesse
11+
*/
12+
public class ReporterFactoryProvider {
13+
14+
public static ReporterFactory createReporterFactory(CompatibilityProxy proxy ) {
15+
ReporterFactory reporterFactory = ReporterFactory.createDefault(proxy);
16+
reporterFactory.registerReporterFactoryMethod(CoreReporters.UT_COVERAGE_HTML_REPORTER.name(), LocalAssetsCoverageHTMLReporter::new, "Will copy all necessary assets to a folder named after the Output-File");
17+
18+
return reporterFactory;
19+
}
20+
}
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: 20 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@
22

33
import com.beust.jcommander.Parameter;
44
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;
69
import org.utplsql.api.compatibility.CompatibilityProxy;
710
import org.utplsql.api.exception.SomeTestsFailedException;
8-
import org.utplsql.api.reporter.CoverageHTMLReporter;
911
import org.utplsql.api.reporter.Reporter;
1012
import org.utplsql.api.reporter.ReporterFactory;
1113
import org.utplsql.cli.exception.DatabaseConnectionFailed;
1214

1315
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;
1816
import java.sql.Connection;
1917
import java.sql.SQLException;
2018
import java.util.ArrayList;
@@ -100,6 +98,8 @@ public class RunCommand {
10098

10199

102100
private CompatibilityProxy compatibilityProxy;
101+
private ReporterFactory reporterFactory;
102+
private ReporterManager reporterManager;
103103

104104
public ConnectionInfo getConnectionInfo() {
105105
return connectionInfoList.get(0);
@@ -116,7 +116,6 @@ public int run() throws Exception {
116116
final ConnectionInfo ci = getConnectionInfo();
117117

118118
final List<Reporter> reporterList;
119-
final List<ReporterOptions> reporterOptionsList = getReporterOptionsList();
120119
final List<String> testPaths = getTestPaths();
121120

122121
final File baseDir = new File("").getAbsoluteFile();
@@ -154,8 +153,9 @@ public int run() throws Exception {
154153

155154
// First of all do a compatibility check and fail-fast
156155
compatibilityProxy = checkFrameworkCompatibility(conn);
156+
reporterFactory = ReporterFactoryProvider.createReporterFactory(compatibilityProxy);
157157

158-
reporterList = initReporters(conn, reporterOptionsList);
158+
reporterList = getReporterManager().initReporters(conn, reporterFactory, compatibilityProxy);
159159

160160
} catch (SQLException e) {
161161
if ( e.getErrorCode() == 1017 || e.getErrorCode() == 12514 ) {
@@ -199,78 +199,15 @@ public int run() throws Exception {
199199
});
200200

201201
// Gather each reporter results on a separate thread.
202-
startReporterGatherers(reporterOptionsList, executorService, ci, returnCode);
202+
getReporterManager().startReporterGatherers(executorService, ci, returnCode);
203203

204204
executorService.shutdown();
205205
executorService.awaitTermination(60, TimeUnit.MINUTES);
206206
return returnCode[0];
207207
}
208208

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<>();
219209

220-
for (ReporterOptions ro : reporterOptionsList) {
221-
Reporter reporter = ReporterFactory.createReporter(ro.getReporterName());
222210

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-
}
274211

275212
/** Returns FileMapperOptions for the first item of a given param list in a baseDir
276213
*
@@ -289,33 +226,6 @@ private FileMapperOptions getFileMapperOptionsByParamListItem(List<String> pathP
289226
return null;
290227
}
291228

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-
319229
/** Checks whether cli is compatible with the database framework
320230
*
321231
* @param conn Active Connection
@@ -393,4 +303,15 @@ public Version getDatabaseVersion() {
393303

394304
return null;
395305
}
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+
}
396317
}

0 commit comments

Comments
 (0)