From bcadc4957393ec868078546c09d44a43d780e7c1 Mon Sep 17 00:00:00 2001 From: Felix Leipold Date: Sun, 11 Apr 2021 21:27:44 +0200 Subject: [PATCH] Improve error handling and test for Exception. --- .../org/buildobjects/process/ProcBuilder.java | 24 ++++- .../buildobjects/process/ProcBuilderTest.java | 93 +++++++++++++++++++ 2 files changed, 115 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/buildobjects/process/ProcBuilder.java b/src/main/java/org/buildobjects/process/ProcBuilder.java index c92fe54..049ea2a 100644 --- a/src/main/java/org/buildobjects/process/ProcBuilder.java +++ b/src/main/java/org/buildobjects/process/ProcBuilder.java @@ -59,6 +59,11 @@ public ProcBuilder withArg(String arg) { * @return this, for chaining * */ public ProcBuilder withOutputStream(OutputStream stdout) { + if (outputConsumer != null) { + throw new IllegalArgumentException("`withOutputStream(OutputStream)` and `withOutputConsumer(OutputConsumer)` " + + "are mutually exclusive."); + } + this.stdout = stdout; return this; } @@ -70,6 +75,10 @@ public ProcBuilder withOutputStream(OutputStream stdout) { * @return this, for chaining * */ public ProcBuilder withErrorStream(OutputStream stderr) { + if (errorConsumer != null) { + throw new IllegalArgumentException("`withErrorStream(OutputStream)` and `withErrorConsumer(OutputConsumer)` " + + "are mutually exclusive."); + } this.stderr = stderr; return this; } @@ -199,11 +208,13 @@ public ProcBuilder ignoreExitStatus() { public ProcResult run() throws StartupException, TimeoutException, ExternalProcessFailureException { if (stdout != defaultStdout && outputConsumer != null) { - throw new IllegalArgumentException("You can either ..."); + throw new IllegalArgumentException("`withOutputStream(OutputStream)` and `withOutputConsumer(OutputConsumer)` " + + "are mutually exclusive."); } if (stderr != null && errorConsumer != null) { - throw new IllegalArgumentException("You can either ..."); + throw new IllegalArgumentException("`withErrorStream(OutputStream)` and `withErrorConsumer(OutputConsumer)` " + + "are mutually exclusive."); } try { @@ -271,6 +282,11 @@ public ProcBuilder withVar(String var, String value) { * @return this, for chaining */ public ProcBuilder withOutputConsumer(StreamConsumer outputConsumer) { + if (stdout != defaultStdout) { + throw new IllegalArgumentException("`withOutputStream(OutputStream)` and `withOutputConsumer(OutputConsumer)` " + + "are mutually exclusive."); + } + this.outputConsumer = outputConsumer; return this; @@ -282,6 +298,10 @@ public ProcBuilder withOutputConsumer(StreamConsumer outputConsumer) { * @return this, for chaining */ public ProcBuilder withErrorConsumer(StreamConsumer errorConsumer) { + if (stderr != null) { + throw new IllegalArgumentException("`withErrorStream(OutputStream)` and `withErrorConsumer(OutputConsumer)` " + + "are mutually exclusive."); + } this.errorConsumer = errorConsumer; return this; } diff --git a/src/test/java/org/buildobjects/process/ProcBuilderTest.java b/src/test/java/org/buildobjects/process/ProcBuilderTest.java index 3733dea..c51b81b 100644 --- a/src/test/java/org/buildobjects/process/ProcBuilderTest.java +++ b/src/test/java/org/buildobjects/process/ProcBuilderTest.java @@ -628,4 +628,97 @@ public void testStringEscaping(){ .getProcString() ); } + + /** + * [NO-DOC] + * */ + @Test + public void testMutualExclusivityOfStdOutConsumption1() { + try { + new ProcBuilder("echo") + .withArgs("Hello\n'World'!") + .withOutputStream(System.out) + .withOutputConsumer(new StreamConsumer() { + @Override + public void consume(InputStream stream) throws IOException { + BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); + String line; + while ((line = reader.readLine()) != null) ; + } + }); + fail("`withOutputStream` and `withOutputConsumer` are mutually exclusive"); + } catch (IllegalArgumentException iae) { + assertEquals("`withOutputStream(OutputStream)` and `withOutputConsumer(OutputConsumer)` are mutually exclusive.", iae.getMessage()); + } + } + + /** + * [NO-DOC] + * */ + @Test + public void testMutualExclusivityOfStdOutConsumption2() { + try { + new ProcBuilder("echo") + .withArgs("Hello\n'World'!") + .withOutputConsumer(new StreamConsumer() { + @Override + public void consume(InputStream stream) throws IOException { + BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); + String line; + while ((line = reader.readLine()) != null) ; + } + }) + .withOutputStream(System.out); + fail("`withOutputStream` and `withOutputConsumer` are mutually exclusive"); + } catch (IllegalArgumentException iae) { + assertEquals("`withOutputStream(OutputStream)` and `withOutputConsumer(OutputConsumer)` are mutually exclusive.", iae.getMessage()); + } + } + + + /** + * [NO-DOC] + * */ + @Test + public void testMutualExclusivityOfStdErrConsumption1() { + try { + new ProcBuilder("echo") + .withArgs("Hello\n'World'!") + .withErrorStream(System.out) + .withErrorConsumer(new StreamConsumer() { + @Override + public void consume(InputStream stream) throws IOException { + BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); + String line; + while ((line = reader.readLine()) != null) ; + } + }); + fail("`withErrorStream` and `withErrorConsumer` are mutually exclusive"); + } catch (IllegalArgumentException iae) { + assertEquals("`withErrorStream(OutputStream)` and `withErrorConsumer(OutputConsumer)` are mutually exclusive.", iae.getMessage()); + } + } + + /** + * [NO-DOC] + * */ + @Test + public void testMutualExclusivityOfStdErrConsumption2() { + try { + new ProcBuilder("echo") + .withArgs("Hello\n'World'!") + .withErrorConsumer(new StreamConsumer() { + @Override + public void consume(InputStream stream) throws IOException { + BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); + String line; + while ((line = reader.readLine()) != null) ; + } + }) + .withErrorStream(System.out); + fail("`withErrorStream` and `withErrorConsumer` are mutually exclusive"); + } catch (IllegalArgumentException iae) { + assertEquals("`withErrorStream(OutputStream)` and `withErrorConsumer(OutputConsumer)` are mutually exclusive.", iae.getMessage()); + } + } }