transcendental-lisp/test/error/ErrorManagerTester.java

109 lines
2.9 KiB
Java
Raw Normal View History

package error;
import static org.junit.Assert.*;
import java.io.*;
import java.util.*;
import org.junit.*;
import environment.RuntimeEnvironment;
public class ErrorManagerTester {
private static final String TERMINATED = "terminated";
private static final String MESSAGE = "message";
private Set<String> indicatorSet;
private ByteArrayOutputStream outputStream;
private ErrorManager createErrorManagerWithIndicators() {
RuntimeEnvironment.getInstance().setErrorTerminationFunction(() -> indicatorSet.add(TERMINATED));
RuntimeEnvironment.getInstance().setErrorOutput(new PrintStream(outputStream));
return new ErrorManager();
}
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;
}
};
}
2016-12-16 14:15:29 -05:00
private void assertTerminated() {
assertTrue(indicatorSet.contains(TERMINATED));
}
private void assertNotTerminated() {
assertFalse(indicatorSet.contains(TERMINATED));
}
private void assertErrorMessageNotWritten() {
assertTrue(outputStream.toByteArray().length == 0);
}
private void assertErrorMessageWritten() {
assertTrue(outputStream.toByteArray().length > 0);
}
@Before
public void setUp() {
this.indicatorSet = new HashSet<>();
this.outputStream = new ByteArrayOutputStream();
}
@Test
public void givenCriticalExceptionSeverity_RunsProvidedTerminationFunction() {
ErrorManager errorManager = createErrorManagerWithIndicators();
errorManager.generateError(createLispException(ErrorManager.CRITICAL_LEVEL));
assertTerminated();
}
@Test
public void givenNonCriticalExceptionSeverity_DoesNotRunProvidedTerminationFunction() {
ErrorManager errorManager = createErrorManagerWithIndicators();
errorManager.generateError(createLispException(0));
assertNotTerminated();
}
@Test
public void usesOutputFunctionToDisplayMessages_NoTermination() {
ErrorManager errorManager = createErrorManagerWithIndicators();
errorManager.generateError(createLispException(0));
assertNotTerminated();
assertErrorMessageWritten();
}
@Test
public void noMessageDisplayedBeforeError() {
createErrorManagerWithIndicators();
assertNotTerminated();
assertErrorMessageNotWritten();
}
@Test
public void usesOutputFunctionToDisplayMessages_WithTermination() {
ErrorManager errorManager = createErrorManagerWithIndicators();
errorManager.generateError(createLispException(ErrorManager.CRITICAL_LEVEL));
assertTerminated();
assertErrorMessageWritten();
}
}