1 54 55 package junitx.ant; 56 57 import junitx.tool.TestClassValidator; 58 import org.apache.tools.ant.AntClassLoader; 59 import org.apache.tools.ant.BuildException; 60 import org.apache.tools.ant.DirectoryScanner; 61 import org.apache.tools.ant.Project; 62 import org.apache.tools.ant.Task; 63 import org.apache.tools.ant.types.FileSet; 64 import org.apache.tools.ant.types.Path; 65 import org.apache.tools.ant.types.Reference; 66 67 import java.io.File ; 68 import java.util.List ; 69 import java.util.Vector ; 70 71 99 public class TestClassValidatorTask 100 extends Task { 101 102 private int verbosity = Project.MSG_VERBOSE; 103 private boolean quiet = false; 104 private boolean failonerror = true; 105 106 private AntClassLoader loader; 107 private Path classpath; 108 private List filesets = new Vector (); 109 110 private TestClassValidator.ClassValidator validator; 111 112 117 public void execute() 118 throws BuildException { 119 this.validator = new TestClassValidator.DefaultClassValidator(); 120 this.validator.setListener(new TestClassValidator.ClassValidatorListener() { 121 public void info(String message) { 122 log("INFO> " + message, verbosity); 123 System.out.println("INFO> " + message); 124 } 125 126 public void warning(String message) { 127 log("WARNING> " + message, verbosity); 128 System.out.println("WARNING> " + message); 129 } 130 131 public void error(String message) { 132 log("ERROR> " + message, verbosity); 133 System.out.println("ERROR> " + message); 134 } 135 }); 136 137 138 if (classpath != null) { 139 classpath.setProject(project); 140 this.loader = new AntClassLoader(project, classpath); 141 } 142 143 log(TestClassValidator.BANNER, Project.MSG_VERBOSE); 144 System.out.println(TestClassValidator.BANNER); 145 int count = 0; 146 for (int i = 0; i < filesets.size(); i++) { 147 FileSet fs = (FileSet) filesets.get(i); 148 149 try { 150 DirectoryScanner ds = fs.getDirectoryScanner(project); 151 ds.scan(); 152 153 String [] files = ds.getIncludedFiles(); 154 155 for (int k = 0; k < files.length; k++) { 156 String pathname = files[k]; 157 if (pathname.endsWith(".class")) { 158 String classname = pathname.substring(0, pathname.length() - ".class".length()).replace(File.separatorChar, '.'); 159 processFile(classname); 160 } 161 } 162 count += files.length; 163 } catch (BuildException e) { 164 if (failonerror) { 165 throw e; 166 } else { 167 log(e.getMessage(), quiet ? Project.MSG_VERBOSE : Project.MSG_WARN); 168 } 169 } catch (ClassNotFoundException e) { 170 if (failonerror) { 171 throw new BuildException(e); 172 } else { 173 log(e.getMessage(), quiet ? Project.MSG_VERBOSE : Project.MSG_WARN); 174 } 175 } 176 log("Number of classes: " + count, Project.MSG_VERBOSE); 177 System.out.println("Number of classes: " + count); 178 } 179 } 180 181 184 public void setClasspath(Path classpath) { 185 createClasspath().append(classpath); 186 } 187 188 191 public Path createClasspath() { 192 if (this.classpath == null) { 193 this.classpath = new Path(project); 194 } 195 return this.classpath.createPath(); 196 } 197 198 201 public void setClasspathRef(Reference r) { 202 createClasspath().setRefid(r); 203 } 204 205 212 public void addFileset(FileSet set) { 213 filesets.add(set); 214 } 215 216 221 public void setVerbose(boolean verbose) { 222 if (verbose) { 223 this.verbosity = Project.MSG_INFO; 224 } else { 225 this.verbosity = Project.MSG_VERBOSE; 226 } 227 } 228 229 private void processFile(String classname) 230 throws ClassNotFoundException { 231 log("processing " + classname, verbosity); 232 233 Class cls; 234 if (this.loader != null) { 235 cls = this.loader.loadClass(classname); 236 } else { 237 ClassLoader l = this.getClass().getClassLoader(); 238 if (l != null) { 241 cls = l.loadClass(classname); 242 } else { 243 cls = Class.forName(classname); 244 } 245 } 246 this.validator.validate(cls); 247 } 248 249 } 250 | Popular Tags |