package error; import static error.ErrorManager.Severity.*; import static org.junit.Assert.*; import java.io.*; import java.util.*; import org.junit.*; import environment.RuntimeEnvironment; import error.ErrorManager.Severity; public class ErrorManagerTester { private static final String TERMINATED = "terminated"; private static final String MESSAGE = "message"; private Set indicatorSet; private ByteArrayOutputStream errorOutputStream; private ByteArrayOutputStream outputStream; private ErrorManager createErrorManagerWithIndicators() { RuntimeEnvironment.getInstance().setErrorTerminationFunction(() -> indicatorSet.add(TERMINATED)); RuntimeEnvironment.getInstance().setErrorOutput(new PrintStream(errorOutputStream)); RuntimeEnvironment.getInstance().setOutput(new PrintStream(outputStream)); return new ErrorManager(); } private LispException createLispException(Severity severity) { return new LispException() { private static final long serialVersionUID = 1L; @Override public Severity getSeverity() { return severity; } @Override public String getMessage() { return MESSAGE; } }; } private void assertTerminated() { assertTrue(indicatorSet.contains(TERMINATED)); } private void assertNotTerminated() { assertFalse(indicatorSet.contains(TERMINATED)); } private void assertWarningMessageNotWritten() { assertTrue(outputStream.toByteArray().length == 0); } private void assertWarningMessageWritten() { assertTrue(outputStream.toByteArray().length > 0); } private void assertErrorMessageNotWritten() { assertTrue(errorOutputStream.toByteArray().length == 0); } private void assertErrorMessageWritten() { assertTrue(errorOutputStream.toByteArray().length > 0); } @Before public void setUp() { this.indicatorSet = new HashSet<>(); this.errorOutputStream = new ByteArrayOutputStream(); this.outputStream = new ByteArrayOutputStream(); } @Test public void givenCriticalExceptionSeverity_RunsProvidedTerminationFunction() { ErrorManager errorManager = createErrorManagerWithIndicators(); errorManager.handle(createLispException(CRITICAL)); assertTerminated(); } @Test public void givenWarningExceptionSeverity_DoesNotRunProvidedTerminationFunction() { ErrorManager errorManager = createErrorManagerWithIndicators(); errorManager.handle(createLispException(WARNING)); assertNotTerminated(); } @Test public void givenErrorExceptionSeverity_DoesNotRunProvidedTerminationFunction() { ErrorManager errorManager = createErrorManagerWithIndicators(); errorManager.handle(createLispException(ERROR)); assertNotTerminated(); } @Test public void usesOutputToDisplayWarningMessage() { ErrorManager errorManager = createErrorManagerWithIndicators(); errorManager.handle(createLispException(WARNING)); assertNotTerminated(); assertErrorMessageNotWritten(); assertWarningMessageWritten(); } @Test public void usesErrorOutputToDisplayErrorMessage() { ErrorManager errorManager = createErrorManagerWithIndicators(); errorManager.handle(createLispException(ERROR)); assertNotTerminated(); assertWarningMessageNotWritten(); assertErrorMessageWritten(); } @Test public void noMessageDisplayedBeforeError() { createErrorManagerWithIndicators(); assertNotTerminated(); assertErrorMessageNotWritten(); } @Test public void usesOutputFunctionToDisplayMessages_WithTermination() { ErrorManager errorManager = createErrorManagerWithIndicators(); errorManager.handle(createLispException(CRITICAL)); assertTerminated(); assertErrorMessageWritten(); } @Test public void severityCoverage() { Severity.valueOf(WARNING.toString()); Severity.valueOf(ERROR.toString()); Severity.valueOf(CRITICAL.toString()); } }