2016-12-16 12:31:16 -05:00
|
|
|
package error;
|
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
import static error.ErrorManager.Severity.*;
|
2017-01-17 13:54:21 -05:00
|
|
|
import static org.junit.Assert.*;
|
2016-12-16 12:31:16 -05:00
|
|
|
|
2017-01-17 13:54:21 -05:00
|
|
|
import java.io.*;
|
|
|
|
import java.util.*;
|
2016-12-16 12:31:16 -05:00
|
|
|
|
2017-01-17 13:54:21 -05:00
|
|
|
import org.junit.*;
|
|
|
|
|
2017-02-06 13:39:05 -05:00
|
|
|
import environment.RuntimeEnvironment;
|
2017-02-11 10:42:07 -05:00
|
|
|
import error.ErrorManager.Severity;
|
2016-12-16 12:31:16 -05:00
|
|
|
|
|
|
|
public class ErrorManagerTester {
|
|
|
|
|
|
|
|
private static final String TERMINATED = "terminated";
|
|
|
|
private static final String MESSAGE = "message";
|
|
|
|
|
2016-12-16 13:12:13 -05:00
|
|
|
private Set<String> indicatorSet;
|
2017-02-11 10:42:07 -05:00
|
|
|
private ByteArrayOutputStream errorOutputStream;
|
2017-01-17 13:54:21 -05:00
|
|
|
private ByteArrayOutputStream outputStream;
|
2017-02-11 10:51:37 -05:00
|
|
|
private RuntimeEnvironment environment;
|
|
|
|
|
|
|
|
public ErrorManagerTester() {
|
|
|
|
this.environment = RuntimeEnvironment.getInstance();
|
|
|
|
}
|
2016-12-16 13:12:13 -05:00
|
|
|
|
|
|
|
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);
|
2017-01-17 13:54:21 -05:00
|
|
|
|
|
|
|
return new ErrorManager();
|
2016-12-16 12:31:16 -05:00
|
|
|
}
|
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
private LispException createLispException(Severity severity) {
|
2016-12-16 12:31:16 -05:00
|
|
|
return new LispException() {
|
|
|
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
|
|
@Override
|
2017-02-11 10:42:07 -05:00
|
|
|
public Severity getSeverity() {
|
2016-12-16 12:31:16 -05:00
|
|
|
return severity;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getMessage() {
|
|
|
|
return MESSAGE;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2016-12-16 14:15:29 -05:00
|
|
|
|
2017-01-17 13:54:21 -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() {
|
2017-01-17 13:54:21 -05:00
|
|
|
assertTrue(outputStream.toByteArray().length == 0);
|
|
|
|
}
|
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
private void assertWarningMessageWritten() {
|
2017-01-17 13:54:21 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-12-16 13:12:13 -05:00
|
|
|
@Before
|
|
|
|
public void setUp() {
|
2017-02-11 10:51:37 -05:00
|
|
|
indicatorSet = new HashSet<>();
|
|
|
|
errorOutputStream = new ByteArrayOutputStream();
|
|
|
|
outputStream = new ByteArrayOutputStream();
|
2017-03-05 10:20:31 -05:00
|
|
|
environment.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
@After
|
|
|
|
public void tearDown() {
|
|
|
|
environment.reset();
|
2016-12-16 13:12:13 -05:00
|
|
|
}
|
2016-12-16 12:31:16 -05:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void givenCriticalExceptionSeverity_RunsProvidedTerminationFunction() {
|
2016-12-16 13:12:13 -05:00
|
|
|
ErrorManager errorManager = createErrorManagerWithIndicators();
|
2016-12-16 12:31:16 -05:00
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
errorManager.handle(createLispException(CRITICAL));
|
2017-01-17 13:54:21 -05:00
|
|
|
assertTerminated();
|
2016-12-16 12:31:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@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() {
|
2016-12-16 13:12:13 -05:00
|
|
|
ErrorManager errorManager = createErrorManagerWithIndicators();
|
2016-12-16 12:31:16 -05:00
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
errorManager.handle(createLispException(WARNING));
|
2017-01-17 13:54:21 -05:00
|
|
|
assertNotTerminated();
|
2017-02-11 10:42:07 -05:00
|
|
|
assertErrorMessageNotWritten();
|
|
|
|
assertWarningMessageWritten();
|
2016-12-16 12:31:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-02-11 10:42:07 -05:00
|
|
|
public void usesErrorOutputToDisplayErrorMessage() {
|
2017-01-17 13:54:21 -05:00
|
|
|
ErrorManager errorManager = createErrorManagerWithIndicators();
|
2016-12-16 12:31:16 -05:00
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
errorManager.handle(createLispException(ERROR));
|
2017-01-17 13:54:21 -05:00
|
|
|
assertNotTerminated();
|
2017-02-11 10:42:07 -05:00
|
|
|
assertWarningMessageNotWritten();
|
2017-01-17 13:54:21 -05:00
|
|
|
assertErrorMessageWritten();
|
2016-12-16 12:31:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-01-17 13:54:21 -05:00
|
|
|
public void noMessageDisplayedBeforeError() {
|
|
|
|
createErrorManagerWithIndicators();
|
2016-12-16 12:31:16 -05:00
|
|
|
|
2017-01-17 13:54:21 -05:00
|
|
|
assertNotTerminated();
|
|
|
|
assertErrorMessageNotWritten();
|
2016-12-16 12:31:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void usesOutputFunctionToDisplayMessages_WithTermination() {
|
2016-12-16 13:12:13 -05:00
|
|
|
ErrorManager errorManager = createErrorManagerWithIndicators();
|
2016-12-16 12:31:16 -05:00
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
errorManager.handle(createLispException(CRITICAL));
|
2017-01-17 13:54:21 -05:00
|
|
|
assertTerminated();
|
|
|
|
assertErrorMessageWritten();
|
2016-12-16 12:31:16 -05:00
|
|
|
}
|
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
@Test
|
|
|
|
public void severityCoverage() {
|
|
|
|
Severity.valueOf(WARNING.toString());
|
|
|
|
Severity.valueOf(ERROR.toString());
|
|
|
|
Severity.valueOf(CRITICAL.toString());
|
|
|
|
}
|
|
|
|
|
2016-12-16 12:31:16 -05:00
|
|
|
}
|