Beispiel #1
0
  protected static void writeTestCase(Writer out, TestCase result) throws IOException {
    long time = result.stop_time - result.start_time;
    // StringBuilder sb=new StringBuilder();
    out.write("\n    <testcase classname=\"" + result.classname);
    out.write("\"  name=\"" + result.name + "\" time=\"" + (time / 1000.0) + "\">");

    switch (result.status) {
      case ITestResult.FAILURE:
        String failure =
            writeFailure("failure", result.failure_type, result.failure_msg, result.stack_trace);
        if (failure != null) out.write(failure);
        break;
      case ITestResult.SKIP:
        failure =
            writeFailure("error", result.failure_type, result.failure_msg, result.stack_trace);
        if (failure != null) out.write(failure);
        break;
    }
    out.write("\n    </testcase>\n");
  }
Beispiel #2
0
  protected static void process(File dir) throws IOException {
    File file = new File(dir, TESTS);
    if (!file.exists()) throw new IOException(file + " not found");
    List<TestCase> test_cases = new ArrayList<>();
    DataInputStream input = new DataInputStream(new FileInputStream(file));
    try {
      for (; ; ) {
        TestCase test_case = new TestCase();
        try {
          test_case.readFrom(input);
          test_cases.add(test_case);
        } catch (Exception e) {
          break;
        }
      }
    } finally {
      Util.close(input);
    }

    if (test_cases.isEmpty()) return;

    Reader stdout_reader = null, stderr_reader = null;
    File tmp = new File(dir, STDOUT);
    if (tmp.exists() && tmp.length() > 0) stdout_reader = new FileReader(tmp);

    tmp = new File(dir, STDERR);
    if (tmp.exists() && tmp.length() > 0) stderr_reader = new FileReader(tmp);
    File parent = dir.getParentFile();
    File xml_file = new File(parent, "TESTS-" + dir.getName() + "-" + parent.getName() + ".xml");
    Writer out = new FileWriter(xml_file);
    String classname = dir.getName();
    String suffix = parent.getName();
    if (suffix != null && !suffix.isEmpty()) classname = classname + "-" + suffix;
    try {
      generateReport(out, classname, test_cases, stdout_reader, stderr_reader);
    } finally {
      out.close();
      if (stdout_reader != null) stdout_reader.close();
      if (stderr_reader != null) stderr_reader.close();
    }
  }
Beispiel #3
0
  /** Copies the contents of in into out */
  protected static int copy(Reader in, Writer out) {
    int count = 0;

    char[] buf = new char[1024];

    while (true) {
      try {
        int num = in.read(buf, 0, buf.length);
        if (num == -1) break;
        out.write(buf, 0, num);
        count += num;
      } catch (IOException e) {
        break;
      }
    }

    return count;
  }
Beispiel #4
0
 protected static void writeOutput(int type, Reader in, Writer out) throws IOException {
   out.write("\n<" + (type == 2 ? SYSTEM_ERR : SYSTEM_OUT) + "><" + CDATA + "\n");
   copy(in, out);
   out.write("\n]]>");
   out.write("\n</" + (type == 2 ? SYSTEM_ERR : SYSTEM_OUT) + ">");
 }
Beispiel #5
0
  /** Generate the XML report from all the test results */
  protected static void generateReport(
      Writer out, String classname, List<TestCase> results, Reader stdout, Reader stderr)
      throws IOException {
    int num_failures = getFailures(results);
    int num_skips = getSkips(results);
    int num_errors = getErrors(results);
    long total_time = getTotalTime(results);

    try {
      out.write(XML_DEF + "\n");

      out.write(
          "\n<testsuite "
              + "name=\""
              + classname
              + "\" "
              + "tests=\""
              + results.size()
              + "\" "
              + "failures=\""
              + num_failures
              + "\" "
              + "errors=\""
              + num_errors
              + "\" "
              + "skips=\""
              + num_skips
              + "\" "
              + "time=\""
              + (total_time / 1000.0)
              + "\">");

      out.write("\n<properties>");
      Properties props = System.getProperties();

      for (Map.Entry<Object, Object> tmp : props.entrySet()) {
        out.write(
            "\n    <property name=\""
                + tmp.getKey()
                + "\""
                + " value=\""
                + tmp.getValue()
                + "\"/>");
      }
      out.write("\n</properties>\n");

      for (TestCase result : results) {
        if (result == null) continue;

        try {
          writeTestCase(out, result);
        } catch (Throwable t) {
          t.printStackTrace();
        }
      }

      if (stdout != null) writeOutput(1, stdout, out);
      if (stderr != null) writeOutput(2, stderr, out);
    } finally {
      out.write("\n</testsuite>\n");
    }
  }