1 #include <cppunit/Exception.h>
2 #include <cppunit/Test.h>
3 #include <cppunit/TestFailure.h>
4 #include <cppunit/TestResultCollector.h>
5 #include <cppunit/XmlOutputter.h>
6 #include <cppunit/XmlOutputterHook.h>
7 #include <cppunit/tools/XmlDocument.h>
8 #include <cppunit/tools/XmlElement.h>
16 XmlOutputter::XmlOutputter( TestResultCollector *result,
18 std::string encoding )
21 , m_xml( new XmlDocument( encoding ) )
26 XmlOutputter::~XmlOutputter()
33 XmlOutputter::addHook( XmlOutputterHook *hook )
35 m_hooks.push_back( hook );
40 XmlOutputter::removeHook( XmlOutputterHook *hook )
42 m_hooks.erase( std::find( m_hooks.begin(), m_hooks.end(), hook ) );
50 m_stream << m_xml->toString();
55 XmlOutputter::setStyleSheet( const std::string &styleSheet )
57 m_xml->setStyleSheet( styleSheet );
62 XmlOutputter::setStandalone( bool standalone )
64 m_xml->setStandalone( standalone );
69 XmlOutputter::setRootNode()
71 XmlElement *rootNode = new XmlElement( "TestRun" );
72 m_xml->setRootElement( rootNode );
74 for ( Hooks::iterator it = m_hooks.begin(); it != m_hooks.end(); ++it )
75 (*it)->beginDocument( m_xml );
77 FailedTests failedTests;
78 fillFailedTestsMap( failedTests );
80 addFailedTests( failedTests, rootNode );
81 addSuccessfulTests( failedTests, rootNode );
82 addStatistics( rootNode );
84 for ( Hooks::iterator itEnd = m_hooks.begin(); itEnd != m_hooks.end(); ++itEnd )
85 (*itEnd)->endDocument( m_xml );
90 XmlOutputter::fillFailedTestsMap( FailedTests &failedTests )
92 const TestResultCollector::TestFailures &failures = m_result->failures();
93 TestResultCollector::TestFailures::const_iterator itFailure = failures.begin();
94 while ( itFailure != failures.end() )
96 TestFailure *failure = *itFailure++;
97 failedTests.insert( std::pair<Test* const, TestFailure*>(failure->failedTest(), failure ) );
103 XmlOutputter::addFailedTests( FailedTests &failedTests,
104 XmlElement *rootNode )
106 XmlElement *testsNode = new XmlElement( "FailedTests" );
107 rootNode->addElement( testsNode );
109 const TestResultCollector::Tests &tests = m_result->tests();
110 for ( unsigned int testNumber = 0; testNumber < tests.size(); ++testNumber )
112 Test *test = tests[testNumber];
113 if ( failedTests.find( test ) != failedTests.end() )
114 addFailedTest( test, failedTests[test], testNumber+1, testsNode );
120 XmlOutputter::addSuccessfulTests( FailedTests &failedTests,
121 XmlElement *rootNode )
123 XmlElement *testsNode = new XmlElement( "SuccessfulTests" );
124 rootNode->addElement( testsNode );
126 const TestResultCollector::Tests &tests = m_result->tests();
127 for ( unsigned int testNumber = 0; testNumber < tests.size(); ++testNumber )
129 Test *test = tests[testNumber];
130 if ( failedTests.find( test ) == failedTests.end() )
131 addSuccessfulTest( test, testNumber+1, testsNode );
137 XmlOutputter::addStatistics( XmlElement *rootNode )
139 XmlElement *statisticsElement = new XmlElement( "Statistics" );
140 rootNode->addElement( statisticsElement );
141 statisticsElement->addElement( new XmlElement( "Tests", m_result->runTests() ) );
142 statisticsElement->addElement( new XmlElement( "FailuresTotal",
143 m_result->testFailuresTotal() ) );
144 statisticsElement->addElement( new XmlElement( "Errors", m_result->testErrors() ) );
145 statisticsElement->addElement( new XmlElement( "Failures", m_result->testFailures() ) );
147 for ( Hooks::iterator it = m_hooks.begin(); it != m_hooks.end(); ++it )
148 (*it)->statisticsAdded( m_xml, statisticsElement );
153 XmlOutputter::addFailedTest( Test *test,
154 TestFailure *failure,
156 XmlElement *testsNode )
158 Exception *thrownException = failure->thrownException();
160 XmlElement *testElement = new XmlElement( "FailedTest" );
161 testsNode->addElement( testElement );
162 testElement->addAttribute( "id", testNumber );
163 testElement->addElement( new XmlElement( "Name", test->getName() ) );
164 testElement->addElement( new XmlElement( "FailureType",
165 failure->isError() ? "Error" :
168 if ( failure->sourceLine().isValid() )
169 addFailureLocation( failure, testElement );
171 testElement->addElement( new XmlElement( "Message", thrownException->what() ) );
173 for ( Hooks::iterator it = m_hooks.begin(); it != m_hooks.end(); ++it )
174 (*it)->failTestAdded( m_xml, testElement, test, failure );
179 XmlOutputter::addFailureLocation( TestFailure *failure,
180 XmlElement *testElement )
182 XmlElement *locationNode = new XmlElement( "Location" );
183 testElement->addElement( locationNode );
184 SourceLine sourceLine = failure->sourceLine();
185 locationNode->addElement( new XmlElement( "File", sourceLine.fileName() ) );
186 locationNode->addElement( new XmlElement( "Line", sourceLine.lineNumber() ) );
191 XmlOutputter::addSuccessfulTest( Test *test,
193 XmlElement *testsNode )
195 XmlElement *testElement = new XmlElement( "Test" );
196 testsNode->addElement( testElement );
197 testElement->addAttribute( "id", testNumber );
198 testElement->addElement( new XmlElement( "Name", test->getName() ) );
200 for ( Hooks::iterator it = m_hooks.begin(); it != m_hooks.end(); ++it )
201 (*it)->successfulTestAdded( m_xml, testElement, test );