1 2 3 package org.quilt.frontend.ant; 4 5 import org.apache.tools.ant.BuildException; 6 import org.apache.tools.ant.Project; 7 import org.apache.tools.ant.Task; 8 import org.apache.tools.ant.taskdefs.optional.junit.Enumerations; 9 import org.apache.tools.ant.types.Commandline; 10 import org.apache.tools.ant.types.CommandlineJava; 11 import org.apache.tools.ant.types.Environment; 12 import org.apache.tools.ant.types.Path; 13 14 import java.io.File ; 15 import java.net.URL ; 16 import java.util.Enumeration ; 17 import java.util.Vector ; 18 19 import org.quilt.cl.QuiltClassLoader; 20 import org.quilt.framework.QuiltTest; 21 import org.quilt.reports.FmtSelector; 22 import org.quilt.cover.stmt.StmtRegistry; 23 25 76 public class QuiltTask extends Task { 77 private Scheduler sch = null; 78 private TaskControl tc = null; 79 80 84 private boolean includeAntRuntime = true; 86 private Path antRuntimeClasses = null; 87 88 91 private QuiltTest qt = new QuiltTest(); public void setCheckCoverage (boolean b) { 107 sch.schedule(); 108 while ( (qt = sch.nextTest()) != null) { 109 qt.setCheckCoverage(b); 110 } 111 } 112 public void setCheckExcludes (String s) { 113 sch.schedule(); 114 while ( (qt = sch.nextTest()) != null) { 115 qt.setCheckExcludes(s); 116 } 117 } 118 public void setCheckIncludes (String s) { 119 sch.schedule(); 120 while ( (qt = sch.nextTest()) != null) { 121 qt.setCheckIncludes(s); 122 } 123 } 124 public void setErrorProperty(String propertyName) { 125 sch.schedule(); 126 while ( (qt = sch.nextTest()) != null) { 127 qt.setErrorProperty(propertyName); 128 } 129 } 130 public void setFailureProperty(String propertyName) { 131 sch.schedule(); 132 while ( (qt = sch.nextTest()) != null) { 133 qt.setFailureProperty(propertyName); 134 } 135 } 136 public void setFiltertrace(boolean b) { 137 sch.schedule(); 138 while ( (qt = sch.nextTest()) != null) { 139 qt.setFiltertrace(b); 140 } 141 } 142 public void setFork(boolean b) { 143 sch.schedule(); 144 while ( (qt = sch.nextTest()) != null) { 145 qt.setFork(b); 146 } 147 } 148 public void setHaltOnError(boolean b) { 149 sch.schedule(); 150 while ( (qt = sch.nextTest()) != null) { 151 qt.setHaltOnError(b); 152 } 153 } 154 public void setHaltOnFailure(boolean b) { 155 sch.schedule(); 156 while ( (qt = sch.nextTest()) != null) { 157 qt.setHaltOnFailure(b); 158 } 159 } 160 public void setMockTestRun(boolean b) { 161 sch.schedule(); 162 while ( (qt = sch.nextTest()) != null) { 163 qt.setMockTestRun (b); 164 } 165 } 166 public void setShowOutput(boolean b) { 167 tc.setShowOutput(b); sch.schedule(); 169 while ( (qt = sch.nextTest()) != null) { 170 qt.setShowOutput(b); 171 } 172 } 173 174 177 public BatchTest createBatchTest () { 178 BatchTest bt = new BatchTest (getProject()); 179 sch.addBatchTest(bt); 180 return bt; } 182 183 public Path createClasspath() { 184 return tc.createClasspath(); 186 } 187 public void setDir(File dir) { 188 tc.setDir(dir); 189 } 190 public void addEnv(Environment.Variable var) { 191 tc.addEnv(var); 192 } 193 public void addFormatter(FmtSelector fe) { 194 tc.addFormatter(fe); 195 } 196 public void setIncludeAntRuntime(boolean b) { 197 tc.setIncludeAntRuntime(b); 198 } 199 public void setJvm(String value) { 200 tc.setJvm(value); 201 } 202 public Commandline.Argument createJvmarg() { 203 return tc.createJvmarg(); 204 } 205 public void setMaxmemory(String max) { 206 tc.setMaxmemory (max); 207 } 208 public void setMockExec(boolean b) { 209 tc.setMockExec(b); 210 } 211 public void setNewenvironment(boolean b) { 212 tc.setNewEnvironment(b); 213 } 214 public void setPrintsummary(String sValue) { 216 SummaryAttribute sa = new SummaryAttribute(sValue); 217 tc.setSummary (sa.asBoolean()); 218 tc.setSummaryValue (sa.getValue()); 219 } 220 public void addSysproperty(Environment.Variable sysp) { 221 tc.addSysproperty(sysp); 222 } 223 public void addTest(QuiltTest qt) { 224 sch.addTest (qt); 225 } 226 public void setTimeout(Long t) { 227 tc.setTimeout (t); 228 } 229 230 public QuiltTask() throws Exception { 232 sch = new Scheduler (this); 233 tc = sch.getTaskControl(); 234 } 235 236 private MockExec mockE = null; 237 private TestExec testE = null; 238 private boolean mockery = false; 239 240 private boolean firstTimeThrough = true; 241 private void addCPEs () { 242 mockery = tc.getMockExec(); 243 if (mockery) { 244 mockE = new MockExec(); 245 } else { 246 testE = new TestExec(); 247 } 248 addClasspathEntry("/junit/framework/TestCase.class"); 249 addClasspathEntry("/org/apache/tools/ant/Task.class"); 250 addClasspathEntry("/org/quilt/runner/BaseTestRunner.class"); 251 } 252 public void init() { 255 antRuntimeClasses = new Path(getProject()); 256 } 257 258 public void execute() throws BuildException { 261 if (firstTimeThrough) { 262 firstTimeThrough = false; 263 addCPEs(); 264 sch.unbatch(); } 266 sch.schedule(); 267 268 Path quiltClassPath = tc.getCommandline().getClasspath(); 269 if (tc.getIncludeAntRuntime()) { 270 quiltClassPath.append ( tc.getAntRuntimeClasses() ); 271 } 272 QuiltClassLoader qcl = new QuiltClassLoader ( 273 QuiltClassLoader.cpToURLs( 274 tc.getCommandline().getClasspath().toString()), 275 this.getClass().getClassLoader(), (String [])null, (String [])null, (String [])null); 278 StmtRegistry stmtReg = (StmtRegistry)qcl.addQuiltRegistry( 279 "org.quilt.cover.stmt.StmtRegistry" ); 280 if (stmtReg == null) { 281 System.out.println( 282 "QuiltTask.execute: org.quilt.cover.stmt.StmtRegistry not found\n" 283 + " classpath error?"); 284 } 285 tc.setLoader(qcl); 286 while ( (qt = sch.nextTest()) != null) { 287 if (tc.getMockExec()) { 288 mockE.run(qt, tc); 289 } else if (qt.runMe (getProject())) { 290 testE.execute(qt, tc); 291 } 292 } 293 if (stmtReg != null) { 294 System.out.println ( 295 stmtReg.getReport() 296 ); 297 } 298 else System.out.println( 300 "QuiltTask.execute: after running tests, stmtReg is null"); 301 } 303 304 308 protected void addClasspathEntry(String resource) { 309 URL url = getClass().getResource(resource); 310 if (url != null) { 311 String u = url.toString(); 312 if (u.startsWith("jar:file:")) { 313 int pling = u.indexOf("!"); String jarName = u.substring(9, pling); 315 log("Found " + jarName, Project.MSG_DEBUG); 316 antRuntimeClasses.createPath() 317 .setLocation(new File ((new File (jarName)) 318 .getAbsolutePath())); 319 } else if (u.startsWith("file:")) { 320 int tail = u.indexOf(resource); 321 String dirName = u.substring(5, tail); 322 log("Found " + dirName, Project.MSG_DEBUG); 323 antRuntimeClasses.createPath() 324 .setLocation(new File ((new File (dirName)) 325 .getAbsolutePath())); 326 } else { 327 log("Don\'t know how to handle resource URL " + u, 328 Project.MSG_DEBUG); 329 } 330 } else { 331 log("Couldn\'t find " + resource, Project.MSG_DEBUG); 332 } 333 } 334 public void handleTheOutput(String line) { 336 super.handleOutput(line); 337 } 338 public void handleTheFlush(String line) { 339 super.handleFlush(line); 340 } 341 public void handleTheErrorOutput(String line) { 342 super.handleErrorOutput(line); 343 } 344 public void handleTheErrorFlush(String line) { 345 super.handleErrorFlush(line); 346 } 347 } 348 | Popular Tags |