TestRunner.java :  » Rule-Engine » Mandarax » test » org » mandarax » testsupport » Java Open Source

Java Open Source » Rule Engine » Mandarax 
Mandarax » test » org » mandarax » testsupport » TestRunner.java
/*
 * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package test.org.mandarax.testsupport;

import org.apache.log4j.BasicConfigurator;

/**
 * Utility class to build test runner. For a detailed description
 * how it works, read the comment for run(Class,String[])
 * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
 * @version 3.4 <7 March 05>
 * @since 1.2
 */
public class TestRunner {

  public static String AWT = "awt";
  public static String TEXT = "text";
  public static String RELOAD = "reload";

  /**
   * Check whether the parameter list contains a certain string.
   * @return the result of the check
   * @param pars the parameter list
   * @param par the string
   */
  private static boolean check(String[] pars, String par) {
    for (int i = 0; i < pars.length; i++) {
      if (pars[i].equalsIgnoreCase(par)) {
        return true;
      }
    }

    return false;
  }

  /**
   * Run a test suite. suite is a class that has a static suite() method returning a test suite.
   * The parameters determine how the test should be performed.
   * By default, the swing test runner (junit.swingui.TestRunner) is used.
   * If one parameter equals "awt", we use the awt based test runner, if "text" is specified, we use
   * the text based swing runner. By default, classes are not reloaded.
   * @param suite a test suite
   * @param pars java.lang.String[]
   */
  public static void run(Class suite, String[] pars) {
    run(suite,pars,true);
  }
  /**
    * Run a test suite. suite is a class that has a static suite() method returning a test suite.
    * The parameters determine how the test should be performed.
    * By default, the swing test runner (junit.swingui.TestRunner) is used.
    * If one parameter equals "awt", we use the awt based test runner, if "text" is specified, we use
    * the text based swing runner. By default, classes are not reloaded.
    * @param suite a test suite
    * @param pars java.lang.String[]
    * @param initLog whether to initialize log4j
    */
  public static void run(Class suite, String[] pars, boolean initLog) {
    if (initLog) BasicConfigurator.configure();

    boolean reload = check(pars, RELOAD);

    String[] args;
    // reloading will only be supported from the ui versions
    if (reload || check(pars, TEXT)) {
      // default is reload
      args = new String[] { suite.getName()};
    } else {
      // from JUnit 3.7 on, reloading can be specified as an argument
      args = new String[] { suite.getName(), "-noloading" };
    }

    if (check(pars, AWT)) {
      junit.awtui.TestRunner.main(args);
    } else {
      if (check(pars, TEXT)) {
        junit.textui.TestRunner.main(args);
      } else {
        junit.swingui.TestRunner.main(args);
      }
    }
  }
}
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.