From b7f16f0a0b9a68e1f2a2da2dc85d054daeb285b6 Mon Sep 17 00:00:00 2001 From: Michael Ruocco Date: Sun, 30 Aug 2020 06:53:09 +0100 Subject: [PATCH 1/6] specifies surefire plugin versiont to use and updates test to show callables are not throwing exceptions as expected --- .gitignore | 2 ++ pom.xml | 1 + .../systemlambda/WithEnvironmentVariableTest.java | 12 ++++++++---- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 8643541..33cd2bd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .flattened-pom.xml target +.idea +*.iml diff --git a/pom.xml b/pom.xml index bc2722e..df71e18 100644 --- a/pom.xml +++ b/pom.xml @@ -67,6 +67,7 @@ maven-surefire-plugin + 3.0.0-M5 1 false diff --git a/src/test/java/com/github/stefanbirkner/systemlambda/WithEnvironmentVariableTest.java b/src/test/java/com/github/stefanbirkner/systemlambda/WithEnvironmentVariableTest.java index d5c5697..b9925fb 100644 --- a/src/test/java/com/github/stefanbirkner/systemlambda/WithEnvironmentVariableTest.java +++ b/src/test/java/com/github/stefanbirkner/systemlambda/WithEnvironmentVariableTest.java @@ -249,13 +249,14 @@ void after_callable_throws_exception() { Map originalEnvironmentVariables = new HashMap<>(getenv()); - ignoreException( + Throwable error = catchThrowable( () -> withEnvironmentVariable("dummy name", randomValue()) .execute(() -> (Callable) () -> { throw new RuntimeException("dummy exception"); }) ); + assertThat(error).hasMessage("dummy exception"); assertThat(getenv()).isEqualTo(originalEnvironmentVariables); } @@ -264,13 +265,14 @@ void after_statement_throws_exception() { Map originalEnvironmentVariables = new HashMap<>(getenv()); - ignoreException( + Throwable error = catchThrowable( () -> withEnvironmentVariable("dummy name", randomValue()) .execute(() -> { throw new RuntimeException("dummy exception"); } ) ); + assertThat(error).hasMessage("dummy exception"); assertThat(getenv()).isEqualTo(originalEnvironmentVariables); } } @@ -304,13 +306,14 @@ void after_statement_is_executed( void after_callable_throws_exception() { String originalValue = getenv("dummy name"); - ignoreException( + Throwable error = catchThrowable( () -> withEnvironmentVariable("dummy name", randomValue()) .execute(() -> (Callable) () -> { throw new RuntimeException("dummy exception"); }) ); + assertThat(error).hasMessage("dummy exception"); assertThat(getenv("dummy name")).isEqualTo(originalValue); } @@ -318,7 +321,7 @@ void after_callable_throws_exception() { void after_statement_throws_exception() { String originalValue = getenv("dummy name"); - ignoreException( + Throwable error = catchThrowable( () -> withEnvironmentVariable("dummy name", randomValue()) .execute(() -> { throw new RuntimeException("dummy exception"); @@ -326,6 +329,7 @@ void after_statement_throws_exception() { ) ); + assertThat(error).hasMessage("dummy exception"); assertThat(getenv("dummy name")).isEqualTo(originalValue); } } From 2b6013a2d2c7090658ace5f88b11044890e2055b Mon Sep 17 00:00:00 2001 From: Michael Ruocco Date: Sun, 30 Aug 2020 06:56:32 +0100 Subject: [PATCH 2/6] fixes double nested lambda bug that was causing exceptions to not be thrown as expected --- .../systemlambda/WithEnvironmentVariableTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/github/stefanbirkner/systemlambda/WithEnvironmentVariableTest.java b/src/test/java/com/github/stefanbirkner/systemlambda/WithEnvironmentVariableTest.java index b9925fb..c761ff1 100644 --- a/src/test/java/com/github/stefanbirkner/systemlambda/WithEnvironmentVariableTest.java +++ b/src/test/java/com/github/stefanbirkner/systemlambda/WithEnvironmentVariableTest.java @@ -11,7 +11,6 @@ import java.util.Map; import java.util.concurrent.Callable; -import static com.github.stefanbirkner.fishbowl.Fishbowl.ignoreException; import static com.github.stefanbirkner.systemlambda.SystemLambda.withEnvironmentVariable; import static java.lang.System.getenv; import static org.assertj.core.api.Assertions.assertThat; @@ -251,7 +250,7 @@ void after_callable_throws_exception() { Throwable error = catchThrowable( () -> withEnvironmentVariable("dummy name", randomValue()) - .execute(() -> (Callable) () -> { + .execute((Callable) () -> { throw new RuntimeException("dummy exception"); }) ); @@ -308,7 +307,7 @@ void after_callable_throws_exception() { Throwable error = catchThrowable( () -> withEnvironmentVariable("dummy name", randomValue()) - .execute(() -> (Callable) () -> { + .execute((Callable) () -> { throw new RuntimeException("dummy exception"); }) ); From c340f8c64b69019a5e79bcc7792d116652e08820 Mon Sep 17 00:00:00 2001 From: Michael Ruocco Date: Sun, 30 Aug 2020 07:08:53 +0100 Subject: [PATCH 3/6] refactors to remove small amount of duplication from exception tests --- .../systemlambda/FailingCallableMock.java | 14 +++++++++ .../systemlambda/FailingStatementMock.java | 12 ++++++++ .../WithEnvironmentVariableTest.java | 30 ++++++++----------- 3 files changed, 38 insertions(+), 18 deletions(-) create mode 100644 src/test/java/com/github/stefanbirkner/systemlambda/FailingCallableMock.java create mode 100644 src/test/java/com/github/stefanbirkner/systemlambda/FailingStatementMock.java diff --git a/src/test/java/com/github/stefanbirkner/systemlambda/FailingCallableMock.java b/src/test/java/com/github/stefanbirkner/systemlambda/FailingCallableMock.java new file mode 100644 index 0000000..837b7a1 --- /dev/null +++ b/src/test/java/com/github/stefanbirkner/systemlambda/FailingCallableMock.java @@ -0,0 +1,14 @@ +package com.github.stefanbirkner.systemlambda; + +import java.util.concurrent.Callable; + +class FailingCallableMock implements Callable { + boolean hasBeenEvaluated = false; + Exception exception = new Exception("failing callable mock exception"); + + @Override + public String call() throws Exception { + hasBeenEvaluated = true; + throw exception; + } +} diff --git a/src/test/java/com/github/stefanbirkner/systemlambda/FailingStatementMock.java b/src/test/java/com/github/stefanbirkner/systemlambda/FailingStatementMock.java new file mode 100644 index 0000000..c8f4a75 --- /dev/null +++ b/src/test/java/com/github/stefanbirkner/systemlambda/FailingStatementMock.java @@ -0,0 +1,12 @@ +package com.github.stefanbirkner.systemlambda; + +class FailingStatementMock implements Statement { + boolean hasBeenEvaluated = false; + Exception exception = new Exception("failing statement mock exception"); + + @Override + public void execute() throws Exception { + hasBeenEvaluated = true; + throw exception; + } +} diff --git a/src/test/java/com/github/stefanbirkner/systemlambda/WithEnvironmentVariableTest.java b/src/test/java/com/github/stefanbirkner/systemlambda/WithEnvironmentVariableTest.java index c761ff1..78f1e32 100644 --- a/src/test/java/com/github/stefanbirkner/systemlambda/WithEnvironmentVariableTest.java +++ b/src/test/java/com/github/stefanbirkner/systemlambda/WithEnvironmentVariableTest.java @@ -9,7 +9,6 @@ import java.util.HashMap; import java.util.Map; -import java.util.concurrent.Callable; import static com.github.stefanbirkner.systemlambda.SystemLambda.withEnvironmentVariable; import static java.lang.System.getenv; @@ -247,15 +246,14 @@ void after_statement_is_executed( void after_callable_throws_exception() { Map originalEnvironmentVariables = new HashMap<>(getenv()); + FailingCallableMock failingCallable = new FailingCallableMock(); Throwable error = catchThrowable( () -> withEnvironmentVariable("dummy name", randomValue()) - .execute((Callable) () -> { - throw new RuntimeException("dummy exception"); - }) + .execute(failingCallable) ); - assertThat(error).hasMessage("dummy exception"); + assertThat(error).isEqualTo(failingCallable.exception); assertThat(getenv()).isEqualTo(originalEnvironmentVariables); } @@ -263,15 +261,14 @@ void after_callable_throws_exception() { void after_statement_throws_exception() { Map originalEnvironmentVariables = new HashMap<>(getenv()); + FailingStatementMock failingStatement = new FailingStatementMock(); Throwable error = catchThrowable( () -> withEnvironmentVariable("dummy name", randomValue()) - .execute(() -> { - throw new RuntimeException("dummy exception"); } - ) + .execute(failingStatement) ); - assertThat(error).hasMessage("dummy exception"); + assertThat(error).isEqualTo(failingStatement.exception); assertThat(getenv()).isEqualTo(originalEnvironmentVariables); } } @@ -304,31 +301,28 @@ void after_statement_is_executed( @Test void after_callable_throws_exception() { String originalValue = getenv("dummy name"); + FailingCallableMock failingCallable = new FailingCallableMock(); Throwable error = catchThrowable( () -> withEnvironmentVariable("dummy name", randomValue()) - .execute((Callable) () -> { - throw new RuntimeException("dummy exception"); - }) + .execute(failingCallable) ); - assertThat(error).hasMessage("dummy exception"); + assertThat(error).isEqualTo(failingCallable.exception); assertThat(getenv("dummy name")).isEqualTo(originalValue); } @Test void after_statement_throws_exception() { String originalValue = getenv("dummy name"); + FailingStatementMock failingStatement = new FailingStatementMock(); Throwable error = catchThrowable( () -> withEnvironmentVariable("dummy name", randomValue()) - .execute(() -> { - throw new RuntimeException("dummy exception"); - } - ) + .execute(failingStatement) ); - assertThat(error).hasMessage("dummy exception"); + assertThat(error).isEqualTo(failingStatement.exception); assertThat(getenv("dummy name")).isEqualTo(originalValue); } } From 17c71f9deeae02a2d982596f7129d7e5bb3adf7a Mon Sep 17 00:00:00 2001 From: Michael Ruocco Date: Sun, 30 Aug 2020 07:19:26 +0100 Subject: [PATCH 4/6] updates to throw unchecked rather than checked exception so users are not forced to handle exceptions if they do not need to --- .../systemlambda/SystemLambda.java | 10 ++-- .../SystemLambdaExecutionException.java | 8 +++ .../SystemLambdaExecutionExceptionTest.java | 17 ++++++ .../WithEnvironmentVariableTest.java | 52 +++++++++++-------- 4 files changed, 61 insertions(+), 26 deletions(-) create mode 100644 src/main/java/com/github/stefanbirkner/systemlambda/SystemLambdaExecutionException.java create mode 100644 src/test/java/com/github/stefanbirkner/systemlambda/SystemLambdaExecutionExceptionTest.java diff --git a/src/main/java/com/github/stefanbirkner/systemlambda/SystemLambda.java b/src/main/java/com/github/stefanbirkner/systemlambda/SystemLambda.java index 94f21b8..64b78d4 100644 --- a/src/main/java/com/github/stefanbirkner/systemlambda/SystemLambda.java +++ b/src/main/java/com/github/stefanbirkner/systemlambda/SystemLambda.java @@ -1111,7 +1111,6 @@ private String format( * @param the type of {@code callable}'s result * @param callable an arbitrary piece of code. * @return the return value of {@code callable}. - * @throws Exception any exception thrown by the callable. * @since 1.1.0 * @see #withEnvironmentVariable(String, String) * @see #and(String, String) @@ -1119,11 +1118,13 @@ private String format( */ public T execute( Callable callable - ) throws Exception { + ) { Map originalVariables = new HashMap<>(getenv()); try { setEnvironmentVariables(); return callable.call(); + } catch (Exception e) { + throw new SystemLambdaExecutionException(e); } finally { restoreOriginalVariables(originalVariables); } @@ -1159,7 +1160,6 @@ public T execute( * environment variables map. It fails if your {@code SecurityManager} forbids * such modifications. * @param statement an arbitrary piece of code. - * @throws Exception any exception thrown by the statement. * @since 1.0.0 * @see #withEnvironmentVariable(String, String) * @see WithEnvironmentVariables#and(String, String) @@ -1167,11 +1167,13 @@ public T execute( */ public void execute( Statement statement - ) throws Exception { + ) { Map originalVariables = new HashMap<>(getenv()); try { setEnvironmentVariables(); statement.execute(); + } catch (Exception e) { + throw new SystemLambdaExecutionException(e); } finally { restoreOriginalVariables(originalVariables); } diff --git a/src/main/java/com/github/stefanbirkner/systemlambda/SystemLambdaExecutionException.java b/src/main/java/com/github/stefanbirkner/systemlambda/SystemLambdaExecutionException.java new file mode 100644 index 0000000..7c1e1a3 --- /dev/null +++ b/src/main/java/com/github/stefanbirkner/systemlambda/SystemLambdaExecutionException.java @@ -0,0 +1,8 @@ +package com.github.stefanbirkner.systemlambda; + +public class SystemLambdaExecutionException extends RuntimeException { + + public SystemLambdaExecutionException(Throwable cause) { + super(cause); + } +} diff --git a/src/test/java/com/github/stefanbirkner/systemlambda/SystemLambdaExecutionExceptionTest.java b/src/test/java/com/github/stefanbirkner/systemlambda/SystemLambdaExecutionExceptionTest.java new file mode 100644 index 0000000..fc46664 --- /dev/null +++ b/src/test/java/com/github/stefanbirkner/systemlambda/SystemLambdaExecutionExceptionTest.java @@ -0,0 +1,17 @@ +package com.github.stefanbirkner.systemlambda; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class SystemLambdaExecutionExceptionTest { + + @Test + void shouldReturnCause() { + Throwable cause = new Exception("cause"); + + Throwable exception = new SystemLambdaExecutionException(cause); + + assertThat(exception.getCause()).isEqualTo(cause); + } +} diff --git a/src/test/java/com/github/stefanbirkner/systemlambda/WithEnvironmentVariableTest.java b/src/test/java/com/github/stefanbirkner/systemlambda/WithEnvironmentVariableTest.java index 78f1e32..5637976 100644 --- a/src/test/java/com/github/stefanbirkner/systemlambda/WithEnvironmentVariableTest.java +++ b/src/test/java/com/github/stefanbirkner/systemlambda/WithEnvironmentVariableTest.java @@ -19,7 +19,7 @@ class WithEnvironmentVariableTest { @Test void callable_is_executed( - ) throws Exception { + ) { CallableMock callable = new CallableMock(); withEnvironmentVariable("dummy name", "dummy value") .execute(callable); @@ -29,7 +29,7 @@ void callable_is_executed( @Test void return_value_of_callable_is_exposed( - ) throws Exception { + ) { String value = withEnvironmentVariable("dummy name", "dummy value") .execute(() -> "return value"); @@ -38,7 +38,7 @@ void return_value_of_callable_is_exposed( @Test void statement_is_executed( - ) throws Exception { + ) { StatementMock statementMock = new StatementMock(); withEnvironmentVariable("dummy name", "dummy value") @@ -51,7 +51,7 @@ void statement_is_executed( class environment_variable_that_is_set_to_some_value { @Test void is_available_in_the_callable( - ) throws Exception { + ) { withEnvironmentVariable("dummy name", "dummy value") .execute(() -> { assertThat(getenv("dummy name")).isEqualTo("dummy value"); @@ -61,7 +61,7 @@ void is_available_in_the_callable( @Test void is_available_in_the_statement( - ) throws Exception { + ) { withEnvironmentVariable("dummy name", "dummy value") .execute(() -> assertThat(getenv("dummy name")).isEqualTo("dummy value") @@ -70,7 +70,7 @@ void is_available_in_the_statement( @Test void is_available_in_the_callable_from_environment_variables_map( - ) throws Exception { + ) { withEnvironmentVariable("dummy name", "dummy value") .execute(() -> { assertThat(getenv()).containsEntry("dummy name", "dummy value"); @@ -80,7 +80,7 @@ void is_available_in_the_callable_from_environment_variables_map( @Test void is_available_in_the_statement_from_environment_variables_map( - ) throws Exception { + ) { withEnvironmentVariable("dummy name", "dummy value") .execute(() -> assertThat(getenv()).containsEntry("dummy name", "dummy value") @@ -92,7 +92,7 @@ void is_available_in_the_statement_from_environment_variables_map( class multiple_environment_variable_that_are_set_to_some_value { @Test void are_available_in_the_callable( - ) throws Exception { + ) { withEnvironmentVariable("first", "first value") .and("second", "second value") .execute(() -> { @@ -104,7 +104,7 @@ void are_available_in_the_callable( @Test void are_available_in_the_statement( - ) throws Exception { + ) { withEnvironmentVariable("first", "first value") .and("second", "second value") .execute(() -> { @@ -118,7 +118,7 @@ void are_available_in_the_statement( class environment_variable_that_is_set_to_null { @Test void is_null_in_the_callable( - ) throws Exception { + ) { //we need to set a value because it is null by default withEnvironmentVariable("dummy name", randomValue()) .execute(() -> @@ -132,7 +132,7 @@ void is_null_in_the_callable( @Test void is_null_in_the_statement( - ) throws Exception { + ) { //we need to set a value because it is null by default withEnvironmentVariable("dummy name", randomValue()) .execute(() -> @@ -143,7 +143,7 @@ void is_null_in_the_statement( @Test void is_not_stored_in_the_environment_variables_map_in_the_callable( - ) throws Exception { + ) { //we need to set a value because it is null by default withEnvironmentVariable("dummy name", randomValue()) .execute(() -> @@ -157,7 +157,7 @@ void is_not_stored_in_the_environment_variables_map_in_the_callable( @Test void is_not_stored_in_the_environment_variables_map_in_the_statement( - ) throws Exception { + ) { //we need to set a value because it is null by default withEnvironmentVariable("dummy name", randomValue()) .execute(() -> @@ -202,7 +202,7 @@ void when_an_environment_variable_is_set_twice_null_is_not_enclosed_in_single_qu @Test void the_and_method_creates_a_new_object_so_that_EnvironmentVariables_object_can_be_reused( - ) throws Exception { + ) { WithEnvironmentVariables baseSetting = withEnvironmentVariable("first", "first value"); baseSetting.and("second", "second value") .execute(() -> {}); @@ -219,7 +219,7 @@ void the_and_method_creates_a_new_object_so_that_EnvironmentVariables_object_can class environment_variables_map_contains_same_values_as_before { @Test void after_callable_is_executed( - ) throws Exception { + ) { Map originalEnvironmentVariables = new HashMap<>(getenv()); @@ -231,7 +231,7 @@ void after_callable_is_executed( @Test void after_statement_is_executed( - ) throws Exception { + ) { Map originalEnvironmentVariables = new HashMap<>(getenv()); @@ -253,7 +253,9 @@ void after_callable_throws_exception() { .execute(failingCallable) ); - assertThat(error).isEqualTo(failingCallable.exception); + assertThat(error) + .isInstanceOf(SystemLambdaExecutionException.class) + .hasCause(failingCallable.exception); assertThat(getenv()).isEqualTo(originalEnvironmentVariables); } @@ -268,7 +270,9 @@ void after_statement_throws_exception() { .execute(failingStatement) ); - assertThat(error).isEqualTo(failingStatement.exception); + assertThat(error) + .isInstanceOf(SystemLambdaExecutionException.class) + .hasCause(failingStatement.exception); assertThat(getenv()).isEqualTo(originalEnvironmentVariables); } } @@ -277,7 +281,7 @@ void after_statement_throws_exception() { class environment_variables_are_the_same_as_before { @Test void after_callable_is_executed( - ) throws Exception { + ) { String originalValue = getenv("dummy name"); withEnvironmentVariable("dummy name", randomValue()) @@ -288,7 +292,7 @@ void after_callable_is_executed( @Test void after_statement_is_executed( - ) throws Exception { + ) { String originalValue = getenv("dummy name"); withEnvironmentVariable("dummy name", randomValue()) @@ -308,7 +312,9 @@ void after_callable_throws_exception() { .execute(failingCallable) ); - assertThat(error).isEqualTo(failingCallable.exception); + assertThat(error) + .isInstanceOf(SystemLambdaExecutionException.class) + .hasCause(failingCallable.exception); assertThat(getenv("dummy name")).isEqualTo(originalValue); } @@ -322,7 +328,9 @@ void after_statement_throws_exception() { .execute(failingStatement) ); - assertThat(error).isEqualTo(failingStatement.exception); + assertThat(error) + .isInstanceOf(SystemLambdaExecutionException.class) + .hasCause(failingStatement.exception); assertThat(getenv("dummy name")).isEqualTo(originalValue); } } From c103c5529c149ac766c62ab6058467aa4b3c968f Mon Sep 17 00:00:00 2001 From: Michael Ruocco Date: Sun, 30 Aug 2020 07:23:43 +0100 Subject: [PATCH 5/6] splits out specific tests for exception handling and reverts old tests to ignore exceptions when restoring original env var values --- .../WithEnvironmentVariableTest.java | 56 ++++++++++++------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/src/test/java/com/github/stefanbirkner/systemlambda/WithEnvironmentVariableTest.java b/src/test/java/com/github/stefanbirkner/systemlambda/WithEnvironmentVariableTest.java index 5637976..26d8cba 100644 --- a/src/test/java/com/github/stefanbirkner/systemlambda/WithEnvironmentVariableTest.java +++ b/src/test/java/com/github/stefanbirkner/systemlambda/WithEnvironmentVariableTest.java @@ -10,6 +10,7 @@ import java.util.HashMap; import java.util.Map; +import static com.github.stefanbirkner.fishbowl.Fishbowl.ignoreException; import static com.github.stefanbirkner.systemlambda.SystemLambda.withEnvironmentVariable; import static java.lang.System.getenv; import static org.assertj.core.api.Assertions.assertThat; @@ -246,16 +247,12 @@ void after_statement_is_executed( void after_callable_throws_exception() { Map originalEnvironmentVariables = new HashMap<>(getenv()); - FailingCallableMock failingCallable = new FailingCallableMock(); - Throwable error = catchThrowable( + ignoreException( () -> withEnvironmentVariable("dummy name", randomValue()) - .execute(failingCallable) + .execute(new FailingCallableMock()) ); - assertThat(error) - .isInstanceOf(SystemLambdaExecutionException.class) - .hasCause(failingCallable.exception); assertThat(getenv()).isEqualTo(originalEnvironmentVariables); } @@ -263,16 +260,12 @@ void after_callable_throws_exception() { void after_statement_throws_exception() { Map originalEnvironmentVariables = new HashMap<>(getenv()); - FailingStatementMock failingStatement = new FailingStatementMock(); - Throwable error = catchThrowable( + ignoreException( () -> withEnvironmentVariable("dummy name", randomValue()) - .execute(failingStatement) + .execute(new FailingStatementMock()) ); - assertThat(error) - .isInstanceOf(SystemLambdaExecutionException.class) - .hasCause(failingStatement.exception); assertThat(getenv()).isEqualTo(originalEnvironmentVariables); } } @@ -305,33 +298,56 @@ void after_statement_is_executed( @Test void after_callable_throws_exception() { String originalValue = getenv("dummy name"); + + ignoreException( + () -> withEnvironmentVariable("dummy name", randomValue()) + .execute(new FailingCallableMock()) + ); + + assertThat(getenv("dummy name")).isEqualTo(originalValue); + } + + @Test + void after_statement_throws_exception() { + String originalValue = getenv("dummy name"); + + ignoreException( + () -> withEnvironmentVariable("dummy name", randomValue()) + .execute(new FailingStatementMock()) + ); + + assertThat(getenv("dummy name")).isEqualTo(originalValue); + } + } + + @Nested + class should_wrap_exceptions { + @Test + void when_callable_throws_exception() { FailingCallableMock failingCallable = new FailingCallableMock(); Throwable error = catchThrowable( - () -> withEnvironmentVariable("dummy name", randomValue()) - .execute(failingCallable) + () -> withEnvironmentVariable("dummy name", randomValue()) + .execute(failingCallable) ); assertThat(error) .isInstanceOf(SystemLambdaExecutionException.class) .hasCause(failingCallable.exception); - assertThat(getenv("dummy name")).isEqualTo(originalValue); } @Test - void after_statement_throws_exception() { - String originalValue = getenv("dummy name"); + void when_statement_throws_exception() { FailingStatementMock failingStatement = new FailingStatementMock(); Throwable error = catchThrowable( - () -> withEnvironmentVariable("dummy name", randomValue()) - .execute(failingStatement) + () -> withEnvironmentVariable("dummy name", randomValue()) + .execute(failingStatement) ); assertThat(error) .isInstanceOf(SystemLambdaExecutionException.class) .hasCause(failingStatement.exception); - assertThat(getenv("dummy name")).isEqualTo(originalValue); } } From 0e95daa404aefd4c2eeb70ae89102559edccb053 Mon Sep 17 00:00:00 2001 From: Michael Ruocco Date: Sun, 20 Dec 2020 08:17:45 +0000 Subject: [PATCH 6/6] adds wrapper methods to return system out and err as a collection of lines --- .../systemlambda/SystemLambda.java | 20 +++++++++++++++++++ .../systemlambda/TapSystemErrTest.java | 17 +++++++++++++++- .../systemlambda/TapSystemOutTest.java | 16 ++++++++++++++- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/github/stefanbirkner/systemlambda/SystemLambda.java b/src/main/java/com/github/stefanbirkner/systemlambda/SystemLambda.java index b8fd258..f8125fc 100644 --- a/src/main/java/com/github/stefanbirkner/systemlambda/SystemLambda.java +++ b/src/main/java/com/github/stefanbirkner/systemlambda/SystemLambda.java @@ -6,6 +6,7 @@ import java.security.Permission; import java.util.*; import java.util.concurrent.Callable; +import java.util.stream.Collectors; import static java.lang.Class.forName; import static java.lang.System.*; @@ -522,6 +523,12 @@ public static String tapSystemErr( return tapStream.textThatWasWritten(); } + public static Collection tapSystemErrToCollection( + Statement statement + ) throws Exception { + return toCollection(tapSystemOut(statement)); + } + /** * Executes the statement and returns the text that was written to * {@code System.err} by the statement. New line characters are replaced @@ -581,6 +588,12 @@ public static String tapSystemOut( return tapStream.textThatWasWritten(); } + public static Collection tapSystemOutToCollection( + Statement statement + ) throws Exception { + return toCollection(tapSystemOut(statement)); + } + /** * Executes the statement and returns the text that was written to * {@code System.out} by the statement. New line characters are replaced @@ -807,6 +820,13 @@ private static PrintStream wrap( ); } + private static Collection toCollection( + String value + ) { + String[] lines = value.split(System.lineSeparator()); + return Arrays.stream(lines).collect(Collectors.toList()); + } + private static class DisallowWriteStream extends OutputStream { @Override public void write( diff --git a/src/test/java/com/github/stefanbirkner/systemlambda/TapSystemErrTest.java b/src/test/java/com/github/stefanbirkner/systemlambda/TapSystemErrTest.java index 69fd93c..667cf70 100644 --- a/src/test/java/com/github/stefanbirkner/systemlambda/TapSystemErrTest.java +++ b/src/test/java/com/github/stefanbirkner/systemlambda/TapSystemErrTest.java @@ -5,8 +5,12 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import java.util.Collection; + import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemErr; +import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemErrToCollection; import static java.lang.System.err; +import static java.lang.System.out; import static org.assertj.core.api.Assertions.assertThat; @DisplayNameGeneration(ReplaceUnderscores.class) @@ -23,6 +27,17 @@ void taps_text_that_is_written_to_System_err_by_statement( .isEqualTo("some text"); } + @Test + void taps_text_that_is_written_to_System_err_by_statement_to_collection( + ) throws Exception { + Collection linesWrittenToSystemErr = tapSystemErrToCollection( + () -> { out.println("some text"); out.println("more text"); } + ); + + assertThat(linesWrittenToSystemErr) + .containsExactly("some text", "more text"); + } + @Test void tapped_text_is_empty_when_statement_does_not_write_to_System_err( ) throws Exception { @@ -31,7 +46,7 @@ void tapped_text_is_empty_when_statement_does_not_write_to_System_err( ); assertThat(textWrittenToSystemErr) - .isEqualTo(""); + .isEmpty(); } @Nested diff --git a/src/test/java/com/github/stefanbirkner/systemlambda/TapSystemOutTest.java b/src/test/java/com/github/stefanbirkner/systemlambda/TapSystemOutTest.java index 080dfcc..29b8d8d 100644 --- a/src/test/java/com/github/stefanbirkner/systemlambda/TapSystemOutTest.java +++ b/src/test/java/com/github/stefanbirkner/systemlambda/TapSystemOutTest.java @@ -5,7 +5,10 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import java.util.Collection; + import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemOut; +import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemOutToCollection; import static java.lang.System.*; import static org.assertj.core.api.Assertions.assertThat; @@ -23,6 +26,17 @@ void taps_text_that_is_written_to_System_out_by_statement( .isEqualTo("some text"); } + @Test + void taps_text_that_is_written_to_System_out_by_statement_to_collection( + ) throws Exception { + Collection linesWrittenToSystemOut = tapSystemOutToCollection( + () -> { out.println("some text"); out.println("more text"); } + ); + + assertThat(linesWrittenToSystemOut) + .containsExactly("some text", "more text"); + } + @Test void tapped_text_is_empty_when_statement_does_not_write_to_System_out( ) throws Exception { @@ -31,7 +45,7 @@ void tapped_text_is_empty_when_statement_does_not_write_to_System_out( ); assertThat(textWrittenToSystemOut) - .isEqualTo(""); + .isEmpty(); } @Nested