Runner.java :  » Testing » MockCreator » net » sf » mockcreator » Java Open Source

Java Open Source » Testing » MockCreator 
MockCreator » net » sf » mockcreator » Runner.java
package net.sf.mockcreator;

import junit.framework.Test;
import junit.framework.TestResult;
import junit.framework.TestSuite;

import junit.textui.TestRunner;

import java.io.File;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
 * Scans directories to find *Test.java files and
 * adds them into list of tests; if class name is passed as CLI argument,
 * performs that test only
 *
 * @author dozen
 */
public class Runner extends TestSuite {
    static String[] args = null;

    /**
     * Main entry point for CLI run.
     *
     * @param args Contains test names if any.
     */
    public static void main(String[] a) {
        args = a;
        ListenedRunner.run(suite());
        System.exit(ListenedRunner.tr.wasSuccessful() ? 0 : 1);
    }

    /**
     * Forms testsuite to run
     *
     * @param List of test classes.
     *
     * @return The suite
     */
    public static Test suite() {
        Class[] params = new Class[] { String.class };
        Object[] constructparams = new Object[1];

        TestSuite suite = new TestSuite();

        List lst = new ArrayList();

        if ((args != null) && (args.length > 0)) {
            lst.add(args[0]);
        } else {
            lst = scan(".", "", lst);
        }

        // iterate all passed/found tests
        Iterator tit = lst.iterator();

        while (tit.hasNext()) {
            String test = (String) tit.next();

            // add the test; ignore missed
            try {
                Class testCase = Class.forName(test);
                suite.addTestSuite(testCase);
            } catch (ClassNotFoundException ex) {
                System.err.println("Test class was not found: " + test);
            }
        }

        return suite;
    }

    /**
     * Scans given directory for Test*.java files.
     *
     * @param dir Directory Name
     * @param name File name or subdir name
     * @return List of collected names
     */
    private static List scan(String dir, String name, List lst) {
        String base = dir + "/" + name;

        File fil = new File((base == null) ? "." : base);

        if (fil.isDirectory()) {
            // scan directory for files
            String[] names = fil.list();

            for (int i = 0; i < names.length; i++) {
                scan(base, names[i], lst);
            }
        } else {
            if ((name.indexOf("Test") >= 0) && name.endsWith(".class") &&
                    (name.indexOf("$") < 0) &&
                    (name.indexOf("TestCase.class") < 0)) {
                // found a test; store it in Java package notation
                String toadd = base;

                while ((toadd.charAt(0) == '.') || (toadd.charAt(0) == '/')) {
                    toadd = toadd.substring(1, toadd.length());
                }

                toadd = toadd.replace('/', '.');
                toadd = toadd.substring(0, toadd.lastIndexOf(".class"));
                lst.add(toadd);
            }
        }

        return lst;
    }

    /**
     * Prints usage instructions.
     */
    public static void Usage() {
        System.out.println("Usage:");
        System.out.println("junit.textui.TestRunner de.abstract.Suite <Tests>");
    }

    public static class ListenedRunner extends junit.textui.TestRunner {
        public static TestResult tr;

        protected TestResult createTestResult() {
            if (tr == null) {
                tr = new TestResult();
            }

            return tr;
        }

        // dozen: I hate the way JUnit provides so-called extensibility >:E
        static public TestResult run(Test test) {
            TestRunner runner = new ListenedRunner();

            return runner.doRun(test);
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.