Skip to content

Commit

Permalink
Improve error handling and test for Exception.
Browse files Browse the repository at this point in the history
  • Loading branch information
fleipold committed Apr 11, 2021
1 parent 6db33b1 commit bcadc49
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/main/java/org/buildobjects/process/ProcBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
93 changes: 93 additions & 0 deletions src/test/java/org/buildobjects/process/ProcBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}

0 comments on commit bcadc49

Please sign in to comment.