001: package groovy.util;
002:
003: import groovy.lang.GroovyClassLoader;
004: import groovy.lang.Script;
005: import junit.framework.Test;
006: import junit.framework.TestSuite;
007: import org.codehaus.groovy.control.CompilationFailedException;
008: import org.codehaus.groovy.runtime.ScriptTestAdapter;
009:
010: import java.io.File;
011: import java.io.IOException;
012: import java.util.Collection;
013: import java.util.Iterator;
014: import java.util.logging.Logger;
015:
016: /**
017: * AllTestSuite can be used in extension of GroovyTestSuite to execute TestCases written in Groovy
018: * from inside a Java IDE.
019: * AllTestSuite collects all files below a given directory that comply to a given pattern.
020: * From these files, a TestSuite is constructed that can be run via an IDE graphical Test runner.
021: * The files are assumed to be Groovy source files and be either a TestCase or a Script that can
022: * be wrapped transparently into a TestCase.
023: * The directory and the pattern can be set via System properties (see this classes' constants for details.)
024: *
025: * When setting the loglevel of this class to FINEST, all file loading will be logged.
026: *
027: * See also groovy.util.AllTestSuiteTest.groovy
028: * @author Dierk Koenig based on a prototype by Andrew Glover
029: * todo: dk: make FileNameFinder injectable
030: */
031: public class AllTestSuite extends TestSuite {
032:
033: /** The System Property to set as base directory for collection of Test Cases.
034: * The pattern will be used as an Ant fileset include basedir.
035: * Key is "groovy.test.dir".
036: * Default value is "./test/".
037: */
038: public static final String SYSPROP_TEST_DIR = "groovy.test.dir";
039:
040: /** The System Property to set as the filename pattern for collection of Test Cases.
041: * The pattern will be used as Regualar Expression pattern applied with the find
042: * operator agains each candidate file.path.
043: * Key is "groovy.test.pattern".
044: * Default value is "Test.groovy".
045: */
046: public static final String SYSPROP_TEST_PATTERN = "groovy.test.pattern";
047:
048: private static Logger LOG = Logger.getLogger(AllTestSuite.class
049: .getName());
050: private static ClassLoader JAVA_LOADER = AllTestSuite.class
051: .getClassLoader();
052: private static GroovyClassLoader GROOVY_LOADER = new GroovyClassLoader(
053: JAVA_LOADER);
054:
055: private static final String[] EMPTY_ARGS = new String[] {};
056: private static IFileNameFinder FINDER = null;
057:
058: static { // this is only needed since the Groovy Build compiles *.groovy files after *.java files
059: try {
060: Class finderClass = Class
061: .forName("groovy.util.FileNameFinder");
062: FINDER = (IFileNameFinder) finderClass.newInstance();
063: } catch (Exception e) {
064: throw new RuntimeException(
065: "Cannot find and instantiate class FileNameFinder",
066: e);
067: }
068: }
069:
070: public static Test suite() {
071: String basedir = System
072: .getProperty(SYSPROP_TEST_DIR, "./test/");
073: String pattern = System.getProperty(SYSPROP_TEST_PATTERN,
074: "**/*Test.groovy");
075: return suite(basedir, pattern);
076: }
077:
078: public static Test suite(String basedir, String pattern) {
079: AllTestSuite suite = new AllTestSuite();
080: String fileName = "";
081: try {
082: Collection filenames = FINDER
083: .getFileNames(basedir, pattern);
084: for (Iterator iter = filenames.iterator(); iter.hasNext();) {
085: fileName = (String) iter.next();
086: LOG.finest("trying to load " + fileName);
087: suite.loadTest(fileName);
088: }
089: } catch (CompilationFailedException e1) {
090: e1.printStackTrace();
091: throw new RuntimeException(
092: "CompilationFailedException when loading "
093: + fileName, e1);
094: } catch (IOException e2) {
095: throw new RuntimeException("IOException when loading "
096: + fileName, e2);
097: }
098: return suite;
099: }
100:
101: protected void loadTest(String fileName)
102: throws CompilationFailedException, IOException {
103: Class type = compile(fileName);
104: if (!Test.class.isAssignableFrom(type)
105: && Script.class.isAssignableFrom(type)) {
106: addTest(new ScriptTestAdapter(type, EMPTY_ARGS));
107: } else {
108: addTestSuite(type);
109: }
110: }
111:
112: protected Class compile(String fileName)
113: throws CompilationFailedException, IOException {
114: return GROOVY_LOADER.parseClass(new File(fileName));
115: }
116: }
|