mirror of
https://github.com/itplr-kosit/validator.git
synced 2026-05-26 01:05:38 +00:00
#67 [CLI,DAEMON] Return proper return codes / status codes
This commit is contained in:
parent
fa7faf9961
commit
f2223552ad
21 changed files with 424 additions and 248 deletions
|
|
@ -53,14 +53,14 @@ public class CheckAssertionActionTest {
|
|||
@Before
|
||||
public void setup() throws IOException {
|
||||
this.commandLine = new CommandLine();
|
||||
this.commandLine.activate();
|
||||
CommandLine.activate();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyInput() {
|
||||
final CheckAssertionAction a = new CheckAssertionAction(new Assertions(), TestObjectFactory.createProcessor());
|
||||
a.check(new CheckAction.Bag(InputFactory.read(SAMPLE), new CreateReportInput()));
|
||||
assertThat(this.commandLine.getErrorOutput()).contains("Can not find assertions for");
|
||||
assertThat(CommandLine.getErrorOutput()).contains("Can not find assertions for");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -72,6 +72,6 @@ public class CheckAssertionActionTest {
|
|||
final CheckAssertionAction a = new CheckAssertionAction(assertions, TestObjectFactory.createProcessor());
|
||||
a.check(bag);
|
||||
|
||||
assertThat(this.commandLine.getErrorOutput()).contains("Assertion mismatch");
|
||||
assertThat(CommandLine.getErrorOutput()).contains("Assertion mismatch");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,14 @@
|
|||
|
||||
package de.kosit.validationtool.cmd;
|
||||
|
||||
import java.io.*;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.io.Reader;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
|
@ -35,23 +42,85 @@ import lombok.Setter;
|
|||
*/
|
||||
public class CommandLine {
|
||||
|
||||
private static ReplaceableOutputStream<ByteArrayOutputStream> out = new ReplaceableOutputStream<>();
|
||||
/**
|
||||
* Simpler Proxy für {@link OutputStream}, dessen target ausgetauscht werden kann.
|
||||
*
|
||||
* @param <O> Typ des eigentlichen {@link OutputStream}
|
||||
*/
|
||||
private static class ReplaceableOutputStream<O extends OutputStream> extends OutputStream {
|
||||
|
||||
private static ReplaceableOutputStream<ByteArrayOutputStream> error = new ReplaceableOutputStream<>();
|
||||
@Getter
|
||||
@Setter
|
||||
private O out;
|
||||
|
||||
@Override
|
||||
public void write(final int idx) throws IOException {
|
||||
if (this.out != null) {
|
||||
this.out.write(idx);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(final byte[] bts) throws IOException {
|
||||
if (this.out != null) {
|
||||
this.out.write(bts);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(final byte[] bts, final int st, final int end) throws IOException {
|
||||
if (this.out != null) {
|
||||
this.out.write(bts, st, end);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() throws IOException {
|
||||
|
||||
if (this.out != null) {
|
||||
this.out.flush();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
if (this.out != null) {
|
||||
this.out.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final ReplaceableOutputStream<ByteArrayOutputStream> out = new ReplaceableOutputStream<>();
|
||||
|
||||
private static final ReplaceableOutputStream<ByteArrayOutputStream> error = new ReplaceableOutputStream<>();
|
||||
|
||||
static {
|
||||
// Initialisierung muss vor SL4J's SimpleLogger erfolgen, sonst sind logs nicht erfasst.
|
||||
// deshalb darf diese Klasse kein Log haben
|
||||
System.setOut(new PrintStream(new TeeOutputStream(System.out, out)));
|
||||
System.setErr(new PrintStream(new TeeOutputStream(System.err, error)));
|
||||
setStandardInput(nullInputStream());
|
||||
}
|
||||
|
||||
public String getOutput() {
|
||||
public static void setStandardInput(final InputStream in) {
|
||||
System.setIn(in);
|
||||
}
|
||||
|
||||
public static InputStream nullInputStream() {
|
||||
return new InputStream() {
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static String getOutput() {
|
||||
return new String(out.getOut().toByteArray());
|
||||
}
|
||||
|
||||
|
||||
public String getErrorOutput() {
|
||||
public static String getErrorOutput() {
|
||||
return new String(error.getOut().toByteArray());
|
||||
}
|
||||
|
||||
|
|
@ -63,68 +132,26 @@ public class CommandLine {
|
|||
return readLines(error.getOut().toByteArray());
|
||||
}
|
||||
|
||||
private List<String> readLines(byte[] bytes) {
|
||||
try ( ByteArrayInputStream in = new ByteArrayInputStream(bytes);
|
||||
Reader r = new InputStreamReader(in) ) {
|
||||
private List<String> readLines(final byte[] bytes) {
|
||||
try ( final ByteArrayInputStream in = new ByteArrayInputStream(bytes);
|
||||
final Reader r = new InputStreamReader(in) ) {
|
||||
|
||||
return IOUtils.readLines(r);
|
||||
} catch (IOException e) {
|
||||
} catch (final IOException e) {
|
||||
throw new IllegalStateException("Can not read input");
|
||||
}
|
||||
}
|
||||
|
||||
public void activate() {
|
||||
public static void activate() {
|
||||
out.setOut(new ByteArrayOutputStream());
|
||||
error.setOut(new ByteArrayOutputStream());
|
||||
|
||||
}
|
||||
|
||||
public void deactivate() {
|
||||
public static void deactivate() {
|
||||
out.setOut(null);
|
||||
error.setOut(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Simpler Proxy für {@link OutputStream}, dessen target ausgetauscht werden kann.
|
||||
*
|
||||
* @param <O> Typ des eigentlichen {@link OutputStream}
|
||||
*/
|
||||
private static class ReplaceableOutputStream<O extends OutputStream> extends OutputStream {
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private O out;
|
||||
|
||||
public void write(int idx) throws IOException {
|
||||
if (out != null) {
|
||||
this.out.write(idx);
|
||||
}
|
||||
}
|
||||
|
||||
public void write(byte[] bts) throws IOException {
|
||||
if (out != null) {
|
||||
this.out.write(bts);
|
||||
}
|
||||
}
|
||||
|
||||
public void write(byte[] bts, int st, int end) throws IOException {
|
||||
if (out != null) {
|
||||
this.out.write(bts, st, end);
|
||||
}
|
||||
}
|
||||
|
||||
public void flush() throws IOException {
|
||||
|
||||
if (out != null) {
|
||||
this.out.flush();
|
||||
}
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
if (out != null) {
|
||||
this.out.close();
|
||||
}
|
||||
}
|
||||
setStandardInput(nullInputStream());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@ public class CommandlineApplicationTest {
|
|||
@Before
|
||||
public void setup() throws IOException {
|
||||
this.commandLine = new CommandLine();
|
||||
this.commandLine.activate();
|
||||
CommandLine.activate();
|
||||
if (Files.exists(this.output)) {
|
||||
FileUtils.deleteDirectory(this.output.toFile());
|
||||
}
|
||||
|
|
@ -71,13 +71,14 @@ public class CommandlineApplicationTest {
|
|||
log.error("Error deleting file", e);
|
||||
}
|
||||
});
|
||||
CommandLine.deactivate();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHelp() {
|
||||
final String[] args = new String[] { "-?" };
|
||||
CommandLineApplication.mainProgram(args);
|
||||
assertThat(this.commandLine.getErrorOutput()).isEmpty();
|
||||
assertThat(CommandLine.getErrorOutput()).isEmpty();
|
||||
checkForHelp(this.commandLine.getOutputLines());
|
||||
}
|
||||
|
||||
|
|
@ -90,24 +91,24 @@ public class CommandlineApplicationTest {
|
|||
public void testRequiredScenarioFile() {
|
||||
final String[] args = new String[] { "-d", "arguments", "egal welche", "argument drin sind" };
|
||||
CommandLineApplication.mainProgram(args);
|
||||
assertThat(this.commandLine.getErrorOutput()).isNotEmpty();
|
||||
assertThat(this.commandLine.getErrorOutput()).contains("Missing required option: s");
|
||||
assertThat(CommandLine.getErrorOutput()).isNotEmpty();
|
||||
assertThat(CommandLine.getErrorOutput()).contains("Missing required option: s");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotExistingScenarioFile() {
|
||||
final String[] args = new String[] { "-s", Paths.get(Simple.NOT_EXISTING).toString(), Paths.get(Simple.NOT_EXISTING).toString() };
|
||||
CommandLineApplication.mainProgram(args);
|
||||
assertThat(this.commandLine.getErrorOutput()).isNotEmpty();
|
||||
assertThat(this.commandLine.getErrorOutput()).contains("Not a valid path for scenario definition specified");
|
||||
assertThat(CommandLine.getErrorOutput()).isNotEmpty();
|
||||
assertThat(CommandLine.getErrorOutput()).contains("Not a valid path for scenario definition specified");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIncorrectRepository() {
|
||||
final String[] args = new String[] { "-s", Paths.get(Simple.SCENARIOS).toString(), Paths.get(Simple.NOT_EXISTING).toString() };
|
||||
CommandLineApplication.mainProgram(args);
|
||||
assertThat(this.commandLine.getErrorOutput()).isNotEmpty();
|
||||
assertThat(this.commandLine.getErrorOutput()).contains("Can not resolve");
|
||||
assertThat(CommandLine.getErrorOutput()).isNotEmpty();
|
||||
assertThat(CommandLine.getErrorOutput()).contains("Can not resolve");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -115,8 +116,8 @@ public class CommandlineApplicationTest {
|
|||
final String[] args = new String[] { "-s", Paths.get(Simple.SCENARIOS).toString(), "-r",
|
||||
Paths.get(Simple.REPOSITORY_URI).toString(), Paths.get(Simple.NOT_EXISTING).toString() };
|
||||
CommandLineApplication.mainProgram(args);
|
||||
assertThat(this.commandLine.getErrorOutput()).isNotEmpty();
|
||||
assertThat(this.commandLine.getErrorOutput()).contains("No test targets found");
|
||||
assertThat(CommandLine.getErrorOutput()).isNotEmpty();
|
||||
assertThat(CommandLine.getErrorOutput()).contains("No test targets found");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -124,7 +125,7 @@ public class CommandlineApplicationTest {
|
|||
final String[] args = new String[] { "-s", Paths.get(Simple.SCENARIOS).toString(), "-r",
|
||||
Paths.get(Simple.REPOSITORY_URI).toString(), Paths.get(Simple.SIMPLE_VALID).toString() };
|
||||
CommandLineApplication.mainProgram(args);
|
||||
assertThat(this.commandLine.getErrorOutput()).contains(RESULT_OUTPUT);
|
||||
assertThat(CommandLine.getErrorOutput()).contains(RESULT_OUTPUT);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -133,8 +134,8 @@ public class CommandlineApplicationTest {
|
|||
Paths.get(Simple.REPOSITORY_URI).toString(), Paths.get(Simple.SIMPLE_VALID).toString(), "--report-prefix", "somePrefix",
|
||||
"--report-postfix", "somePostfix" };
|
||||
CommandLineApplication.mainProgram(args);
|
||||
assertThat(this.commandLine.getErrorOutput()).contains(RESULT_OUTPUT);
|
||||
assertThat(this.commandLine.getErrorOutput()).contains("somePrefix-simple-somePostfix");
|
||||
assertThat(CommandLine.getErrorOutput()).contains(RESULT_OUTPUT);
|
||||
assertThat(CommandLine.getErrorOutput()).contains("somePrefix-simple-somePostfix");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -142,7 +143,7 @@ public class CommandlineApplicationTest {
|
|||
final String[] args = new String[] { "-s", Paths.get(Simple.SCENARIOS).toString(), "-o", this.output.toString(), "-r",
|
||||
Paths.get(Simple.REPOSITORY_URI).toString(), Paths.get(Simple.SIMPLE_VALID).toString(), Paths.get(Simple.FOO).toString() };
|
||||
CommandLineApplication.mainProgram(args);
|
||||
assertThat(this.commandLine.getErrorOutput()).contains("Processing 2 object(s) completed");
|
||||
assertThat(CommandLine.getErrorOutput()).contains("Processing 2 object(s) completed");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -150,7 +151,7 @@ public class CommandlineApplicationTest {
|
|||
final String[] args = new String[] { "-s", Paths.get(Simple.SCENARIOS).toString(), "-o", this.output.toString(), "-r",
|
||||
Paths.get(Simple.REPOSITORY_URI).toString(), Paths.get(Simple.EXAMPLES).toString() };
|
||||
CommandLineApplication.mainProgram(args);
|
||||
assertThat(this.commandLine.getErrorOutput()).contains("Processing 8 object(s) completed");
|
||||
assertThat(CommandLine.getErrorOutput()).contains("Processing 8 object(s) completed");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -159,7 +160,7 @@ public class CommandlineApplicationTest {
|
|||
final String[] args = new String[] { "-s", Paths.get(Simple.SCENARIOS).toString(), "-o", this.output.toString(), "-r",
|
||||
Paths.get(Simple.REPOSITORY_URI).toString(), Paths.get(Simple.SIMPLE_VALID).toString() };
|
||||
CommandLineApplication.mainProgram(args);
|
||||
assertThat(this.commandLine.getErrorOutput()).contains(RESULT_OUTPUT);
|
||||
assertThat(CommandLine.getErrorOutput()).contains(RESULT_OUTPUT);
|
||||
assertThat(this.output).exists();
|
||||
assertThat(Files.list(this.output)).hasSize(1);
|
||||
}
|
||||
|
|
@ -179,7 +180,7 @@ public class CommandlineApplicationTest {
|
|||
final String[] args = new String[] { "-s", Paths.get(Simple.SCENARIOS).toString(), "-p", "-r",
|
||||
Paths.get(Simple.REPOSITORY_URI).toString(), "-o", this.output.toString(), Paths.get(Simple.SIMPLE_VALID).toString() };
|
||||
CommandLineApplication.mainProgram(args);
|
||||
assertThat(this.commandLine.getErrorOutput()).contains(RESULT_OUTPUT);
|
||||
assertThat(CommandLine.getErrorOutput()).contains(RESULT_OUTPUT);
|
||||
assertThat(this.commandLine.getOutputLines()).haveAtLeastOne(new Condition<>(
|
||||
s -> StringUtils.contains(s, "<?xml version=\"1.0\" " + "encoding=\"UTF-8\"?>"), "Must " + "contain xml preambel"));
|
||||
}
|
||||
|
|
@ -190,7 +191,7 @@ public class CommandlineApplicationTest {
|
|||
this.output.toAbsolutePath().toString(), "-r", Paths.get(Simple.REPOSITORY_URI).toString(),
|
||||
Paths.get(Simple.SIMPLE_VALID).toString() };
|
||||
CommandLineApplication.mainProgram(args);
|
||||
assertThat(this.commandLine.getErrorOutput()).contains(RESULT_OUTPUT);
|
||||
assertThat(CommandLine.getErrorOutput()).contains(RESULT_OUTPUT);
|
||||
assertThat(Files.list(this.output).filter(f -> f.toString().endsWith(".html")).count()).isGreaterThan(0);
|
||||
}
|
||||
|
||||
|
|
@ -200,8 +201,8 @@ public class CommandlineApplicationTest {
|
|||
Paths.get(Simple.REPOSITORY_URI).toString(), "-o", this.output.toString(), "-c", Paths.get(ASSERTIONS).toString(),
|
||||
Paths.get(Simple.REPOSITORY_URI).toString(), Paths.get(Simple.SIMPLE_VALID).toString() };
|
||||
CommandLineApplication.mainProgram(args);
|
||||
assertThat(this.commandLine.getErrorOutput()).contains(RESULT_OUTPUT);
|
||||
assertThat(this.commandLine.getErrorOutput()).contains("Can not find assertions for ");
|
||||
assertThat(CommandLine.getErrorOutput()).contains(RESULT_OUTPUT);
|
||||
assertThat(CommandLine.getErrorOutput()).contains("Can not find assertions for ");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -209,7 +210,7 @@ public class CommandlineApplicationTest {
|
|||
final String[] args = new String[] { "-s", Paths.get(Simple.SCENARIOS).toString(), "-r", "unknown", "-o", this.output.toString(),
|
||||
"-d", Paths.get(ASSERTIONS).toString() };
|
||||
CommandLineApplication.mainProgram(args);
|
||||
assertThat(this.commandLine.getErrorOutput()).contains("at de.kosit.validationtool");
|
||||
assertThat(CommandLine.getErrorOutput()).contains("at de.kosit.validationtool");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -217,7 +218,16 @@ public class CommandlineApplicationTest {
|
|||
final String[] args = new String[] { "-m", "-s", Paths.get(Simple.SCENARIOS).toString(), "-r",
|
||||
Paths.get(Simple.REPOSITORY_URI).toString(), Paths.get(Simple.SIMPLE_VALID).toString() };
|
||||
CommandLineApplication.mainProgram(args);
|
||||
assertThat(this.commandLine.getErrorOutput()).contains(RESULT_OUTPUT);
|
||||
assertThat(this.commandLine.getErrorOutput()).contains("total");
|
||||
assertThat(CommandLine.getErrorOutput()).contains(RESULT_OUTPUT);
|
||||
assertThat(CommandLine.getErrorOutput()).contains("total");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadFromPipe() throws IOException {
|
||||
final String[] args = new String[] { "-s", Paths.get(Simple.SCENARIOS).toString(), "-r",
|
||||
Paths.get(Simple.REPOSITORY_URI).toString() };
|
||||
CommandLine.setStandardInput(Files.newInputStream(Paths.get(Simple.SIMPLE_VALID)));
|
||||
CommandLineApplication.mainProgram(args);
|
||||
assertThat(CommandLine.getErrorOutput()).contains(RESULT_OUTPUT);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,13 +46,13 @@ public class PrintReportActionTest {
|
|||
@Before
|
||||
public void setup() {
|
||||
this.commandLine = new CommandLine();
|
||||
this.commandLine.activate();
|
||||
CommandLine.activate();
|
||||
this.action = new PrintReportAction(TestObjectFactory.createProcessor());
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
this.commandLine.deactivate();
|
||||
CommandLine.deactivate();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -62,9 +62,9 @@ public class PrintReportActionTest {
|
|||
assertThat(this.action.isSkipped(b)).isFalse();
|
||||
this.action.check(b);
|
||||
assertThat(b.isStopped()).isFalse();
|
||||
assertThat(this.commandLine.getOutput()).isNotEmpty();
|
||||
assertThat(this.commandLine.getOutput()).contains("<?xml version=\"1.0\" ");
|
||||
assertThat(this.commandLine.getErrorOutput()).isEmpty();
|
||||
assertThat(CommandLine.getOutput()).isNotEmpty();
|
||||
assertThat(CommandLine.getOutput()).contains("<?xml version=\"1.0\" ");
|
||||
assertThat(CommandLine.getErrorOutput()).isEmpty();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue