transcendental-lisp/test/error/ErrorManagerTester.java

148 lines
4.2 KiB
Java
Raw Normal View History

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