1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.jameleon.ant;
20
21 import net.sf.jameleon.ExecuteTestCase;
22 import net.sf.jameleon.util.Configurator;
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.DirectoryScanner;
25 import org.apache.tools.ant.Task;
26 import org.apache.tools.ant.types.FileSet;
27 import org.apache.tools.ant.types.Parameter;
28
29 import java.io.File;
30 import java.util.Iterator;
31 import java.util.LinkedList;
32 import java.util.List;
33 import java.util.Map;
34
35 public class ExecuteTestCaseTask extends Task {
36
37 protected final LinkedList fileSets = new LinkedList();
38 protected static final String LB = new String("\n##############################################################\n");
39 protected static final String DASH = new String("\n-------------------------------------------------------------\n");
40 protected static final String US = new String("\n_______________________________________________________________\n");
41 protected boolean debug = false;
42 protected boolean throwExceptionOnFailure = true;
43 protected boolean printTestSuiteSummary = true;
44 protected long waitTimeBetweenScripts = 0;
45 protected final LinkedList contextVariables = new LinkedList();
46 protected final LinkedList environmentSettings = new LinkedList();
47
48 /***
49 * Jameleon's implementation of Task.execute().
50 *
51 * @exception BuildException If anything goes wrong.
52 */
53 public final void execute() throws BuildException {
54 try{
55 Configurator.clearInstance();
56 validateOptions();
57 ExecuteTestCase exec = new ExecuteTestCase(debug);
58 exec.setWaitTimeBetweenScripts(waitTimeBetweenScripts);
59 setEnvironmentVariables(environmentSettings);
60 exec.registerEventListeners(false);
61 setParametersInContext(exec.getContextVars());
62 for ( int i = 0; i < fileSets.size(); i++ ) {
63 FileSet fs = ( FileSet ) fileSets.get( i );
64 DirectoryScanner ds = fs.getDirectoryScanner( getProject() );
65 String[] files = ds.getIncludedFiles();
66 for (int j = 0; j < files.length; j++) {
67
68 exec.addFile(new File(ds.getBasedir().getPath()+File.separator+files[j]));
69 }
70 }
71
72
73 setEnvironmentVariables(environmentSettings);
74 String errMsg = exec.executeFiles();
75 exec.deregisterEventListeners();
76 if (printTestSuiteSummary) {
77 ExecuteTestCase.closeAllLogs();
78 }
79
80
81 if ( errMsg.length() > 0 ) {
82 if (throwExceptionOnFailure) {
83 throw new BuildException(LB+"The following test cases failed:"+errMsg+
84 "\n\n"+"See 'TestResults.xml' and 'TestResults.html' for more details on the failure(s)", getLocation());
85 }else{
86 System.err.println(LB+"The following test cases failed:"+errMsg+
87 "\n\n"+"See 'TestResults.xml' and 'TestResults.html' for more details on the failure(s)");
88 }
89 }}catch(RuntimeException re){
90 re.printStackTrace();
91 throw re;
92 }
93 }
94
95 public void setPrintTestSuiteSummary(boolean printTestSuiteSummary){
96 this.printTestSuiteSummary = printTestSuiteSummary;
97 }
98
99 public void setThrowExceptionOnFailure(boolean throwExceptionOnFailure){
100 this.throwExceptionOnFailure = throwExceptionOnFailure;
101 }
102
103 public void setDebug(boolean debug){
104 this.debug = debug;
105 }
106
107 public void setWaitTimeBetweenScripts(long waitTimeBetweenScripts){
108 this.waitTimeBetweenScripts = waitTimeBetweenScripts;
109 }
110
111 /***
112 * Adds a variable to be set in the context
113 *
114 * @param parameter a name/value parameter.
115 */
116 public void addVariable(Parameter parameter){
117 contextVariables.add(parameter);
118 }
119
120 /***
121 * Adds a setting to jameleon.conf
122 *
123 * @param parameter a name/value parameter.
124 */
125 public void addConfig(Parameter parameter){
126 environmentSettings.add(parameter);
127 }
128
129 /***
130 * Ant's <fileset> definition. To define the files to parse.
131 *
132 * @param set a fileset to add
133 */
134 public void addFileset( FileSet set ) {
135 fileSets.add( set );
136 }
137
138 /***
139 * Validate required options are set.
140 *
141 * @exception BuildException if a fileset isn't set.
142 */
143 protected void validateOptions() throws BuildException {
144 if ( fileSets.size() == 0 ) {
145 throw new BuildException( "At least one fileset must be specified", getLocation() );
146 }
147 }
148
149 protected void setParametersInContext(Map contextVars){
150 Parameter param = null;
151 for (Iterator it = contextVariables.iterator(); it.hasNext(); ) {
152 param = (Parameter)it.next();
153 contextVars.put(param.getName(), param.getValue());
154 }
155 }
156
157 protected void setEnvironmentVariables(List environmentSettings){
158 Configurator config = Configurator.getInstance();
159 Parameter param = null;
160 for (Iterator it = environmentSettings.iterator(); it.hasNext(); ) {
161 param = (Parameter) it.next();
162 config.setValue(param.getName(), param.getValue());
163 }
164 }
165
166 }
167