package error; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.util.HashSet; import java.util.Set; import org.junit.Before; import org.junit.Test; public class ErrorManagerTester { private static final String TERMINATED = "terminated"; private static final String MESSAGE = "message"; private Set indicatorSet; private ErrorManager createErrorManagerWithIndicators() { return new ErrorManager(() -> indicatorSet.add(TERMINATED), (String) -> indicatorSet.add(MESSAGE)); } private LispException createLispException(int severity) { return new LispException() { private static final long serialVersionUID = 1L; @Override public int getSeverity() { return severity; } @Override public String getMessage() { return MESSAGE; } }; } @Before public void setUp() { this.indicatorSet = new HashSet<>(); } @Test public void givenCriticalExceptionSeverity_RunsProvidedTerminationFunction() { ErrorManager errorManager = createErrorManagerWithIndicators(); errorManager.generateError(createLispException(ErrorManager.CRITICAL_LEVEL)); assertTrue(indicatorSet.contains(TERMINATED)); } @Test public void givenNonCriticalExceptionSeverity_DoesNotRunProvidedTerminationFunction() { ErrorManager errorManager = createErrorManagerWithIndicators(); errorManager.generateError(createLispException(0)); assertFalse(indicatorSet.contains(TERMINATED)); } @Test public void noMessageDisplayedBeforeError() { createErrorManagerWithIndicators(); assertFalse(indicatorSet.contains(TERMINATED)); assertFalse(indicatorSet.contains(MESSAGE)); } @Test public void usesOutputFunctionToDisplayMessages_NoTermination() { ErrorManager errorManager = createErrorManagerWithIndicators(); errorManager.generateError(createLispException(0)); assertFalse(indicatorSet.contains(TERMINATED)); assertTrue(indicatorSet.contains(MESSAGE)); } @Test public void usesOutputFunctionToDisplayMessages_WithTermination() { ErrorManager errorManager = createErrorManagerWithIndicators(); errorManager.generateError(createLispException(ErrorManager.CRITICAL_LEVEL)); assertTrue(indicatorSet.contains(TERMINATED)); assertTrue(indicatorSet.contains(MESSAGE)); } }