[14982] | 1 | package ise.antelope.tasks;
|
---|
| 2 |
|
---|
| 3 | import java.util.*;
|
---|
| 4 |
|
---|
| 5 | import org.apache.tools.ant.BuildException;
|
---|
| 6 | import org.apache.tools.ant.Target;
|
---|
| 7 | import org.apache.tools.ant.Task;
|
---|
| 8 |
|
---|
| 9 | /**
|
---|
| 10 | * Modeled after the TestSuite provided by jUnit, this class is an Ant task that
|
---|
| 11 | * looks through the build file that contains this task, calls a 'setUp' target
|
---|
| 12 | * (if it exists), then all targets whose names start with 'test', and last calls
|
---|
| 13 | * a target named 'tearDown' (if it exists). Both 'setUp' and 'tearDown' are
|
---|
| 14 | * optional targets in the build file. <p>
|
---|
| 15 | * Ant stores targets in a hashtable, so there is no guaranteed order in which
|
---|
| 16 | * the 'test*' classes will be called. If order is important, use the 'depends'
|
---|
| 17 | * attribue of a target to enforce order, and do not name dependent targets with
|
---|
| 18 | * a name starting with 'test'. <p>
|
---|
| 19 | * Test targets may also be imported with the import task. Imported files should
|
---|
| 20 | * not have a "suite" task in the implicit target as such a task would rerun
|
---|
| 21 | * all test targets in all files.
|
---|
| 22 | *
|
---|
| 23 | * @author Dale Anson
|
---|
| 24 | */
|
---|
| 25 | public class SuiteOriginal extends Task {
|
---|
| 26 |
|
---|
| 27 | private Target setUp = null;
|
---|
| 28 | private Target tearDown = null;
|
---|
| 29 | private Vector testTargets = new Vector();
|
---|
| 30 | private Vector failures = new Vector();
|
---|
| 31 | private boolean showSummary = true;
|
---|
| 32 | private boolean showOutput = true;
|
---|
| 33 |
|
---|
| 34 | private int tests_passed = 0;
|
---|
| 35 | private int tests_failed = 0;
|
---|
| 36 |
|
---|
| 37 | public int getTestCaseCount() {
|
---|
| 38 | return testTargets.size();
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | public int getRanCount() {
|
---|
| 42 | return tests_passed + tests_failed;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | public int getFailedCount() {
|
---|
| 46 | return tests_failed;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | public int getPassedCount() {
|
---|
| 50 | return tests_passed;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | public Enumeration getFailures() {
|
---|
| 54 | return failures.elements();
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | public void setShowoutput( boolean b ) {
|
---|
| 58 | showOutput = b;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | public void setShowsummary(boolean b ) {
|
---|
| 62 | showSummary = b;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | /** Run tests. */
|
---|
| 66 | public void execute() {
|
---|
| 67 |
|
---|
| 68 | // get the setUp, tearDown, and test targets
|
---|
| 69 | Hashtable targets = getProject().getTargets();
|
---|
| 70 | Enumeration en = targets.keys();
|
---|
| 71 | while ( en.hasMoreElements() ) {
|
---|
| 72 | String target = ( String ) en.nextElement();
|
---|
| 73 | if ( target.equals( "setUp" ) )
|
---|
| 74 | setUp = ( Target ) targets.get( target );
|
---|
| 75 | else if ( target.equals( "tearDown" ) )
|
---|
| 76 | tearDown = ( Target ) targets.get( target );
|
---|
| 77 | else if ( target.startsWith( "test" ) )
|
---|
| 78 | testTargets.addElement( targets.get( target ) );
|
---|
| 79 | // also check for imported targets. Imported targets are named like
|
---|
| 80 | // projectname.targetname, so check for whatever follows the last dot.
|
---|
| 81 | // This is somewhat naive, as there is no restriction using dots in
|
---|
| 82 | // target names, so a target named "build.testimonials" would be treated
|
---|
| 83 | // as a test target.
|
---|
| 84 | else if ( target.lastIndexOf( "." ) > 0 && target.substring( target.lastIndexOf( "." ) + 1 ).startsWith( "test" ) ) {
|
---|
| 85 | testTargets.addElement( targets.get( target ) );
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | // run the setUp target
|
---|
| 90 | if ( setUp != null )
|
---|
| 91 | setUp.execute();
|
---|
| 92 | en = testTargets.elements();
|
---|
| 93 |
|
---|
| 94 | // run the test targets
|
---|
| 95 | StringBuffer messages = new StringBuffer();
|
---|
| 96 | while ( en.hasMoreElements() ) {
|
---|
| 97 | Target target = ( Target ) en.nextElement();
|
---|
| 98 | try {
|
---|
| 99 | executeDependencies( target );
|
---|
| 100 | target.performTasks();
|
---|
| 101 | if (showOutput)
|
---|
| 102 | log( target.getName() + " passed." );
|
---|
| 103 | ++tests_passed;
|
---|
| 104 | }
|
---|
| 105 | catch ( Exception e ) {
|
---|
| 106 | ++tests_failed;
|
---|
| 107 | if (showOutput)
|
---|
| 108 | log( target.getName() + " failed: " + e.getMessage() );
|
---|
| 109 | failures.addElement( target.getName() + " failed: " + e.getMessage() );
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | // run the tearDown target
|
---|
| 114 | if ( tearDown != null )
|
---|
| 115 | tearDown.execute();
|
---|
| 116 |
|
---|
| 117 | // print any error messages
|
---|
| 118 | if ( showSummary ) {
|
---|
| 119 | // log the failures
|
---|
| 120 | if ( failures.size() > 0 ) {
|
---|
| 121 | log( "" );
|
---|
| 122 | log( "---- Errors ---------------------------------" );
|
---|
| 123 | en = failures.elements();
|
---|
| 124 | while(en.hasMoreElements()) {
|
---|
| 125 | String msg = (String)en.nextElement();
|
---|
| 126 | log(msg);
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | // log the test count info
|
---|
| 131 | log( "" );
|
---|
| 132 | log( "---- Results --------------------------------" );
|
---|
| 133 | log( "Ran " + getRanCount() + " out of " + getTestCaseCount() + " tests." );
|
---|
| 134 | log( "Passed: " + getPassedCount() );
|
---|
| 135 | log( "Failed: " + getFailedCount() );
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | /**
|
---|
| 140 | * Execute a target's dependencies followed by the target itself.
|
---|
| 141 | * @param target the target to execute
|
---|
| 142 | */
|
---|
| 143 | private void executeDependencies( Target target ) {
|
---|
| 144 | if ( target == null )
|
---|
| 145 | return ;
|
---|
| 146 | Enumeration en = target.getDependencies();
|
---|
| 147 | if ( en == null )
|
---|
| 148 | return ;
|
---|
| 149 | while ( en.hasMoreElements() ) {
|
---|
| 150 | String name = ( String ) en.nextElement();
|
---|
| 151 | Target t = ( Target ) getProject().getTargets().get( name );
|
---|
| 152 | executeDependencies( t );
|
---|
| 153 | t.performTasks();
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 |
|
---|