| 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 | |
| 30 | #include <QtTest/QtTest> |
| 31 | |
| 32 | #include <cstdlib> |
| 33 | |
| 34 | #include "TestSuite.h" |
| 35 | #include "TestSuiteResult.h" |
| 36 | #include "XMLWriter.h" |
| 37 | #include "ExitCode.h" |
| 38 | #include "Worker.h" |
| 39 | #include "private/qautoptr_p.h" |
| 40 | #include "tst_suitetest.h" |
| 41 | |
| 42 | using namespace QPatternistSDK; |
| 43 | |
| 44 | tst_SuiteTest::tst_SuiteTest(const SuiteType suiteType, |
| 45 | const bool alwaysRun) : m_existingBaseline(QFINDTESTDATA("Baseline.xml" )) |
| 46 | , m_candidateBaseline(QDir::current().filePath(fileName: "CandidateBaseline.xml" )) |
| 47 | , m_abortRun(!alwaysRun && !QFile::exists(fileName: QLatin1String("runTests" ))) |
| 48 | , m_suiteType(suiteType) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | /*! |
| 53 | Returns an absolute path to the XQTS catalog, or flags a failure using |
| 54 | QTestLib's mechanisms. |
| 55 | */ |
| 56 | void tst_SuiteTest::initTestCase() |
| 57 | { |
| 58 | catalogPath(write&: m_catalogPath); |
| 59 | } |
| 60 | |
| 61 | /*! |
| 62 | Just runs the test suite and writes the result to m_candidateBaseline. |
| 63 | */ |
| 64 | void tst_SuiteTest::runTestSuite() const |
| 65 | { |
| 66 | if(m_abortRun) |
| 67 | QSKIP("The test suite is not available, no tests are run." ); |
| 68 | |
| 69 | QByteArray range = qgetenv(varName: "XMLPATTERNSXQTS_TESTRANGE" ); |
| 70 | char *endptr; |
| 71 | TreeItem::executeRange.first = strtol(nptr: range.constData(), endptr: &endptr, base: 10); |
| 72 | long e = 0; |
| 73 | if (endptr - range.constData() < range.size()) |
| 74 | e = strtol(nptr: endptr + 1, endptr: &endptr, base: 10); |
| 75 | TreeItem::executeRange.second = (e == 0 ? INT_MAX : e); |
| 76 | QString errMsg; |
| 77 | const QFileInfo fi(m_catalogPath); |
| 78 | const QUrl catalogPath(QUrl::fromLocalFile(localfile: fi.absoluteFilePath())); |
| 79 | |
| 80 | TestSuite::SuiteType suiteType(TestSuite::XQuerySuite); |
| 81 | switch (m_suiteType) { |
| 82 | case XQuerySuite: |
| 83 | suiteType = TestSuite::XQuerySuite; |
| 84 | break; |
| 85 | case XsltSuite: |
| 86 | suiteType = TestSuite::XsltSuite; |
| 87 | break; |
| 88 | case XsdSuite: |
| 89 | suiteType = TestSuite::XsdSuite; |
| 90 | break; |
| 91 | default: |
| 92 | break; |
| 93 | } |
| 94 | |
| 95 | TestSuite *const ts = TestSuite::openCatalog(catalogFile: catalogPath, errorMsg&: errMsg, useExclusionList: true, type: suiteType); |
| 96 | |
| 97 | QVERIFY2(ts, qPrintable(QString::fromLatin1("Failed to open the catalog, maybe it doesn't exist or is broken: %1" ).arg(errMsg))); |
| 98 | |
| 99 | /* Run the tests, and serialize the result(as according to XQTSResult.xsd) to standard out. */ |
| 100 | TestSuiteResult *const result = ts->runSuite(); |
| 101 | QVERIFY(result); |
| 102 | |
| 103 | QFile out(m_candidateBaseline); |
| 104 | QVERIFY(out.open(QIODevice::WriteOnly)); |
| 105 | |
| 106 | XMLWriter serializer(&out); |
| 107 | result->toXML(receiver&: serializer); |
| 108 | |
| 109 | delete result; |
| 110 | delete ts; |
| 111 | } |
| 112 | |
| 113 | void tst_SuiteTest::checkTestSuiteResult() const |
| 114 | { |
| 115 | if(m_abortRun) |
| 116 | QSKIP("This test takes too long time to run on the majority of platforms." ); |
| 117 | |
| 118 | typedef QList<QFileInfo> QFileInfoList; |
| 119 | |
| 120 | const QFileInfo baseline(m_existingBaseline); |
| 121 | const QFileInfo result(m_candidateBaseline); |
| 122 | QFileInfoList list; |
| 123 | list.append(t: baseline); |
| 124 | list.append(t: result); |
| 125 | |
| 126 | const QFileInfoList::const_iterator end(list.constEnd()); |
| 127 | |
| 128 | QEventLoop eventLoop; |
| 129 | const QPatternist::AutoPtr<Worker> worker(new Worker(eventLoop, m_existingBaseline, result)); |
| 130 | |
| 131 | /* Passed to ResultThreader so it knows what kind of file it is handling. */ |
| 132 | ResultThreader::Type type = ResultThreader::Baseline; |
| 133 | |
| 134 | for(QFileInfoList::const_iterator it(list.constBegin()); it != end; ++it) |
| 135 | { |
| 136 | QFileInfo i(*it); |
| 137 | i.makeAbsolute(); |
| 138 | |
| 139 | QVERIFY2(i.exists(), qPrintable(QString::fromLatin1("File %1 does not exist." ) |
| 140 | .arg(i.fileName()))); |
| 141 | |
| 142 | QFile *const file = new QFile(i.absoluteFilePath(), worker.data()); |
| 143 | |
| 144 | QVERIFY2(file->open(QIODevice::ReadOnly), qPrintable(QString::fromLatin1("Could not open file %1 for reading." ) |
| 145 | .arg(i.fileName()))); |
| 146 | |
| 147 | ResultThreader *handler = new ResultThreader(file, type, worker.data()); |
| 148 | |
| 149 | QObject::connect(sender: handler, SIGNAL(finished()), receiver: worker.data(), SLOT(threadFinished())); |
| 150 | |
| 151 | handler->start(); /* Start the thread. It now parses the file |
| 152 | and emits threadFinished() when done. */ |
| 153 | type = ResultThreader::Result; |
| 154 | } |
| 155 | |
| 156 | const int exitCode = eventLoop.exec(); |
| 157 | |
| 158 | QCOMPARE(exitCode, 0); |
| 159 | } |
| 160 | |
| 161 | bool tst_SuiteTest::dontRun() const |
| 162 | { |
| 163 | return m_abortRun; |
| 164 | } |
| 165 | |
| 166 | // vim: et:ts=4:sw=4:sts=4 |
| 167 | |