KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > pth > TestSuite


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

5 package polyglot.pth;
6
7 import java.util.*;
8 import java.util.regex.Pattern JavaDoc;
9
10 /**
11  *
12  */

13 public class TestSuite extends AbstractTest {
14     protected List tests;
15     protected boolean haltOnFirstFailure = false;
16     protected int totalTests = 0;
17     protected int successfulTests = 0;
18
19     public TestSuite(String JavaDoc name) {
20         this(name, new ArrayList(), false);
21     }
22
23     public TestSuite(String JavaDoc name, List tests) {
24         this(name, tests, false);
25     }
26
27     public TestSuite(String JavaDoc name, List tests, boolean haltOnFirstFailure) {
28         super(name);
29         this.tests = tests;
30         this.haltOnFirstFailure = haltOnFirstFailure;
31     }
32     
33     public boolean getHaltOnFirstFailure() {
34         return haltOnFirstFailure;
35     }
36     
37     public void setOutputController(OutputController output) {
38         super.setOutputController(output);
39         for (Iterator iter = tests.iterator(); iter.hasNext(); ) {
40             Test t = (Test)iter.next();
41             t.setOutputController(output);
42         }
43     }
44
45     protected boolean runTest() {
46         boolean okay = true;
47
48         if (this.getTestSuiteResult() == null) {
49             this.setTestResult(this.createTestResult(null));
50         }
51         
52         Map oldTestResults = new HashMap(this.getTestSuiteResult().testResults);
53         Map newResults = new HashMap();
54         
55         for (Iterator i = tests.iterator(); i.hasNext(); ) {
56             Test t = (Test)i.next();
57             
58             TestResult tr = (TestResult)oldTestResults.get(t.getName());
59             if (executeTest(t.getName(), tr)) {
60                 totalTests++;
61                 if (tr != null) {
62                     t.setTestResult(tr);
63                 }
64                 boolean result = t.run();
65                 okay = okay && result;
66                 
67                 tr = t.getTestResult();
68                 
69                 if (!result && haltOnFirstFailure) {
70                     break;
71                 }
72                 else if (result) {
73                     successfulTests++;
74                 }
75             }
76             this.getTestSuiteResult().testResults.put(t.getName(), tr);
77             newResults.put(t.getName(), tr);
78             this.postIndividualTest();
79         }
80         this.getTestSuiteResult().testResults.clear();
81         this.getTestSuiteResult().testResults.putAll(newResults);
82         return okay;
83     }
84     
85     protected void postIndividualTest() {
86     }
87
88     public int getTotalTestCount() {
89         return totalTests;
90     }
91     public int getSuccesfulTestCount() {
92         return successfulTests;
93     }
94     public int getFailedTestCount() {
95         return totalTests - successfulTests;
96     }
97     
98     protected static boolean executeTest(String JavaDoc testName, TestResult tr) {
99         if (Main.options.testFilter != null &&
100             !Pattern.matches(Main.options.testFilter, testName)) {
101             return false;
102         }
103         
104         if (Main.options.testPreviouslyFailedOnly &&
105             tr != null && tr.dateLastSuccess != null &&
106             tr.dateLastSuccess.equals(tr.dateTestRun)) {
107                 return false;
108         }
109         return true;
110     }
111
112     protected TestSuiteResult getTestSuiteResult() {
113         return (TestSuiteResult)this.getTestResult();
114     }
115     
116     public List getTests() {
117         return Collections.unmodifiableList(this.tests);
118     }
119
120     protected TestResult createTestResult(Date lastSuccess) {
121         Map testResults;
122         if (this.getTestSuiteResult() != null) {
123             testResults = getTestSuiteResult().testResults;
124         }
125         else {
126             testResults = new LinkedHashMap();
127         }
128         Date lastRun = new Date();
129         if (this.success()) {
130             lastSuccess = lastRun;
131         }
132         return new TestSuiteResult(this, lastRun, testResults, lastSuccess);
133     }
134 }
135
Popular Tags