View Javadoc
1 package junit.quilt.ant; 2 3 import java.net.URL; 4 5 import java.io.File; 6 import java.io.IOException; 7 import java.io.OutputStream; 8 import java.io.FileOutputStream; 9 10 import java.util.List; 11 import java.util.Iterator; 12 import java.util.ArrayList; 13 import java.util.Enumeration; 14 import java.util.StringTokenizer; 15 16 import org.apache.tools.ant.*; 17 import org.apache.tools.ant.types.*; 18 19 import junit.framework.*; 20 21 import junit.quilt.framework.*; 22 import junit.quilt.reports.*; 23 import junit.quilt.runner.*; 24 25 public class AntQuiltRunner 26 extends Task 27 { 28 // Will try to implement fork="yes" 29 private boolean fork = false; 30 private List filesets = new ArrayList(); 31 private Report reporter = null; 32 private Class registryClass = null; 33 private File report = null; 34 private List packages = new ArrayList(); 35 private Path compileClasspath = null; 36 37 private class AQR 38 extends QuiltRunner 39 { 40 public AQR( Class registryClass, 41 ClassLoader parent, 42 List urls, 43 List packages ) 44 throws Exception 45 { 46 super( registryClass, parent, urls, packages ); 47 } 48 49 public void startTest( Test test ) { } 50 public void endTest( Test test ) { } 51 public void runFailed( String reason ) { } 52 public void addFailure( Test test, AssertionFailedError error ) 53 { 54 System.out.println("Failure: " + test.toString()); 55 System.out.println("\t: " + error.toString()); 56 } 57 58 public void addError( Test test, Throwable error ) 59 { 60 System.out.println(); 61 System.out.println("Error: " + test.toString()); 62 System.out.println("\t: " + error.toString()); 63 } 64 } 65 66 public AntQuiltRunner() { 67 } 68 69 /*** 70 * Determines whether to start a new JVM or not. 71 */ 72 public void setFork( boolean value ) { 73 fork = true; 74 } 75 76 /*** 77 * Sets the output directory for reports. 78 */ 79 public void setReport( File report ) { 80 this.report = report; 81 } 82 83 /*** 84 * This is a semicolon list of packages to be 85 * instrumented. 86 */ 87 public void setPackages( String packs ) { 88 StringTokenizer tokens = new StringTokenizer(packs, ";"); 89 while (tokens.hasMoreElements()) { 90 packages.add( tokens.nextToken() ); 91 } 92 } 93 94 /*** 95 * This will set the Coverage technique. 96 * Current registers are: 97 * junit.quilt.cover.state.StateMachineRegister 98 * junit.quilt.cover.ball94.B94Register 99 */ 100 public void setRegister( String register ) 101 throws BuildException 102 { 103 try { 104 this.registryClass = 105 getClass().getClassLoader().loadClass( register ); 106 } catch (Exception e) { 107 throw new BuildException( e.toString() ); 108 } 109 } 110 111 /*** 112 * This determines which type of reports will 113 * be generated. 114 * Current values are: 115 * junit.quilt.reports.XMLSummary 116 * junit.quilt.reports.TreeSummary 117 */ 118 public void setReporter( String reporter ) 119 throws BuildException 120 { 121 try { 122 ClassLoader mine = getClass().getClassLoader(); 123 124 Class reporterClass = mine.loadClass( reporter ); 125 this.reporter = (Report) reporterClass.newInstance(); 126 } catch (Exception ex) { 127 throw new BuildException( reporter.toString() + 128 " is not a valid Reporter."); 129 } 130 } 131 132 /*** 133 * This sets the full ClassPath for running the 134 * tests. Instrumented code is determined by 135 * the packages parameter. 136 */ 137 public void setClasspath( Path classPath ) { 138 this.compileClasspath = classPath; 139 } 140 141 /*** 142 * This sets a classpath reference it can use. 143 */ 144 public void setClasspathRef( Reference ref ) { 145 createClasspath().setRefid( ref ); 146 } 147 148 /*** 149 * This allows for the classpath to be embedded 150 * between the tags. 151 */ 152 public Path createClasspath() { 153 if (compileClasspath == null) { 154 compileClasspath = new Path( project ); 155 } 156 return compileClasspath; 157 } 158 159 /*** 160 * This fileset is the tests to be run. 161 */ 162 public void addFileset( FileSet fileset ) { 163 filesets.add( fileset ); 164 } 165 166 /*** 167 * Useful method for getting the ClassPath. 168 */ 169 public Path getClasspath() { 170 return compileClasspath; 171 } 172 173 /*** 174 * Adds a System Property. 175 */ 176 public void addSyproperty( Environment.Variable sysp ) { 177 // TODO: Do something here. . . 178 } 179 180 /*** 181 * makeTestName 182 * 183 * Test names come in looking like /a/b/c/d.class 184 * or /a/b/c/d.java 185 * 186 * Change them into a.b.c.d 187 */ 188 public String makeTestName( String rawTestName ) { 189 190 String RC = rawTestName; 191 if (RC.startsWith(File.separator)) 192 RC = RC.substring( 1 ); 193 194 if ((RC.endsWith(".class")) || 195 (RC.endsWith(".java"))) 196 RC = RC.substring( 0, RC.lastIndexOf('.')); 197 198 RC = RC.replace(File.separatorChar,'.'); 199 return RC; 200 } 201 202 203 /*** 204 * Ant calls this to start our coverage analasys. 205 */ 206 public void execute() { 207 try { 208 Path cp = null; 209 if (getClasspath() != null) { 210 cp = getClasspath(); 211 } else { 212 cp = Path.systemClasspath; 213 } 214 215 List urls = new ArrayList(); 216 String paths[] = cp.list(); 217 for (int i = 0; i < paths.length; i++) { 218 URL url = new File( paths[i] ).toURL(); 219 urls.add( url ); 220 } 221 222 QuiltRunner runner = new AQR( registryClass, 223 getClass().getClassLoader(), 224 urls, 225 packages ); 226 227 Iterator i = filesets.iterator(); 228 while (i.hasNext()) { 229 FileSet fileset = (FileSet) i.next(); 230 FileScanner scanner = fileset.getDirectoryScanner( getProject() ); 231 232 String files[] = scanner.getIncludedFiles(); 233 234 for (int j = 0; j < files.length; j++) { 235 TestResult result = 236 runner.executeTest(makeTestName( files[j] )); 237 if (!result.wasSuccessful()) { 238 System.err.println("There were failures." + 239 "Use JUnit to get " + 240 "more information."); 241 System.err.println("If JUnit did not report these errors, "); 242 System.err.println("please report problem to "); 243 System.err.println("http://sourceforge.net/projects/quilt"); 244 System.err.println(); 245 246 if (result.errorCount() != 0) { 247 System.err.println("# ERRORS: " + result.errorCount()); 248 Enumeration errs = result.errors(); 249 while (errs.hasMoreElements()) { 250 TestFailure t = (TestFailure) errs.nextElement(); 251 System.err.println(t.failedTest() ); 252 t.thrownException().printStackTrace(); 253 } 254 } 255 256 if (result.failureCount() != 0) { 257 System.err.println("# FAILURES: " + result.failureCount() ); 258 } 259 } 260 261 try { 262 reporter.writeReport( new FileOutputStream( report ), 263 runner.getRegistry() ); 264 } catch (IOException e) { 265 throw new BuildException("Failed to write report: " + 266 e.toString() ); 267 } 268 } 269 } 270 } catch (Exception ex) { 271 throw new BuildException( ex ); 272 } 273 } 274 275 } 276

This page was automatically generated by Maven