1/****************************************************************************
2**
3** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
5**
6** This file is part of the QtTest module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Digia. For licensing terms and
14** conditions see http://qt.digia.com/licensing. For further information
15** use the contact form at http://qt.digia.com/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 2.1 requirements
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24**
25** In addition, as a special exception, Digia gives you certain additional
26** rights. These rights are described in the Digia Qt LGPL Exception
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28**
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: http://www.gnu.org/copyleft/gpl.html.
36**
37**
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qtestxmlstreamer.h"
43#include "qtestelement.h"
44#include "qtestelementattribute.h"
45#include "qtestlogger_p.h"
46
47#include "QtTest/private/qtestlog_p.h"
48#include "QtTest/private/qtestresult_p.h"
49#include "QtTest/private/qxmltestlogger_p.h"
50
51#include <string.h>
52#include <stdio.h>
53
54QT_BEGIN_NAMESPACE
55
56QTestXmlStreamer::QTestXmlStreamer()
57 :QTestBasicStreamer()
58{
59}
60
61QTestXmlStreamer::~QTestXmlStreamer()
62{}
63
64void QTestXmlStreamer::formatStart(const QTestElement *element, QTestCharBuffer *formatted) const
65{
66 if(!element || !formatted)
67 return;
68
69 switch(element->elementType()){
70 case QTest::LET_TestCase: {
71 QTestCharBuffer quotedTf;
72 QXmlTestLogger::xmlQuote(&quotedTf, element->attributeValue(QTest::AI_Name));
73
74 QTest::qt_asprintf(formatted, "<TestFunction name=\"%s\">\n", quotedTf.constData());
75 break;
76 }
77 case QTest::LET_Failure: {
78 QTestCharBuffer cdataDesc;
79 QXmlTestLogger::xmlCdata(&cdataDesc, element->attributeValue(QTest::AI_Description));
80
81 QTestCharBuffer location;
82 QTestCharBuffer quotedFile;
83 QXmlTestLogger::xmlQuote(&quotedFile, element->attributeValue(QTest::AI_File));
84
85 QTest::qt_asprintf(&location, "%s=\"%s\" %s=\"%s\"",
86 element->attributeName(QTest::AI_File),
87 quotedFile.constData(),
88 element->attributeName(QTest::AI_Line),
89 element->attributeValue(QTest::AI_Line));
90
91 if (element->attribute(QTest::AI_Tag)) {
92 QTestCharBuffer cdataTag;
93 QXmlTestLogger::xmlCdata(&cdataTag, element->attributeValue(QTest::AI_Tag));
94 QTest::qt_asprintf(formatted, "<Incident type=\"%s\" %s>\n"
95 " <DataTag><![CDATA[%s]]></DataTag>\n"
96 " <Description><![CDATA[%s]]></Description>\n"
97 "</Incident>\n", element->attributeValue(QTest::AI_Result),
98 location.constData(), cdataTag.constData(), cdataDesc.constData());
99 }
100 else {
101 QTest::qt_asprintf(formatted, "<Incident type=\"%s\" %s>\n"
102 " <Description><![CDATA[%s]]></Description>\n"
103 "</Incident>\n", element->attributeValue(QTest::AI_Result),
104 location.constData(), cdataDesc.constData());
105 }
106 break;
107 }
108 case QTest::LET_Error: {
109 // assuming type and attribute names don't need quoting
110 QTestCharBuffer quotedFile;
111 QTestCharBuffer cdataDesc;
112 QXmlTestLogger::xmlQuote(&quotedFile, element->attributeValue(QTest::AI_File));
113 QXmlTestLogger::xmlCdata(&cdataDesc, element->attributeValue(QTest::AI_Description));
114
115 QTestCharBuffer tagbuf;
116 if (element->attribute(QTest::AI_Tag)) {
117 QTestCharBuffer cdataTag;
118 QXmlTestLogger::xmlCdata(&cdataTag, element->attributeValue(QTest::AI_Tag));
119 QTest::qt_asprintf(&tagbuf, " <DataTag><![CDATA[%s]]></DataTag>\n", cdataTag.constData());
120 }
121
122 QTest::qt_asprintf(formatted, "<Message type=\"%s\" %s=\"%s\" %s=\"%s\">\n%s <Description><![CDATA[%s]]></Description>\n</Message>\n",
123 element->attributeValue(QTest::AI_Type),
124 element->attributeName(QTest::AI_File),
125 quotedFile.constData(),
126 element->attributeName(QTest::AI_Line),
127 element->attributeValue(QTest::AI_Line),
128 tagbuf.constData(),
129 cdataDesc.constData());
130 break;
131 }
132 case QTest::LET_Benchmark: {
133 // assuming value and iterations don't need quoting
134 QTestCharBuffer quotedMetric;
135 QTestCharBuffer quotedTag;
136 QXmlTestLogger::xmlQuote(&quotedMetric, element->attributeValue(QTest::AI_Metric));
137 QXmlTestLogger::xmlQuote(&quotedTag, element->attributeValue(QTest::AI_Tag));
138
139 QTest::qt_asprintf(formatted, "<BenchmarkResult %s=\"%s\" %s=\"%s\" %s=\"%s\" %s=\"%s\" />\n",
140 element->attributeName(QTest::AI_Metric),
141 quotedMetric.constData(),
142 element->attributeName(QTest::AI_Tag),
143 quotedTag.constData(),
144 element->attributeName(QTest::AI_Value),
145 element->attributeValue(QTest::AI_Value),
146 element->attributeName(QTest::AI_Iterations),
147 element->attributeValue(QTest::AI_Iterations) );
148 break;
149 }
150 default:
151 formatted->data()[0] = '\0';
152 }
153}
154
155void QTestXmlStreamer::formatEnd(const QTestElement *element, QTestCharBuffer *formatted) const
156{
157 if(!element || !formatted)
158 return;
159
160 if (element->elementType() == QTest::LET_TestCase) {
161 bool failed = false;
162 for (QTestElement* child = element->childElements(); child; child = child->nextElement()) {
163 if ( child->elementType() == QTest::LET_Failure
164 && child->attribute(QTest::AI_Result)
165 && ( !strcmp(child->attributeValue(QTest::AI_Result), "fail")
166 || !strcmp(child->attributeValue(QTest::AI_Result), "xpass"))
167 )
168 {
169 failed = true;
170 break;
171 }
172 }
173
174 // For passing functions, no Incident has been output yet.
175 // For failing functions, we already output one.
176 // Please note: we are outputting "pass" even if there was an xfail etc.
177 // This is by design (arguably bad design, but dangerous to change now!)
178 if (element->attribute(QTest::AI_Result) && !failed) {
179 QTest::qt_asprintf(formatted, "<Incident type=\"pass\" file=\"\" line=\"0\" />\n</TestFunction>\n");
180 }
181 else {
182 QTest::qt_asprintf(formatted, "</TestFunction>\n");
183 }
184 } else {
185 formatted->data()[0] = '\0';
186 }
187}
188
189void QTestXmlStreamer::formatBeforeAttributes(const QTestElement *element, QTestCharBuffer *formatted) const
190{
191 Q_UNUSED(element);
192 if (!formatted)
193 return;
194
195 formatted->data()[0] = '\0';
196}
197
198void QTestXmlStreamer::output(QTestElement *element) const
199{
200 QTestCharBuffer buf;
201 QTestCharBuffer quotedTc;
202 QXmlTestLogger::xmlQuote(&quotedTc, QTestResult::currentTestObjectName());
203
204 QTest::qt_asprintf(&buf, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<TestCase name=\"%s\">\n",
205 quotedTc.constData());
206 outputString(buf.constData());
207
208 if (logger()->hasRandomSeed()) {
209 QTest::qt_asprintf(&buf, "<Environment>\n <QtVersion>%s</QtVersion>\n <QTestVersion>%s</QTestVersion>\n <RandomSeed>%d</RandomSeed>\n",
210 qVersion(), QTEST_VERSION_STR, logger()->randomSeed() );
211 } else {
212 QTest::qt_asprintf(&buf, "<Environment>\n <QtVersion>%s</QtVersion>\n <QTestVersion>%s</QTestVersion>\n",
213 qVersion(), QTEST_VERSION_STR );
214 }
215 outputString(buf.constData());
216
217 QTest::qt_asprintf(&buf, "</Environment>\n");
218 outputString(buf.constData());
219
220 QTestBasicStreamer::output(element);
221
222 QTest::qt_asprintf(&buf, "</TestCase>\n");
223 outputString(buf.constData());
224}
225
226QT_END_NAMESPACE
227
228