1 5 package polyglot.pth; 6 7 import java.io.*; 8 import java.io.File ; 9 import java.io.FileOutputStream ; 10 import java.io.ObjectOutputStream ; 11 import java.util.List ; 12 13 16 public class ScriptTestSuite extends TestSuite { 17 protected File scriptFile; 18 protected boolean saveProblem = false; 19 private boolean scriptLoaded = false; 20 21 public ScriptTestSuite(String scriptFilename) { 22 super(scriptFilename); 23 this.scriptFile = new File (scriptFilename); 24 this.loadResults(); 25 } 26 public ScriptTestSuite(File scriptFile) { 27 super(scriptFile.getName()); 28 this.scriptFile = scriptFile; 29 this.loadResults(); 30 } 31 32 protected boolean loadScript() { 33 if (scriptLoaded) return true; 34 scriptLoaded = true; 35 if (!this.scriptFile.exists()) { 36 this.setFailureMessage("File " + scriptFile.getName() + " not found."); 37 return false; 38 } 39 else { 40 if (!parseScript()) { 41 return false; 42 } 43 } 44 return true; 45 } 46 47 protected boolean runTest() { 48 saveProblem = false; 49 if (!loadScript()) { 50 return false; 51 } 52 53 this.setOutputController(output); 54 return super.runTest(); 55 } 56 57 protected void postIndividualTest() { 58 if (!saveProblem) { 59 this.saveProblem = !saveResults(); 60 } 61 } 62 63 protected void postRun() { 64 super.postRun(); 65 this.saveResults(); 66 } 67 68 protected boolean parseScript() { 69 Grm grm = new Grm(this.scriptFile); 70 try { 71 this.tests = (List )grm.parse().value; 72 } 73 catch (Exception e) { 74 this.setFailureMessage("Parsing error: " + e.getMessage()); 75 return false; 76 } 77 return true; 78 } 79 80 protected void loadResults() { 81 try { 82 ObjectInputStream ois = 83 new ObjectInputStream( 84 new FileInputStream(TestSuiteResult.getResultsFileName(this.scriptFile))); 85 TestResult tr = (TestResult)ois.readObject(); 86 this.setTestResult(tr); 87 } 88 catch (FileNotFoundException e) { 89 } 91 catch (ClassNotFoundException e) { 92 System.err.println("Unable to load results for test suite " + this.getName() + ": " + e.getMessage()); 93 } 94 catch (IOException e) { 95 System.err.println("Unable to load results for test suite " + this.getName() + ": " + e.getMessage()); 96 } 97 } 98 protected boolean saveResults() { 99 try { 100 ObjectOutputStream oos = 101 new ObjectOutputStream ( 102 new FileOutputStream (TestSuiteResult.getResultsFileName(this.scriptFile))); 103 oos.writeObject(this.getTestSuiteResult()); 104 } 105 catch (IOException e) { 106 System.err.println("Unable to save results for test suite " + this.getName()); 107 return false; 108 } 109 return true; 110 } 111 112 public List getTests() { 113 this.loadScript(); 114 return super.getTests(); 115 } 116 117 } 118 | Popular Tags |