001 package test;
002
003 import java.io.BufferedReader;
004 import java.io.ByteArrayOutputStream;
005 import java.io.File;
006 import java.io.FileNotFoundException;
007 import java.io.FileReader;
008 import java.io.IOException;
009 import java.io.OutputStream;
010 import java.io.PrintStream;
011 import java.io.StringReader;
012 import java.net.URISyntaxException;
013
014 import junit.framework.Test;
015 import junit.framework.TestCase;
016 import junit.framework.TestSuite;
017 import ui.TextUI;
018
019 /**
020 * Script unit tests for the TextUI class.
021 *
022 * Based off 6.170's ps4.test.ScriptFileTests.
023 **/
024 public class TextUITestScript extends TestCase {
025
026
027 private final File testFile;
028
029 public TextUITestScript(File testFile) {
030 super("testScriptFile");
031 this.testFile = testFile;
032 }
033
034
035 /**
036 * @throws FileNotFoundException, IOException
037 * @requires that the specified File exists
038 * @returns the contents of that file
039 */
040 protected String fileContents(File f) throws IOException {
041 if (f == null) {
042 throw new RuntimeException("No file specified");
043 }
044
045 BufferedReader input = new BufferedReader(new FileReader(f));
046 String inputLine;
047 StringBuffer result = new StringBuffer();
048 while ((inputLine = input.readLine()) != null) {
049 result.append(inputLine);
050 result.append("\n");
051 }
052
053 return result.toString();
054 }
055
056
057 /**
058 * @param newSuffix
059 * @return a File with the same name as testDriver, except that the test
060 * suffix is replaced by the given suffix
061 */
062 protected File fileWithSuffix(String newSuffix) {
063 File parent = testFile.getParentFile();
064 String driverName = testFile.getName();
065 String baseName = driverName.substring(0, driverName.length() - "test".length());
066
067 return new File(parent, baseName + newSuffix);
068 }
069
070 // JUnit calls setUp() before each test__ method is run
071 protected void setUp() {
072
073 }
074
075 public void testScriptFile() throws IOException {
076 File expected = fileWithSuffix("expected");
077 runTest(fileContents(testFile), fileContents(expected),
078 "Test " + testFile.getName() + " failed");
079 }
080
081
082 private static void runTest(String in, String out, String err){
083 BufferedReader inStream = new BufferedReader(new StringReader(in));
084 OutputStream oStream = new ByteArrayOutputStream();
085 PrintStream output = new PrintStream(oStream);
086
087 TextUI t = new TextUI(inStream, output);
088 try {
089 t.interactiveLoop();
090 } catch (IOException e) {
091 fail("IOException in loop");
092 }
093 assertEquals(err,
094 oStream.toString().trim(), out.trim());
095 }
096
097
098
099 public static Test suite() {
100 TestSuite suite = new TestSuite();
101 try {
102 File myDirectory = new File(TextUITestScript.class.getResource("TextUITestScript.class").toURI()).getParentFile();
103 for (File f : myDirectory.listFiles()) {
104 if (f.getName().endsWith(".test")) {
105 suite.addTest(new TextUITestScript(f));
106 }
107 }
108 return suite;
109 } catch (URISyntaxException e) {
110 throw new RuntimeException(e);
111 }
112
113 }
114 }