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 java.io.File;
38 import java.util.ArrayList;
39 import java.util.Iterator;
40 import java.util.List;
41
42 import junit.framework.Test;
43 import junit.framework.TestSuite;
44 import junit.textui.TestRunner;
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 *
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 *
53 * <code>
54 * java groovy.util.GroovyTestSuite src/test/Foo.groovy
55 * </code>
56 *
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 *
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.3 $
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 }
81 catch (Exception e) {
82 e.printStackTrace();
83 throw new RuntimeException("Could not create the test suite: " + e, e);
84 }
85 return suite;
86 }
87
88 public void loadTestSuite() throws Exception {
89 recurseDirectory(new File(testDirectory));
90 }
91
92 protected void recurseDirectory(File dir) throws Exception {
93 File[] files = dir.listFiles();
94 List traverseList = new ArrayList();
95 for (int i = 0; i < files.length; i++) {
96 File file = files[i];
97 if (file.isDirectory()) {
98 traverseList.add(file);
99 }
100 else {
101 String name = file.getName();
102 if (name.endsWith("Test.class") || name.endsWith("Bug.class")) {
103 addTest(file);
104 }
105 }
106 }
107 for (Iterator iter = traverseList.iterator(); iter.hasNext();) {
108 recurseDirectory((File) iter.next());
109 }
110 }
111
112 protected void addTest(File file) throws Exception {
113 String name = file.getPath();
114
115 name = name.substring(testDirectory.length() + 1, name.length() - ".class".length());
116 name = name.replace(File.separatorChar, '.');
117
118
119 Class type = loadClass(name);
120 addTestSuite(type);
121 }
122
123 protected Class loadClass(String name) throws ClassNotFoundException {
124 try {
125 return Thread.currentThread().getContextClassLoader().loadClass(name);
126 }
127 catch (ClassNotFoundException e) {
128 try {
129 return getClass().getClassLoader().loadClass(name);
130 }
131 catch (ClassNotFoundException e1) {
132 return Class.forName(name);
133 }
134 }
135 }
136 }