1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QFileInfo>
30#include <QVariant>
31#include <QtDebug>
32
33#include "Global.h"
34#include "TestSuiteHandler.h"
35#include "TestSuiteResult.h"
36#include "XmlParseHelper.h"
37#include "XMLWriter.h"
38#include "XSLTTestSuiteHandler.h"
39#include "XSDTestSuiteHandler.h"
40#include <private/qxmldebug_p.h>
41
42#include "TestSuite.h"
43
44using namespace QPatternistSDK;
45using namespace QPatternist;
46
47TestSuite::TestSuite()
48{
49}
50
51QVariant TestSuite::data(const Qt::ItemDataRole role, int column) const
52{
53 if(role != Qt::DisplayRole)
54 return QVariant();
55
56 switch(column)
57 {
58 case 0:
59 return title();
60 case 1:
61 return QString();
62 default:
63 {
64 Q_ASSERT(false);
65 return QString();
66 }
67 }
68}
69
70TestSuiteResult *TestSuite::runSuite()
71{
72 const QDate date(QDate::currentDate());
73 TestResult::List result(execute(stage: CompileAndRun, ts: this));
74
75 return new TestSuiteResult(version(), date, result);
76}
77
78TestSuite *TestSuite::openCatalog(const QUrl &catalogURI,
79 QString &errorMsg,
80 const bool useExclusionList,
81 SuiteType suiteType)
82{
83 pDebug() << "Opening catalog:" << catalogURI.toString();
84 QFile ts(catalogURI.toLocalFile());
85 Q_ASSERT(catalogURI.isValid());
86
87 if(!ts.exists())
88 {
89 errorMsg = QString::fromLatin1(str: "The test suite catalog \"%1\" could not be found.\n")
90 .arg(a: ts.fileName());
91 return 0;
92 }
93
94 const QFileInfo info(ts);
95
96 if(!info.isReadable())
97 {
98 errorMsg = QString::fromLatin1(str: "Cannot read the test suite catalog.\n");
99 return 0;
100 }
101 else if(!info.isFile())
102 {
103 errorMsg = QString::fromLatin1(str: "The specified test suite catalog \"%1\" is not a file. "
104 "The test suite catalog must be a file, it cannot be "
105 "a directory, for example.\n")
106 .arg(a: ts.fileName());
107 return 0;
108 }
109 else if(!ts.open(flags: QIODevice::ReadOnly | QIODevice::Text))
110 {
111 errorMsg = QString::fromLatin1(str: "Failed to open the test suite catalog, \"%1\".\n")
112 .arg(a: ts.fileName());
113 return 0;
114 }
115
116 return openCatalog(input: &ts, errorMsg, fileName: catalogURI, useExclusionList, type: suiteType);
117}
118
119TestSuite *TestSuite::openCatalog(QIODevice *input,
120 QString &errorMsg,
121 const QUrl &fileName,
122 const bool useExclusionList,
123 SuiteType suiteType)
124{
125 Q_ASSERT(input);
126
127 typedef QPatternist::AutoPtr<XmlParseHelper> HandlerPtr;
128
129 HandlerPtr loader;
130
131 switch (suiteType) {
132 case XQuerySuite: loader = HandlerPtr(new TestSuiteHandler(fileName, useExclusionList)); break;
133 case XsltSuite: loader = HandlerPtr(new XSLTTestSuiteHandler(fileName)); break;
134 case XsdSuite: loader = HandlerPtr(new XSDTestSuiteHandler(fileName)); break;
135 default: Q_ASSERT(false); break;
136 }
137
138 if (!loader.data()->parse(input)) {
139 errorMsg = QString::fromLatin1(str: "Couldn't parse %1").arg(a: fileName.toString());
140 return 0;
141 }
142
143 TestSuite *suite = 0;
144 switch (suiteType) {
145 case XQuerySuite: suite = static_cast<TestSuiteHandler *>(loader.data())->testSuite(); break;
146 case XsltSuite: suite = static_cast<XSLTTestSuiteHandler *>(loader.data())->testSuite(); break;
147 case XsdSuite: suite = static_cast<XSDTestSuiteHandler *>(loader.data())->testSuite(); break;
148 default: Q_ASSERT(false); break;
149 }
150
151 if(suite)
152 return suite;
153
154 errorMsg = QString::fromLatin1(str: "Failed to load \"%1\". "
155 "It appears to have no test-suite element.\n").arg(a: fileName.toString());
156 return 0;
157}
158
159void TestSuite::toXML(XMLWriter &receiver, TestCase *const tc) const
160{
161 // TODO startElement() endElement() calls can be simplified.
162
163 Q_ASSERT(tc);
164
165 receiver.startDocument();
166 /* <test-suite> */
167 QXmlStreamAttributes test_suiteAtts;
168 test_suiteAtts.append(qualifiedName: QLatin1String("CatalogDesignDate"), value: m_designDate.toString(format: Qt::ISODate));
169 test_suiteAtts.append(qualifiedName: QLatin1String("version"), value: m_version);
170 test_suiteAtts.append(qualifiedName: QLatin1String("SourceOffsetPath"), value: QString());
171 test_suiteAtts.append(qualifiedName: QLatin1String("ResultOffsetPath"), value: QString());
172 test_suiteAtts.append(qualifiedName: QLatin1String("XQueryQueryOffsetPath"), value: QString());
173 test_suiteAtts.append(qualifiedName: QLatin1String("QueryXQueryOffsetPath"), value: QString());
174 test_suiteAtts.append(qualifiedName: QLatin1String("XQueryFileExtension"), value: QString());
175 test_suiteAtts.append(qualifiedName: QLatin1String("XQueryXFileExtension"), value: QString());
176
177 receiver.startPrefixMapping(prefix: QString(), namespaceURI: Global::xqtsCatalogNS);
178 receiver.startElement(qName: QLatin1String("test-suite"), atts: test_suiteAtts);
179
180 /* <test-group> */
181 QXmlStreamAttributes test_groupAtts;
182 test_groupAtts.append(qualifiedName: QLatin1String("GeneratedGroupByPatternistSDKRunSuite"), value: QString());
183 receiver.startElement(qName: QLatin1String("test-group"), atts: test_groupAtts);
184
185 /* <GroupInfo> */
186 receiver.startElement(qName: QLatin1String("GroupInfo"), atts: test_groupAtts);
187
188 /* <title> */
189 receiver.startElement(qName: QLatin1String("title"), atts: test_groupAtts);
190 receiver.characters(ch: QLatin1String("Contains the test case generated by PatternistSDKRunSuite."));
191
192 /* </title> */
193 receiver.endElement(qName: QLatin1String("title"));
194
195 /* <description> */
196 receiver.startElement(qName: QLatin1String("description"), atts: test_groupAtts);
197 /* </description> */
198 receiver.endElement(qName: QLatin1String("description"));
199
200 /* </GroupInfo> */
201 receiver.endElement(qName: QLatin1String("GroupInfo"));
202
203 /* <test-case> */
204 tc->toXML(receiver);
205 /* </test-case> */
206
207 /* </test-group> */
208 receiver.endElement(qName: QLatin1String("test-group"));
209
210 /* </test-suite> */
211 receiver.endElement(qName: QLatin1String("test-suite"));
212}
213
214QString TestSuite::version() const
215{
216 return m_version;
217}
218
219QDate TestSuite::designDate() const
220{
221 return m_designDate;
222}
223
224void TestSuite::setVersion(const QString &ver)
225{
226 m_version = ver;
227}
228
229void TestSuite::setDesignDate(const QDate &date)
230{
231 m_designDate = date;
232}
233
234TestContainer *TestSuite::parent() const
235{
236 return 0;
237}
238
239// vim: et:ts=4:sw=4:sts=4
240

source code of qtxmlpatterns/tests/auto/xmlpatternssdk/TestSuite.cpp