⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 midlettestrunner.java

📁 MoMEUnit是一个单元测试的J2ME的应用程序xUnit架构实例。这是来自JUnit框架
💻 JAVA
字号:
package momeunit.runner;import java.util.Timer;import java.util.TimerTask;import java.util.Vector;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.Font;import javax.microedition.midlet.MIDletStateChangeException;import mome.MoXMIDlet;import mome.ext.Utils;import momeunit.framework.TestSuite;/** * MIDletTestRunner is a fully configurable MIDlet. It is intended to run tests * in emulator (e.g. WTK emulator) or mobile device. It shows the results of * tests run (progress bar, statistics, lists of failures or errors with * detailed messages) in one screen. The tests can be run automatically during * start-up (depends on configuration) and re executed more times later. It can * print descriptive messages of failures and errors thrown together with * stack-trace to the standard error output. It can also stop tests execution, * when duration of their run (measured from first test start) exceeds specified * timeout. You can find more details at * {@link momeunit.runner package description}. * <p> * <strong>Note:</strong> It is built upon <strong><a * href="http://momelib.sourceforge.net" target="_blank" >MoMELib</a></strong> * library. This is a J2ME library that offers possibilities to execute commands * in separate from AMS (Application Management Software) callback thread, issue * commands programmatically, associate key (game action) or sequence of keys * and/or game actions with command, use any complimentary argument with command * and much more. If you are interested, visit <a * href="http://momelib.sourceforge.net" target="_blank" >MoMELib Home Page</a> * </p> *  * @author Sergio Morozov * @version 1.1.2 */public class MIDletTestRunner extends MoXMIDlet{  /**   * Selected item foreground color property name.   *    * @since 1.0   */  public static final String SELECTED_FGCOLOR_PROPERTY = "MoMEUnit-SelectedFGColor";  /**   * Selected item background color property name.   *    * @since 1.0   */  public static final String SELECTED_BGCOLOR_PROPERTY = "MoMEUnit-SelectedBGColor";  /**   * Error color property name. Color of progress bar when some tests completed   * with errors.   *    * @since 1.0   */  public static final String ERROR_COLOR_PROPERTY = "MoMEUnit-ErrorColor";  /**   * Failure color property name. Color of progress bar when some tests fail and   * there are no errors.   *    * @since 1.0   */  public static final String FAILURE_COLOR_PROPERTY = "MoMEUnit-FailureColor";  /**   * Ok color property name. Color of progress bar when tests completed   * successfully.   *    * @since 1.0   */  public static final String OK_COLOR_PROPERTY = "MoMEUnit-OkColor";  /**   * Foreground color property name.   *    * @since 1.0   */  public static final String FGCOLOR_PROPERTY = "MoMEUnit-FGColor";  /**   * Background color property name.   *    * @since 1.0   */  public static final String BGCOLOR_PROPERTY = "MoMEUnit-BGColor";  /**   * Font property name.   *    * @since 1.0   */  public static final String FONT_PROPERTY = "MoMEUnit-Font";  /**   * Statistics font property name.   *    * @since 1.0   */  public static final String STATISTICS_FONT_PROPERTY = "MoMEUnit-StatisticsFont";  /**   * Autostart property name.   *    * @since 1.0   */  public static final String AUTO_START_PROPERTY = "MoMEUnit-AutoStart";  /**   * Timeout property name.   *    * @since 1.1.1   */  public static final String TIMEOUT_PROPERTY = "MoMEUnit-TimeOut";  /**   * Print stack-trace flag property name.   *    * @since 1.0   */  public static final String PRINTSTACKTRACE_PROPERTY = "MoMEUnit-PrintStackTrace";  /**   * Print test events flag property name.   *    * @since 1.0   */  public static final String PRINT2STDERR_PROPERTY = "MoMEUnit-Print2StdErr";  /**   * Test suite description property prefix.   *    * @since 1.0   */  public static final String TEST_PROPERTY_PREFIX = "MoMEUnit-Test-";  /**   * Global test suite description property name.   *    * @since 1.0   */  public static final String TESTS_PROPERTY = "MoMEUnit-Tests";  /**   * Global test suite name property name.   *    * @since 1.0   */  public static final String TESTSNAME_PROPERTY = "MoMEUnit-TestsName";  /**   * Global test suite default name.   *    * @since 1.0   */  public static final String DEFAULT_SUITE_NAME = "Tests";  /**   * Default autostart.   */  private static final boolean DEFAULT_AUTOSTART = true;  /**   * Exit command.   */  private static final Command EXIT = new Command("Exit", "Exit Runner",      Command.EXIT, 1);  /**   * Test command.   */  private static final Command TEST = new Command("Run", "Run Tests",      Command.SCREEN, 32);  /**   * Global test suite.   */  private TestSuite suite = null;  /**   * Test screen canvas   */  private TestScreen main = null;  /**   * Sutostart flag.   */  private boolean autoStart = DEFAULT_AUTOSTART;  private Timer timer = null;  private TimerTask timeoutTask = null;  private String timeoutProp = null;  private long timeout = -1;  /**   * Generates {@link TestSuite} of given name that contains tests specified as   * string. Tests in the list can be afully specified class name as argument to   * {@link Class#forName(String)} or name of test suite as specified by   * respective configuration property. See   * {@link momeunit.runner#config package description} for more details.   *    * @param name   *          name of test suite to generate.   * @param tests   *          list of tests that constitute test suite.   * @param usedTests   *          test already added an this branch. Used to detect cycles.   * @return TestSuite generated.   */  private TestSuite generateTestSuite(String name, String tests,      Vector usedTests)  {    TestSuite res = new TestSuite(name);    if (tests != null)    {      char[] cTests = tests.toCharArray();      int end = cTests.length;      while (--end >= 0 && Utils.isTestSeparator(cTests[end]));      for (int start = end; start > 0; end = start)      {        for (; start >= 0 && !Utils.isTestSeparator(cTests[start]); start--);        String test = String.valueOf(cTests, start + 1, end - start);        String testTests = this.getAppProperty(TEST_PROPERTY_PREFIX + test);        if (testTests == null) res.addTest(test);        else        {          if (usedTests == null) usedTests = new Vector();          if (usedTests.contains(test)) throw new IllegalStateException(              "Test cycle detected: \"" + test + "\"");          usedTests.addElement(test);          res.addTest(this.generateTestSuite(test, testTests, usedTests));          usedTests.removeElement(test);        }        while (--start >= 0 && Utils.isTestSeparator(cTests[start]));      }    }    return res;  }  /**   * Generates global test suite, instantiates test screen canvas.   *    * @see mome.MoXMIDlet#initApp()   * @since 1.0   */  protected void initApp() throws MIDletStateChangeException  {    super.initApp();    String prop = this.getAppProperty(AUTO_START_PROPERTY);    if (prop != null) this.autoStart = Utils.parseBoolean(prop);    prop = this.getAppProperty(TESTSNAME_PROPERTY);    if (prop == null) prop = DEFAULT_SUITE_NAME;    setTimeOut();    this.suite = this.generateTestSuite(prop, this        .getAppProperty(TESTS_PROPERTY), null);    this.main = new TestScreen(this.suite.getName(), Utils.parseBoolean(this        .getAppProperty(PRINT2STDERR_PROPERTY)), Utils.parseBoolean(this        .getAppProperty(PRINTSTACKTRACE_PROPERTY)));    this.initColors();    this.initFonts();    this.main.clearResult(this.suite.countTestCases());    this.main.setCommandListener(this);    this.main.addCommand(EXIT);    this.main.addCommand(TEST);  }  protected void setTimeOut()  {    this.timeoutProp = getAppProperty(TIMEOUT_PROPERTY);    if (this.timeoutProp != null)    {      this.timeout = Utils.parseTimeInterval(this.timeoutProp);      if (this.timeout > 0)      {        this.timer = new Timer();        this.timeoutTask = null;      } else System.err.println("Invalid value of property \""          + TIMEOUT_PROPERTY + "\" specified " + this.timeoutProp          + "\nExecuting without any timeout checking.");    }  }  /**   * Configures colors of test screen canvas.   */  private void initColors()  {    String prop = this.getAppProperty(BGCOLOR_PROPERTY);    if (prop != null) this.main.setBgColor(Utils.parseColor(prop));    prop = this.getAppProperty(FGCOLOR_PROPERTY);    if (prop != null) this.main.setFgColor(Utils.parseColor(prop));    prop = this.getAppProperty(SELECTED_BGCOLOR_PROPERTY);    if (prop != null) this.main.setSelBgColor(Utils.parseColor(prop));    prop = this.getAppProperty(SELECTED_FGCOLOR_PROPERTY);    if (prop != null) this.main.setSelFgColor(Utils.parseColor(prop));    prop = this.getAppProperty(OK_COLOR_PROPERTY);    if (prop != null) this.main.setOkColor(Utils.parseColor(prop));    prop = this.getAppProperty(FAILURE_COLOR_PROPERTY);    if (prop != null) this.main.setFailureColor(Utils.parseColor(prop));    prop = this.getAppProperty(ERROR_COLOR_PROPERTY);    if (prop != null) this.main.setErrorColor(Utils.parseColor(prop));  }  /**   * Configures fonts of test screen canvas.   */  private void initFonts()  {    Font font = Utils.parseFont(this.getAppProperty(FONT_PROPERTY));    if (font != null) this.main.setFont(font);    font = Utils.parseFont(this.getAppProperty(STATISTICS_FONT_PROPERTY));    if (font != null) this.main.setStatisticsFont(font);  }  /**   * Makes the test screen current and starts tests execution if   * <code>autostart</code> flag is <code>true</code>.   *    * @see mome.MoXMIDlet#startApp()   * @since 1.0   */  protected void startApp() throws MIDletStateChangeException  {    super.startApp();    this.getDisplay().setCurrent(this.main);    if (this.autoStart) this.pushCommand(TEST, this.main);  }  /*   * (non-Javadoc)   *    * @see mome.MoXMIDlet#xCommandAction(java.lang.Object, java.lang.Object)   */  public void xCommandAction(Object cmd, Object src)  {    if (cmd == TEST)    {      this.main.clearResult(this.suite.countTestCases());      if (this.timer != null) this.timer.schedule(          this.timeoutTask = new TimerTask()            {              public void run()              {                if (Character.isDigit(timeoutProp                    .charAt(timeoutProp.length() - 1))) timeoutProp = timeoutProp                    + "ms";                System.err.println("\"" + suite.getName()                    + "\" tests execution timeouts after " + timeoutProp                    + " at test " + main.getLastTestName() + ".");                timeoutTask = null;                exit();              }            }, this.timeout);      this.suite.run(this.main.getTestResult());      if (this.timeoutTask != null)      {        this.timeoutTask.cancel();        this.timeoutTask = null;      }    } else if (cmd == EXIT) this.exit();  }}

⌨️ 快捷键说明

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