001: /*
002: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package test.org.mandarax.testsupport;
019:
020: import org.apache.log4j.BasicConfigurator;
021:
022: /**
023: * Utility class to build test runner. For a detailed description
024: * how it works, read the comment for run(Class,String[])
025: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
026: * @version 3.4 <7 March 05>
027: * @since 1.2
028: */
029: public class TestRunner {
030:
031: public static String AWT = "awt";
032: public static String TEXT = "text";
033: public static String RELOAD = "reload";
034:
035: /**
036: * Check whether the parameter list contains a certain string.
037: * @return the result of the check
038: * @param pars the parameter list
039: * @param par the string
040: */
041: private static boolean check(String[] pars, String par) {
042: for (int i = 0; i < pars.length; i++) {
043: if (pars[i].equalsIgnoreCase(par)) {
044: return true;
045: }
046: }
047:
048: return false;
049: }
050:
051: /**
052: * Run a test suite. suite is a class that has a static suite() method returning a test suite.
053: * The parameters determine how the test should be performed.
054: * By default, the swing test runner (junit.swingui.TestRunner) is used.
055: * If one parameter equals "awt", we use the awt based test runner, if "text" is specified, we use
056: * the text based swing runner. By default, classes are not reloaded.
057: * @param suite a test suite
058: * @param pars java.lang.String[]
059: */
060: public static void run(Class suite, String[] pars) {
061: run(suite, pars, true);
062: }
063:
064: /**
065: * Run a test suite. suite is a class that has a static suite() method returning a test suite.
066: * The parameters determine how the test should be performed.
067: * By default, the swing test runner (junit.swingui.TestRunner) is used.
068: * If one parameter equals "awt", we use the awt based test runner, if "text" is specified, we use
069: * the text based swing runner. By default, classes are not reloaded.
070: * @param suite a test suite
071: * @param pars java.lang.String[]
072: * @param initLog whether to initialize log4j
073: */
074: public static void run(Class suite, String[] pars, boolean initLog) {
075: if (initLog)
076: BasicConfigurator.configure();
077:
078: boolean reload = check(pars, RELOAD);
079:
080: String[] args;
081: // reloading will only be supported from the ui versions
082: if (reload || check(pars, TEXT)) {
083: // default is reload
084: args = new String[] { suite.getName() };
085: } else {
086: // from JUnit 3.7 on, reloading can be specified as an argument
087: args = new String[] { suite.getName(), "-noloading" };
088: }
089:
090: if (check(pars, AWT)) {
091: junit.awtui.TestRunner.main(args);
092: } else {
093: if (check(pars, TEXT)) {
094: junit.textui.TestRunner.main(args);
095: } else {
096: junit.swingui.TestRunner.main(args);
097: }
098: }
099: }
100: }
|