KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > pth > ScriptTestSuite


1 /*
2  * Author : Stephen Chong
3  * Created: Nov 24, 2003
4  */

5 package polyglot.pth;
6
7 import java.io.*;
8 import java.io.File JavaDoc;
9 import java.io.FileOutputStream JavaDoc;
10 import java.io.ObjectOutputStream JavaDoc;
11 import java.util.List JavaDoc;
12
13 /**
14  *
15  */

16 public class ScriptTestSuite extends TestSuite {
17     protected File JavaDoc scriptFile;
18     protected boolean saveProblem = false;
19     private boolean scriptLoaded = false;
20     
21     public ScriptTestSuite(String JavaDoc scriptFilename) {
22         super(scriptFilename);
23         this.scriptFile = new File JavaDoc(scriptFilename);
24         this.loadResults();
25     }
26     public ScriptTestSuite(File JavaDoc 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 JavaDoc)grm.parse().value;
72         }
73         catch (Exception JavaDoc 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             // ignore, and fail silently
90
}
91         catch (ClassNotFoundException JavaDoc 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 JavaDoc oos =
101                  new ObjectOutputStream JavaDoc(
102                           new FileOutputStream JavaDoc(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 JavaDoc getTests() {
113         this.loadScript();
114         return super.getTests();
115     }
116
117 }
118
Popular Tags