| 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 <QtDebug> |
| 30 | |
| 31 | #include <private/qacceltreeresourceloader_p.h> |
| 32 | #include <private/qnetworkaccessdelegator_p.h> |
| 33 | |
| 34 | #include "Global.h" |
| 35 | #include "TestBaseLine.h" |
| 36 | #include "TestGroup.h" |
| 37 | |
| 38 | #include "XSLTTestSuiteHandler.h" |
| 39 | |
| 40 | using namespace QPatternistSDK; |
| 41 | |
| 42 | static QNetworkAccessManager *s_networkAccessManager = 0; |
| 43 | |
| 44 | static void cleanupNetworkAccessManager() |
| 45 | { |
| 46 | delete s_networkAccessManager; |
| 47 | s_networkAccessManager = 0; |
| 48 | |
| 49 | } |
| 50 | static QNetworkAccessManager *networkAccessManager() |
| 51 | { |
| 52 | if (!s_networkAccessManager) { |
| 53 | s_networkAccessManager = new QNetworkAccessManager; |
| 54 | qAddPostRoutine(cleanupNetworkAccessManager); |
| 55 | } |
| 56 | return s_networkAccessManager; |
| 57 | } |
| 58 | |
| 59 | XSLTTestSuiteHandler::XSLTTestSuiteHandler(const QUrl &catalogFile) : m_ts(0) |
| 60 | , m_tc(0) |
| 61 | , m_baseLine(0) |
| 62 | , m_catalogFile(catalogFile) |
| 63 | , m_removeTestcase(false) |
| 64 | { |
| 65 | const QPatternist::NetworkAccessDelegator::Ptr networkDelegator(new QPatternist::NetworkAccessDelegator(networkAccessManager(), networkAccessManager())); |
| 66 | |
| 67 | m_resourceLoader = QPatternist::ResourceLoader::Ptr(new QPatternist::AccelTreeResourceLoader(Global::namePool(), |
| 68 | networkDelegator)); |
| 69 | Q_ASSERT(!m_catalogFile.isRelative()); |
| 70 | } |
| 71 | |
| 72 | bool XSLTTestSuiteHandler::startElement(const QStringRef &namespaceURI, const QStringRef &localName, |
| 73 | const QStringRef & /*qName*/, const QXmlStreamAttributes &atts) |
| 74 | { |
| 75 | if(namespaceURI != Global::xsltsCatalogNS) |
| 76 | return true; |
| 77 | |
| 78 | /* The elements are handled roughly in the order of highest occurrence in the catalog file. */ |
| 79 | if(localName == QLatin1String("testcase" )) |
| 80 | { |
| 81 | /* We pass m_ts temporarily, and change it later. */ |
| 82 | m_tc = new XQTSTestCase(TestCase::Standard, 0, QXmlQuery::XSLT20); |
| 83 | |
| 84 | m_currentQueryPath = |
| 85 | m_queryOffset.resolved(relative: QUrl(atts.value(qualifiedName: QLatin1String("FilePath" )).toString())); |
| 86 | m_currentBaselinePath = |
| 87 | m_baselineOffset.resolved(relative: QUrl(atts.value(qualifiedName: QLatin1String("FilePath" )).toString())); |
| 88 | } |
| 89 | else if(localName == QLatin1String("stylesheet" )) |
| 90 | m_tc->setQueryPath( |
| 91 | m_currentQueryPath.resolved(relative: atts.value(qualifiedName: QLatin1String("file" )).toString())); |
| 92 | else if(localName == QLatin1String("error" )) |
| 93 | { |
| 94 | m_baseLine = new TestBaseLine(TestBaseLine::ExpectedError); |
| 95 | m_baseLine->setDetails(atts.value(qualifiedName: QLatin1String("error-id" )).toString()); |
| 96 | m_tc->addBaseLine(lines: m_baseLine); |
| 97 | } |
| 98 | else if(localName == QLatin1String("testcases" )) |
| 99 | { |
| 100 | m_ts = new TestSuite(); |
| 101 | m_ts->setVersion(atts.value(qualifiedName: QLatin1String("testSuiteVersion" )).toString()); |
| 102 | |
| 103 | m_queryOffset = |
| 104 | m_catalogFile.resolved(relative: atts.value(qualifiedName: QLatin1String("InputOffsetPath" )).toString()); |
| 105 | m_baselineOffset = |
| 106 | m_catalogFile.resolved(relative: atts.value(qualifiedName: QLatin1String("ResultOffsetPath" )).toString()); |
| 107 | m_sourceOffset = |
| 108 | m_catalogFile.resolved(relative: atts.value(qualifiedName: QLatin1String("InputOffsetPath" )).toString()); |
| 109 | } |
| 110 | else if(localName == QLatin1String("source-document" )) |
| 111 | { |
| 112 | if(atts.value(qualifiedName: QLatin1String("role" )) == QLatin1String("principal" )) |
| 113 | m_tc->setContextItemSource( |
| 114 | m_sourceOffset.resolved(relative: QUrl(atts.value(qualifiedName: QLatin1String("file" )).toString()))); |
| 115 | } |
| 116 | else if(localName == QLatin1String("result-document" )) |
| 117 | { |
| 118 | m_baseLine = new TestBaseLine( |
| 119 | TestBaseLine::identifierFromString(string: atts.value(qualifiedName: QLatin1String("type" )).toString())); |
| 120 | m_baseLine->setDetails( |
| 121 | m_currentBaselinePath.resolved(relative: atts.value(qualifiedName: QLatin1String("file" )).toString()) |
| 122 | .toString()); |
| 123 | m_tc->addBaseLine(lines: m_baseLine); |
| 124 | } |
| 125 | else if(localName == QLatin1String("discretionary-feature" )) |
| 126 | { |
| 127 | const QString feature(atts.value(qualifiedName: QLatin1String("name" )).toString()); |
| 128 | |
| 129 | m_removeTestcase = feature == QLatin1String("schema_aware" ) || |
| 130 | feature == QLatin1String("namespace_axis" ) || |
| 131 | feature == QLatin1String("disabling_output_escaping" ) || |
| 132 | feature == QLatin1String("XML_1.1" ); |
| 133 | } |
| 134 | else if(localName == QLatin1String("discretionary-choice" )) |
| 135 | { |
| 136 | m_baseLine = new TestBaseLine(TestBaseLine::ExpectedError); |
| 137 | m_baseLine->setDetails(atts.value(qualifiedName: QLatin1String("name" )).toString()); |
| 138 | m_tc->addBaseLine(lines: m_baseLine); |
| 139 | const QString feature(atts.value(qualifiedName: QLatin1String("name" )).toString()); |
| 140 | |
| 141 | m_removeTestcase = feature == QLatin1String("schema_aware" ) || |
| 142 | feature == QLatin1String("namespace_axis" ) || |
| 143 | feature == QLatin1String("disabling_output_escaping" ) || |
| 144 | feature == QLatin1String("XML_1.1" ); |
| 145 | } |
| 146 | else if(localName == QLatin1String("entry-named-template" )) |
| 147 | { |
| 148 | const QString name(atts.value(qualifiedName: QLatin1String("qname" )).toString()); |
| 149 | |
| 150 | if(!name.contains(c: QLatin1Char(':'))) |
| 151 | { |
| 152 | // TODO do namespace processing |
| 153 | const QXmlName complete(Global::namePool()->allocateQName(uri: QString(), localName: name)); |
| 154 | |
| 155 | m_tc->setInitialTemplateName(complete); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | TestGroup *XSLTTestSuiteHandler::containerFor(const QString &name) |
| 163 | { |
| 164 | TestGroup *& c = m_containers[name]; |
| 165 | |
| 166 | if(!c) |
| 167 | { |
| 168 | c = new TestGroup(m_ts); |
| 169 | c->setTitle(name); |
| 170 | Q_ASSERT(c); |
| 171 | m_ts->appendChild(item: c); |
| 172 | } |
| 173 | |
| 174 | return c; |
| 175 | } |
| 176 | |
| 177 | bool XSLTTestSuiteHandler::endElement(const QStringRef &namespaceURI, |
| 178 | const QStringRef &localName, |
| 179 | const QStringRef &/*qName*/) |
| 180 | { |
| 181 | if(namespaceURI != Global::xsltsCatalogNS) |
| 182 | return true; |
| 183 | |
| 184 | /* The elements are handled roughly in the order of highest occurrence in the catalog file. */ |
| 185 | if(localName == QLatin1String("description" )) |
| 186 | { |
| 187 | if(m_tc) |
| 188 | { |
| 189 | /* We're inside a <testcase>, so the <description> belongs |
| 190 | * to the testcase. */ |
| 191 | m_tc->setDescription(m_ch.simplified()); |
| 192 | } |
| 193 | } |
| 194 | else if(localName == QLatin1String("testcase" )) |
| 195 | { |
| 196 | Q_ASSERT(m_tc->baseLines().count() >= 1); |
| 197 | Q_ASSERT(m_resourceLoader); |
| 198 | // TODO can this be removed? |
| 199 | m_tc->setExternalVariableLoader(QPatternist::ExternalVariableLoader::Ptr |
| 200 | (new ExternalSourceLoader(m_tcSourceInputs, |
| 201 | m_resourceLoader))); |
| 202 | m_tcSourceInputs.clear(); |
| 203 | |
| 204 | if(!m_removeTestcase) |
| 205 | { |
| 206 | /* |
| 207 | TestContainer *const container = containerFor(m_currentCategory); |
| 208 | delete m_tc; |
| 209 | container->removeLast(); |
| 210 | */ |
| 211 | TestContainer *const container = containerFor(name: m_currentCategory); |
| 212 | m_tc->setParent(container); |
| 213 | Q_ASSERT(m_tc); |
| 214 | container->appendChild(item: m_tc); |
| 215 | } |
| 216 | |
| 217 | m_tc = 0; |
| 218 | m_removeTestcase = false; |
| 219 | } |
| 220 | else if(localName == QLatin1String("name" )) |
| 221 | m_tc->setName(m_ch); |
| 222 | else if(localName == QLatin1String("creator" )) |
| 223 | m_tc->setCreator(m_ch); |
| 224 | else if(localName == QLatin1String("contextItem" )) |
| 225 | m_contextItemSource = m_ch; |
| 226 | else if(localName == QLatin1String("category" )) |
| 227 | m_currentCategory = m_ch; |
| 228 | |
| 229 | return true; |
| 230 | } |
| 231 | |
| 232 | bool XSLTTestSuiteHandler::characters(const QStringRef &ch) |
| 233 | { |
| 234 | m_ch = ch.toString(); |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | TestSuite *XSLTTestSuiteHandler::testSuite() const |
| 239 | { |
| 240 | return m_ts; |
| 241 | } |
| 242 | |
| 243 | // vim: et:ts=4:sw=4:sts=4 |
| 244 | |
| 245 | |