1 35 package org.codehaus.groovy.tools; 36 37 import junit.framework.Test; 38 import junit.framework.TestSuite; 39 import junit.textui.TestRunner; 40 41 import java.io.File ; 42 import java.util.ArrayList ; 43 import java.util.Iterator ; 44 import java.util.List ; 45 46 68 public class FindAllTestsSuite extends TestSuite { 69 70 protected static String testDirectory = "target/test-classes"; 71 72 public static void main(String [] args) { 73 TestRunner.run(suite()); 74 } 75 76 public static Test suite() { 77 FindAllTestsSuite suite = new FindAllTestsSuite(); 78 try { 79 suite.loadTestSuite(); 80 } catch (Exception e) { 81 throw new RuntimeException ("Could not create the test suite: " + e, e); 82 } 83 return suite; 84 } 85 86 public void loadTestSuite() throws Exception { 87 recurseDirectory(new File (testDirectory)); 88 } 89 90 protected void recurseDirectory(File dir) throws Exception { 91 File [] files = dir.listFiles(); 92 List traverseList = new ArrayList (); 93 for (int i = 0; i < files.length; i++) { 94 File file = files[i]; 95 if (file.isDirectory()) { 96 traverseList.add(file); 97 } else { 98 String name = file.getName(); 99 if (name.endsWith("Test.class") || name.endsWith("Bug.class")) { 100 addTest(file); 101 } 102 } 103 } 104 for (Iterator iter = traverseList.iterator(); iter.hasNext();) { 105 recurseDirectory((File ) iter.next()); 106 } 107 } 108 109 protected void addTest(File file) throws Exception { 110 String name = file.getPath(); 111 112 name = name.substring(testDirectory.length() + 1, name.length() - ".class".length()); 113 name = name.replace(File.separatorChar, '.'); 114 115 Class type = loadClass(name); 117 addTestSuite(type); 118 } 119 120 protected Class loadClass(String name) throws ClassNotFoundException { 121 try { 122 return Thread.currentThread().getContextClassLoader().loadClass(name); 123 } catch (ClassNotFoundException e) { 124 try { 125 return getClass().getClassLoader().loadClass(name); 126 } catch (ClassNotFoundException e1) { 127 return Class.forName(name); 128 } 129 } 130 } 131 } 132 | Popular Tags |