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 private List filesets = new ArrayList();
29
30 private Report reporter = null;
31 private Class registryClass = null;
32 private AntClassLoader antLoader = null;
33 private File reportDir = null;
34 private List packages = new ArrayList();
35 private Path compileClasspath = null;
36 private String ext = "rpt";
37
38 private class AQR
39 extends QuiltRunner
40 {
41 public AQR( Class registryClass,
42 ClassLoader parent,
43 List urls,
44 List packages )
45 throws Exception
46 {
47 super( registryClass, parent, urls, packages );
48 }
49
50 public void startTest( Test test ) { }
51 public void endTest( Test test ) { }
52 public void runFailed( String reason ) { }
53 public void addFailure( Test test, AssertionFailedError error )
54 {
55 System.out.println("Failure: " + test.toString());
56 System.out.println("\t: " + error.toString());
57 }
58
59 public void addError( Test test, Throwable error )
60 {
61 System.out.println();
62 System.out.println("Error: " + test.toString());
63 System.out.println("\t: " + error.toString());
64 }
65 }
66
67 public AntQuiltRunner() {
68 }
69
70 public void setReportdir( File reportDir ) {
71 this.reportDir = reportDir;
72 }
73
74 public void setPackages( String packs ) {
75 StringTokenizer tokens = new StringTokenizer(packs, ";");
76 while (tokens.hasMoreElements()) {
77 packages.add( tokens.nextToken() );
78 }
79 }
80
81 public void setRegister( String register )
82 throws BuildException
83 {
84 try {
85 this.registryClass =
86 getClass().getClassLoader().loadClass( register );
87 } catch (Exception e) {
88 throw new BuildException( e.toString() );
89 }
90 }
91
92 public void setReporter( String reporter )
93 throws BuildException
94 {
95 try {
96 ClassLoader mine = getClass().getClassLoader();
97
98 Class reporterClass = mine.loadClass( reporter );
99 this.reporter = (Report) reporterClass.newInstance();
100 } catch (Exception ex) {
101 throw new BuildException( reporter.toString() +
102 " is not a valid Reporter.");
103 }
104 }
105
106
107 // public void setIgnoredpkg( String ignoredPkg ) {
108 // StringTokenizer tokenizer = new StringTokenizer( ignoredPkg, "," );
109 // while (tokenizer.hasMoreElements()) {
110 // String pack = tokenizer.nextToken();
111 // ignoredPkgs.add( pack + "." );
112 // }
113 // }
114
115 public void setClasspath( Path classPath ) {
116 this.compileClasspath = classPath;
117 }
118
119 public void setClasspathRef( Reference ref ) {
120 createClasspath().setRefid( ref );
121 }
122
123 public Path createClasspath() {
124 if (compileClasspath == null) {
125 compileClasspath = new Path( project );
126 }
127 return compileClasspath;
128 }
129
130 public Path getClasspath() {
131 return compileClasspath;
132 }
133
134 public void addFileset( FileSet fileset ) {
135 filesets.add( fileset );
136 }
137
138 /***
139 * makeTestName
140 *
141 * Test names come in looking like /a/b/c/d.class
142 * or /a/b/c/d.java
143 *
144 * Change them into a.b.c.d
145 */
146 public String makeTestName( String rawTestName ) {
147 String RC = rawTestName;
148 if (RC.startsWith("/"))
149 RC = RC.substring( 1 );
150
151 if ((RC.endsWith(".class")) ||
152 (RC.endsWith(".java")))
153 RC = RC.substring( 0, RC.lastIndexOf('.'));
154
155 RC = RC.replace('/','.');
156
157 return RC;
158 }
159
160
161 /***
162 * getReportOutput
163 *
164 * Return an output stream for writing the coverage
165 * results.
166 */
167 public OutputStream getReportOutput( String testName )
168 throws IOException
169 {
170 File outFile = new File( reportDir, "COVER-" + testName + "." + ext);
171 return new FileOutputStream( outFile );
172 }
173
174
175 public void execute() {
176 try {
177 Path cp = null;
178 if (getClasspath() != null) {
179 cp = getClasspath();
180 } else {
181 cp = Path.systemClasspath;
182 }
183
184 List urls = new ArrayList();
185 String paths[] = cp.list();
186 for (int i = 0; i < paths.length; i++) {
187 URL url = new File( paths[i] ).toURL();
188 urls.add( url );
189 }
190
191 Iterator i = filesets.iterator();
192 while (i.hasNext()) {
193 QuiltRunner runner = new AQR( registryClass,
194 getClass().getClassLoader(),
195 urls,
196 packages );
197
198 FileSet fileset = (FileSet) i.next();
199 FileScanner scanner = fileset.getDirectoryScanner( getProject() );
200
201 String files[] = scanner.getIncludedFiles();
202
203 for (int j = 0; j < files.length; j++) {
204 TestResult result =
205 runner.executeTest(makeTestName( files[j] ));
206 if (result.wasSuccessful()) {
207 try {
208 reporter.writeReport( getReportOutput( makeTestName( files[j] )),
209 runner.getRegistry() );
210 } catch (IOException e) {
211 throw new BuildException("Failed to write report: " +
212 e.toString() );
213 }
214 } else {
215 System.err.println("There were failures." +
216 "Use JUnit to get " +
217 "more information.");
218 System.err.println("If JUnit did not report these errors, ");
219 System.err.println("please report problem to ");
220 System.err.println("http://sourceforge.net/projects/quilt");
221 System.err.println();
222
223 if (result.errorCount() != 0) {
224 System.err.println("# ERRORS: " + result.errorCount());
225 Enumeration errs = result.errors();
226 while (errs.hasMoreElements()) {
227 TestFailure t = (TestFailure) errs.nextElement();
228 System.err.println(t.failedTest() );
229 t.thrownException().printStackTrace();
230 }
231 }
232
233 if (result.failureCount() != 0) {
234 System.err.println("# FAILURES: " + result.failureCount() );
235 }
236 }
237 }
238 }
239 } catch (Exception ex) {
240 throw new BuildException( ex );
241 }
242 }
243
244 }
This page was automatically generated by Maven