| 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 <QtTest/QtTest> |
| 30 | |
| 31 | #include <qcoreapplication.h> |
| 32 | #include <qdebug.h> |
| 33 | #include <qxml.h> |
| 34 | |
| 35 | class tst_QXml : public QObject |
| 36 | { |
| 37 | Q_OBJECT |
| 38 | |
| 39 | private slots: |
| 40 | #if QT_DEPRECATED_SINCE(5, 15) |
| 41 | void getSetCheck(); |
| 42 | void interpretedAs0D() const; |
| 43 | #ifndef QT_NO_EXCEPTIONS |
| 44 | void exception(); |
| 45 | #endif |
| 46 | #endif // QT_DEPRECATED_SINCE(5, 15) |
| 47 | }; |
| 48 | |
| 49 | #if QT_DEPRECATED_SINCE(5, 15) |
| 50 | |
| 51 | class MyXmlEntityResolver : public QXmlEntityResolver |
| 52 | { |
| 53 | public: |
| 54 | MyXmlEntityResolver() : QXmlEntityResolver() {} |
| 55 | QString errorString() const { return QString(); } |
| 56 | bool resolveEntity(const QString &, const QString &, QXmlInputSource *&) { return false; } |
| 57 | }; |
| 58 | |
| 59 | class MyXmlContentHandler : public QXmlContentHandler |
| 60 | { |
| 61 | public: |
| 62 | MyXmlContentHandler() : QXmlContentHandler() {} |
| 63 | bool characters(const QString &) { return false; } |
| 64 | bool endDocument() { return false; } |
| 65 | bool endElement(const QString &, const QString &, const QString &) { return false; } |
| 66 | bool endPrefixMapping(const QString &) { return false; } |
| 67 | QString errorString() const { return QString(); } |
| 68 | bool ignorableWhitespace(const QString &) { return false; } |
| 69 | bool processingInstruction(const QString &, const QString &) { return false; } |
| 70 | void setDocumentLocator(QXmlLocator *) { } |
| 71 | bool skippedEntity(const QString &) { return false; } |
| 72 | bool startDocument() { return false; } |
| 73 | bool startElement(const QString &, const QString &, const QString &, const QXmlAttributes &) { return false; } |
| 74 | bool startPrefixMapping(const QString &, const QString &) { return false; } |
| 75 | }; |
| 76 | |
| 77 | class MyXmlErrorHandler : public QXmlErrorHandler |
| 78 | { |
| 79 | public: |
| 80 | MyXmlErrorHandler() : QXmlErrorHandler() {} |
| 81 | QString errorString() const { return QString(); } |
| 82 | bool error(const QXmlParseException &) { return false; } |
| 83 | bool fatalError(const QXmlParseException &) { return false; } |
| 84 | bool warning(const QXmlParseException &) { return false; } |
| 85 | }; |
| 86 | |
| 87 | class MyXmlLexicalHandler : public QXmlLexicalHandler |
| 88 | { |
| 89 | public: |
| 90 | MyXmlLexicalHandler() : QXmlLexicalHandler() {} |
| 91 | bool comment(const QString &) { return false; } |
| 92 | bool endCDATA() { return false; } |
| 93 | bool endDTD() { return false; } |
| 94 | bool endEntity(const QString &) { return false; } |
| 95 | QString errorString() const { return QString(); } |
| 96 | bool startCDATA() { return false; } |
| 97 | bool startDTD(const QString &, const QString &, const QString &) { return false; } |
| 98 | bool startEntity(const QString &) { return false; } |
| 99 | }; |
| 100 | |
| 101 | class MyXmlDeclHandler : public QXmlDeclHandler |
| 102 | { |
| 103 | public: |
| 104 | MyXmlDeclHandler() : QXmlDeclHandler() {} |
| 105 | bool attributeDecl(const QString &, const QString &, const QString &, const QString &, const QString &) { return false; } |
| 106 | QString errorString() const { return QString(); } |
| 107 | bool externalEntityDecl(const QString &, const QString &, const QString &) { return false; } |
| 108 | bool internalEntityDecl(const QString &, const QString &) { return false; } |
| 109 | }; |
| 110 | |
| 111 | // Testing get/set functions |
| 112 | void tst_QXml::getSetCheck() |
| 113 | { |
| 114 | QXmlSimpleReader obj1; |
| 115 | // QXmlEntityResolver* QXmlSimpleReader::entityResolver() |
| 116 | // void QXmlSimpleReader::setEntityResolver(QXmlEntityResolver*) |
| 117 | MyXmlEntityResolver *var1 = new MyXmlEntityResolver; |
| 118 | obj1.setEntityResolver(var1); |
| 119 | QCOMPARE(static_cast<QXmlEntityResolver *>(var1), obj1.entityResolver()); |
| 120 | obj1.setEntityResolver((QXmlEntityResolver *)0); |
| 121 | QCOMPARE((QXmlEntityResolver *)0, obj1.entityResolver()); |
| 122 | delete var1; |
| 123 | |
| 124 | // QXmlContentHandler* QXmlSimpleReader::contentHandler() |
| 125 | // void QXmlSimpleReader::setContentHandler(QXmlContentHandler*) |
| 126 | MyXmlContentHandler *var2 = new MyXmlContentHandler; |
| 127 | obj1.setContentHandler(var2); |
| 128 | QCOMPARE(static_cast<QXmlContentHandler *>(var2), obj1.contentHandler()); |
| 129 | obj1.setContentHandler((QXmlContentHandler *)0); |
| 130 | QCOMPARE((QXmlContentHandler *)0, obj1.contentHandler()); |
| 131 | delete var2; |
| 132 | |
| 133 | // QXmlErrorHandler* QXmlSimpleReader::errorHandler() |
| 134 | // void QXmlSimpleReader::setErrorHandler(QXmlErrorHandler*) |
| 135 | MyXmlErrorHandler *var3 = new MyXmlErrorHandler; |
| 136 | obj1.setErrorHandler(var3); |
| 137 | QCOMPARE(static_cast<QXmlErrorHandler *>(var3), obj1.errorHandler()); |
| 138 | obj1.setErrorHandler((QXmlErrorHandler *)0); |
| 139 | QCOMPARE((QXmlErrorHandler *)0, obj1.errorHandler()); |
| 140 | delete var3; |
| 141 | |
| 142 | // QXmlLexicalHandler* QXmlSimpleReader::lexicalHandler() |
| 143 | // void QXmlSimpleReader::setLexicalHandler(QXmlLexicalHandler*) |
| 144 | MyXmlLexicalHandler *var4 = new MyXmlLexicalHandler; |
| 145 | obj1.setLexicalHandler(var4); |
| 146 | QCOMPARE(static_cast<QXmlLexicalHandler *>(var4), obj1.lexicalHandler()); |
| 147 | obj1.setLexicalHandler((QXmlLexicalHandler *)0); |
| 148 | QCOMPARE((QXmlLexicalHandler *)0, obj1.lexicalHandler()); |
| 149 | delete var4; |
| 150 | |
| 151 | // QXmlDeclHandler* QXmlSimpleReader::declHandler() |
| 152 | // void QXmlSimpleReader::setDeclHandler(QXmlDeclHandler*) |
| 153 | MyXmlDeclHandler *var5 = new MyXmlDeclHandler; |
| 154 | obj1.setDeclHandler(var5); |
| 155 | QCOMPARE(static_cast<QXmlDeclHandler *>(var5), obj1.declHandler()); |
| 156 | obj1.setDeclHandler((QXmlDeclHandler *)0); |
| 157 | QCOMPARE((QXmlDeclHandler *)0, obj1.declHandler()); |
| 158 | delete var5; |
| 159 | } |
| 160 | |
| 161 | void tst_QXml::interpretedAs0D() const |
| 162 | { |
| 163 | /* See task 172632. */ |
| 164 | |
| 165 | class MyHandler : public QXmlDefaultHandler |
| 166 | { |
| 167 | public: |
| 168 | virtual bool startElement(const QString &namespaceURI, |
| 169 | const QString &localName, |
| 170 | const QString &qName, |
| 171 | const QXmlAttributes &atts) |
| 172 | { |
| 173 | Q_UNUSED(namespaceURI); |
| 174 | Q_UNUSED(localName); |
| 175 | Q_UNUSED(qName); |
| 176 | attrName = atts.qName(index: 0); |
| 177 | attrCount = atts.count(); |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | QString attrName; |
| 182 | int attrCount; |
| 183 | }; |
| 184 | |
| 185 | const QString document(QLatin1String("<element " ) + |
| 186 | QChar(0x010D) + |
| 187 | QLatin1String("reated-by=\"an attr value\"/>" )); |
| 188 | |
| 189 | QString testFile = QFINDTESTDATA("0x010D.xml" ); |
| 190 | if (testFile.isEmpty()) |
| 191 | QFAIL("Cannot find test file 0x010D.xml!" ); |
| 192 | QFile f(testFile); |
| 193 | QVERIFY(f.open(QIODevice::ReadOnly)); |
| 194 | QXmlInputSource data(&f); |
| 195 | |
| 196 | QXmlSimpleReader reader; |
| 197 | |
| 198 | MyHandler myHandler; |
| 199 | reader.setContentHandler(&myHandler); |
| 200 | reader.setErrorHandler(&myHandler); |
| 201 | |
| 202 | QVERIFY(reader.parse(&data)); |
| 203 | |
| 204 | QCOMPARE(myHandler.attrCount, 1); |
| 205 | QCOMPARE(myHandler.attrName, QChar(0x010D) + QString::fromLatin1("reated-by" )); |
| 206 | } |
| 207 | |
| 208 | #ifndef QT_NO_EXCEPTIONS |
| 209 | void tst_QXml::exception() |
| 210 | { |
| 211 | QString message = QString::fromLatin1(str: "message" ); |
| 212 | int column = 3; |
| 213 | int line = 2; |
| 214 | QString publicId = QString::fromLatin1(str: "publicId" ); |
| 215 | QString systemId = QString::fromLatin1(str: "systemId" ); |
| 216 | |
| 217 | try { |
| 218 | QXmlParseException e(message, column, line, publicId, systemId); |
| 219 | throw e; |
| 220 | } |
| 221 | catch (QXmlParseException e) { |
| 222 | QCOMPARE(e.message(), message); |
| 223 | QCOMPARE(e.columnNumber(), column); |
| 224 | QCOMPARE(e.lineNumber(), line); |
| 225 | QCOMPARE(e.publicId(), publicId); |
| 226 | QCOMPARE(e.systemId(), systemId); |
| 227 | } |
| 228 | } |
| 229 | #endif |
| 230 | |
| 231 | #endif // QT_DEPRECATED_SINCE(5, 15) |
| 232 | |
| 233 | QTEST_MAIN(tst_QXml) |
| 234 | #include "tst_qxml.moc" |
| 235 | |