package FileSystem; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.PrintStream; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; import junit.framework.TestCase; public class FileSystemTest extends TestCase { public void testBlankInput() { runTestFile("testing/blanktest.in", "testing/blanktest.out"); } public void testExample() { runTestFile("testing/specexample.in", "testing/specexample.out"); } /** * Runs your FileSystem.main with the inputfile and compares your program's * output to expectedFile. * * @param inputfile * @param expectedFile */ public void runTestFile(String inputfile, String expectedFile) { assertEquals("incorrect exit code", 0, runMain(inputfile)); assertEquals("incorrect output", normalize(fileContents(expectedFile)), normalize(out)); assertEquals("program produced error output", "", err); } /* * Some useful utilities, usable from other JUnit files in this package */ private final static PrintStream realStandardOut = System.out, realStandardErr = System.err; private static ByteArrayOutputStream capturedOut, capturedErr; static String out, err; /** * Run the main program as if started with java FileSystem FILE That is, * start the program and cause System.in to come from FILE. Set out to * everything the program writes to the standard output, and err to * everything it writes to the standard error file. In both cases, * canonicalize "\r\n" to "\n" to avoid Windows/Unix differences. Returns * the exit code the program would have if run as a regular main program. */ static int runMain(String... args) { out = err = ""; captureOutput(); try { FileSystem.main(args); return 0; } catch (ExitException e) { return e.getCode(); } finally { /* * Always restore System.out and System.err, even if main () causes * an exception. */ restoreOutput(); out = capturedOut.toString().replace("\r", ""); err = capturedErr.toString().replace("\r", ""); } } /** * Divert output through System.out and System.err to newly created streams * in capturedOut and capturedErr. */ static void captureOutput() { capturedOut = new ByteArrayOutputStream(); capturedErr = new ByteArrayOutputStream(); System.setOut(new PrintStream(capturedOut)); System.setErr(new PrintStream(capturedErr)); } /** Restore System.out and System.err to their initial values. */ static void restoreOutput() { System.setOut(realStandardOut); System.setErr(realStandardErr); } static final Pattern NORM_PATN = Pattern.compile("(?m)(?: +$|> |( +))"); /** The string S with all prompts ("> ") and trailing spaces removed. */ String normalize(String s) { Matcher m = NORM_PATN.matcher(s); StringBuffer out = new StringBuffer(); while (m.find()) { if (m.group(1) == null) m.appendReplacement(out, ""); else m.appendReplacement(out, " "); } m.appendTail(out); return out.toString().replaceAll("\n\\s*(\n|$)", "\n").trim(); } /** * The contents of the file named FILE as a String. Removes "\r" to avoid * Unix/Windows differences. This method is useful for reading a file * containing the expected output of your program. */ String fileContents(String file) { try { Scanner inp = new Scanner(new File(file)); String val = inp.findWithinHorizon("(?s).*", 0); inp.close(); return val.replace("\r", ""); } catch (IOException e) { fail("Problem reading " + file); return null; } } }