1 19 20 package edu.umd.cs.findbugs.tools.junit; 21 22 import java.io.File ; 23 import java.net.URL ; 24 import java.net.URLClassLoader ; 25 import java.security.AccessControlContext ; 26 import java.security.AccessController ; 27 import java.security.PrivilegedExceptionAction ; 28 import java.util.ArrayList ; 29 import java.util.Enumeration ; 30 import java.util.StringTokenizer ; 31 import java.util.jar.JarEntry ; 32 import java.util.jar.JarFile ; 33 34 import junit.framework.TestSuite; 35 36 45 public class JUnitJarRunner { 46 private String jarFileName; 47 private String classpath; 48 49 53 public JUnitJarRunner(String jarFileName) { 54 this.jarFileName = jarFileName; 55 } 56 57 62 public void setClassPath(String classpath) { 63 this.classpath = classpath; 64 } 65 66 71 public TestSuite buildTestSuite() throws Exception { 72 TestSuite suite = new TestSuite(); 73 74 final ArrayList <URL > urlList = new ArrayList <URL >(); 75 urlList.add(new URL ("file:" + jarFileName)); 76 if (classpath != null) { 77 StringTokenizer tok = new StringTokenizer (classpath, File.pathSeparator); 78 while (tok.hasMoreTokens()) { 79 urlList.add(new URL ("file:" + tok.nextToken())); 80 } 81 } 82 83 ClassLoader cl = 84 AccessController.doPrivileged(new PrivilegedExceptionAction <URLClassLoader >() { 85 86 public URLClassLoader run() throws Exception { 87 return new URLClassLoader (urlList.toArray(new URL [urlList.size()])); 88 89 }}); 90 91 Class <?> testCaseClass = cl.loadClass("junit.framework.TestCase"); 92 93 JarFile jarFile = new JarFile (jarFileName); 94 Enumeration <JarEntry > e = jarFile.entries(); 95 while (e.hasMoreElements()) { 96 JarEntry entry = e.nextElement(); 97 String entryName = entry.getName(); 98 if (entryName.endsWith(".class")) { 99 String className = 100 entryName.substring(0, entryName.length() - ".class".length()).replace('/', '.'); 101 if (!className.endsWith("Test")) 102 continue; 103 System.out.println("Loading test class: " + className); 104 System.out.flush(); 105 Class <?> jarClass = cl.loadClass(className); 106 if (testCaseClass.isAssignableFrom(jarClass)) 107 suite.addTestSuite(jarClass); 108 } 109 } 110 111 return suite; 112 } 113 114 public void run(TestSuite suite, String how) { 115 if (how.equals("-textui")) { 116 junit.textui.TestRunner.run(suite); 117 } else if (how.equals("-swingui")) { 118 throw new UnsupportedOperationException ("I don't know how to run the Swing UI on a test suite yet"); 120 } else 121 throw new IllegalArgumentException ("Unknown option: " + how); 122 } 123 124 public static void main(String [] argv) throws Exception { 125 if (argv.length < 1) { 126 System.err.println("Usage: " + JUnitJarRunner.class.getName() + 127 " [-textui|-swingui]" + 128 " <test suite jar file> [<classpath with code to test>]"); 129 System.exit(1); 130 } 131 String how = "-textui"; 132 int arg = 0; 133 if (argv[arg].startsWith("-")) { 134 how = argv[arg++]; 135 } 136 String jarFileName = argv[arg++]; 137 JUnitJarRunner runner = new JUnitJarRunner(jarFileName); 138 if (arg < argv.length) 139 runner.setClassPath(argv[arg++]); 140 TestSuite suite = runner.buildTestSuite(); 141 runner.run(suite, how); 142 } 143 } 144 145 | Popular Tags |