001: package junit.extensions.abbot;
002:
003: import java.io.File;
004: import java.io.UnsupportedEncodingException;
005: import java.net.URL;
006: import java.net.URLDecoder;
007: import java.util.ArrayList;
008: import java.util.Locale;
009:
010: import junit.framework.*;
011: import junit.textui.*;
012:
013: import abbot.Log;
014:
015: /** Provides automatic test suite generation given command-line arguments.
016: * Also allows for a single test to be run if it is specified. Supports "-v"
017: * (verbose) to display each test's name prior to running the test (aids in
018: * identifying which test failed when the VM crashes).
019: * The locale may be set explicitly by the following system properties:
020: * <ul>
021: * <li>abbot.locale.language
022: * <li>abbot.locale.country
023: * <li>abbot.locale.variant
024: * </ul>
025: */
026:
027: public class TestHelper {
028: protected TestHelper() {
029: }
030:
031: /** Returns the classpath directory in which the given class is found. */
032: public static File getClasspathDirectory(Class test) {
033: URL url = test.getResource("/"
034: + test.getName().replace('.', '/') + ".class");
035: try {
036: // 1.4+ only
037: return new File(URLDecoder.decode(url.getFile(), "UTF-8"))
038: .getParentFile();
039: } catch (UnsupportedEncodingException e) {
040: // ain't gonna happen
041: throw new Error(e.toString());
042: }
043: }
044:
045: public static TestSuite generateSuite(Class[] classes) {
046: TestSuite suite = new TestSuite();
047: for (int i = 0; i < classes.length; i++) {
048: try {
049: java.lang.reflect.Method suiteMethod = classes[i]
050: .getMethod("suite", null);
051: suite.addTest((Test) suiteMethod.invoke(null, null));
052: } catch (Exception exc) {
053: suite.addTest(new TestSuite(classes[i]));
054: }
055: }
056: return suite;
057: }
058:
059: private static boolean printTestNames = false;
060:
061: protected static String[] parseArgs(String[] args) {
062: String language = System.getProperty("abbot.locale.language");
063: if (language != null) {
064: String country = System.getProperty("abbot.locale.country",
065: "");
066: String variant = System.getProperty(
067: "abbot.locale.language", "");
068: Locale locale = new Locale(language, country, variant);
069: Locale.setDefault(locale);
070: System.out.println("Using locale "
071: + locale.getDisplayName());
072: }
073: ArrayList newArgs = new ArrayList();
074: for (int i = 0; i < args.length; i++) {
075: if (args[i].equals("-v"))
076: printTestNames = true;
077: else {
078: newArgs.add(args[i]);
079: }
080: }
081: return (String[]) newArgs.toArray(new String[newArgs.size()]);
082: }
083:
084: protected static Test collectTests(String[] args, Class testClass)
085: throws NoSuchMethodException, InstantiationException,
086: IllegalAccessException,
087: java.lang.reflect.InvocationTargetException {
088: Test test;
089: if (args.length == 1 && args[0].startsWith("test")) {
090: try {
091: test = (Test) testClass.newInstance();
092: ((TestCase) test).setName(args[0]);
093: } catch (InstantiationException e) {
094: test = (Test) testClass.getConstructor(
095: new Class[] { String.class }).newInstance(
096: new Object[] { args[0] });
097: }
098: } else {
099: try {
100: test = (Test) testClass.getMethod("suite", null)
101: .invoke(null, null);
102: } catch (Exception exc) {
103: test = new TestSuite(testClass);
104: }
105: }
106: return test;
107: }
108:
109: protected static void runTest(Test test) {
110: try {
111: TestRunner runner = new TestRunner(new ResultPrinter(
112: System.out) {
113: public void startTest(Test test) {
114: if (printTestNames)
115: getWriter().print(test.toString());
116: super .startTest(test);
117: }
118:
119: public void endTest(Test test) {
120: super .endTest(test);
121: if (printTestNames)
122: getWriter().println();
123: }
124: });
125: try {
126: TestResult r = runner.doRun(test, false);
127: if (!r.wasSuccessful())
128: System.exit(-1);
129: System.exit(0);
130: } catch (Throwable thr) {
131: System.err.println(thr.getMessage());
132: System.exit(-2);
133: }
134: } catch (Exception exc) {
135: System.err.println(exc.getMessage());
136: System.exit(-2);
137: }
138: }
139:
140: public static void runTests(String[] args, Class testClass) {
141: args = Log.init(args);
142: args = parseArgs(args);
143: try {
144: runTest(collectTests(args, testClass));
145: } catch (Exception e) {
146: System.err.println(e.getMessage());
147: System.exit(-2);
148: }
149: }
150: }
|