1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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 /***
47 * A TestSuite which will run a Groovy unit test case inside any Java IDE
48 * either as a unit test case or as an application.
49 * <p/>
50 * You can specify the GroovyUnitTest to run by running this class as an appplication
51 * and specifying the script to run on the command line.
52 * <p/>
53 * <code>
54 * java groovy.util.GroovyTestSuite src/test/Foo.groovy
55 * </code>
56 * <p/>
57 * Or to run the test suite as a unit test suite in an IDE you can use
58 * the 'test' system property to define the test script to run.
59 * e.g. pass this into the JVM when the unit test plugin runs...
60 * <p/>
61 * <code>
62 * -Dtest=src/test/Foo.groovy
63 * </code>
64 *
65 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
66 * @version $Revision: 1.5 $
67 */
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
116 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 }