1#ifndef CPPUNIT_XMLTESTRESULTOUTPUTTER_H
2#define CPPUNIT_XMLTESTRESULTOUTPUTTER_H
3
4#include <cppunit/Portability.h>
5
6#if CPPUNIT_NEED_DLL_DECL
7#pragma warning( push )
8#pragma warning( disable: 4251 )  // X needs to have dll-interface to be used by clients of class Z
9#endif
10
11#include <cppunit/Outputter.h>
12#include <deque>
13#include <iostream>
14#include <map>
15#include <string>
16#include <utility>
17
18
19namespace CppUnit
20{
21
22class Test;
23class TestFailure;
24class TestResultCollector;
25
26
27/*! \brief Outputs a TestResultCollector in XML format.
28 * \ingroup WritingTestResult
29 */
30class CPPUNIT_API XmlOutputter : public Outputter
31{
32public:
33  /*! Constructs a XmlOutputter object.
34   * \param result Result of the test run.
35   * \param stream Stream used to output the XML output.
36   * \param encoding Encoding used in the XML file (default is Latin-1).
37   */
38  XmlOutputter( TestResultCollector *result,
39                std::ostream &stream,
40                std::string encoding = "ISO-8859-1" );
41
42  /// Destructor.
43  virtual ~XmlOutputter();
44
45  /*! Writes the specified result as an XML document to the stream.
46   *
47   * Refer to examples/cppunittest/XmlOutputterTest.cpp for example
48   * of use and XML document structure.
49   */
50  virtual void write();
51
52  /*! \brief An XML Element.
53   * \warning This class will probably be replaced with an abstract
54   * builder in future version.
55   */
56  class CPPUNIT_API Node
57  {
58  public:
59    Node( std::string elementName,
60          std::string content ="" );
61    Node( std::string elementName,
62          int numericContent );
63    virtual ~Node();
64
65    void addAttribute( std::string attributeName,
66                       std::string value );
67    void addAttribute( std::string attributeName,
68                       int numericValue );
69    void addNode( Node *node );
70
71    std::string toString() const;
72
73  private:
74    typedef std::pair<std::string,std::string> Attribute;
75
76    std::string attributesAsString() const;
77    std::string escape( std::string value ) const;
78    static std::string asString( int value );
79
80  private:
81    std::string m_name;
82    std::string m_content;
83    typedef std::deque<Attribute> Attributes;
84    Attributes m_attributes;
85    typedef std::deque<Node *> Nodes;
86    Nodes m_nodes;
87  };
88
89
90  virtual void writeProlog();
91  virtual void writeTestsResult();
92
93  typedef std::map<Test *,TestFailure*> FailedTests;
94  virtual Node *makeRootNode();
95  virtual void addFailedTests( FailedTests &failedTests,
96                               Node *rootNode );
97  virtual void addSucessfulTests( FailedTests &failedTests,
98                                  Node *rootNode );
99  virtual void addStatistics( Node *rootNode );
100  virtual void addFailedTest( Test *test,
101                              TestFailure *failure,
102                              int testNumber,
103                              Node *testsNode );
104  virtual void addFailureLocation( TestFailure *failure,
105                                   Node *testNode );
106  virtual void addSucessfulTest( Test *test,
107                                 int testNumber,
108                                 Node *testsNode );
109protected:
110  virtual void fillFailedTestsMap( FailedTests &failedTests );
111
112protected:
113  TestResultCollector *m_result;
114  std::ostream &m_stream;
115  std::string m_encoding;
116
117private:
118  /// Prevents the use of the copy constructor.
119  XmlOutputter( const XmlOutputter &copy );
120
121  /// Prevents the use of the copy operator.
122  void operator =( const XmlOutputter &copy );
123
124private:
125};
126
127
128
129}  // namespace CppUnit
130
131
132#if CPPUNIT_NEED_DLL_DECL
133#pragma warning( pop )
134#endif
135
136#endif  // CPPUNIT_XMLTESTRESULTOUTPUTTER_H
137