Skip to content

Commit 0148224

Browse files
committed
Add reporter params
1 parent 1b328aa commit 0148224

File tree

4 files changed

+185
-21
lines changed

4 files changed

+185
-21
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package io.github.utplsql.cli;
2+
3+
import com.sun.istack.internal.NotNull;
4+
5+
/**
6+
* Created by Vinicius on 20/05/2017.
7+
*/
8+
public class ReporterOptions {
9+
10+
private String reporterName;
11+
private String outputFileName;
12+
private boolean outputToScreen;
13+
private boolean forceOutputToScreen;
14+
15+
public ReporterOptions(String reporterName, String outputFileName, boolean outputToScreen) {
16+
setReporterName(reporterName);
17+
setOutputFileName(outputFileName);
18+
this.outputToScreen = outputToScreen;
19+
this.forceOutputToScreen = false;
20+
}
21+
22+
public ReporterOptions(String reporterName) {
23+
this(reporterName, null, true);
24+
}
25+
26+
public String getReporterName() {
27+
return reporterName;
28+
}
29+
30+
@NotNull
31+
public void setReporterName(String reporterName) {
32+
this.reporterName = reporterName.toUpperCase();
33+
}
34+
35+
public String getOutputFileName() {
36+
return outputFileName;
37+
}
38+
39+
public void setOutputFileName(String outputFileName) {
40+
this.outputFileName = outputFileName;
41+
this.outputToScreen = false;
42+
}
43+
44+
public boolean outputToFile() {
45+
return outputFileName != null && !outputFileName.isEmpty();
46+
}
47+
48+
public boolean outputToScreen() {
49+
return outputToScreen || forceOutputToScreen;
50+
}
51+
52+
public void forceOutputToScreen(boolean outputToScreen) {
53+
this.forceOutputToScreen = outputToScreen;
54+
}
55+
56+
}

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

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import io.github.utplsql.api.OutputBuffer;
66
import io.github.utplsql.api.TestRunner;
77
import io.github.utplsql.api.types.BaseReporter;
8+
import io.github.utplsql.api.types.CustomTypes;
89
import io.github.utplsql.api.types.DocumentationReporter;
910

1011
import java.sql.Connection;
1112
import java.sql.SQLException;
13+
import java.util.ArrayList;
1214
import java.util.List;
1315
import java.util.concurrent.ExecutorService;
1416
import java.util.concurrent.Executors;
@@ -24,20 +26,20 @@ public class RunCommand {
2426
required = true, converter = ConnectionStringConverter.class,
2527
arity = 1,
2628
description = "user/pass@[[host][:port]/]db")
27-
private List<ConnectionInfo> connectionInfoList;
29+
private List<ConnectionInfo> connectionInfoList = new ArrayList<>();
2830

2931
@Parameter(
3032
names = {"-p", "--path"},
3133
description = "run suites/tests by path, format: \n" +
3234
"-p schema or schema:[suite ...][.test] or schema[.suite ...][.test]")
33-
private List<String> testPaths;
35+
private List<String> testPaths = new ArrayList<>();
3436

3537
@Parameter(
3638
names = {"-f", "--format"},
3739
variableArity = true,
3840
description = "output reporter format: \n" +
3941
"-f reporter_name [output_file] [console_output]")
40-
private List<String> reporterParams;
42+
private List<String> reporterParams = new ArrayList<>();
4143

4244
public ConnectionInfo getConnectionInfo() {
4345
return connectionInfoList.get(0);
@@ -50,8 +52,31 @@ public String getTestPaths() {
5052
return (testPaths == null) ? null : String.join(",", testPaths);
5153
}
5254

53-
public List<String> getReporterParams() {
54-
return reporterParams;
55+
public List<ReporterOptions> getReporterOptionsList() {
56+
List<ReporterOptions> reporterOptionsList = new ArrayList<>();
57+
ReporterOptions reporterOptions = null;
58+
59+
for (String p : reporterParams) {
60+
if (reporterOptions == null || !p.startsWith("-")) {
61+
reporterOptions = new ReporterOptions(p);
62+
reporterOptionsList.add(reporterOptions);
63+
}
64+
else
65+
if (p.startsWith("-o=")) {
66+
reporterOptions.setOutputFileName(p.substring(3));
67+
}
68+
else
69+
if (p.equals("-s")) {
70+
reporterOptions.forceOutputToScreen(true);
71+
}
72+
}
73+
74+
// If no reporter parameters were passed, use default reporter.
75+
if (reporterOptionsList.isEmpty()) {
76+
reporterOptionsList.add(new ReporterOptions(CustomTypes.UT_DOCUMENTATION_REPORTER.getName()));
77+
}
78+
79+
return reporterOptionsList;
5580
}
5681

5782
public void run() throws Exception {

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

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package io.github.utplsql.cli;
2+
3+
import com.beust.jcommander.JCommander;
4+
import io.github.utplsql.api.types.CustomTypes;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
import java.util.List;
9+
10+
/**
11+
* Unit test for run command.
12+
*/
13+
public class RunCommandTest {
14+
15+
private RunCommand createCommand(String... args) {
16+
RunCommand runCmd = new RunCommand();
17+
18+
JCommander.newBuilder()
19+
.addObject(runCmd)
20+
.args(args)
21+
.build();
22+
23+
return runCmd;
24+
}
25+
26+
@Test
27+
public void reporterOptions_Default() {
28+
RunCommand runCmd = createCommand("run", "app/app");
29+
30+
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();
31+
32+
ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
33+
Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER.getName(), reporterOptions1.getReporterName());
34+
Assert.assertNull(reporterOptions1.getOutputFileName());
35+
Assert.assertFalse(reporterOptions1.outputToFile());
36+
Assert.assertTrue(reporterOptions1.outputToScreen());
37+
}
38+
39+
@Test
40+
public void reporterOptions_OneReporter() {
41+
RunCommand runCmd = createCommand("run", "app/app@docker/xe", "-f=ut_documentation_reporter", "-o=output.txt");
42+
43+
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();
44+
45+
ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
46+
Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER.getName(), reporterOptions1.getReporterName());
47+
Assert.assertEquals(reporterOptions1.getOutputFileName(), "output.txt");
48+
Assert.assertTrue(reporterOptions1.outputToFile());
49+
Assert.assertFalse(reporterOptions1.outputToScreen());
50+
}
51+
52+
@Test
53+
public void reporterOptions_OneReporterForceScreen() {
54+
RunCommand runCmd = createCommand("run", "app/app@docker/xe", "-f=ut_documentation_reporter", "-o=output.txt", "-s");
55+
56+
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();
57+
58+
ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
59+
Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER.getName(), reporterOptions1.getReporterName());
60+
Assert.assertEquals(reporterOptions1.getOutputFileName(), "output.txt");
61+
Assert.assertTrue(reporterOptions1.outputToFile());
62+
Assert.assertTrue(reporterOptions1.outputToScreen());
63+
}
64+
65+
@Test
66+
public void reporterOptions_OneReporterForceScreenInverse() {
67+
RunCommand runCmd = createCommand("run", "app/app@docker/xe", "-f=ut_documentation_reporter", "-s", "-o=output.txt");
68+
69+
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();
70+
71+
ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
72+
Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER.getName(), reporterOptions1.getReporterName());
73+
Assert.assertEquals(reporterOptions1.getOutputFileName(), "output.txt");
74+
Assert.assertTrue(reporterOptions1.outputToFile());
75+
Assert.assertTrue(reporterOptions1.outputToScreen());
76+
}
77+
78+
@Test
79+
public void reporterOptions_TwoReporters() {
80+
RunCommand runCmd = createCommand("run", "app/app@docker/xe",
81+
"-f=ut_documentation_reporter",
82+
"-f=ut_coverage_html_reporter", "-o=coverage.html", "-s");
83+
84+
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();
85+
86+
ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
87+
Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER.getName(), reporterOptions1.getReporterName());
88+
Assert.assertNull(reporterOptions1.getOutputFileName());
89+
Assert.assertFalse(reporterOptions1.outputToFile());
90+
Assert.assertTrue(reporterOptions1.outputToScreen());
91+
92+
ReporterOptions reporterOptions2 = reporterOptionsList.get(1);
93+
Assert.assertEquals(CustomTypes.UT_COVERAGE_HTML_REPORTER.getName(), reporterOptions2.getReporterName());
94+
Assert.assertEquals(reporterOptions2.getOutputFileName(), "coverage.html");
95+
Assert.assertTrue(reporterOptions2.outputToFile());
96+
Assert.assertTrue(reporterOptions2.outputToScreen());
97+
}
98+
99+
}

0 commit comments

Comments
 (0)