Cleaned up the ErrorManager and associated unit tests

This commit is contained in:
Mike Cifelli 2016-12-16 13:12:13 -05:00
parent 25c0b5813b
commit a137e41a14
2 changed files with 29 additions and 22 deletions

View File

@ -10,10 +10,9 @@ public class ErrorManager {
public static final int CRITICAL_LEVEL = 3; public static final int CRITICAL_LEVEL = 3;
public static final String ANSI_RESET = "\u001B[0m"; private static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_RED = "\u001B[31m"; private static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_YELLOW = "\u001B[33m"; private static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_PURPLE = "\u001B[35m";
private Runnable systemTerminatingFunction; private Runnable systemTerminatingFunction;
private Consumer<String> outputFunction; private Consumer<String> outputFunction;
@ -24,15 +23,20 @@ public class ErrorManager {
} }
public void generateError(LispException lispException) { public void generateError(LispException lispException) {
String color = (lispException.getSeverity() >= CRITICAL_LEVEL) ? ANSI_PURPLE : ANSI_RED; outputFunction.accept(formatMessage(lispException));
String formattedMessage = MessageFormat.format("{0}error: {1}{2}\n", color, lispException.getMessage(),
ANSI_RESET);
outputFunction.accept(formattedMessage); if (isCritical(lispException))
if (lispException.getSeverity() >= CRITICAL_LEVEL) {
systemTerminatingFunction.run(); systemTerminatingFunction.run();
} }
private String formatMessage(LispException lispException) {
String color = isCritical(lispException) ? ANSI_PURPLE : ANSI_RED;
return MessageFormat.format("{0}error: {1}{2}\n", color, lispException.getMessage(), ANSI_RESET);
}
private boolean isCritical(LispException lispException) {
return lispException.getSeverity() >= CRITICAL_LEVEL;
} }
} }

View File

@ -7,6 +7,7 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
public class ErrorManagerTester { public class ErrorManagerTester {
@ -14,7 +15,9 @@ public class ErrorManagerTester {
private static final String TERMINATED = "terminated"; private static final String TERMINATED = "terminated";
private static final String MESSAGE = "message"; private static final String MESSAGE = "message";
private ErrorManager createErrorManagerWithIndicators(Set<String> indicatorSet) { private Set<String> indicatorSet;
private ErrorManager createErrorManagerWithIndicators() {
Runnable terminationFunction = new Runnable() { Runnable terminationFunction = new Runnable() {
@Override @Override
@ -50,11 +53,15 @@ public class ErrorManagerTester {
} }
}; };
} }
@Before
public void setUp() {
this.indicatorSet = new HashSet<>();
}
@Test @Test
public void givenCriticalExceptionSeverity_RunsProvidedTerminationFunction() { public void givenCriticalExceptionSeverity_RunsProvidedTerminationFunction() {
Set<String> indicatorSet = new HashSet<>(); ErrorManager errorManager = createErrorManagerWithIndicators();
ErrorManager errorManager = createErrorManagerWithIndicators(indicatorSet);
errorManager.generateError(createLispException(ErrorManager.CRITICAL_LEVEL)); errorManager.generateError(createLispException(ErrorManager.CRITICAL_LEVEL));
assertTrue(indicatorSet.contains(TERMINATED)); assertTrue(indicatorSet.contains(TERMINATED));
@ -62,8 +69,7 @@ public class ErrorManagerTester {
@Test @Test
public void givenNonCriticalExceptionSeverity_DoesNotRunProvidedTerminationFunction() { public void givenNonCriticalExceptionSeverity_DoesNotRunProvidedTerminationFunction() {
Set<String> indicatorSet = new HashSet<>(); ErrorManager errorManager = createErrorManagerWithIndicators();
ErrorManager errorManager = createErrorManagerWithIndicators(indicatorSet);
errorManager.generateError(createLispException(0)); errorManager.generateError(createLispException(0));
assertFalse(indicatorSet.contains(TERMINATED)); assertFalse(indicatorSet.contains(TERMINATED));
@ -71,8 +77,7 @@ public class ErrorManagerTester {
@Test @Test
public void noMessageDisplayedBeforeError() { public void noMessageDisplayedBeforeError() {
Set<String> indicatorSet = new HashSet<>(); createErrorManagerWithIndicators();
createErrorManagerWithIndicators(indicatorSet);
assertFalse(indicatorSet.contains(TERMINATED)); assertFalse(indicatorSet.contains(TERMINATED));
assertFalse(indicatorSet.contains(MESSAGE)); assertFalse(indicatorSet.contains(MESSAGE));
@ -80,8 +85,7 @@ public class ErrorManagerTester {
@Test @Test
public void usesOutputFunctionToDisplayMessages_NoTermination() { public void usesOutputFunctionToDisplayMessages_NoTermination() {
Set<String> indicatorSet = new HashSet<>(); ErrorManager errorManager = createErrorManagerWithIndicators();
ErrorManager errorManager = createErrorManagerWithIndicators(indicatorSet);
errorManager.generateError(createLispException(0)); errorManager.generateError(createLispException(0));
assertFalse(indicatorSet.contains(TERMINATED)); assertFalse(indicatorSet.contains(TERMINATED));
@ -90,8 +94,7 @@ public class ErrorManagerTester {
@Test @Test
public void usesOutputFunctionToDisplayMessages_WithTermination() { public void usesOutputFunctionToDisplayMessages_WithTermination() {
Set<String> indicatorSet = new HashSet<>(); ErrorManager errorManager = createErrorManagerWithIndicators();
ErrorManager errorManager = createErrorManagerWithIndicators(indicatorSet);
errorManager.generateError(createLispException(ErrorManager.CRITICAL_LEVEL)); errorManager.generateError(createLispException(ErrorManager.CRITICAL_LEVEL));
assertTrue(indicatorSet.contains(TERMINATED)); assertTrue(indicatorSet.contains(TERMINATED));