momeunittask.java

来自「MoMEUnit是一个单元测试的J2ME的应用程序xUnit架构实例。这是来自J」· Java 代码 · 共 1,807 行 · 第 1/4 页

JAVA
1,807
字号
package org.momeunit.ant.taskdefs;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.util.Enumeration;import java.util.HashSet;import java.util.Iterator;import java.util.Properties;import java.util.Set;import java.util.Vector;import java.util.jar.JarFile;import momeunit.framework.Test;import org.apache.tools.ant.AntClassLoader;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.Project;import org.apache.tools.ant.Task;import org.apache.tools.ant.taskdefs.Expand;import org.apache.tools.ant.taskdefs.Jar;import org.apache.tools.ant.taskdefs.Manifest;import org.apache.tools.ant.taskdefs.ManifestException;import org.apache.tools.ant.taskdefs.Zip.Duplicate;import org.apache.tools.ant.types.FileSet;import org.apache.tools.ant.types.Path;import org.apache.tools.ant.types.ZipFileSet;import org.apache.tools.ant.util.ClasspathUtils;import org.apache.tools.ant.util.LoaderUtils;import org.momeunit.ant.core.OutputLogger;import org.momeunit.ant.core.OutputToString;import org.momeunit.ant.core.TestListHolder;import org.momeunit.ant.core.Utility;import org.momeunit.ant.emulator.Emulator;import org.momeunit.ant.event.TestEventsFilter;import org.momeunit.ant.formatter.TestErrorIndicator;import org.momeunit.ant.formatter.UnitResultFormatter;import org.momeunit.ant.jad.JAD;import org.momeunit.ant.jad.MIDletInfo;import org.momeunit.ant.preverifier.Preverifier;/** * MoMEUnit task intended to run tests from the MoMEUnit testing framework. It * runs tests from specified (via <code>appjar</code> attribute) built J2ME * application or builds test application on behalf of the user. * </p> * <p> * It creates the list of test cases to run from classes of specified J2ME * application or classes and/or java source files specified via tests * descriptions. Test cases included are classes and/or java source files that * implement {@link Test} interface and/or match specified pattern. There is no * need to specify tests explicitly. The format of pattern resembles ant path * pattern. Instead of slash (<code>'/'</code>), dot (<code>'.'</code>) * is used. It looks like ant package pattern. ( e.g. * <code>&quot;**.*Test&quot;</code> matches any class that ends with Test at * any package, <code>&quot;test.**.*&quot;</code> matches any class below * test package, <code>&quot;**.*&quot;</code> matches all classes). * </p> * <p> * Tests can be described by using <code>&lt;testdir&gt;</code>, * <code>&lt;tesjar&gt;</code> nested tags. As <code>&lt;momeunit&gt;</code> * task is an implicit test, test cases to run can be described using * <code>testdir</code>, <code>testtype</code> and <code>testjar</code> * attributes. * </p> * <p> * Results of tests run can be formatted by predefined or custom formatters * (like in <code>&lt;junit&gt;</code> task). There are two predefined * formatters <code>&quot;brief&quot;</code> and <code>&quot;xml&quot;</code>. * These formatters fully resemble their <code>junit</code> analogs. * <code>xml</code> formatter produces report files of the same format as it's * <code>&lt;junit&gt;</code> analog. Because of this, it is possible to use * <code>&lt;junitreport&gt;</code> task to further process report files. * </p> *  * @author Sergio Morozov * @version 1.1.2 */public class MoMEUnitTask extends Task{  /**   * Default test suite name.   *    * @since 1.1   */  public static final String DEFAULT_TESTS_NAME = "app";  /**   * Name of system java property that designates temporary directory.   *    * @since 1.1   */  public static final String TEMPDIR_JAVA_SYSPROPERTY = "java.io.tmpdir";  /**   * Name of ant property that designates temporary dir   *    * @since 1.1   */  public static final String TEMPDIR_PROPERTY = "tmp.dir";  /**   * Max value of random part of momeunit temp directory name.   *    * @since 1.1   */  public static final int MOMEUNIT_TMPDIR_RND_MAX = 99999;  /**   * Prefix of momeunit temp directory name.   *    * @since 1.1   */  public static final String MOMEUNIT_TMPDIR_PREFIX = "mu";  /**   * Default type of runner.   *    * @since 1.1   */  public static final String DEFAULT_RUNNER_TYPE = "ant";  /**   * Default jad encoding.   *    * @since 1.1   */  public static final String DEFAULT_JAD_ENCODING = "utf-8";  /**   * Name of environment variable that designates default WTk home directory.   *    * @since 1.1   */  public static final String WTK_HOME_ENV = "WTK_HOME";  /**   * Name of ant property that designates default WTK home directory.   *    * @since 1.1   */  public static final String WTK_HOME_PROPERTY = "wtk.home";  /**   * Name of appproperty that specifies tests to be run.   */  private static final String MOMEUNIT_TESTS_KEY = "MoMEUnit-Tests";  /**   * Name of appproperty that specifies global test suite name.   */  private static final String MOMEUNIT_TESTSNAME_KEY = "MoMEUnit-TestsName";  private static final String MOMEUNIT_TIMEOUT_KEY = "MoMEUnit-TimeOut";  /**   * HaltOnError property name.   *    * @since 1.1.2   */  private static final String MOMEUNIT_HALTONERROR_KEY = "MoMEUnit-HaltOnError";  /**   * HaltOnFailure property name.   *    * @since 1.1.2   */  private static final String MOMEUNIT_HALTONFAILURE_KEY = "MoMEUnit-HaltOnFailure";  /**   * Default CLDC version.   *    * @since 1.1   */  public static final String DEFAULT_CONFIGURATION = "1.1";  /**   * Default MIDP version.   *    * @since 1.1   */  public static final String DEFAULT_PROFILE = "2.0";  /**   * cldc version.   */  private String config = DEFAULT_CONFIGURATION;  /**   * midp version.   */  private String profile = DEFAULT_PROFILE;  /**   * Temp dir.   */  private File tmpDir = null;  /**   * Wtk home dir.   */  private File wtkHome = null;  /**   * List of files to delelte. Made because of invalid implementation of   * {@link File#deleteOnExit()} of some jvms (kaffe).   */  private Vector filesToDelete = new Vector(); // <File>  /**   * MoMEUnit results formatters.   */  private Set formatters = null;  /**   * Flag that sets formatters to listen on std out for test events.   */  private boolean listenOnOut = true;  /**   * Flag that sets formatters to listen on std err for test events.   */  private boolean listenOnErr = true;  /**   * Test suite name.   */  private String testsName = DEFAULT_TESTS_NAME;  /**   * Directory where to generate reports.   */  private File reportDir = null;  /**   * Report files basename.   */  private String reportFile = null;  /**   * Flag that indicates Whether to direct output to ant log.   */  private boolean showOutput = false;  /**   * Flag that indicates Whether to fail build on error.   */  private HaltOnType haltOnError = new HaltOnType("false");  /**   * Flag that indicates Whether to fail build on failure.   */  private HaltOnType haltOnFailure = new HaltOnType("false");  /**   * Maximum duration of tests execution.   */  private String timeout = null;  /**   * Name of property to set on error.   */  private String errorProperty = null;  /**   * Name of property to set on failure.   */  private String failureProperty = null;  /**   * Filesets to be included in built app.   */  private Set libs = null;  /**   * Pattern of implicit test and appjar.   */  private String pattern = null;  /**   * Default pattern for implicit ,given tests and appjar.   */  private String defaultPattern = TestElement.DEFAULT_PATTERN;  /**   * classpath delegate.   */  private ClasspathUtils.Delegate cpDelegate = null;  private AntClassLoader loader = null;  /**   * Test list holder.   */  private TestListHolder testListHolder = null;  /**   * Flag that indicates whether tests must implement   * {@link momeunit.framework.Test} interfave.   */  private boolean onlyTests = true;  /**   * Set of specified tests.   */  private Set tests = null;  /**   * Implicit test holder.   */  private TestElement implicitTest = null;  /**   * App name.   */  private String appName = null;  /**   * Preverify attribute.   */  private PreverifyType preverify = new PreverifyType("on");  /**   * Emulator specification.   */  private EmulatorElement emulator = null;  /**   * Preverifier specification.   */  private PreverifierElement preverifier = null;  /**   * Compiler.   */  private Compiler compiler = null;  /**   * RunnerType type.   */  private RunnerType runnerType = new RunnerType(DEFAULT_RUNNER_TYPE);  /**   * Flag that indicates Whether to delete app after test run.   */  private boolean delOnExit = true;  /**   * Flag that indicates Whether delonexit was specified.   */  private boolean delOnExitSet = false;  /**   * App jad.   */  private JAD jad = null;  /**   * Set of jad nested tags.   */  private Set jadElements = null;  /**   * App jad encoding.   */  private String jadEncoding = DEFAULT_JAD_ENCODING;  /**   * file of app to test.   */  private File appJar = null;  /**   * dir where to build app.   */  private File appDir = null;  /**   * Flag that indicates Whether to print summary.   */  private SummaryAttribute printSummary = new SummaryAttribute("off");  /**   * FileSet of momeuint-ant lib.   */  private File MULib = null;  /**   * Creates momeunit task.   *    * @since 1.1   */  public MoMEUnitTask()  {    super();    this.libs = new HashSet();    this.tests = new HashSet();    this.jadElements = new HashSet();    this.formatters = new HashSet();    this.cpDelegate = ClasspathUtils.getDelegate(this);  }  /**   * Returns configured classpath. Classpath is configured by classpath, libdir,   * libjar, testdir, testjar nested tags and/or classpath, classpathref,   * testdir, testjar, appjar attributes. <strong>Note:</strong> Wtk midp and   * cldc libraries are added by default.   *    * @return configured classpath   */  private Path getClasspath()  {    Path res = this.cpDelegate.getClasspath();    return res != null ? res : this.cpDelegate.createClasspath();  }  /**   * Executes task.   *    * @see org.apache.tools.ant.Task#execute()   * @since 1.1   */  public void execute() throws BuildException  {    try    {      log(getTaskType() + " task started", Project.MSG_VERBOSE);      addDefaultClasspath();      initAppDir();      if (this.appJar == null)      {        log("No appJar specified. Building application.", Project.MSG_VERBOSE);        addImplicitTest();        addTestsToLibsAndClasspath();        compile();      } else if (this.implicitTest != null || this.tests.size() > 0          || this.libs.size() > 0) log(          "As appjar attribute is set <testdir>,<testjar>,<libdir,<libjar> nested tags and "              + "testdir, testjar, testtype attributes of <"              + getTaskType()              + "> are ignored.", Project.MSG_WARN);      setCPClassLoader();      calculateTestList();      initJAD();      if (this.appJar == null)      {        this.appJar = new File(this.appDir, this.getAppName() + ".jar");        if (this.delOnExit) this.filesToDelete.add(this.appJar);        makeJAR(this.appJar);      }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?