source: release-kits/wirk3/ant-scripts/tasks/antelope/src/ise/antelope/tasks/Suite.java@ 15023

Last change on this file since 15023 was 15023, checked in by oranfry, 16 years ago

did the bulk of the work on wirk3

File size: 9.9 KB
Line 
1package ise.antelope.tasks;
2
3import java.io.File;
4import java.util.*;
5
6import org.apache.tools.ant.BuildException;
7import org.apache.tools.ant.DirectoryScanner;
8import org.apache.tools.ant.TaskContainer;
9import org.apache.tools.ant.Target;
10import org.apache.tools.ant.Task;
11import org.apache.tools.ant.types.FileSet;
12
13import ise.library.ascii.MessageBox;
14
15/**
16 * Modeled after the TestSuite provided by jUnit.
17 *
18 * @author Dale Anson
19 */
20public class Suite extends Task implements TaskContainer, TestStatisticAccumulator {
21
22 // name for the suite
23 private String name = null;
24
25 // vector to hold nested testcases
26 private Vector tasks = null;
27
28 // vector to hold filesets
29 private Vector filesets = null;
30
31 // stats accumulators
32 private int totalTestCount = 0;
33 private int totalRanCount = 0;
34 private int totalPassedCount = 0;
35 private int totalFailedCount = 0;
36 private int totalWarningCount = 0;
37 private Vector failures = new Vector();
38
39 // should the results be shown?
40 private boolean showSummary = true;
41
42 // should the tests be ran?
43 private boolean enabled = true;
44
45 // should Asserts be enabled?
46 private boolean assertEnabled = true;
47
48 // should the suite throw a build exception on error?
49 private boolean failonerror = false;
50
51 public void init() {
52 super.init();
53 setTaskName( "suite" );
54 }
55
56 /**
57 * Add a testcase to the suite.
58 * @param tc the testcase to add
59 */
60 public void addTestCase( TestCase tc ) {
61 addTask( tc );
62 }
63
64 /**
65 * Add a stuie to the suite.
66 * @param s the suite to add
67 */
68 public void addSuite( Suite s ) {
69 addTask( s );
70 }
71
72 /**
73 * Add a task to execute, most likely a testcase, but really can be any task. <p>
74 *
75 * @param task Nested task to execute. <p>
76 */
77 public void addTask( Task task ) {
78 if ( tasks == null )
79 tasks = new Vector();
80 tasks.addElement( task );
81 }
82
83 /**
84 * Add a fileset to the suite. For each file in the fileset, a testcase
85 * will be created.
86 */
87 public void addFileset( FileSet fs ) {
88 if ( tasks == null )
89 tasks = new Vector();
90 tasks.addElement( fs );
91 }
92
93 /**
94 * Set a name for the suite, optional attribute.
95 * @param s the name for the suite.
96 */
97 public void setName( String s ) {
98 name = s;
99 }
100
101 /**
102 * @return the name of the suite, may be null.
103 */
104 public String getName() {
105 return name;
106 }
107
108 public void setFailonerror(boolean f) {
109 failonerror = f;
110 }
111
112 public boolean getFailonerror() {
113 return failonerror;
114 }
115
116 /**
117 * Set to true if the test should be allowed to run.
118 * @param b if true, execute the test. This is handy for enabling or disabling
119 * groups of tests by setting a single property. Optional, default
120 * is true, the suite should run.
121 */
122 public void setEnabled( boolean b ) {
123 enabled = b;
124 }
125
126 /**
127 * Should asserts be enabled? Asserts are enabled by default.
128 * @param b if false, disable asserts
129 */
130 public void setAssertsenabled( boolean b ) {
131 assertEnabled = b;
132 }
133
134 /**
135 * Should the results be shown?
136 * @param b show the results if true
137 */
138 public void setShowsummary( boolean b ) {
139 showSummary = b;
140 }
141
142 public int getTestCaseCount() {
143 return totalTestCount;
144 }
145
146 public int getRanCount() {
147 return totalRanCount;
148 }
149
150 public int getPassedCount() {
151 return totalPassedCount;
152 }
153
154 public int getFailedCount() {
155 return totalFailedCount;
156 }
157
158 /**
159 * @return an Enumeration of the failures. Individual elements are Strings
160 * containing the name of the failed target and the reason why it
161 * failed.
162 */
163 public Enumeration getFailures() {
164 return failures.elements();
165 }
166
167 public int getWarningCount() {
168 return totalWarningCount;
169 }
170
171 /** Run tests. */
172 public void execute() {
173 if ( !enabled )
174 return ;
175
176 String ae = assertEnabled ? "true" : "false";
177 if ( assertEnabled )
178 getProject().setProperty( "ant.enable.asserts", ae );
179
180 try {
181 // get the setUp and tearDown targets
182 Target setUp = null;
183 Target tearDown = null;
184 Hashtable targets = getProject().getTargets();
185 Enumeration en = targets.keys();
186 while ( en.hasMoreElements() ) {
187 String target_name = ( String ) en.nextElement();
188 if ( target_name.equals( "setUp" ) )
189 setUp = ( Target ) targets.get( target_name );
190 else if ( target_name.equals( "tearDown" ) )
191 tearDown = ( Target ) targets.get( target_name );
192 }
193
194 // run the setUp target
195 if ( setUp != null )
196 setUp.execute();
197
198 // create testcases out of any filesets, maintaining order with
199 // testcases already added.
200 Vector testcases = new Vector();
201 if ( tasks != null && tasks.size() > 0 ) {
202 for ( Enumeration e = tasks.elements(); e.hasMoreElements(); ) {
203 Object o = e.nextElement();
204 if ( o instanceof FileSet ) {
205 loadTestFiles( ( FileSet ) o, testcases );
206 }
207 else {
208 testcases.addElement( o );
209 }
210 }
211
212 // actually execute the testcases
213 for ( Enumeration e = testcases.elements(); e.hasMoreElements(); ) {
214 Task task = ( Task ) e.nextElement();
215 task.perform();
216 }
217 }
218
219 // run the tearDown target
220 if ( tearDown != null )
221 tearDown.execute();
222
223 // tabulate the results. The individual testcases contain their
224 // own stats, so it is just a matter of reading them and accumulating
225 // the totals.
226 for ( Enumeration e = testcases.elements(); e.hasMoreElements(); ) {
227 Task task = ( Task ) e.nextElement();
228 if ( task instanceof TestStatisticAccumulator ) {
229 TestStatisticAccumulator acc = ( TestStatisticAccumulator ) task;
230 totalTestCount += acc.getTestCaseCount();
231 totalRanCount += acc.getRanCount();
232 totalPassedCount += acc.getPassedCount();
233 totalFailedCount += acc.getFailedCount();
234 totalWarningCount += acc.getWarningCount();
235 for (Enumeration fen = acc.getFailures(); fen.hasMoreElements(); ) {
236 failures.add(fen.nextElement());
237 }
238 }
239 }
240 if ( showSummary ) {
241 log( getSummary() );
242 }
243
244 if (failonerror && totalFailedCount > 0)
245 throw new BuildException("+++++ FAILED +++++\n" + getSummary());
246 }
247 catch ( Exception ex ) {
248 ex.printStackTrace();
249 throw new BuildException( ex.getMessage() );
250 }
251 }
252
253 public String getSummary() {
254 String title = (name == null ? "Suite" : name ) + " Totals";
255 StringBuffer msg = new StringBuffer();
256 String ls = System.getProperty( "line.separator" );
257
258 // log the failures
259 if (failures.size() > 0) {
260 String error_title = "Errors";
261 StringBuffer error_msg = new StringBuffer();
262 Enumeration en = failures.elements();
263 while (en.hasMoreElements()) {
264 error_msg.append((String) en.nextElement()).append(ls);
265 }
266 int box_width = MessageBox.getMaxWidth();
267 MessageBox.setMaxWidth(box_width - 8);
268 msg.append(MessageBox.box(error_title, error_msg));
269 MessageBox.setMaxWidth(box_width);
270 msg.append(ls);
271 }
272
273 msg.append( "Total Ran: " ).append( totalRanCount ).append( " out of " ).append( totalTestCount ).append( " tests." ).append( ls );
274 msg.append( "Total Passed: " ).append( totalPassedCount ).append( ls );
275 msg.append( "Total Warnings: " ).append( totalWarningCount ).append( ls );
276 msg.append( "Total Failed: " ).append( totalFailedCount ).append( ls );
277 return MessageBox.box(title, msg);
278 }
279
280 /**
281 * Create TestCases from the files specified in a FileSet.
282 * @param fs the fileset to use for testcases
283 * @param destination where to store the newly created TestCases.
284 */
285 protected void loadTestFiles( FileSet fs, Vector destination ) {
286 File d = fs.getDir( getProject() );
287 DirectoryScanner ds = fs.getDirectoryScanner( getProject() );
288 String[] files = ds.getIncludedFiles();
289 String[] dirs = ds.getIncludedDirectories();
290 if ( files.length > 0 ) {
291 for ( int j = 0; j < files.length; j++ ) {
292 File f = new File( d, files[ j ] );
293 TestCase tc = createTestCase( f );
294 destination.addElement( tc );
295 }
296 }
297
298 if ( dirs.length > 0 ) {
299 for ( int j = dirs.length - 1; j >= 0; j-- ) {
300 File dir = new File( d, dirs[ j ] );
301 String[] dirFiles = dir.list();
302 if ( dirFiles != null && dirFiles.length > 0 ) {
303 for ( int i = 0; i < dirFiles.length; i++ ) {
304 File f = new File( dir, dirFiles[ i ] );
305 TestCase tc = createTestCase( f );
306 destination.addElement( tc );
307 }
308 }
309 }
310 }
311 }
312
313 private TestCase createTestCase( File f ) {
314 TestCase tc = new TestCase();
315 tc.init();
316 tc.setFile( f );
317 tc.setProject( getProject() );
318 return tc;
319 }
320}
321
Note: See TracBrowser for help on using the repository browser.