/** The whole testsuite ended. */
 public void endTestSuite(JUnitTest suite) throws BuildException {
   rootElement.setAttribute(ATTR_TESTS, "" + suite.runCount());
   rootElement.setAttribute(ATTR_FAILURES, "" + suite.failureCount());
   rootElement.setAttribute(ATTR_ERRORS, "" + suite.errorCount());
   rootElement.setAttribute(ATTR_TIME, "" + (suite.getRunTime() / 1000.0));
   if (out != null) {
     Writer wri = null;
     try {
       wri = new BufferedWriter(new OutputStreamWriter(out, "UTF8"));
       wri.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
       (new DOMElementWriter()).write(rootElement, wri, 0, "  ");
       wri.flush();
     } catch (IOException exc) {
       throw new BuildException("Unable to write log file", exc);
     } finally {
       if (out != System.out && out != System.err) {
         if (wri != null) {
           try {
             wri.close();
           } catch (IOException e) {
             // ignore
           }
         }
       }
     }
   }
 }
예제 #2
0
 /** @since Ant 1.5 */
 public Object clone() {
   try {
     JUnitTest t = (JUnitTest) super.clone();
     t.props = props == null ? null : (Properties) props.clone();
     t.formatters = (Vector) formatters.clone();
     return t;
   } catch (CloneNotSupportedException e) {
     // plain impossible
     return this;
   }
 }
  /** The whole testsuite ended. */
  public void endTestSuite(JUnitTest suite) throws BuildException {
    String newLine = System.getProperty("line.separator");
    StringBuffer sb = new StringBuffer("Testsuite: ");
    sb.append(suite.getName());
    sb.append(newLine);
    sb.append("Tests run: ");
    sb.append(suite.runCount());
    sb.append(", Failures: ");
    sb.append(suite.failureCount());
    sb.append(", Errors: ");
    sb.append(suite.errorCount());
    sb.append(", Time elapsed: ");
    sb.append(nf.format(suite.getRunTime() / 1000.0));
    sb.append(" sec");
    sb.append(newLine);

    // append the err and output streams to the log
    if (systemOutput != null && systemOutput.length() > 0) {
      sb.append("------------- Standard Output ---------------")
          .append(newLine)
          .append(systemOutput)
          .append("------------- ---------------- ---------------")
          .append(newLine);
    }

    if (systemError != null && systemError.length() > 0) {
      sb.append("------------- Standard Error -----------------")
          .append(newLine)
          .append(systemError)
          .append("------------- ---------------- ---------------")
          .append(newLine);
    }

    sb.append(newLine);

    if (out != null) {
      try {
        out.write(sb.toString().getBytes());
        wri.close();
        out.write(inner.toString().getBytes());
        out.flush();
      } catch (IOException ioex) {
        throw new BuildException("Unable to write output", ioex);
      } finally {
        if (out != System.out && out != System.err) {
          try {
            out.close();
          } catch (IOException e) {
          }
        }
      }
    }
  }
  /** The whole testsuite started. */
  public void startTestSuite(JUnitTest suite) {
    doc = getDocumentBuilder().newDocument();
    rootElement = doc.createElement(TESTSUITE);
    rootElement.setAttribute(ATTR_NAME, suite.getName());

    // Output properties
    Element propsElement = doc.createElement(PROPERTIES);
    rootElement.appendChild(propsElement);
    Properties props = suite.getProperties();
    if (props != null) {
      Enumeration e = props.propertyNames();
      while (e.hasMoreElements()) {
        String name = (String) e.nextElement();
        Element propElement = doc.createElement(PROPERTY);
        propElement.setAttribute(ATTR_NAME, name);
        propElement.setAttribute(ATTR_VALUE, props.getProperty(name));
        propsElement.appendChild(propElement);
      }
    }
  }
  /**
   * The testsuite started.
   *
   * @param suite the testsuite.
   */
  public void startTestSuite(JUnitTest suite) {
    String newLine = System.getProperty("line.separator");
    StringBuffer sb = new StringBuffer("Running ");
    sb.append(suite.getName());
    sb.append(newLine);

    try {
      out.write(sb.toString().getBytes());
      out.flush();
    } catch (IOException ioex) {
      throw new BuildException("Unable to write summary output", ioex);
    }
  }
  /**
   * The whole testsuite ended.
   *
   * @param suite the testsuite.
   * @throws BuildException if there is an error.
   */
  public void endTestSuite(JUnitTest suite) throws BuildException {
    String newLine = System.getProperty("line.separator");
    StringBuffer sb = new StringBuffer("Tests run: ");
    sb.append(suite.runCount());
    sb.append(", Failures: ");
    sb.append(suite.failureCount());
    sb.append(", Errors: ");
    sb.append(suite.errorCount());
    sb.append(", Time elapsed: ");
    sb.append(nf.format(suite.getRunTime() / ONE_SECOND));
    sb.append(" sec");
    sb.append(newLine);

    if (withOutAndErr) {
      if (systemOutput != null && systemOutput.length() > 0) {
        sb.append("Output:").append(newLine).append(systemOutput).append(newLine);
      }

      if (systemError != null && systemError.length() > 0) {
        sb.append("Error: ").append(newLine).append(systemError).append(newLine);
      }
    }

    try {
      out.write(sb.toString().getBytes());
      out.flush();
    } catch (IOException ioex) {
      throw new BuildException("Unable to write summary output", ioex);
    } finally {
      if (out != System.out && out != System.err) {
        try {
          out.close();
        } catch (IOException e) {
          // ignore
        }
      }
    }
  }
예제 #7
0
  protected void startTests() {
    final TestSuite suite = new TestSuite();
    final TestResult result = new TestResult();

    final JUnitTest jUnitTest = new JUnitTest("ch.ethz.iks.slp.test");
    jUnitTest.setProperties(System.getProperties());

    // create the xml result formatter
    final JUnitResultFormatter xmlResultFormatter = new XMLJUnitResultFormatter();
    final File file = new File(outputDirectory, "TEST-ch.ethz.iks.slp.test" + ".xml");
    try {
      xmlResultFormatter.setOutput(new FileOutputStream(file));
    } catch (FileNotFoundException e) {
      // may never happen
      e.printStackTrace();
    }
    result.addListener(xmlResultFormatter);
    // create a result formatter that prints to the console
    final JUnitResultFormatter consoleResultFormatter = new BriefJUnitResultFormatter();
    consoleResultFormatter.setOutput(System.out);
    result.addListener(consoleResultFormatter);

    // add the actual tests to the test suite
    Collection collection = new ArrayList();
    collection.add(SelfDiscoveryTest.class);
    for (Iterator iterator = collection.iterator(); iterator.hasNext(); ) {
      Class clazz = (Class) iterator.next();
      // run all methods starting with "test*"
      Method[] methods = clazz.getMethods();
      for (int i = 0; i < methods.length; i++) {
        if (methods[i].getName().startsWith("test")) {
          TestCase testCase;
          try {
            testCase = (TestCase) clazz.newInstance();
            testCase.setName(methods[i].getName());
            suite.addTest(testCase);
          } catch (InstantiationException e) {
            // may never happen
            e.printStackTrace();
          } catch (IllegalAccessException e) {
            // may never happen
            e.printStackTrace();
          }
        }
      }
    }

    // prepare to run tests
    final long start = System.currentTimeMillis();
    xmlResultFormatter.startTestSuite(jUnitTest);
    consoleResultFormatter.startTestSuite(jUnitTest);

    // run tests
    suite.run(result);

    // write stats and close reultformatter
    jUnitTest.setCounts(result.runCount(), result.failureCount(), result.errorCount());
    jUnitTest.setRunTime(System.currentTimeMillis() - start);
    xmlResultFormatter.endTestSuite(jUnitTest);
    consoleResultFormatter.endTestSuite(jUnitTest);

    // print success of failure
    if (result.wasSuccessful()) {
      System.exit(0);
    } else {
      if (result.errorCount() > 0) {
        System.err.println("Errors:");
        for (Enumeration errors = result.errors(); errors.hasMoreElements(); ) {
          TestFailure error = (TestFailure) errors.nextElement();
          System.err.println(error.trace());
        }
      }
      if (result.failureCount() > 0) {
        System.err.println("Failures:");
        for (Enumeration failures = result.failures(); failures.hasMoreElements(); ) {
          TestFailure failure = (TestFailure) failures.nextElement();
          System.err.println(failure.trace());
        }
      }
      System.exit(1);
    }
    ;
  }