Skip to content

Commit 4f14802

Browse files
committed
Add support for multiple reporters
1 parent 0148224 commit 4f14802

File tree

3 files changed

+64
-32
lines changed

3 files changed

+64
-32
lines changed

src/main/java/io/github/utplsql/cli/ReporterOptions.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.github.utplsql.cli;
22

3-
import com.sun.istack.internal.NotNull;
3+
import io.github.utplsql.api.types.BaseReporter;
44

55
/**
66
* Created by Vinicius on 20/05/2017.
@@ -12,6 +12,8 @@ public class ReporterOptions {
1212
private boolean outputToScreen;
1313
private boolean forceOutputToScreen;
1414

15+
private BaseReporter reporterObj = null;
16+
1517
public ReporterOptions(String reporterName, String outputFileName, boolean outputToScreen) {
1618
setReporterName(reporterName);
1719
setOutputFileName(outputFileName);
@@ -23,13 +25,20 @@ public ReporterOptions(String reporterName) {
2325
this(reporterName, null, true);
2426
}
2527

28+
public BaseReporter getReporterObj() {
29+
return reporterObj;
30+
}
31+
32+
public void setReporterObj(BaseReporter reporterObj) {
33+
this.reporterObj = reporterObj;
34+
}
35+
2636
public String getReporterName() {
27-
return reporterName;
37+
return reporterName.toUpperCase();
2838
}
2939

30-
@NotNull
3140
public void setReporterName(String reporterName) {
32-
this.reporterName = reporterName.toUpperCase();
41+
this.reporterName = reporterName;
3342
}
3443

3544
public String getOutputFileName() {

src/main/java/io/github/utplsql/cli/RunCommand.java

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import io.github.utplsql.api.TestRunner;
77
import io.github.utplsql.api.types.BaseReporter;
88
import io.github.utplsql.api.types.CustomTypes;
9-
import io.github.utplsql.api.types.DocumentationReporter;
109

10+
import java.io.FileNotFoundException;
11+
import java.io.FileOutputStream;
12+
import java.io.PrintStream;
1113
import java.sql.Connection;
1214
import java.sql.SQLException;
1315
import java.util.ArrayList;
@@ -45,11 +47,8 @@ public ConnectionInfo getConnectionInfo() {
4547
return connectionInfoList.get(0);
4648
}
4749

48-
public String getTestPaths() {
49-
// if (testPaths != null && testPaths.size() > 1)
50-
// throw new RuntimeException("Multiple test paths not supported yet.");
51-
52-
return (testPaths == null) ? null : String.join(",", testPaths);
50+
public List<String> getTestPaths() {
51+
return testPaths;
5352
}
5453

5554
public List<ReporterOptions> getReporterOptionsList() {
@@ -73,7 +72,7 @@ public List<ReporterOptions> getReporterOptionsList() {
7372

7473
// If no reporter parameters were passed, use default reporter.
7574
if (reporterOptionsList.isEmpty()) {
76-
reporterOptionsList.add(new ReporterOptions(CustomTypes.UT_DOCUMENTATION_REPORTER.getName()));
75+
reporterOptionsList.add(new ReporterOptions(CustomTypes.UT_DOCUMENTATION_REPORTER));
7776
}
7877

7978
return reporterOptionsList;
@@ -83,38 +82,62 @@ public void run() throws Exception {
8382
final ConnectionInfo ci = getConnectionInfo();
8483
System.out.println("Running Tests For: " + ci.toString());
8584

86-
String tempTestPaths = getTestPaths();
87-
if (tempTestPaths == null) tempTestPaths = ci.getUser();
85+
final List<ReporterOptions> reporterOptionsList = getReporterOptionsList();
86+
final List<BaseReporter> reporterList = new ArrayList<>();
87+
final List<String> testPaths = getTestPaths();
8888

89-
final BaseReporter reporter = new DocumentationReporter();
90-
final String testPaths = tempTestPaths;
89+
if (testPaths.isEmpty()) testPaths.add(ci.getUser());
9190

91+
// Do the reporters initialization, so we can use the id to run and gather results.
9292
try (Connection conn = ci.getConnection()) {
93-
reporter.init(conn);
93+
for (ReporterOptions ro : reporterOptionsList) {
94+
BaseReporter reporter = CustomTypes.createReporter(ro.getReporterName());
95+
reporter.init(conn);
96+
ro.setReporterObj(reporter);
97+
reporterList.add(reporter);
98+
}
9499
} catch (SQLException e) {
95100
// TODO
96101
e.printStackTrace();
97102
}
98103

99-
ExecutorService executorService = Executors.newFixedThreadPool(2);
104+
ExecutorService executorService = Executors.newFixedThreadPool(1 + reporterList.size());
100105

101106
executorService.submit(() -> {
102107
try (Connection conn = ci.getConnection()){
103-
new TestRunner().run(conn, testPaths, reporter);
108+
new TestRunner().run(conn, testPaths, reporterList);
104109
} catch (SQLException e) {
105110
// TODO
106111
e.printStackTrace();
107112
}
108113
});
109114

110-
executorService.submit(() -> {
111-
try (Connection conn = ci.getConnection()){
112-
new OutputBuffer(reporter).printAvailable(conn, System.out);
113-
} catch (SQLException e) {
114-
// TODO
115-
e.printStackTrace();
116-
}
117-
});
115+
116+
for (ReporterOptions ro : reporterOptionsList) {
117+
executorService.submit(() -> {
118+
List<PrintStream> printStreams = new ArrayList<>();
119+
PrintStream fileOutStream = null;
120+
121+
try (Connection conn = ci.getConnection()) {
122+
if (ro.outputToScreen()) {
123+
printStreams.add(System.out);
124+
}
125+
126+
if (ro.outputToFile()) {
127+
fileOutStream = new PrintStream(new FileOutputStream(ro.getOutputFileName()));
128+
printStreams.add(fileOutStream);
129+
}
130+
131+
new OutputBuffer(ro.getReporterObj()).printAvailable(conn, printStreams);
132+
} catch (SQLException | FileNotFoundException e) {
133+
// TODO
134+
e.printStackTrace();
135+
} finally {
136+
if (fileOutStream != null)
137+
fileOutStream.close();
138+
}
139+
});
140+
}
118141

119142
executorService.shutdown();
120143
executorService.awaitTermination(60, TimeUnit.MINUTES);

src/test/java/io/github/utplsql/cli/RunCommandTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void reporterOptions_Default() {
3030
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();
3131

3232
ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
33-
Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER.getName(), reporterOptions1.getReporterName());
33+
Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER, reporterOptions1.getReporterName());
3434
Assert.assertNull(reporterOptions1.getOutputFileName());
3535
Assert.assertFalse(reporterOptions1.outputToFile());
3636
Assert.assertTrue(reporterOptions1.outputToScreen());
@@ -43,7 +43,7 @@ public void reporterOptions_OneReporter() {
4343
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();
4444

4545
ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
46-
Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER.getName(), reporterOptions1.getReporterName());
46+
Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER, reporterOptions1.getReporterName());
4747
Assert.assertEquals(reporterOptions1.getOutputFileName(), "output.txt");
4848
Assert.assertTrue(reporterOptions1.outputToFile());
4949
Assert.assertFalse(reporterOptions1.outputToScreen());
@@ -56,7 +56,7 @@ public void reporterOptions_OneReporterForceScreen() {
5656
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();
5757

5858
ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
59-
Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER.getName(), reporterOptions1.getReporterName());
59+
Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER, reporterOptions1.getReporterName());
6060
Assert.assertEquals(reporterOptions1.getOutputFileName(), "output.txt");
6161
Assert.assertTrue(reporterOptions1.outputToFile());
6262
Assert.assertTrue(reporterOptions1.outputToScreen());
@@ -69,7 +69,7 @@ public void reporterOptions_OneReporterForceScreenInverse() {
6969
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();
7070

7171
ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
72-
Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER.getName(), reporterOptions1.getReporterName());
72+
Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER, reporterOptions1.getReporterName());
7373
Assert.assertEquals(reporterOptions1.getOutputFileName(), "output.txt");
7474
Assert.assertTrue(reporterOptions1.outputToFile());
7575
Assert.assertTrue(reporterOptions1.outputToScreen());
@@ -84,13 +84,13 @@ public void reporterOptions_TwoReporters() {
8484
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();
8585

8686
ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
87-
Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER.getName(), reporterOptions1.getReporterName());
87+
Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER, reporterOptions1.getReporterName());
8888
Assert.assertNull(reporterOptions1.getOutputFileName());
8989
Assert.assertFalse(reporterOptions1.outputToFile());
9090
Assert.assertTrue(reporterOptions1.outputToScreen());
9191

9292
ReporterOptions reporterOptions2 = reporterOptionsList.get(1);
93-
Assert.assertEquals(CustomTypes.UT_COVERAGE_HTML_REPORTER.getName(), reporterOptions2.getReporterName());
93+
Assert.assertEquals(CustomTypes.UT_COVERAGE_HTML_REPORTER, reporterOptions2.getReporterName());
9494
Assert.assertEquals(reporterOptions2.getOutputFileName(), "coverage.html");
9595
Assert.assertTrue(reporterOptions2.outputToFile());
9696
Assert.assertTrue(reporterOptions2.outputToScreen());

0 commit comments

Comments
 (0)