Skip to content
Snippets Groups Projects
Commit 95207b24 authored by Brady James Garvin's avatar Brady James Garvin
Browse files

Added test cases for error handling.

parent 87419f4a
Branches master
No related tags found
No related merge requests found
......@@ -7,6 +7,7 @@ import junit.framework.TestSuite;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.PrintStream;
import java.security.Permission;
import java.io.ByteArrayOutputStream;
public class SortFourPairsTest extends TestCase {
......@@ -34,6 +35,64 @@ public class SortFourPairsTest extends TestCase {
return collector.toString();
}
@SuppressWarnings("serial")
protected static class ExitException extends SecurityException {
public final int status;
public ExitException(int status) {
super("Exit with status " + status);
this.status = status;
}
}
private static class TestingSecurityManager extends SecurityManager {
@Override
public void checkPermission(Permission perm) {
}
@Override
public void checkPermission(Permission perm, Object context) {
}
@Override
public void checkExit(int status) {
super.checkExit(status);
throw new ExitException(status);
}
}
@Override
protected void setUp() throws Exception {
super.setUp();
System.setSecurityManager(new TestingSecurityManager());
}
@Override
protected void tearDown() throws Exception {
System.setSecurityManager(null);
super.tearDown();
}
protected static String runMainForError(int expectedStatus, String... arguments) {
InputStream in = System.in;
PrintStream err = System.err;
System.setIn(new ByteArrayInputStream("".getBytes()));
ByteArrayOutputStream collector = new ByteArrayOutputStream();
System.setErr(new PrintStream(collector));
boolean exited = false;
try {
SortFourPairs.main(arguments);
} catch (ExitException expected) {
assertEquals(expectedStatus, expected.status);
exited = true;
} finally {
System.setIn(in);
System.setErr(err);
}
assertTrue(exited);
return collector.toString();
}
public void testZeros() {
assertEquals(assemble("(0.0, 0.0)", "(0.0, 0.0)", "(0.0, 0.0)", "(0.0, 0.0)"),
runMain("0", "0", "0", "0", "0", "0", "0", "0"));
......@@ -188,4 +247,18 @@ public class SortFourPairsTest extends TestCase {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"),
runMain("1", "1", "0", "0", "2", "2", "3", "3"));
}
public void testTooFewArguments() {
assertEquals(
assemble("Sorts four points, by their x coordinates, breaking ties using y coordinates.",
"Usage: [Program] [X0] [Y0] [X1] [Y1] [X2] [Y2] [X3] [Y3]"),
runMainForError(1, "0", "0", "1", "1", "2", "2", "3"));
}
public void testNonNumericArguments() {
assertEquals(
assemble("Sorts four points, by their x coordinates, breaking ties using y coordinates.",
"Usage: [Program] [X0] [Y0] [X1] [Y1] [X2] [Y2] [X3] [Y3]"),
runMainForError(1, "0", "0", "1", "1", "2", "2", "3", "Three"));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment