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 groovy.util;
36
37 import groovy.lang.GroovyClassLoader;
38 import groovy.lang.Script;
39 import junit.framework.Test;
40 import junit.framework.TestSuite;
41 import junit.textui.TestRunner;
42 import org.codehaus.groovy.runtime.ScriptTestAdapter;
43
44 import java.io.File;
45
46
47 /***
48 * A TestSuite which will run a Groovy unit test case inside any Java IDE
49 * either as a unit test case or as an application.
50 * <p/>
51 * You can specify the GroovyUnitTest to run by running this class as an appplication
52 * and specifying the script to run on the command line.
53 * <p/>
54 * <code>
55 * java groovy.util.GroovyTestSuite src/test/Foo.groovy
56 * </code>
57 * <p/>
58 * Or to run the test suite as a unit test suite in an IDE you can use
59 * the 'test' system property to define the test script to run.
60 * e.g. pass this into the JVM when the unit test plugin runs...
61 * <p/>
62 * <code>
63 * -Dtest=src/test/Foo.groovy
64 * </code>
65 *
66 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
67 * @version $Revision: 1.9 $
68 */
69 public class GroovyTestSuite extends TestSuite {
70
71 protected static String file = null;
72
73 protected GroovyClassLoader loader = new GroovyClassLoader(GroovyTestSuite.class.getClassLoader());
74
75 public static void main(String[] args) {
76 if (args.length > 0) {
77 file = args[0];
78 }
79 TestRunner.run(suite());
80 }
81
82 public static Test suite() {
83 GroovyTestSuite suite = new GroovyTestSuite();
84 try {
85 suite.loadTestSuite();
86 } catch (Exception e) {
87 throw new RuntimeException("Could not create the test suite: " + e, e);
88 }
89 return suite;
90 }
91
92 public void loadTestSuite() throws Exception {
93 String fileName = System.getProperty("test", file);
94 if (fileName == null) {
95 throw new RuntimeException("No filename given in the 'test' system property so cannot run a Groovy unit test");
96 }
97 System.out.println("Compiling: " + fileName);
98 Class type = compile(fileName);
99 String[] args = {};
100 if (!Test.class.isAssignableFrom(type) && Script.class.isAssignableFrom(type)) {
101
102 addTest(new ScriptTestAdapter(type, args));
103 } else {
104 addTestSuite(type);
105 }
106 }
107
108 public Class compile(String fileName) throws Exception {
109 return loader.parseClass(new File(fileName));
110 }
111 }