transcendental-lisp/test/error/ErrorManagerTester.java

162 lines
4.5 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;
2017-02-11 10:51:37 -05:00
private RuntimeEnvironment environment;
public ErrorManagerTester() {
this.environment = RuntimeEnvironment.getInstance();
}
private ErrorManager createErrorManagerWithIndicators() {
2017-02-11 10:51:37 -05:00
environment.setErrorTerminationFunction(() -> indicatorSet.add(TERMINATED));
environment.setErrorOutput(new PrintStream(errorOutputStream));
environment.setOutput(new PrintStream(outputStream));
2017-02-11 13:33:34 -05:00
environment.setWarningOutputDecorator(s -> s);
environment.setErrorOutputDecorator(s -> s);
environment.setCriticalOutputDecorator(s -> s);
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() {
2017-02-11 10:51:37 -05:00
indicatorSet = new HashSet<>();
errorOutputStream = new ByteArrayOutputStream();
outputStream = new ByteArrayOutputStream();
environment.reset();
}
@After
public void tearDown() {
environment.reset();
}
@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());
}
}