File: TestReport.java

package info (click to toggle)
mauve 20080616-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 26,856 kB
  • ctags: 21,952
  • sloc: java: 234,107; sh: 2,834; xml: 208; makefile: 59
file content (143 lines) | stat: -rw-r--r-- 4,762 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (c) 2004 Noa Resare.
// Written by Noa Resre <noa@resare.com>
									       
// This file is part of Mauve.
									       
// Mauve is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
									       
// Mauve is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
									       
// You should have received a copy of the GNU General Public License
// along with Mauve; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.

package gnu.testlet;

import java.util.*;
import java.io.*;

/**
 * A TestReport represents all the results of a test run. The TestReport
 * can be serialized to xml with the writeXml method.
 */
public class TestReport
{
  private Properties systemProperties;
  private List testResults;

  private static final String ENCODING = "UTF-8";


  /**
   * Creates a new TestReport object with jvmName and jvmVersion set.
   *
   * @param systemProperties the Properties object returned from
   * System.getProperties() of the jvm that is tested.
   */
  public TestReport(Properties systemProperties)
  {
    this.systemProperties = systemProperties;
    this.testResults = new ArrayList();
  }

  /**
   * Adds a TestResult object to this TestReport.
   *
   * @param result the TestResult object to be added
   */
  public void addTestResult(TestResult result)
  {
    this.testResults.add(result);
  }

  /**
   * Writes a representation of this TestReport object in xml format.
   *
   * @param f the file where the xml stream gets written
   */
  public void writeXml(File f) throws IOException
  {
    Writer out = new OutputStreamWriter(new FileOutputStream(f), ENCODING);
    out.write("<?xml version='1.0' encoding='" + ENCODING + "'?>\n");
    out.write("<testreport version='0.1'>\n  <jvm name='"
              + escAttrib(systemProperties.get("java.vm.vendor"))
              + "'\n    version='"
              + escAttrib(systemProperties.get("java.vm.version")) + "' \n"
              + "    os='" + escAttrib(systemProperties.get("os.name")) + " "
              + escAttrib(systemProperties.get("os.version")) + " "
              + escAttrib(systemProperties.get("os.arch")) + "' />\n");
    Collections.sort(testResults);
    Iterator results = testResults.iterator();
    while (results.hasNext())
      {
        TestResult tr = (TestResult) results.next();
        String[] failures = tr.getFailMessags();
        String[] passes = tr.getPassMessages();
        out.write("  <testresult testlet='" + escAttrib(tr.getTestletName()));
        if (failures.length > 0 || passes.length > 0
            || tr.getException() != null)
          out.write("'>\n");
        else
          out.write("'/>\n");

        for (int i = 0; i < failures.length; i++)
          out.write("    <failure>" + esc(failures[i]) + "</failure>\n");
        
        if (tr.getException() != null)
          {
            Throwable t = tr.getException();
            out.write("    <failure>\n      <exception class='"
                      + escAttrib(t.getClass().getName())
                      + "'>\n        <reason>" + esc(tr.getExceptionMessage())
                      + "</reason>\n        <message>\n" 
                      + esc(tr.getExceptionReason())
                      + "\n        </message>\n      </exception>" 
                      + "\n    </failure>\n");
          }

        for (int i = 0; i < passes.length; i++)
          out.write("    <pass>" + esc(passes[i]) + "</pass>\n");

        if (failures.length > 0 || passes.length > 0
            || tr.getException() != null)
          out.write("  </testresult>\n");
      }
    out.write("</testreport>\n");
    out.close();
  }

  /**
   * Escapes chars &lt; &gt; and &amp; in str so that the result is
   * suitable for inclusion in an xml stream.
   */
  private String esc(String str)
  {
    if (str == null)
      return null;
    str = str.replaceAll("&", "&amp;");
    str = str.replaceAll("<", "&lt;");
    str = str.replaceAll(">", "&gt;");
    // This is a workaround for java.util.regex.Pattern.pcrematches.
    str = str.replace('', '?');
    return str;
  }

  /**
   * Escapes single quotes in string by prepending a backslash.
   */
  private String escAttrib(Object obj)
  {
    if (obj == null)
      return null;
    String str = (String)obj;
    str = str.replaceAll("'", "\\'");
    return str;
  }
}