XmlOutputter.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #ifndef CPPUNIT_XMLTESTRESULTOUTPUTTER_H
  2. #define CPPUNIT_XMLTESTRESULTOUTPUTTER_H
  3. #include <cppunit/Portability.h>
  4. #if CPPUNIT_NEED_DLL_DECL
  5. #pragma warning( push )
  6. #pragma warning( disable: 4251 ) // X needs to have dll-interface to be used by clients of class Z
  7. #endif
  8. #include <cppunit/Outputter.h>
  9. #include <cppunit/portability/CppUnitDeque.h>
  10. #include <cppunit/portability/CppUnitMap.h>
  11. #include <cppunit/portability/Stream.h>
  12. CPPUNIT_NS_BEGIN
  13. class Test;
  14. class TestFailure;
  15. class TestResultCollector;
  16. class XmlDocument;
  17. class XmlElement;
  18. class XmlOutputterHook;
  19. /*! \brief Outputs a TestResultCollector in XML format.
  20. * \ingroup WritingTestResult
  21. *
  22. * Save the test result as a XML stream.
  23. *
  24. * Additional datas can be added to the XML document using XmlOutputterHook.
  25. * Hook are not owned by the XmlOutputter. They should be valid until
  26. * destruction of the XmlOutputter. They can be removed with removeHook().
  27. *
  28. * \see XmlDocument, XmlElement, XmlOutputterHook.
  29. */
  30. class CPPUNIT_API XmlOutputter : public Outputter
  31. {
  32. public:
  33. /*! \brief 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. OStream &stream,
  40. std::string encoding = std::string("ISO-8859-1") );
  41. /// Destructor.
  42. virtual ~XmlOutputter();
  43. /*! \brief Adds the specified hook to the outputter.
  44. * \param hook Hook to add. Must not be \c NULL.
  45. */
  46. virtual void addHook( XmlOutputterHook *hook );
  47. /*! \brief Removes the specified hook from the outputter.
  48. * \param hook Hook to remove.
  49. */
  50. virtual void removeHook( XmlOutputterHook *hook );
  51. /*! \brief Writes the specified result as an XML document to the stream.
  52. *
  53. * Refer to examples/cppunittest/XmlOutputterTest.cpp for example
  54. * of use and XML document structure.
  55. */
  56. virtual void write();
  57. /*! \brief Sets the XSL style sheet used.
  58. *
  59. * \param styleSheet Name of the style sheet used. If empty, then no style sheet
  60. * is used (default).
  61. */
  62. virtual void setStyleSheet( const std::string &styleSheet );
  63. /*! \brief set the output document as standalone or not.
  64. *
  65. * For the output document, specify wether it's a standalone XML
  66. * document, or not.
  67. *
  68. * \param standalone if true, the output will be specified as standalone.
  69. * if false, it will be not.
  70. */
  71. virtual void setStandalone( bool standalone );
  72. typedef CppUnitMap<Test *,TestFailure*, std::less<Test*> > FailedTests;
  73. /*! \brief Sets the root element and adds its children.
  74. *
  75. * Set the root element of the XML Document and add its child elements.
  76. *
  77. * For all hooks, call beginDocument() just after creating the root element (it
  78. * is empty at this time), and endDocument() once all the datas have been added
  79. * to the root element.
  80. */
  81. virtual void setRootNode();
  82. virtual void addFailedTests( FailedTests &failedTests,
  83. XmlElement *rootNode );
  84. virtual void addSuccessfulTests( FailedTests &failedTests,
  85. XmlElement *rootNode );
  86. /*! \brief Adds the statics element to the root node.
  87. *
  88. * Creates a new element containing statistics data and adds it to the root element.
  89. * Then, for all hooks, call statisticsAdded().
  90. * \param rootNode Root element.
  91. */
  92. virtual void addStatistics( XmlElement *rootNode );
  93. /*! \brief Adds a failed test to the failed tests node.
  94. * Creates a new element containing datas about the failed test, and adds it to
  95. * the failed tests element.
  96. * Then, for all hooks, call failTestAdded().
  97. */
  98. virtual void addFailedTest( Test *test,
  99. TestFailure *failure,
  100. int testNumber,
  101. XmlElement *testsNode );
  102. virtual void addFailureLocation( TestFailure *failure,
  103. XmlElement *testElement );
  104. /*! \brief Adds a successful test to the successful tests node.
  105. * Creates a new element containing datas about the successful test, and adds it to
  106. * the successful tests element.
  107. * Then, for all hooks, call successfulTestAdded().
  108. */
  109. virtual void addSuccessfulTest( Test *test,
  110. int testNumber,
  111. XmlElement *testsNode );
  112. protected:
  113. virtual void fillFailedTestsMap( FailedTests &failedTests );
  114. protected:
  115. typedef CppUnitDeque<XmlOutputterHook *> Hooks;
  116. TestResultCollector *m_result;
  117. OStream &m_stream;
  118. std::string m_encoding;
  119. std::string m_styleSheet;
  120. XmlDocument *m_xml;
  121. Hooks m_hooks;
  122. private:
  123. /// Prevents the use of the copy constructor.
  124. XmlOutputter( const XmlOutputter &copy );
  125. /// Prevents the use of the copy operator.
  126. void operator =( const XmlOutputter &copy );
  127. private:
  128. };
  129. CPPUNIT_NS_END
  130. #if CPPUNIT_NEED_DLL_DECL
  131. #pragma warning( pop )
  132. #endif
  133. #endif // CPPUNIT_XMLTESTRESULTOUTPUTTER_H