Skip to content

Commit 8eec2cb

Browse files
committed
Implement rules for printToScreen Reporters: Only one can go to screen
1 parent c80dc8c commit 8eec2cb

File tree

5 files changed

+201
-92
lines changed

5 files changed

+201
-92
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ public class ReporterOptions {
1414

1515
private Reporter reporterObj = null;
1616

17-
public ReporterOptions(String reporterName, String outputFileName, boolean outputToScreen) {
17+
public ReporterOptions(String reporterName, String outputFileName ) {
1818
setReporterName(reporterName);
1919
setOutputFileName(outputFileName);
20-
this.outputToScreen = outputToScreen;
20+
this.outputToScreen = (outputFileName == null); // If outputFileName is null we assume it should be sent to screen
2121
this.forceOutputToScreen = false;
2222
}
2323

2424
public ReporterOptions(String reporterName) {
25-
this(reporterName, null, true);
25+
this(reporterName, null);
2626
}
2727

2828
public Reporter getReporterObj() {

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.utplsql.cli;
22

3-
import com.beust.jcommander.Parameter;
4-
import com.beust.jcommander.Parameters;
53
import com.zaxxer.hikari.HikariDataSource;
64
import org.slf4j.Logger;
75
import org.slf4j.LoggerFactory;
@@ -23,7 +21,6 @@
2321
import org.utplsql.cli.log.StringBlockFormatter;
2422

2523
import javax.sql.DataSource;
26-
import javax.xml.crypto.dsig.keyinfo.KeyValue;
2724
import java.io.File;
2825
import java.sql.Connection;
2926
import java.sql.SQLException;
@@ -280,11 +277,18 @@ private ReporterManager getReporterManager() {
280277
ReporterConfig[] reporterConfigs = config.getReporters();
281278
if ( reporterConfigs != null ) {
282279
ReporterOptions[] options = new ReporterOptions[reporterConfigs.length];
280+
boolean printToScreen = false;
283281
for (int i = 0; i<reporterConfigs.length; i++ ) {
284282
options[i] = new ReporterOptions(
285283
reporterConfigs[i].getName(),
286-
reporterConfigs[i].getOutput(),
287-
reporterConfigs[i].isScreen());
284+
reporterConfigs[i].getOutput());
285+
286+
options[i].forceOutputToScreen(reporterConfigs[i].isForceToScreen());
287+
288+
// Check printToScreen validity
289+
if ( options[i].outputToScreen() && printToScreen )
290+
throw new IllegalArgumentException("You cannot configure more than one reporter to output to screen");
291+
printToScreen = options[i].outputToScreen();
288292
}
289293
reporterManager = new ReporterManager(options);
290294
}

src/main/java/org/utplsql/cli/config/ReporterConfig.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ public class ReporterConfig {
66

77
private final String name;
88
private final String output;
9-
private boolean screen = false;
9+
private boolean forceToScreen = false;
1010

11-
@ConstructorProperties({"name", "output", "screen"})
12-
public ReporterConfig( String name, String output, Boolean screen ) {
11+
@ConstructorProperties({"name", "output", "forceToScreen"})
12+
public ReporterConfig( String name, String output, Boolean forceToScreen) {
1313
this.name = name;
1414
this.output = output;
15-
if ( screen != null ) this.screen = screen;
15+
if ( forceToScreen != null ) this.forceToScreen = forceToScreen;
1616
}
1717

1818
public String getName() {
@@ -23,7 +23,7 @@ public String getOutput() {
2323
return output;
2424
}
2525

26-
public boolean isScreen() {
27-
return screen;
26+
public boolean isForceToScreen() {
27+
return forceToScreen;
2828
}
2929
}

src/test/java/org/utplsql/cli/PicocliRunCommandTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
package org.utplsql.cli;
22

3-
import org.hamcrest.Matchers;
43
import org.junit.jupiter.api.Test;
54
import org.utplsql.cli.config.FileMapperConfig;
65
import org.utplsql.cli.config.ReporterConfig;
76
import org.utplsql.cli.config.RunCommandConfig;
8-
import picocli.CommandLine;
9-
10-
import java.util.List;
117

128
import static org.hamcrest.MatcherAssert.assertThat;
139
import static org.hamcrest.Matchers.*;
@@ -139,7 +135,7 @@ void singleReporter() throws Exception {
139135
ReporterConfig reporterConfig = config.getReporters()[0];
140136
assertEquals("ut_documentation_reporter", reporterConfig.getName());
141137
assertNull(reporterConfig.getOutput());
142-
assertFalse(reporterConfig.isScreen());
138+
assertFalse(reporterConfig.isForceToScreen());
143139
}
144140

145141
@Test
@@ -157,12 +153,12 @@ void multipleReporters() throws Exception {
157153
ReporterConfig reporterConfig = config.getReporters()[0];
158154
assertEquals("ut_documentation_reporter", reporterConfig.getName());
159155
assertEquals("output1.txt", reporterConfig.getOutput());
160-
assertFalse(reporterConfig.isScreen());
156+
assertFalse(reporterConfig.isForceToScreen());
161157

162158
reporterConfig = config.getReporters()[1];
163159
assertEquals("ut_coverage_html", reporterConfig.getName());
164160
assertEquals("output2.html", reporterConfig.getOutput());
165-
assertTrue(reporterConfig.isScreen());
161+
assertTrue(reporterConfig.isForceToScreen());
166162
}
167163

168164
@Test

src/test/java/org/utplsql/cli/RunCommandTest.java

Lines changed: 180 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package org.utplsql.cli;
22

3+
import org.junit.jupiter.api.Nested;
34
import org.junit.jupiter.api.Test;
45
import org.utplsql.api.TestRunnerOptions;
56
import org.utplsql.api.reporter.CoreReporters;
67

78
import java.util.ArrayList;
9+
import java.util.Arrays;
810
import java.util.List;
911

1012
import static org.hamcrest.CoreMatchers.equalTo;
@@ -17,77 +19,184 @@
1719
*/
1820
class RunCommandTest {
1921

20-
@Test
21-
void reporterOptions_Default() {
22-
IRunCommand runCmd = TestHelper.createRunCommand(TestHelper.getConnectionString());
23-
24-
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();
25-
26-
ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
27-
assertEquals(CoreReporters.UT_DOCUMENTATION_REPORTER.name(), reporterOptions1.getReporterName());
28-
assertNull(reporterOptions1.getOutputFileName());
29-
assertFalse(reporterOptions1.outputToFile());
30-
assertTrue(reporterOptions1.outputToScreen());
31-
}
32-
33-
@Test
34-
void reporterOptions_OneReporter() {
35-
IRunCommand runCmd = TestHelper.createRunCommand(TestHelper.getConnectionString(), "-f=ut_documentation_reporter", "-o=output.txt");
36-
37-
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();
38-
39-
ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
40-
assertEquals(CoreReporters.UT_DOCUMENTATION_REPORTER.name(), reporterOptions1.getReporterName());
41-
assertEquals(reporterOptions1.getOutputFileName(), "output.txt");
42-
assertTrue(reporterOptions1.outputToFile());
43-
assertFalse(reporterOptions1.outputToScreen());
44-
}
45-
46-
@Test
47-
void reporterOptions_OneReporterForceScreen() {
48-
IRunCommand runCmd = TestHelper.createRunCommand(TestHelper.getConnectionString(), "-f=ut_documentation_reporter", "-o=output.txt", "-s");
49-
50-
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();
51-
52-
ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
53-
assertEquals(CoreReporters.UT_DOCUMENTATION_REPORTER.name(), reporterOptions1.getReporterName());
54-
assertEquals(reporterOptions1.getOutputFileName(), "output.txt");
55-
assertTrue(reporterOptions1.outputToFile());
56-
assertTrue(reporterOptions1.outputToScreen());
57-
}
58-
59-
@Test
60-
void reporterOptions_OneReporterForceScreenInverse() {
61-
IRunCommand runCmd = TestHelper.createRunCommand(TestHelper.getConnectionString(), "-f=ut_documentation_reporter", "-s", "-o=output.txt");
62-
63-
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();
64-
65-
ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
66-
assertEquals(CoreReporters.UT_DOCUMENTATION_REPORTER.name(), reporterOptions1.getReporterName());
67-
assertEquals(reporterOptions1.getOutputFileName(), "output.txt");
68-
assertTrue(reporterOptions1.outputToFile());
69-
assertTrue(reporterOptions1.outputToScreen());
70-
}
71-
72-
@Test
73-
void reporterOptions_TwoReporters() {
74-
IRunCommand runCmd = TestHelper.createRunCommand(TestHelper.getConnectionString(),
75-
"-f=ut_documentation_reporter",
76-
"-f=ut_coverage_html_reporter", "-o=coverage.html", "-s");
77-
78-
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();
79-
80-
ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
81-
assertEquals(CoreReporters.UT_DOCUMENTATION_REPORTER.name(), reporterOptions1.getReporterName());
82-
assertNull(reporterOptions1.getOutputFileName());
83-
assertFalse(reporterOptions1.outputToFile());
84-
assertFalse(reporterOptions1.outputToScreen());
85-
86-
ReporterOptions reporterOptions2 = reporterOptionsList.get(1);
87-
assertEquals(CoreReporters.UT_COVERAGE_HTML_REPORTER.name(), reporterOptions2.getReporterName());
88-
assertEquals(reporterOptions2.getOutputFileName(), "coverage.html");
89-
assertTrue(reporterOptions2.outputToFile());
90-
assertTrue(reporterOptions2.outputToScreen());
22+
@Nested
23+
class A_reporter {
24+
25+
List<ReporterOptions> getReporterOptionsWithArgs(String... args) {
26+
ArrayList<String> newArgs = new ArrayList<>(args.length + 1);
27+
newArgs.add(TestHelper.getConnectionString());
28+
newArgs.addAll(Arrays.asList(args));
29+
IRunCommand cmd = TestHelper.createRunCommand(newArgs.toArray(new String[0]));
30+
return cmd.getReporterOptionsList();
31+
}
32+
33+
@Nested
34+
class Is_output_to_screen {
35+
36+
@Test
37+
void by_default() {
38+
List<ReporterOptions> options = getReporterOptionsWithArgs();
39+
40+
assertTrue(options.get(0).outputToScreen());
41+
}
42+
43+
@Test
44+
void when_only_reporter_without_output_specified() {
45+
List<ReporterOptions> options = getReporterOptionsWithArgs("-f=ut_documentation_reporter");
46+
47+
assertTrue(options.get(0).outputToScreen());
48+
}
49+
50+
@Test
51+
void when_without_output_and_no_other_reporter_has_forceToScreen_flag() {
52+
List<ReporterOptions> options = getReporterOptionsWithArgs(
53+
"-f=ut_coverage_sonar_reporter", "-o=output.txt",
54+
"-f=ut_coverage_html_reporter");
55+
56+
assertTrue(options.get(1).outputToScreen());
57+
}
58+
59+
@Test
60+
void when_only_reporter_with_forceToScreen_flag() {
61+
List<ReporterOptions> options = getReporterOptionsWithArgs(
62+
"-f=ut_coverage_sonar_reporter", "-o=output.txt",
63+
"-f=ut_coverage_html_reporter", "-o=output.html", "-s",
64+
"-f=ut_coverage_cobertura_reporter", "-o=cobertura.html");
65+
66+
assertTrue(options.get(1).outputToScreen());
67+
}
68+
}
69+
70+
@Nested
71+
class Is_not_output_to_screen {
72+
73+
@Test
74+
void when_it_has_output_specified_but_no_forceToScreen() {
75+
List<ReporterOptions> options = getReporterOptionsWithArgs(
76+
"-f=ut_documentation_reporter", "-o=output.txt",
77+
"-f=ut_coverage_html_reporter", "-o=output.html"
78+
);
79+
80+
assertFalse(options.get(0).outputToScreen());
81+
assertFalse(options.get(1).outputToScreen());
82+
}
83+
}
84+
85+
@Nested
86+
class Cannot_be_run {
87+
88+
@Test
89+
void when_more_than_one_forceToScreen_flags() {
90+
assertThrows(IllegalArgumentException.class, () -> {
91+
getReporterOptionsWithArgs(
92+
"-f=ut_coverage_sonar_reporter", "-o=output.txt", "-s",
93+
"-f=ut_coverage_html_reporter", "-o=output.html", "-s");
94+
});
95+
}
96+
97+
@Test
98+
void when_more_than_one_reporters_without_output() {
99+
assertThrows(IllegalArgumentException.class, () -> {
100+
getReporterOptionsWithArgs(
101+
"-f=ut_coverage_sonar_reporter",
102+
"-f=ut_coverage_html_reporter");
103+
});
104+
}
105+
@Test
106+
void when_one_reporter_without_output_and_one_with_forceToScreen() {
107+
assertThrows(IllegalArgumentException.class, () -> {
108+
getReporterOptionsWithArgs(
109+
"-f=ut_coverage_sonar_reporter",
110+
"-f=ut_coverage_html_reporter", "-o=output.html", "-s");
111+
});
112+
}
113+
}
114+
115+
@Test
116+
void reporterOptions_Default() {
117+
IRunCommand runCmd = TestHelper.createRunCommand(TestHelper.getConnectionString());
118+
119+
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();
120+
121+
ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
122+
assertEquals(CoreReporters.UT_DOCUMENTATION_REPORTER.name(), reporterOptions1.getReporterName());
123+
assertNull(reporterOptions1.getOutputFileName());
124+
assertFalse(reporterOptions1.outputToFile());
125+
assertTrue(reporterOptions1.outputToScreen());
126+
}
127+
128+
@Test
129+
void reporterOptions_OneReporter() {
130+
IRunCommand runCmd = TestHelper.createRunCommand(TestHelper.getConnectionString(), "-f=ut_documentation_reporter", "-o=output.txt");
131+
132+
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();
133+
134+
ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
135+
assertEquals(CoreReporters.UT_DOCUMENTATION_REPORTER.name(), reporterOptions1.getReporterName());
136+
assertEquals("output.txt", reporterOptions1.getOutputFileName());
137+
assertTrue(reporterOptions1.outputToFile());
138+
assertFalse(reporterOptions1.outputToScreen());
139+
}
140+
141+
@Test
142+
void reporterOptions_OneReporter_DefaultToScreen() {
143+
IRunCommand runCmd = TestHelper.createRunCommand(TestHelper.getConnectionString(), "-f=ut_documentation_reporter");
144+
145+
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();
146+
147+
ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
148+
assertEquals(CoreReporters.UT_DOCUMENTATION_REPORTER.name(), reporterOptions1.getReporterName());
149+
assertNull(reporterOptions1.getOutputFileName());
150+
assertFalse(reporterOptions1.outputToFile());
151+
assertTrue(reporterOptions1.outputToScreen());
152+
}
153+
154+
@Test
155+
void reporterOptions_OneReporterForceScreen() {
156+
IRunCommand runCmd = TestHelper.createRunCommand(TestHelper.getConnectionString(), "-f=ut_documentation_reporter", "-o=output.txt", "-s");
157+
158+
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();
159+
160+
ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
161+
assertEquals(CoreReporters.UT_DOCUMENTATION_REPORTER.name(), reporterOptions1.getReporterName());
162+
assertEquals("output.txt", reporterOptions1.getOutputFileName());
163+
assertTrue(reporterOptions1.outputToFile());
164+
assertTrue(reporterOptions1.outputToScreen());
165+
}
166+
167+
@Test
168+
void reporterOptions_OneReporterForceScreenInverse() {
169+
IRunCommand runCmd = TestHelper.createRunCommand(TestHelper.getConnectionString(), "-f=ut_documentation_reporter", "-s", "-o=output.txt");
170+
171+
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();
172+
173+
ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
174+
assertEquals(CoreReporters.UT_DOCUMENTATION_REPORTER.name(), reporterOptions1.getReporterName());
175+
assertEquals("output.txt", reporterOptions1.getOutputFileName());
176+
assertTrue(reporterOptions1.outputToFile());
177+
assertTrue(reporterOptions1.outputToScreen());
178+
}
179+
180+
@Test
181+
void reporterOptions_TwoReporters() {
182+
IRunCommand runCmd = TestHelper.createRunCommand(TestHelper.getConnectionString(),
183+
"-f=ut_documentation_reporter", "-o=output.txt",
184+
"-f=ut_coverage_html_reporter", "-o=coverage.html", "-s");
185+
186+
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();
187+
188+
ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
189+
assertEquals(CoreReporters.UT_DOCUMENTATION_REPORTER.name(), reporterOptions1.getReporterName());
190+
assertEquals("output.txt", reporterOptions1.getOutputFileName());
191+
assertTrue(reporterOptions1.outputToFile());
192+
assertFalse(reporterOptions1.outputToScreen());
193+
194+
ReporterOptions reporterOptions2 = reporterOptionsList.get(1);
195+
assertEquals(CoreReporters.UT_COVERAGE_HTML_REPORTER.name(), reporterOptions2.getReporterName());
196+
assertEquals("coverage.html", reporterOptions2.getOutputFileName());
197+
assertTrue(reporterOptions2.outputToFile());
198+
assertTrue(reporterOptions2.outputToScreen());
199+
}
91200
}
92201

93202
@Test

0 commit comments

Comments
 (0)