001: package abbot;
002:
003: import java.util.*;
004: import java.io.File;
005: import junit.framework.*;
006: import junit.extensions.abbot.TestHelper;
007:
008: public class UnitTestSuite extends junit.framework.TestSuite {
009:
010: private static TestSuite createSuite(Class cls) {
011: try {
012: java.lang.reflect.Method suiteMethod = cls.getMethod(
013: "suite", null);
014: return (TestSuite) suiteMethod.invoke(null, null);
015: } catch (Exception e) {
016: return new TestSuite(cls);
017: }
018: }
019:
020: private static Map findTestClasses(File file, String name) {
021: if ("test".equals(name))
022: return new HashMap();
023:
024: Map map = new HashMap();
025: File[] files = file.listFiles();
026: if (files == null) {
027: if (name.endsWith("Test.class")) {
028: String className = name.substring(0, name.length() - 6);
029: try {
030: map.put(className, Class.forName(className));
031: } catch (ClassNotFoundException e) {
032: e.printStackTrace();
033: }
034: }
035: } else {
036: for (int i = 0; i < files.length; i++) {
037: map.putAll(findTestClasses(files[i],
038: "".equals(name) ? files[i].getName() : name
039: + "." + files[i].getName()));
040: }
041: }
042: return map;
043: }
044:
045: public static Test suite() {
046: String filter = System.getProperty("abbot.test.filter");
047: if (filter == null)
048: filter = "";
049:
050: // Any classes which should be run first or in some particular order
051: // should be put here. Otherwise test case suites are run in the
052: // order the classes are added.
053: String[] priority = new String[] { "abbot.util.",
054: "abbot.tester.WindowTrackerTest",
055: "junit.extensions.abbot.", "abbot.tester.RobotTest",
056: "abbot.tester.RobotAWTModeTest",
057: "abbot.tester.RobotDragDropTest",
058: "abbot.tester.RobotAppletTest",
059: "abbot.tester.ComponentTesterTest", "abbot.tester.", };
060: // This group has some strange interactions...
061: /*
062: abbot.editor.ComponentBrowserTest.class, //
063: abbot.editor.ComponentNodeTest.class, //
064: abbot.editor.ComponentTreeTest.class, //
065: abbot.editor.recorder.JInternalFrameRecorderTest.class, //
066: abbot.finder.BasicFinderTest.class, //
067: abbot.tester.JInternalFrameTesterTest.class, //
068: */
069: // Now add everything else
070: File file = new File(System.getProperty("user.dir")
071: + File.separator + "build" + File.separator
072: + "test-classes");
073: TestSuite suite = new TestSuite();
074: Map scanned = new HashMap();
075: scanned.putAll(findTestClasses(file, ""));
076: for (Iterator i = scanned.keySet().iterator(); i.hasNext();) {
077: String name = (String) i.next();
078: if (name.indexOf(filter) == -1) {
079: i.remove();
080: }
081: }
082:
083: for (int i = 0; i < priority.length; i++) {
084: String name = priority[i];
085: if (scanned.containsKey(name)) {
086: suite.addTest(createSuite((Class) scanned.get(name)));
087: scanned.remove(name);
088: } else {
089: for (Iterator iter = scanned.keySet().iterator(); iter
090: .hasNext();) {
091: String cname = (String) iter.next();
092: if (cname.startsWith(name)) {
093: suite.addTest(createSuite((Class) scanned
094: .get(cname)));
095: iter.remove();
096: }
097: }
098: }
099: }
100:
101: for (Iterator i = scanned.values().iterator(); i.hasNext();) {
102: suite.addTest(createSuite((Class) i.next()));
103: }
104: return suite;
105: }
106:
107: public UnitTestSuite(String name) {
108: super (name);
109: }
110:
111: public static void main(String[] args) {
112: TestHelper.runTests(args, UnitTestSuite.class);
113: }
114: }
|