transcendental-lisp/test/error/ErrorManagerTester.java

105 lines
2.9 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.Before;
import org.junit.Test;
public class ErrorManagerTester {
private static final String TERMINATED = "terminated";
private static final String MESSAGE = "message";
private Set<String> indicatorSet;
private ErrorManager createErrorManagerWithIndicators() {
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;
}
};
}
@Before
public void setUp() {
this.indicatorSet = new HashSet<>();
}
@Test
public void givenCriticalExceptionSeverity_RunsProvidedTerminationFunction() {
ErrorManager errorManager = createErrorManagerWithIndicators();
errorManager.generateError(createLispException(ErrorManager.CRITICAL_LEVEL));
assertTrue(indicatorSet.contains(TERMINATED));
}
@Test
public void givenNonCriticalExceptionSeverity_DoesNotRunProvidedTerminationFunction() {
ErrorManager errorManager = createErrorManagerWithIndicators();
errorManager.generateError(createLispException(0));
assertFalse(indicatorSet.contains(TERMINATED));
}
@Test
public void noMessageDisplayedBeforeError() {
createErrorManagerWithIndicators();
assertFalse(indicatorSet.contains(TERMINATED));
assertFalse(indicatorSet.contains(MESSAGE));
}
@Test
public void usesOutputFunctionToDisplayMessages_NoTermination() {
ErrorManager errorManager = createErrorManagerWithIndicators();
errorManager.generateError(createLispException(0));
assertFalse(indicatorSet.contains(TERMINATED));
assertTrue(indicatorSet.contains(MESSAGE));
}
@Test
public void usesOutputFunctionToDisplayMessages_WithTermination() {
ErrorManager errorManager = createErrorManagerWithIndicators();
errorManager.generateError(createLispException(ErrorManager.CRITICAL_LEVEL));
assertTrue(indicatorSet.contains(TERMINATED));
assertTrue(indicatorSet.contains(MESSAGE));
}
}