~ubuntu-branches/ubuntu/vivid/psi/vivid

« back to all changes in this revision

Viewing changes to third-party/cppunit/cppunit/src/cppunit/XmlOutputter.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-08-28 18:46:52 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080828184652-iiik12dl91nq7cdi
Tags: 0.12-2
Uploading to unstable (Closes: Bug#494352)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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>
 
9
#include <stdlib.h>
 
10
#include <algorithm>
 
11
 
 
12
 
 
13
CPPUNIT_NS_BEGIN
 
14
 
 
15
 
 
16
XmlOutputter::XmlOutputter( TestResultCollector *result,
 
17
                            OStream &stream,
 
18
                            std::string encoding )
 
19
  : m_result( result )
 
20
  , m_stream( stream )
 
21
  , m_xml( new XmlDocument( encoding ) )
 
22
{
 
23
}
 
24
 
 
25
 
 
26
XmlOutputter::~XmlOutputter()
 
27
{
 
28
  delete m_xml;
 
29
}
 
30
 
 
31
 
 
32
void 
 
33
XmlOutputter::addHook( XmlOutputterHook *hook )
 
34
{
 
35
  m_hooks.push_back( hook );
 
36
}
 
37
 
 
38
 
 
39
void 
 
40
XmlOutputter::removeHook( XmlOutputterHook *hook )
 
41
{
 
42
  m_hooks.erase( std::find( m_hooks.begin(), m_hooks.end(), hook ) );
 
43
}
 
44
 
 
45
 
 
46
void 
 
47
XmlOutputter::write()
 
48
{
 
49
  setRootNode();
 
50
  m_stream  <<  m_xml->toString();
 
51
}
 
52
 
 
53
 
 
54
void 
 
55
XmlOutputter::setStyleSheet( const std::string &styleSheet )
 
56
{
 
57
  m_xml->setStyleSheet( styleSheet );
 
58
}
 
59
 
 
60
 
 
61
void
 
62
XmlOutputter::setStandalone( bool standalone )
 
63
{
 
64
  m_xml->setStandalone( standalone );
 
65
}
 
66
 
 
67
 
 
68
void
 
69
XmlOutputter::setRootNode()
 
70
{
 
71
  XmlElement *rootNode = new XmlElement( "TestRun" );
 
72
  m_xml->setRootElement( rootNode );
 
73
 
 
74
  for ( Hooks::iterator it = m_hooks.begin(); it != m_hooks.end(); ++it )
 
75
    (*it)->beginDocument( m_xml );
 
76
 
 
77
  FailedTests failedTests;
 
78
  fillFailedTestsMap( failedTests );
 
79
 
 
80
  addFailedTests( failedTests, rootNode );
 
81
  addSuccessfulTests( failedTests, rootNode );
 
82
  addStatistics( rootNode );
 
83
 
 
84
  for ( Hooks::iterator itEnd = m_hooks.begin(); itEnd != m_hooks.end(); ++itEnd )
 
85
    (*itEnd)->endDocument( m_xml );
 
86
}
 
87
 
 
88
 
 
89
void 
 
90
XmlOutputter::fillFailedTestsMap( FailedTests &failedTests )
 
91
{
 
92
  const TestResultCollector::TestFailures &failures = m_result->failures();
 
93
  TestResultCollector::TestFailures::const_iterator itFailure = failures.begin();
 
94
  while ( itFailure != failures.end() )
 
95
  {
 
96
    TestFailure *failure = *itFailure++;
 
97
    failedTests.insert( std::pair<Test* const, TestFailure*>(failure->failedTest(), failure ) );
 
98
  }
 
99
}
 
100
 
 
101
 
 
102
void
 
103
XmlOutputter::addFailedTests( FailedTests &failedTests,
 
104
                              XmlElement *rootNode )
 
105
{
 
106
  XmlElement *testsNode = new XmlElement( "FailedTests" );
 
107
  rootNode->addElement( testsNode );
 
108
 
 
109
  const TestResultCollector::Tests &tests = m_result->tests();
 
110
  for ( unsigned int testNumber = 0; testNumber < tests.size(); ++testNumber )
 
111
  {
 
112
    Test *test = tests[testNumber];
 
113
    if ( failedTests.find( test ) != failedTests.end() )
 
114
      addFailedTest( test, failedTests[test], testNumber+1, testsNode );
 
115
  }
 
116
}
 
117
 
 
118
 
 
119
void
 
120
XmlOutputter::addSuccessfulTests( FailedTests &failedTests,
 
121
                                           XmlElement *rootNode )
 
122
{
 
123
  XmlElement *testsNode = new XmlElement( "SuccessfulTests" );
 
124
  rootNode->addElement( testsNode );
 
125
 
 
126
  const TestResultCollector::Tests &tests = m_result->tests();
 
127
  for ( unsigned int testNumber = 0; testNumber < tests.size(); ++testNumber )
 
128
  {
 
129
    Test *test = tests[testNumber];
 
130
    if ( failedTests.find( test ) == failedTests.end() )
 
131
      addSuccessfulTest( test, testNumber+1, testsNode );
 
132
  }
 
133
}
 
134
 
 
135
 
 
136
void
 
137
XmlOutputter::addStatistics( XmlElement *rootNode )
 
138
{
 
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() ) );
 
146
 
 
147
  for ( Hooks::iterator it = m_hooks.begin(); it != m_hooks.end(); ++it )
 
148
    (*it)->statisticsAdded( m_xml, statisticsElement );
 
149
}
 
150
 
 
151
 
 
152
void
 
153
XmlOutputter::addFailedTest( Test *test,
 
154
                             TestFailure *failure,
 
155
                             int testNumber,
 
156
                             XmlElement *testsNode )
 
157
{
 
158
  Exception *thrownException = failure->thrownException();
 
159
  
 
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" : 
 
166
                                                                "Assertion" ) );
 
167
 
 
168
  if ( failure->sourceLine().isValid() )
 
169
    addFailureLocation( failure, testElement );
 
170
 
 
171
  testElement->addElement( new XmlElement( "Message", thrownException->what() ) );
 
172
 
 
173
  for ( Hooks::iterator it = m_hooks.begin(); it != m_hooks.end(); ++it )
 
174
    (*it)->failTestAdded( m_xml, testElement, test, failure );
 
175
}
 
176
 
 
177
 
 
178
void
 
179
XmlOutputter::addFailureLocation( TestFailure *failure,
 
180
                                  XmlElement *testElement )
 
181
{
 
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() ) );
 
187
}
 
188
 
 
189
 
 
190
void
 
191
XmlOutputter::addSuccessfulTest( Test *test, 
 
192
                                 int testNumber,
 
193
                                 XmlElement *testsNode )
 
194
{
 
195
  XmlElement *testElement = new XmlElement( "Test" );
 
196
  testsNode->addElement( testElement );
 
197
  testElement->addAttribute( "id", testNumber );
 
198
  testElement->addElement( new XmlElement( "Name", test->getName() ) );
 
199
 
 
200
  for ( Hooks::iterator it = m_hooks.begin(); it != m_hooks.end(); ++it )
 
201
    (*it)->successfulTestAdded( m_xml, testElement, test );
 
202
}
 
203
 
 
204
 
 
205
CPPUNIT_NS_END