Skip to content

Fixes bug in unit tests that meant exceptions were being "hidden" from callables and uses unchecked instead of checked exceptions #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.flattened-pom.xml
target
.idea
*.iml
Copy link
Owner

@stefanbirkner stefanbirkner Aug 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be part of your personal global gitignore file. From the git docs:

Patterns which a user wants Git to ignore in all situations (e.g., backup or temporary files generated by the user’s editor of choice) generally go into a file specified by core.excludesFile in the user’s ~/.gitconfig.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay nice, ill take a look at setting this up, thanks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be done that way, though it's probably a nicety to allow it in the repo too.

Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -522,6 +523,12 @@ public static String tapSystemErr(
return tapStream.textThatWasWritten();
}

public static Collection<String> 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
Expand Down Expand Up @@ -581,6 +588,12 @@ public static String tapSystemOut(
return tapStream.textThatWasWritten();
}

public static Collection<String> 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
Expand Down Expand Up @@ -807,6 +820,13 @@ private static PrintStream wrap(
);
}

private static Collection<String> 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(
Expand Down Expand Up @@ -1111,19 +1131,20 @@ private String format(
* @param <T> 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)
* @see #execute(Statement)
*/
public <T> T execute(
Callable<T> callable
) throws Exception {
) {
Map<String, String> originalVariables = new HashMap<>(getenv());
try {
setEnvironmentVariables();
return callable.call();
} catch (Exception e) {
throw new SystemLambdaExecutionException(e);
} finally {
restoreOriginalVariables(originalVariables);
}
Expand Down Expand Up @@ -1159,19 +1180,20 @@ public <T> 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)
* @see #execute(Callable)
*/
public void execute(
Statement statement
) throws Exception {
) {
Map<String, String> originalVariables = new HashMap<>(getenv());
try {
setEnvironmentVariables();
statement.execute();
} catch (Exception e) {
throw new SystemLambdaExecutionException(e);
} finally {
restoreOriginalVariables(originalVariables);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.github.stefanbirkner.systemlambda;

public class SystemLambdaExecutionException extends RuntimeException {

public SystemLambdaExecutionException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.stefanbirkner.systemlambda;

import java.util.concurrent.Callable;

class FailingCallableMock implements Callable<String> {
boolean hasBeenEvaluated = false;
Exception exception = new Exception("failing callable mock exception");

@Override
public String call() throws Exception {
hasBeenEvaluated = true;
throw exception;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<String> 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 {
Expand All @@ -31,7 +46,7 @@ void tapped_text_is_empty_when_statement_does_not_write_to_System_err(
);

assertThat(textWrittenToSystemErr)
.isEqualTo("");
.isEmpty();
}

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<String> 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 {
Expand All @@ -31,7 +45,7 @@ void tapped_text_is_empty_when_statement_does_not_write_to_System_out(
);

assertThat(textWrittenToSystemOut)
.isEqualTo("");
.isEmpty();
}

@Nested
Expand Down
Loading