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

📄 testscreen.java

📁 MoMEUnit是一个单元测试的J2ME的应用程序xUnit架构实例。这是来自JUnit框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  /**   * Sets whether test failure and error events should be printed to standard   * error output.   *    * @param printToStdErr   *          <code>true</code> if test failure and error events should be   *          printed to standard error output.   * @since 1.0   */  public void setPrintToStdErr(boolean printToStdErr)  {    this.printToStdErr = printToStdErr;  }  /**   * Returns the TestResult contained in TestScreen.   *    * @return TestResult instance contained in TestScreen.   * @since 1.0   */  public TestResult getTestResult()  {    return this.result;  }  /**   * Checks if test failure and error stack-trace should be printed to standard   * error output.   *    * @return <code>true</code> if test failure and error stack-trace should be   *         printed to standard error output, <code>false</code> otherwise.   * @since 1.0   */  public boolean isPrintStackTrace()  {    return this.printStackTrace;  }  /**   * Sets whether test failure and error stack-trace should be printed to   * standard error output.   *    * @param printStackTrace   *          <code>true</code> if test failure and error stack-trace should   *          be printed to standard error output.   * @since 1.0   */  public void setPrintStackTrace(boolean printStackTrace)  {    this.printStackTrace = printStackTrace;  }  /**   * Prepares TestScreen to a new tests execution.   *    * @param testCasesNumber   *          number of test cases.   * @since 1.0   */  public void clearResult(int testCasesNumber)  {    if (testCasesNumber < 0) throw new IllegalArgumentException(        "testCasesNumber < 0 : " + testCasesNumber);    this.lastTest = null;    this.result = new TestResult();    this.result.setListener(this);    this.testCasesNumber = testCasesNumber;    this.hasFailures = false;    this.failTests.deleteAll();    this.hasErrors = false;    this.errorTests.deleteAll();    this.show();    this.repaint();  }  /**   * Shows a progress bar at specified position and size.   *    * @param x   * @param y   * @param width   * @param height   * @since 1.0   */  private void showProgressBar(int x, int y, int width, int height)  {    if (this.testCasesNumber > 0)    {      int color;      if (this.hasErrors) color = this.errorColor;      else if (this.hasFailures) color = this.failureColor;      else color = this.okColor;      this.g.setColor(color);      this.g.fillRect(x, y, width * this.result.runCount()          / this.testCasesNumber, PROGRESSBAR_HEIGHT);    }  }  /**   * Shows statistics of tests run at specified position.   *    * @param y   * @since 1.0   */  private void showStatistics(int y)  {    this.g.setFont(statisticsFont);    this.g.setColor(this.fgColor);    String testStr = TESTS_COUNT_PREFIX + this.result.runCount();    int testStrLength = statisticsFont.stringWidth(testStr);    String fStr = FAILURES_COUNT_PREFIX + this.result.failureCount();    int fStrLength = statisticsFont.stringWidth(fStr);    String eStr = ERRORS_COUNT_PREFIX + this.result.errorCount();    int eStrLength = statisticsFont.stringWidth(eStr);    int indent = (this.width - testStrLength - fStrLength - eStrLength) / 6;    if (indent < 0)    {      testStr = Integer.toString(this.result.runCount());      testStrLength = statisticsFont.stringWidth(testStr);      fStr = Integer.toString(this.result.failureCount());      fStrLength = statisticsFont.stringWidth(fStr);      eStr = Integer.toString(this.result.errorCount());      eStrLength = statisticsFont.stringWidth(eStr);      indent = (this.width - testStrLength - fStrLength - eStrLength) / 6;    }    int x = 0;    g.drawString(testStr, x += indent, y, TOP_LEFT);    this.g.setColor(this.failureColor);    x += (indent + testStrLength);    if (this.failureListType && this.hasFailures)    {      this.g.fillRect(x, y, fStrLength + 2 * indent, statisticsFontHeight);      this.g.setColor(this.bgColor);    }    g.drawString(fStr, x += indent, y, TOP_LEFT);    this.g.setColor(this.errorColor);    x += (indent + fStrLength);    if (!this.failureListType && this.hasErrors)    {      this.g.fillRect(x, y, eStrLength + 2 * indent, statisticsFontHeight);      this.g.setColor(this.bgColor);    }    g.drawString(eStr, x += indent, y, TOP_LEFT);  }  /**   * Return if problem encountered while executing selected test is a failure or   * an error.   *    * @return <code>true</code> if problem encountered while executing selected   *         test is a failure <code>false</code> otherwise.   * @since 1.0   */  public boolean getFailureListType()  {    return this.failureListType;  }  /**   * Sets type of tests list to display.   *    * @param failureListType   *          if <code>true</code> shows list of tests that fails if there are   *          some, list of error tests if there are some otherwise.   * @since 1.0   */  public void setFailureListType(boolean failureListType)  {    if (failureListType)    {      if (this.hasFailures)      {        this.failureListType = true;        this.show();        this.repaint();      }    } else if (this.hasErrors)    {      this.failureListType = false;      this.show();      this.repaint();    }  }  /**   * Returns error thrown while executing selected test.   *    * @return error thrown while executing selected test.   * @since 1.0   */  public Throwable getSelectedError()  {    Throwable res = null;    if (this.failureListType)    {      if (this.hasFailures) res = (Throwable) this.failures          .elementAt(this.failTests.getSelectedIndex());    } else if (this.hasErrors) res = (Throwable) this.errors        .elementAt(this.errorTests.getSelectedIndex());    return res;  }  /**   * Shows list o tests.   *    * @since 1.0   */  private void showTests()  {    if (this.hasErrors || this.hasFailures) (this.failureListType ? this.failTests        : this.errorTests).show(this.g);  }  /**   * Shows separator.   *    * @param y   * @since 1.0   */  private void showSeparator(int y)  {    int color = this.bgColor;    if (this.hasErrors || this.hasFailures) color = this.failureListType ? this.failureColor        : this.errorColor;    g.setColor(color);    g.drawLine(MARGIN, y, this.width - MARGIN - MARGIN, y);  }  /**   * Shows descriptive message of selected test.   *    * @param x   * @param y   * @since 1.0   */  private void showMessage(int x, int y)  {    Throwable t = this.getSelectedError();    if (t != null)    {      g.setColor(this.fgColor);      g.setFont(this.font);      this.msgWrapper.setString(getMessage(t));      for (; this.msgWrapper.hasMoreElements() && y < this.height; y += fontHeight)        g.drawString((String) this.msgWrapper.nextElement(), x, y, TOP_LEFT);    }  }  /**   * Returns descriptive message based on exception thrown.   *    * @param ex   *          exception thrown.   * @return descriptive message based on exception thrown.   * @since 1.0   */  public static String getMessage(Throwable ex)  {    return ((!(ex instanceof AssertionFailedError)) ? ex.getClass().getName()        + ": " : "")        + ex.getMessage();  }  /**   * Shows screen.   *    * @since 1.0   */  private void show()  {    g.setColor(this.bgColor);    g.fillRect(0, 0, this.width, this.height);    int y = 0;    this.showProgressBar(MARGIN, y, this.width - MARGIN - MARGIN,        PROGRESSBAR_HEIGHT);    this.showStatistics(y += PROGRESSBAR_HEIGHT);    this.showTests();    this        .showSeparator(y += (statisticsFontHeight + this.failTests.getHeight() + 2));    this.showMessage(0, y += 2);  }  /*   * (non-Javadoc)   *    * @see momeunit.framework.TestListener#addError(momeunit.framework.Test,   *      java.lang.Throwable)   */  public void addError(Test test, Throwable t)  {    this.hasErrors = true;    if (!this.hasFailures) this.failureListType = false;    this.errorTests.append(((TestCase) test).getName(), this.font);    this.errors.addElement(t);    if (this.printToStdErr)    {      System.err.println("Error processing test \""          + ((TestCase) test).getName() + "\"\n  " + t.getClass().getName()          + ": " + t.getMessage());      if (this.printStackTrace) t.printStackTrace();    }  }  /*   * (non-Javadoc)   *    * @see momeunit.framework.TestListener#addFailure(momeunit.framework.Test,   *      momeunit.framework.AssertionFailedError)   */  public void addFailure(Test test, AssertionFailedError t)  {    this.hasFailures = true;    this.failureListType = true;    this.failTests.append(((TestCase) test).getName(), this.font);    this.failures.addElement(t);    if (this.printToStdErr)    {      System.err.println("Test \"" + test + "\" failed:\n  " + t.getMessage());      if (this.printStackTrace) t.printStackTrace();    }  }  /*   * (non-Javadoc)   *    * @see momeunit.framework.TestListener#endTest(momeunit.framework.Test)   */  public void endTest(Test test)  {    this.show();    this.repaint();  }  /*   * (non-Javadoc)   *    * @see momeunit.framework.TestListener#startTest(momeunit.framework.Test)   */  public void startTest(Test test)  {    this.lastTest = ((TestCase) test).getName();  }  /**   * Returns name of last started test.   *    * @return the name of last started test.   * @since 1.1.1   */  public String getLastTestName()  {    return this.lastTest;  }  /*   * (non-Javadoc)   *    * @see javax.microedition.lcdui.Canvas#keyPressed(int)   */  protected void keyPressed(int key)  {    switch (this.getGameAction(key))    {    case Canvas.LEFT:    case Canvas.RIGHT:      this.setFailureListType(!this.failureListType);      break;    case Canvas.DOWN:      ListShow ls = (this.failureListType) ? this.failTests : this.errorTests;      if (ls.next())      {        this.show();        this.repaint();      }      break;    case Canvas.UP:      ls = (this.failureListType) ? this.failTests : this.errorTests;      if (ls.prev())      {        this.show();        this.repaint();      }      break;    case Canvas.FIRE:      Throwable failure = this.getSelectedError();      if (failure != null)      {        System.err.println(getMessage(failure));        if (this.printStackTrace) failure.printStackTrace();      }      break;    }  }}

⌨️ 快捷键说明

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