001: package net.sf.mockcreator;
002:
003: import junit.framework.Test;
004: import junit.framework.TestResult;
005: import junit.framework.TestSuite;
006:
007: import junit.textui.TestRunner;
008:
009: import java.io.File;
010:
011: import java.util.ArrayList;
012: import java.util.Iterator;
013: import java.util.List;
014:
015: /**
016: * Scans directories to find *Test.java files and
017: * adds them into list of tests; if class name is passed as CLI argument,
018: * performs that test only
019: *
020: * @author dozen
021: */
022: public class Runner extends TestSuite {
023: static String[] args = null;
024:
025: /**
026: * Main entry point for CLI run.
027: *
028: * @param args Contains test names if any.
029: */
030: public static void main(String[] a) {
031: args = a;
032: ListenedRunner.run(suite());
033: System.exit(ListenedRunner.tr.wasSuccessful() ? 0 : 1);
034: }
035:
036: /**
037: * Forms testsuite to run
038: *
039: * @param List of test classes.
040: *
041: * @return The suite
042: */
043: public static Test suite() {
044: Class[] params = new Class[] { String.class };
045: Object[] constructparams = new Object[1];
046:
047: TestSuite suite = new TestSuite();
048:
049: List lst = new ArrayList();
050:
051: if ((args != null) && (args.length > 0)) {
052: lst.add(args[0]);
053: } else {
054: lst = scan(".", "", lst);
055: }
056:
057: // iterate all passed/found tests
058: Iterator tit = lst.iterator();
059:
060: while (tit.hasNext()) {
061: String test = (String) tit.next();
062:
063: // add the test; ignore missed
064: try {
065: Class testCase = Class.forName(test);
066: suite.addTestSuite(testCase);
067: } catch (ClassNotFoundException ex) {
068: System.err.println("Test class was not found: " + test);
069: }
070: }
071:
072: return suite;
073: }
074:
075: /**
076: * Scans given directory for Test*.java files.
077: *
078: * @param dir Directory Name
079: * @param name File name or subdir name
080: * @return List of collected names
081: */
082: private static List scan(String dir, String name, List lst) {
083: String base = dir + "/" + name;
084:
085: File fil = new File((base == null) ? "." : base);
086:
087: if (fil.isDirectory()) {
088: // scan directory for files
089: String[] names = fil.list();
090:
091: for (int i = 0; i < names.length; i++) {
092: scan(base, names[i], lst);
093: }
094: } else {
095: if ((name.indexOf("Test") >= 0) && name.endsWith(".class")
096: && (name.indexOf("$") < 0)
097: && (name.indexOf("TestCase.class") < 0)) {
098: // found a test; store it in Java package notation
099: String toadd = base;
100:
101: while ((toadd.charAt(0) == '.')
102: || (toadd.charAt(0) == '/')) {
103: toadd = toadd.substring(1, toadd.length());
104: }
105:
106: toadd = toadd.replace('/', '.');
107: toadd = toadd.substring(0, toadd.lastIndexOf(".class"));
108: lst.add(toadd);
109: }
110: }
111:
112: return lst;
113: }
114:
115: /**
116: * Prints usage instructions.
117: */
118: public static void Usage() {
119: System.out.println("Usage:");
120: System.out
121: .println("junit.textui.TestRunner de.abstract.Suite <Tests>");
122: }
123:
124: public static class ListenedRunner extends junit.textui.TestRunner {
125: public static TestResult tr;
126:
127: protected TestResult createTestResult() {
128: if (tr == null) {
129: tr = new TestResult();
130: }
131:
132: return tr;
133: }
134:
135: // dozen: I hate the way JUnit provides so-called extensibility >:E
136: static public TestResult run(Test test) {
137: TestRunner runner = new ListenedRunner();
138:
139: return runner.doRun(test);
140: }
141: }
142: }
|