1 20 package org.apache.cactus.integration.maven; 21 22 import org.apache.tools.ant.DirectoryScanner; 23 import org.apache.tools.ant.Project; 24 import org.apache.tools.ant.types.FileSet; 25 import org.apache.tools.ant.types.Path; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import java.util.List ; 30 import java.util.ArrayList ; 31 import java.util.Iterator ; 32 import java.io.File ; 33 import java.net.MalformedURLException ; 34 import java.net.URLClassLoader ; 35 import java.net.URL ; 36 import java.lang.reflect.Modifier ; 37 import java.lang.reflect.Method ; 38 39 import junit.framework.TestCase; 40 41 57 public class CactusScanner 58 { 59 62 private Log log = LogFactory.getLog(CactusScanner.class); 63 64 67 private Project project; 68 69 72 private List cactusTests = new ArrayList (); 73 74 77 public void setProject(Project theProject) 78 { 79 this.project = theProject; 80 } 81 82 85 public void clear() 86 { 87 this.cactusTests.clear(); 88 } 89 90 93 public Iterator iterator() 94 { 95 return this.cactusTests.iterator(); 96 } 97 98 104 public void processFileSet(FileSet theFileset, Path theClasspath) 105 { 106 DirectoryScanner ds = theFileset.getDirectoryScanner(this.project); 107 ds.scan(); 108 String [] files = ds.getIncludedFiles(); 109 110 for (int i = 0; i < files.length; i++) 111 { 112 117 if (files[i].endsWith(".class")) 119 { 120 String fqn = files[i] 121 .substring(0, files[i].length() - ".class".length()) 122 .replace(File.separatorChar, '.'); 123 124 log.debug("Found candidate class: [" + fqn + "]"); 125 126 if (isJUnitTestCase(fqn, theClasspath)) 128 { 129 log.debug("Found Cactus test case: [" + fqn + "]"); 130 this.cactusTests.add(fqn); 131 } 132 } 133 } 134 } 135 136 141 private boolean isJUnitTestCase(String theClassName, Path theClasspath) 142 { 143 Class clazz = loadClass(theClassName, theClasspath); 144 if (clazz == null) 145 { 146 return false; 147 } 148 149 Class testCaseClass = null; 150 try 151 { 152 testCaseClass = clazz.getClassLoader().loadClass( 153 TestCase.class.getName()); 154 } 155 catch (ClassNotFoundException e) 156 { 157 log.debug("Cannot load class", e); 158 return false; 159 } 160 161 if (!testCaseClass.isAssignableFrom(clazz)) 162 { 163 log.debug("Not a JUnit test as class [" + theClassName + "] does " 164 + "not inherit from [" + TestCase.class.getName() 165 + "]"); 166 return false; 167 } 168 169 if (Modifier.isAbstract(clazz.getModifiers())) 171 { 172 log.debug("Not a JUnit test as class [" + theClassName + "] is " 173 + "abstract"); 174 return false; 175 } 176 177 boolean hasTestMethod = false; 180 Method [] methods = clazz.getMethods(); 181 for (int i = 0; i < methods.length; i++) 182 { 183 if (methods[i].getName().startsWith("test") 184 && (methods[i].getReturnType() == Void.TYPE) 185 && (methods[i].getParameterTypes().length == 0)) 186 { 187 hasTestMethod = true; 188 break; 189 } 190 } 191 192 if (!hasTestMethod) 193 { 194 log.debug("Not a JUnit test as class [" + theClassName + "] has " 195 + "no method that start with \"test\", returns void and has " 196 + "no parameters"); 197 return false; 198 } 199 200 return true; 201 } 202 203 208 private Class loadClass(String theClassName, Path theClasspath) 209 { 210 Class clazz = null; 211 try 212 { 213 clazz = createClassLoader(theClasspath).loadClass(theClassName); 214 } 215 catch (ClassNotFoundException e) 216 { 217 log.error("Failed to load class [" + theClassName + "]", e); 218 } 219 return clazz; 220 } 221 222 227 private ClassLoader createClassLoader(Path theClasspath) 228 { 229 URL [] urls = new URL [theClasspath.size()]; 230 231 try 232 { 233 for (int i = 0; i < theClasspath.size(); i++) 234 { 235 log.debug("Adding [" 236 + new File (theClasspath.list()[i]).toURL() + "] " 237 + "to class loader classpath"); 238 urls[i] = new File (theClasspath.list()[i]).toURL(); 239 } 240 } 241 catch (MalformedURLException e) 242 { 243 log.debug("Invalid URL", e); 244 } 245 246 return new URLClassLoader (urls); 247 } 248 } 249 | Popular Tags |