1 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 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 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 } 112 | Popular Tags |