102 lines
3.1 KiB
Java
102 lines
3.1 KiB
Java
|
package error;
|
||
|
|
||
|
import static org.junit.Assert.assertFalse;
|
||
|
import static org.junit.Assert.assertTrue;
|
||
|
|
||
|
import java.util.HashSet;
|
||
|
import java.util.Set;
|
||
|
import java.util.function.Consumer;
|
||
|
|
||
|
import org.junit.Test;
|
||
|
|
||
|
public class ErrorManagerTester {
|
||
|
|
||
|
private static final String TERMINATED = "terminated";
|
||
|
private static final String MESSAGE = "message";
|
||
|
|
||
|
private ErrorManager createErrorManagerWithIndicators(Set<String> indicatorSet) {
|
||
|
Runnable terminationFunction = new Runnable() {
|
||
|
|
||
|
@Override
|
||
|
public void run() {
|
||
|
indicatorSet.add(TERMINATED);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
Consumer<String> outputFunction = new Consumer<String>() {
|
||
|
|
||
|
@Override
|
||
|
public void accept(String t) {
|
||
|
indicatorSet.add(MESSAGE);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
return new ErrorManager(terminationFunction, outputFunction);
|
||
|
}
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
public void givenCriticalExceptionSeverity_RunsProvidedTerminationFunction() {
|
||
|
Set<String> indicatorSet = new HashSet<>();
|
||
|
ErrorManager errorManager = createErrorManagerWithIndicators(indicatorSet);
|
||
|
|
||
|
errorManager.generateError(createLispException(ErrorManager.CRITICAL_LEVEL));
|
||
|
assertTrue(indicatorSet.contains(TERMINATED));
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
public void givenNonCriticalExceptionSeverity_DoesNotRunProvidedTerminationFunction() {
|
||
|
Set<String> indicatorSet = new HashSet<>();
|
||
|
ErrorManager errorManager = createErrorManagerWithIndicators(indicatorSet);
|
||
|
|
||
|
errorManager.generateError(createLispException(0));
|
||
|
assertFalse(indicatorSet.contains(TERMINATED));
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
public void noMessageDisplayedBeforeError() {
|
||
|
Set<String> indicatorSet = new HashSet<>();
|
||
|
createErrorManagerWithIndicators(indicatorSet);
|
||
|
|
||
|
assertFalse(indicatorSet.contains(TERMINATED));
|
||
|
assertFalse(indicatorSet.contains(MESSAGE));
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
public void usesOutputFunctionToDisplayMessages_NoTermination() {
|
||
|
Set<String> indicatorSet = new HashSet<>();
|
||
|
ErrorManager errorManager = createErrorManagerWithIndicators(indicatorSet);
|
||
|
|
||
|
errorManager.generateError(createLispException(0));
|
||
|
assertFalse(indicatorSet.contains(TERMINATED));
|
||
|
assertTrue(indicatorSet.contains(MESSAGE));
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
public void usesOutputFunctionToDisplayMessages_WithTermination() {
|
||
|
Set<String> indicatorSet = new HashSet<>();
|
||
|
ErrorManager errorManager = createErrorManagerWithIndicators(indicatorSet);
|
||
|
|
||
|
errorManager.generateError(createLispException(ErrorManager.CRITICAL_LEVEL));
|
||
|
assertTrue(indicatorSet.contains(TERMINATED));
|
||
|
assertTrue(indicatorSet.contains(MESSAGE));
|
||
|
}
|
||
|
|
||
|
}
|