2 * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file test_results_collector_xml.cpp
18 * @author Lukasz Wrzosek (l.wrzosek@samsung.com)
19 * @author Marcin Niesluchowski (m.niesluchow@samsung.com)
21 * @brief Source file containing XmlCollector class definition
29 #include <dpl/assert.h>
30 #include <dpl/exception.h>
31 #include <dpl/test/test_results_collector_commons.h>
33 #include "dpl/test/test_results_collector_xml.h"
40 const char *DEFAULT_XML_FILE_NAME = "results.xml";
44 XmlCollector::XmlCollector()
45 : m_filename(DEFAULT_XML_FILE_NAME), m_verbosity(true)
49 TestResultsCollectorBase* XmlCollector::Constructor()
51 return new XmlCollector();
54 void XmlCollector::CollectCurrentTestGroupName(const std::string& name)
56 std::size_t pos = GetCurrentGroupPosition();
57 if (std::string::npos != pos) {
60 m_stats = Statistic();
63 pos = m_outputBuffer.find("</testsuites>");
64 if (std::string::npos == pos) {
65 ThrowMsg(DPL::Exception, "Could not find test suites closing tag");
67 GroupStart(pos, name);
70 void XmlCollector::GroupStart(const std::size_t pos, const std::string& name)
72 std::stringstream groupHeader;
73 groupHeader << "\n\t<testsuite";
74 groupHeader << " name=\"" << EscapeSpecialCharacters(name) << "\"";
75 groupHeader << R"( tests="1")"; // include SegFault
76 groupHeader << R"( failures="1")"; // include SegFault
77 groupHeader << R"( skipped="0")";
80 groupHeader << "\n\t\t<testcase name=\"unknown\" status=\"FAILED\">";
82 "\n\t\t\t<failure type=\"FAILED\" message=\"segmentation fault\"/>";
83 groupHeader << "\n\t\t</testcase>";
85 groupHeader << "\n\t</testsuite>";
87 m_outputBuffer.insert(pos - 1, groupHeader.str());
90 bool XmlCollector::Configure()
92 m_fp.Reset(fopen(m_filename.c_str(), "w"));
94 LogPedantic("Could not open file " << m_filename << " for writing");
100 std::string XmlCollector::CollectorSpecificHelp() const
102 return CollectorFileHelp(DEFAULT_XML_FILE_NAME) + COLLECTOR_NO_VERBOSE_HELP;
105 void XmlCollector::Start()
107 Assert(!!m_fp && "File handle must not be null");
108 m_outputBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
109 m_outputBuffer.append("<testsuites>\n</testsuites>");
113 void XmlCollector::Finish()
115 std::size_t pos = GetCurrentGroupPosition();
116 if (std::string::npos != pos) {
122 bool XmlCollector::ParseCollectorSpecificArg(const std::string& arg)
124 return ParseCollectorFileArg(arg, m_filename) || ParseCollectorNoVerboseArg(arg, m_verbosity);
127 void XmlCollector::CollectResult(const std::string& id,
128 const FailStatus status,
129 const std::string& reason,
130 const ConstPerformanceResultPtr &performanceResult)
132 m_resultBuffer.erase();
133 m_resultBuffer.append("\t\t<testcase name=\"");
134 m_resultBuffer.append(EscapeSpecialCharacters(id));
135 m_resultBuffer.append("\"");
137 std::ostringstream ostr;
139 case FailStatus::NONE:
140 if (!performanceResult) {
141 m_resultBuffer.append(" status=\"OK\"/>\n");
144 if (!performanceResult->IsMaxDuration()) {
145 m_resultBuffer.append(" status=\"OK\" time=\"");
146 ostr << performanceResult->GetDuration().count();
147 m_resultBuffer.append(ostr.str());
148 m_resultBuffer.append("\"/>\n");
151 m_resultBuffer.append(" status=\"OK\" time=\"");
152 ostr << performanceResult->GetDuration().count();
153 m_resultBuffer.append(ostr.str());
154 m_resultBuffer.append("\" time_expected=\"");
156 ostr << performanceResult->GetMaxDuration().count();
157 m_resultBuffer.append(ostr.str());
158 m_resultBuffer.append("\"/>\n");
160 case FailStatus::FAILED:
161 m_resultBuffer.append(" status=\"FAILED\">\n");
162 PrintfErrorMessage("FAILED", EscapeSpecialCharacters(reason));
163 m_resultBuffer.append("\t\t</testcase>\n");
165 case FailStatus::IGNORED:
166 m_resultBuffer.append(" status=\"Ignored\">\n");
167 PrintfIgnoredMessage("Ignored", EscapeSpecialCharacters(reason));
168 m_resultBuffer.append("\t\t</testcase>\n");
171 Assert(false && "Bad status");
173 std::size_t group_pos = GetCurrentGroupPosition();
174 if (std::string::npos == group_pos) {
175 ThrowMsg(DPL::Exception, "No current group set");
178 std::size_t last_case_pos = m_outputBuffer.find(
179 "<testcase name=\"unknown\"",
181 if (std::string::npos == last_case_pos) {
182 ThrowMsg(DPL::Exception, "Could not find SegFault test case");
184 m_outputBuffer.insert(last_case_pos - 2, m_resultBuffer);
186 m_stats.AddTest(status);
188 UpdateGroupHeader(group_pos,
189 m_stats.GetTotal() + 1, // include SegFault
190 m_stats.GetFailed() + 1, // include SegFault
191 m_stats.GetIgnored());
195 std::size_t XmlCollector::GetCurrentGroupPosition() const
197 return m_outputBuffer.rfind("<testsuite ");
200 void XmlCollector::UpdateGroupHeader(const std::size_t groupPosition,
201 const unsigned int tests,
202 const unsigned int failures,
203 const unsigned int skipped)
205 UpdateElementAttribute(groupPosition, "tests", UIntToString(tests));
206 UpdateElementAttribute(groupPosition, "failures", UIntToString(failures));
207 UpdateElementAttribute(groupPosition, "skipped", UIntToString(skipped));
210 void XmlCollector::UpdateElementAttribute(const std::size_t elementPosition,
211 const std::string& name,
212 const std::string& value)
214 std::string pattern = name + "=\"";
216 std::size_t start = m_outputBuffer.find(pattern, elementPosition);
217 if (std::string::npos == start) {
218 ThrowMsg(DPL::Exception,
219 "Could not find attribute " << name << " beginning");
222 std::size_t end = m_outputBuffer.find("\"", start + pattern.length());
223 if (std::string::npos == end) {
224 ThrowMsg(DPL::Exception,
225 "Could not find attribute " << name << " end");
228 m_outputBuffer.replace(start + pattern.length(),
229 end - start - pattern.length(),
233 std::string XmlCollector::UIntToString(const unsigned int value)
235 std::stringstream result;
240 void XmlCollector::GroupFinish(const std::size_t groupPosition)
242 std::size_t segFaultStart =
243 m_outputBuffer.find("<testcase name=\"unknown\"", groupPosition);
244 if (std::string::npos == segFaultStart) {
245 ThrowMsg(DPL::Exception,
246 "Could not find SegFault test case start position");
248 segFaultStart -= 2; // to erase tabs
250 std::string closeTag = "</testcase>";
251 std::size_t segFaultEnd = m_outputBuffer.find(closeTag, segFaultStart);
252 if (std::string::npos == segFaultEnd) {
253 ThrowMsg(DPL::Exception,
254 "Could not find SegFault test case end position");
256 segFaultEnd += closeTag.length() + 1; // to erase new line
258 m_outputBuffer.erase(segFaultStart, segFaultEnd - segFaultStart);
260 UpdateGroupHeader(groupPosition,
263 m_stats.GetIgnored());
266 void XmlCollector::FlushOutput()
268 int fd = fileno(m_fp.Get());
270 const char* errStr = strerror(errno);
271 ThrowMsg(DPL::Exception, errStr);
274 if (-1 == TEMP_FAILURE_RETRY(ftruncate(fd, 0L))) {
275 const char* errStr = strerror(errno);
276 ThrowMsg(DPL::Exception, errStr);
279 if (-1 == TEMP_FAILURE_RETRY(fseek(m_fp.Get(), 0L, SEEK_SET))) {
280 const char* errStr = strerror(errno);
281 ThrowMsg(DPL::Exception, errStr);
284 if (m_outputBuffer.size() !=
285 fwrite(m_outputBuffer.c_str(), 1, m_outputBuffer.size(),
288 const char* errStr = strerror(errno);
289 ThrowMsg(DPL::Exception, errStr);
292 if (-1 == TEMP_FAILURE_RETRY(fflush(m_fp.Get()))) {
293 const char* errStr = strerror(errno);
294 ThrowMsg(DPL::Exception, errStr);
298 void XmlCollector::PrintfErrorMessage(const char* type, const std::string& message)
300 m_resultBuffer.append("\t\t\t<failure type=\"");
301 m_resultBuffer.append(EscapeSpecialCharacters(type));
303 m_resultBuffer.append("\" message=\"");
304 m_resultBuffer.append(EscapeSpecialCharacters(message));
306 m_resultBuffer.append("\"/>\n");
309 void XmlCollector::PrintfIgnoredMessage(const char* type, const std::string& message)
311 m_resultBuffer.append("\t\t\t<skipped type=\"");
312 m_resultBuffer.append(EscapeSpecialCharacters(type));
314 m_resultBuffer.append("\" message=\"");
315 m_resultBuffer.append(EscapeSpecialCharacters(message));
317 m_resultBuffer.append("\"/>\n");
320 std::string XmlCollector::EscapeSpecialCharacters(std::string s)
322 for (unsigned int i = 0; i < s.size();) {
326 s.insert(i, """);
332 s.insert(i, "&");
350 s.insert(i, "'");