1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite 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 The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/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 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include <QBuffer>
43 #include <QUrl>
44 #include <QXmlAttributes>
45 #include <QXmlQuery>
46 #include <QXmlResultItems>
47 #include <QXmlSerializer>
48 #include <qxmlquery_p.h>
49 
50 #include "DebugExpressionFactory.h"
51 #include "ExternalSourceLoader.h"
52 #include "Global.h"
53 #include "TestSuite.h"
54 #include "XMLWriter.h"
55 
56 #include "TestCase.h"
57 
58 using namespace QPatternistSDK;
59 using namespace QPatternist;
60 
61 // STATIC DATA
62 static const DebugExpressionFactory::Ptr s_exprFact(new DebugExpressionFactory());
63 
TestCase()64 TestCase::TestCase() : m_result(0)
65 {
66 }
67 
~TestCase()68 TestCase::~TestCase()
69 {
70     delete m_result;
71 }
72 
execute(const ExecutionStage stage,TestSuite *)73 TestResult::List TestCase::execute(const ExecutionStage stage,
74                                    TestSuite *)
75 {
76     if(name() == QLatin1String("Constr-cont-document-3"))
77     {
78             TestResult::List result;
79             result.append(createTestResult(TestResult::Fail, QLatin1String("Skipped this test, because we loop infinitely on it.")));
80             return result;
81     }
82     else if(name() == QLatin1String("Axes089"))
83     {
84             TestResult::List result;
85             result.append(createTestResult(TestResult::Fail, QLatin1String("Skipped this test, we crash on it.")));
86             return result;
87     }
88 
89     qDebug() << "Running test case: " << name();
90 
91     return execute(stage);
92 
93     Q_ASSERT(false);
94     return TestResult::List();
95 }
96 
createTestResult(const TestResult::Status status,const QString & comment) const97 TestResult *TestCase::createTestResult(const TestResult::Status status,
98                                              const QString &comment) const
99 {
100     TestResult *const result = new TestResult(name(),
101                                               status,
102                                               0 /* We don't have an AST. */,
103                                               ErrorHandler::Message::List(),
104                                               QPatternist::Item::List(),
105                                               QString());
106     result->setComment(comment);
107     return result;
108 }
109 
execute(const ExecutionStage stage)110 TestResult::List TestCase::execute(const ExecutionStage stage)
111 {
112     ErrorHandler errHandler;
113     ErrorHandler::installQtMessageHandler(&errHandler);
114 
115     pDebug() << "TestCase::execute()";
116     delete m_result;
117 
118     QXmlQuery query(language(), Global::namePoolAsPublic());
119 
120     query.d->setExpressionFactory(s_exprFact);
121     query.setInitialTemplateName(initialTemplateName());
122 
123     QXmlQuery openDoc(query.namePool());
124 
125     if(contextItemSource().isValid())
126     {
127         openDoc.setQuery(QString::fromLatin1("doc('") + contextItemSource().toString() + QLatin1String("')"));
128         Q_ASSERT(openDoc.isValid());
129         QXmlResultItems result;
130 
131         openDoc.evaluateTo(&result);
132         const QXmlItem item(result.next());
133         Q_ASSERT(!item.isNull());
134         query.setFocus(item);
135     }
136 
137     TestResult::List retval;
138 
139     const Scenario scen(scenario());
140     TestResult::Status resultStatus = TestResult::Unknown;
141 
142     bool ok = false;
143     const QString queryString(sourceCode(ok));
144 
145     if(!ok)
146     {
147         /* Loading the query file failed, or similar. */
148         resultStatus = TestResult::Fail;
149 
150         m_result = new TestResult(name(), resultStatus, s_exprFact->astTree(),
151                                   errHandler.messages(), QPatternist::Item::List(), QString());
152         retval.append(m_result);
153         ErrorHandler::installQtMessageHandler(0);
154         changed(this);
155         return retval;
156     }
157 
158     query.setMessageHandler(&errHandler);
159     QXmlNamePool namePool(query.namePool());
160 
161     /* Bind variables. */
162     QPatternist::ExternalVariableLoader::Ptr loader(externalVariableLoader());
163     if(loader)
164     {
165         Q_ASSERT(loader);
166         const ExternalSourceLoader::VariableMap vMap(static_cast<const ExternalSourceLoader *>(loader.data())->variableMap());
167         const QStringList variables(vMap.keys());
168 
169         for(int i = 0; i < variables.count(); ++i)
170         {
171             const QXmlName name(namePool, variables.at(i));
172             const QXmlItem val(QPatternist::Item::toPublic(loader->evaluateSingleton(name, QPatternist::DynamicContext::Ptr())));
173             query.bindVariable(name, val);
174         }
175     }
176 
177     /* We pass in the testCasePath(), such that the base URI is correct fort
178      * XSL-T stylesheets. */
179     query.setQuery(queryString, testCasePath());
180 
181     if(!query.isValid())
182     {
183         pDebug() << "Got compilation exception.";
184         resultStatus = TestBaseLine::scanErrors(errHandler.messages(), baseLines());
185 
186         Q_ASSERT(resultStatus != TestResult::Unknown);
187         m_result = new TestResult(name(), resultStatus, s_exprFact->astTree(),
188                                   errHandler.messages(), QPatternist::Item::List(), QString());
189         retval.append(m_result);
190         ErrorHandler::installQtMessageHandler(0);
191         changed(this);
192         return retval;
193     }
194 
195     if(stage == CompileOnly)
196     {
197         m_result = new TestResult(name(), TestResult::Fail, s_exprFact->astTree(),
198                                   errHandler.messages(), QPatternist::Item::List(), QString());
199         retval.append(m_result);
200         return retval;
201     }
202 
203     Q_ASSERT(stage == CompileAndRun);
204 
205     if(scen == ParseError) /* We're supposed to have received an error
206                               at this point. */
207     {
208         m_result = new TestResult(name(), TestResult::Fail, s_exprFact->astTree(),
209                                   errHandler.messages(), QPatternist::Item::List(), QString());
210         ErrorHandler::installQtMessageHandler(0);
211         retval.append(m_result);
212         changed(this);
213         return retval;
214     }
215 
216     QPatternist::Item::List itemList;
217 
218     QByteArray output;
219     QBuffer buffer(&output);
220     buffer.open(QIODevice::WriteOnly);
221 
222     QXmlSerializer serializer(query, &buffer);
223 
224     pDebug() << "-------------------------- evaluateToPushCallback() ---------------------------- ";
225     const bool success = query.evaluateTo(&serializer);
226     pDebug() << "------------------------------------------------------------------------------------ ";
227 
228     buffer.close();
229 
230     const QString serialized(QString::fromUtf8(output.constData(), output.size()));
231 
232     if(!success)
233     {
234         resultStatus = TestBaseLine::scanErrors(errHandler.messages(), baseLines());
235 
236         Q_ASSERT(resultStatus != TestResult::Unknown);
237         m_result = new TestResult(name(), resultStatus, s_exprFact->astTree(),
238                                   errHandler.messages(), QPatternist::Item::List(), serialized);
239         retval.append(m_result);
240         ErrorHandler::installQtMessageHandler(0);
241         changed(this);
242         return retval;
243     }
244 
245     /* It's a regular test. */
246     Q_ASSERT(scen == Standard || scen == RuntimeError);
247 
248     resultStatus = TestBaseLine::scan(serialized, baseLines());
249     Q_ASSERT(resultStatus != TestResult::Unknown);
250 
251     /* Check that errHandler()->messages() at most only contains
252      * warnings, since it shouldn't have errors at this point. */
253     const ErrorHandler::Message::List errors (errHandler.messages());
254     const ErrorHandler::Message::List::const_iterator end(errors.constEnd());
255     ErrorHandler::Message::List::const_iterator it(errors.constBegin());
256 
257     for(; it != end; ++it)
258     {
259         const QtMsgType type = (*it).type();
260         if(type == QtFatalMsg)
261         {
262             m_result = new TestResult(name(), TestResult::Fail, s_exprFact->astTree(),
263                                       errHandler.messages(), itemList, serialized);
264             retval.append(m_result);
265             ErrorHandler::installQtMessageHandler(0);
266             changed(this);
267             return retval;
268         }
269     }
270 
271     m_result = new TestResult(name(), resultStatus, s_exprFact->astTree(),
272                               errHandler.messages(), itemList, serialized);
273     retval.append(m_result);
274     ErrorHandler::installQtMessageHandler(0);
275     changed(this);
276     return retval;
277 }
278 
scenarioFromString(const QString & string)279 TestCase::Scenario TestCase::scenarioFromString(const QString &string)
280 {
281     if(string == QLatin1String("standard"))
282         return Standard;
283     else if(string == QLatin1String("parse-error"))
284         return ParseError;
285     else if(string == QLatin1String("runtime-error"))
286         return RuntimeError;
287     else if(string == QLatin1String("trivial"))
288         return Trivial;
289     else
290     {
291         Q_ASSERT_X(false, Q_FUNC_INFO,
292                    qPrintable(QString::fromLatin1("Invalid string representation for the scenario-enum: %1").arg(string)));
293         return ParseError; /* Silence GCC. */
294     }
295 }
296 
toXML(XMLWriter & receiver) const297 void TestCase::toXML(XMLWriter &receiver) const
298 {
299     /* <test-case> */
300     QXmlAttributes test_caseAtts;
301     test_caseAtts.append(QLatin1String("is-XPath2"), QString(),
302                          QLatin1String("is-XPath2"), isXPath() ? QLatin1String("true")
303                                                                : QLatin1String("false"));
304     test_caseAtts.append(QLatin1String("name"), QString(), QLatin1String("name"), name());
305     test_caseAtts.append(QLatin1String("creator"), QString(), QLatin1String("creator"), creator());
306     QString scen;
307     switch(scenario())
308     {
309         case Standard:
310         {
311             scen = QLatin1String("standard");
312             break;
313         }
314         case ParseError:
315         {
316             scen = QLatin1String("parse-error");
317             break;
318         }
319         case RuntimeError:
320         {
321             scen = QLatin1String("runtime-error");
322             break;
323         }
324         case Trivial:
325         {
326             scen = QLatin1String("trivial");
327             break;
328         }
329         default: /* includes 'AnyError' */
330             Q_ASSERT(false);
331     }
332     test_caseAtts.append(QLatin1String("scenario"), QString(), QLatin1String("scenario"), scen);
333     test_caseAtts.append(QLatin1String(QLatin1String("FilePath")), QString(),
334                          QLatin1String("FilePath"), QString());
335     receiver.startElement(QLatin1String("test-case"), test_caseAtts);
336 
337     /* <description> */
338     receiver.startElement(QLatin1String("description"), test_caseAtts);
339     receiver.characters(description());
340 
341     /* </description> */
342     receiver.endElement(QLatin1String("description"));
343 
344     /* <query> */
345     QXmlAttributes queryAtts;
346     queryAtts.append(QLatin1String("date"), QString(), QLatin1String("date"), /* This date is a dummy. */
347                      QDate::currentDate().toString(Qt::ISODate));
348     queryAtts.append(QLatin1String("name"), QString(), QLatin1String("name"), testCasePath().toString());
349     receiver.startElement(QLatin1String("query"), queryAtts);
350 
351     /* </query> */
352     receiver.endElement(QLatin1String("query"));
353 
354     /* Note: this is invalid, we don't add spec-citation. */
355     TestBaseLine::List bls(baseLines());
356     const TestBaseLine::List::const_iterator end(bls.constEnd());
357     TestBaseLine::List::const_iterator it(bls.constBegin());
358 
359     for(; it != end; ++it)
360         (*it)->toXML(receiver);
361 
362     /* </test-case> */
363     receiver.endElement(QLatin1String("test-case"));
364 }
365 
displayName(const Scenario scen)366 QString TestCase::displayName(const Scenario scen)
367 {
368     switch(scen)
369     {
370         case Standard:
371             return QLatin1String("Standard");
372         case ParseError:
373             return QLatin1String("Parse Error");
374         case RuntimeError:
375             return QLatin1String("Runtime Error");
376         case Trivial:
377             return QLatin1String("Trivial");
378         case AnyError:
379         {
380             Q_ASSERT(false);
381             return QString();
382         }
383     }
384 
385     Q_ASSERT(false);
386     return QString();
387 }
388 
resultSummary() const389 TestItem::ResultSummary TestCase::resultSummary() const
390 {
391     if(m_result)
392         return ResultSummary(m_result->status() == TestResult::Pass ? 1 : 0,
393                              1);
394 
395     return ResultSummary(0, 1);
396 }
397 
appendChild(TreeItem *)398 void TestCase::appendChild(TreeItem *)
399 {
400     Q_ASSERT_X(false, Q_FUNC_INFO, "Makes no sense to call appendChild() for TestCase.");
401 }
402 
child(const unsigned int) const403 TreeItem *TestCase::child(const unsigned int) const
404 {
405     return 0; /* Silence GCC */
406 }
407 
children() const408 TreeItem::List TestCase::children() const
409 {
410     return TreeItem::List();
411 }
412 
childCount() const413 unsigned int TestCase::childCount() const
414 {
415     return 0;
416 }
417 
testResult() const418 TestResult *TestCase::testResult() const
419 {
420     return m_result;
421 }
422 
isFinalNode() const423 bool TestCase::isFinalNode() const
424 {
425     return true;
426 }
427 
language() const428 QXmlQuery::QueryLanguage TestCase::language() const
429 {
430     return QXmlQuery::XQuery10;
431 }
432 
initialTemplateName() const433 QXmlName TestCase::initialTemplateName() const
434 {
435     return QXmlName();
436 }
437 
438 // vim: et:ts=4:sw=4:sts=4
439 
440