2017-02-07 14:38:21 -05:00
|
|
|
package environment;
|
|
|
|
|
2017-11-12 09:42:25 -05:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertNull;
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
import static org.junit.Assert.fail;
|
2017-02-07 14:38:21 -05:00
|
|
|
|
2017-11-12 09:42:25 -05:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
2017-02-07 14:38:21 -05:00
|
|
|
|
2017-11-12 09:42:25 -05:00
|
|
|
import org.junit.After;
|
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
2017-02-07 14:38:21 -05:00
|
|
|
|
2017-02-09 11:00:23 -05:00
|
|
|
import error.ErrorManager;
|
|
|
|
|
2017-03-15 13:37:39 -04:00
|
|
|
public class RuntimeEnvironmentTest {
|
2017-02-09 11:00:23 -05:00
|
|
|
|
2017-02-07 14:38:21 -05:00
|
|
|
private static final String TERMINATED_SUCCESSFULLY = "TERMINATED_SUCCESSFULLY";
|
|
|
|
private static final String TERMINATED_EXCEPTIONALLY = "TERMINATED_EXCEPTIONALLY";
|
|
|
|
|
|
|
|
private RuntimeEnvironment environment;
|
|
|
|
private Set<String> indicatorSet;
|
|
|
|
|
2017-03-15 13:37:39 -04:00
|
|
|
public RuntimeEnvironmentTest() {
|
2017-02-07 14:38:21 -05:00
|
|
|
this.environment = RuntimeEnvironment.getInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setUp() {
|
2017-02-11 10:51:37 -05:00
|
|
|
indicatorSet = new HashSet<>();
|
2017-03-05 10:20:31 -05:00
|
|
|
environment.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
@After
|
|
|
|
public void tearDown() {
|
|
|
|
environment.reset();
|
2017-02-07 14:38:21 -05:00
|
|
|
}
|
|
|
|
|
2017-02-09 11:00:23 -05:00
|
|
|
@Test
|
|
|
|
public void assignInputName() {
|
2017-02-11 10:51:37 -05:00
|
|
|
environment.setInputName("test");
|
2017-02-09 11:00:23 -05:00
|
|
|
|
2017-02-11 10:51:37 -05:00
|
|
|
assertEquals("test", environment.getInputName());
|
2017-02-09 11:00:23 -05:00
|
|
|
}
|
|
|
|
|
2017-02-07 14:38:21 -05:00
|
|
|
@Test
|
|
|
|
public void assignInput() {
|
2017-02-11 10:51:37 -05:00
|
|
|
environment.setInput(System.in);
|
2017-02-07 14:38:21 -05:00
|
|
|
|
2017-02-11 10:51:37 -05:00
|
|
|
assertEquals(System.in, environment.getInput());
|
2017-02-07 14:38:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void assignOutput() {
|
2017-02-11 10:51:37 -05:00
|
|
|
environment.setOutput(System.out);
|
2017-02-07 14:38:21 -05:00
|
|
|
|
2017-02-11 10:51:37 -05:00
|
|
|
assertEquals(System.out, environment.getOutput());
|
2017-02-07 14:38:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void assignErrorOutput() {
|
2017-02-11 10:51:37 -05:00
|
|
|
environment.setErrorOutput(System.err);
|
2017-02-07 14:38:21 -05:00
|
|
|
|
2017-02-11 10:51:37 -05:00
|
|
|
assertEquals(System.err, environment.getErrorOutput());
|
2017-02-07 14:38:21 -05:00
|
|
|
}
|
|
|
|
|
2017-03-05 10:20:31 -05:00
|
|
|
@Test
|
|
|
|
public void assignErrorManager() {
|
|
|
|
ErrorManager errorManager = new ErrorManager();
|
|
|
|
environment.setErrorManager(errorManager);
|
|
|
|
|
|
|
|
assertEquals(errorManager, environment.getErrorManager());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void assignPath() {
|
|
|
|
environment.setPath("testpath/");
|
|
|
|
|
|
|
|
assertEquals("testpath/", environment.getPath());
|
|
|
|
}
|
|
|
|
|
2017-02-07 14:38:21 -05:00
|
|
|
@Test
|
|
|
|
public void assignTerminationFunction() {
|
|
|
|
environment.setTerminationFunction(() -> indicatorSet.add(TERMINATED_SUCCESSFULLY));
|
|
|
|
environment.terminateSuccessfully();
|
|
|
|
|
|
|
|
assertTrue(indicatorSet.contains(TERMINATED_SUCCESSFULLY));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void assignErrorTerminationFunction() {
|
|
|
|
environment.setErrorTerminationFunction(() -> indicatorSet.add(TERMINATED_EXCEPTIONALLY));
|
|
|
|
environment.terminateExceptionally();
|
|
|
|
|
|
|
|
assertTrue(indicatorSet.contains(TERMINATED_EXCEPTIONALLY));
|
|
|
|
}
|
|
|
|
|
2017-02-14 14:28:47 -05:00
|
|
|
@Test
|
2017-03-21 15:03:16 -04:00
|
|
|
public void assignPromptDecorator() {
|
|
|
|
environment.setPromptDecorator(s -> "[" + s + "]");
|
2017-02-14 14:28:47 -05:00
|
|
|
|
2017-03-21 15:03:16 -04:00
|
|
|
assertEquals("[test]", environment.decoratePrompt("test"));
|
2017-02-14 14:28:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void assignValueOutputDecorator() {
|
|
|
|
environment.setValueOutputDecorator(s -> "(" + s + ")");
|
|
|
|
|
|
|
|
assertEquals("(test)", environment.decorateValueOutput("test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void assignWarningOutputDecorator() {
|
|
|
|
environment.setWarningOutputDecorator(s -> "|" + s + "|");
|
|
|
|
|
|
|
|
assertEquals("|test|", environment.decorateWarningOutput("test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void assignErrorOutputDecorator() {
|
|
|
|
environment.setErrorOutputDecorator(s -> "{" + s + "}");
|
|
|
|
|
|
|
|
assertEquals("{test}", environment.decorateErrorOutput("test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void assignCriticalOutputDecorator() {
|
|
|
|
environment.setCriticalOutputDecorator(s -> "/" + s + "/");
|
|
|
|
|
|
|
|
assertEquals("/test/", environment.decorateCriticalOutput("test"));
|
|
|
|
}
|
|
|
|
|
2017-03-05 10:20:31 -05:00
|
|
|
@Test
|
|
|
|
public void resetWorks() {
|
|
|
|
environment.setInputName("test");
|
|
|
|
environment.setInput(System.in);
|
|
|
|
environment.setOutput(System.out);
|
|
|
|
environment.setErrorOutput(System.err);
|
|
|
|
environment.setErrorManager(new ErrorManager());
|
|
|
|
environment.setPath("testpath/");
|
|
|
|
environment.setTerminationFunction(() -> indicatorSet.add(TERMINATED_SUCCESSFULLY));
|
|
|
|
environment.setErrorTerminationFunction(() -> indicatorSet.add(TERMINATED_EXCEPTIONALLY));
|
2017-03-21 15:03:16 -04:00
|
|
|
environment.setPromptDecorator(s -> "[" + s + "]");
|
2017-03-05 10:20:31 -05:00
|
|
|
environment.setValueOutputDecorator(s -> "(" + s + ")");
|
|
|
|
environment.setWarningOutputDecorator(s -> "|" + s + "|");
|
|
|
|
environment.setErrorOutputDecorator(s -> "{" + s + "}");
|
|
|
|
environment.setCriticalOutputDecorator(s -> "/" + s + "/");
|
|
|
|
environment.reset();
|
|
|
|
|
|
|
|
assertNull(environment.getInputName());
|
|
|
|
assertNull(environment.getInput());
|
|
|
|
assertNull(environment.getOutput());
|
|
|
|
assertNull(environment.getErrorOutput());
|
|
|
|
assertNull(environment.getErrorManager());
|
|
|
|
assertNull(environment.getPath());
|
|
|
|
|
|
|
|
try {
|
|
|
|
environment.terminateSuccessfully();
|
|
|
|
fail("terminateSuccessfully");
|
|
|
|
} catch (NullPointerException e) {}
|
|
|
|
|
|
|
|
try {
|
|
|
|
environment.terminateExceptionally();
|
|
|
|
fail("terminateExceptionally");
|
|
|
|
} catch (NullPointerException e) {}
|
|
|
|
|
|
|
|
try {
|
2017-03-21 15:03:16 -04:00
|
|
|
environment.decoratePrompt("");
|
|
|
|
fail("decoratePrompt");
|
2017-03-05 10:20:31 -05:00
|
|
|
} catch (NullPointerException e) {}
|
|
|
|
|
|
|
|
try {
|
|
|
|
environment.decorateValueOutput("");
|
|
|
|
fail("decorateValueOutput");
|
|
|
|
} catch (NullPointerException e) {}
|
|
|
|
|
|
|
|
try {
|
|
|
|
environment.decorateWarningOutput("");
|
|
|
|
fail("decorateWarningOutput");
|
|
|
|
} catch (NullPointerException e) {}
|
|
|
|
|
|
|
|
try {
|
|
|
|
environment.decorateErrorOutput("");
|
|
|
|
fail("decorateErrorOutput");
|
|
|
|
} catch (NullPointerException e) {}
|
|
|
|
|
|
|
|
try {
|
|
|
|
environment.decorateCriticalOutput("");
|
|
|
|
fail("decorateCriticalOutput");
|
|
|
|
} catch (NullPointerException e) {}
|
|
|
|
}
|
|
|
|
|
2017-02-07 14:38:21 -05:00
|
|
|
}
|