| 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 module 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 <QAbstractMessageHandler> |
| 32 | #include <QAbstractUriResolver> |
| 33 | #include <QtNetwork/QNetworkAccessManager> |
| 34 | #include <QXmlName> |
| 35 | #include <QXmlSchema> |
| 36 | |
| 37 | #include "../qabstracturiresolver/TestURIResolver.h" |
| 38 | #include "../qxmlquery/MessageSilencer.h" |
| 39 | |
| 40 | /*! |
| 41 | \class tst_QXmlSchema |
| 42 | \internal |
| 43 | \brief Tests class QXmlSchema. |
| 44 | |
| 45 | This test is not intended for testing the engine, but the functionality specific |
| 46 | to the QXmlSchema class. |
| 47 | */ |
| 48 | class tst_QXmlSchema : public QObject |
| 49 | { |
| 50 | Q_OBJECT |
| 51 | |
| 52 | private Q_SLOTS: |
| 53 | void defaultConstructor() const; |
| 54 | void copyConstructor() const; |
| 55 | void constructorQXmlNamePool() const; |
| 56 | void copyMutationTest() const; |
| 57 | |
| 58 | void isValid() const; |
| 59 | void documentUri() const; |
| 60 | |
| 61 | void loadSchemaUrlSuccess() const; |
| 62 | void loadSchemaUrlFail() const; |
| 63 | void loadSchemaDeviceSuccess() const; |
| 64 | void loadSchemaDeviceFail() const; |
| 65 | void loadSchemaDataSuccess() const; |
| 66 | void loadSchemaDataFail() const; |
| 67 | |
| 68 | void networkAccessManagerSignature() const; |
| 69 | void networkAccessManagerDefaultValue() const; |
| 70 | void networkAccessManager() const; |
| 71 | |
| 72 | void messageHandlerSignature() const; |
| 73 | void messageHandlerDefaultValue() const; |
| 74 | void messageHandler() const; |
| 75 | |
| 76 | void uriResolverSignature() const; |
| 77 | void uriResolverDefaultValue() const; |
| 78 | void uriResolver() const; |
| 79 | }; |
| 80 | |
| 81 | void tst_QXmlSchema::defaultConstructor() const |
| 82 | { |
| 83 | /* Allocate instance in different orders. */ |
| 84 | { |
| 85 | QXmlSchema schema; |
| 86 | } |
| 87 | |
| 88 | { |
| 89 | QXmlSchema schema1; |
| 90 | QXmlSchema schema2; |
| 91 | } |
| 92 | |
| 93 | { |
| 94 | QXmlSchema schema1; |
| 95 | QXmlSchema schema2; |
| 96 | QXmlSchema schema3; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void tst_QXmlSchema::copyConstructor() const |
| 101 | { |
| 102 | /* Verify that we can take a const reference, and simply do a copy of a default constructed object. */ |
| 103 | { |
| 104 | const QXmlSchema schema1; |
| 105 | QXmlSchema schema2(schema1); |
| 106 | } |
| 107 | |
| 108 | /* Copy twice. */ |
| 109 | { |
| 110 | const QXmlSchema schema1; |
| 111 | QXmlSchema schema2(schema1); |
| 112 | QXmlSchema schema3(schema2); |
| 113 | } |
| 114 | |
| 115 | /* Verify that copying default values works. */ |
| 116 | { |
| 117 | const QXmlSchema schema1; |
| 118 | const QXmlSchema schema2(schema1); |
| 119 | QCOMPARE(schema2.messageHandler(), schema1.messageHandler()); |
| 120 | QCOMPARE(schema2.uriResolver(), schema1.uriResolver()); |
| 121 | QCOMPARE(schema2.networkAccessManager(), schema1.networkAccessManager()); |
| 122 | QCOMPARE(schema2.isValid(), schema1.isValid()); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | void tst_QXmlSchema::constructorQXmlNamePool() const |
| 127 | { |
| 128 | QXmlSchema schema; |
| 129 | |
| 130 | QXmlNamePool np = schema.namePool(); |
| 131 | |
| 132 | const QXmlName name(np, QLatin1String("localName" ), |
| 133 | QLatin1String("http://example.com/" ), |
| 134 | QLatin1String("prefix" )); |
| 135 | |
| 136 | QXmlNamePool np2(schema.namePool()); |
| 137 | QCOMPARE(name.namespaceUri(np2), QString::fromLatin1("http://example.com/" )); |
| 138 | QCOMPARE(name.localName(np2), QString::fromLatin1("localName" )); |
| 139 | QCOMPARE(name.prefix(np2), QString::fromLatin1("prefix" )); |
| 140 | |
| 141 | // make sure namePool() is const |
| 142 | const QXmlSchema constSchema; |
| 143 | np = constSchema.namePool(); |
| 144 | } |
| 145 | |
| 146 | void tst_QXmlSchema::copyMutationTest() const |
| 147 | { |
| 148 | QXmlSchema schema1; |
| 149 | QXmlSchema schema2(schema1); |
| 150 | |
| 151 | // check that everything is equal |
| 152 | QVERIFY(schema2.messageHandler() == schema1.messageHandler()); |
| 153 | QVERIFY(schema2.uriResolver() == schema1.uriResolver()); |
| 154 | QVERIFY(schema2.networkAccessManager() == schema1.networkAccessManager()); |
| 155 | |
| 156 | MessageSilencer handler; |
| 157 | const TestURIResolver resolver; |
| 158 | QNetworkAccessManager manager; |
| 159 | |
| 160 | // modify schema1 |
| 161 | schema1.setMessageHandler(&handler); |
| 162 | schema1.setUriResolver(&resolver); |
| 163 | schema1.setNetworkAccessManager(&manager); |
| 164 | |
| 165 | // check that schema2 is not effected by the modifications of schema1 |
| 166 | QVERIFY(schema2.messageHandler() != schema1.messageHandler()); |
| 167 | QVERIFY(schema2.uriResolver() != schema1.uriResolver()); |
| 168 | QVERIFY(schema2.networkAccessManager() != schema1.networkAccessManager()); |
| 169 | |
| 170 | // modify schema1 further |
| 171 | const QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" |
| 172 | "<xsd:schema" |
| 173 | " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" |
| 174 | " xmlns=\"http://www.qt-project.org/xmlschematest\"" |
| 175 | " targetNamespace=\"http://www.qt-project.org/xmlschematest\"" |
| 176 | " version=\"1.0\"" |
| 177 | " elementFormDefault=\"qualified\">" |
| 178 | "</xsd:schema>" ); |
| 179 | |
| 180 | const QUrl documentUri("http://www.qt-project.org/xmlschematest" ); |
| 181 | schema1.load(data, documentUri); |
| 182 | |
| 183 | QVERIFY(schema2.isValid() != schema1.isValid()); |
| 184 | } |
| 185 | |
| 186 | void tst_QXmlSchema::isValid() const |
| 187 | { |
| 188 | /* Check default value. */ |
| 189 | QXmlSchema schema; |
| 190 | QVERIFY(!schema.isValid()); |
| 191 | } |
| 192 | |
| 193 | void tst_QXmlSchema::documentUri() const |
| 194 | { |
| 195 | const QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" |
| 196 | "<xsd:schema" |
| 197 | " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" |
| 198 | " xmlns=\"http://www.qt-project.org/xmlschematest\"" |
| 199 | " targetNamespace=\"http://www.qt-project.org/xmlschematest\"" |
| 200 | " version=\"1.0\"" |
| 201 | " elementFormDefault=\"qualified\">" |
| 202 | "</xsd:schema>" ); |
| 203 | |
| 204 | const QUrl documentUri("http://www.qt-project.org/xmlschematest" ); |
| 205 | QXmlSchema schema; |
| 206 | schema.load(data, documentUri); |
| 207 | |
| 208 | QCOMPARE(documentUri, schema.documentUri()); |
| 209 | } |
| 210 | |
| 211 | void tst_QXmlSchema::loadSchemaUrlSuccess() const |
| 212 | { |
| 213 | /** |
| 214 | TODO: put valid schema file on given url and enable test |
| 215 | const QUrl url("http://notavailable/"); |
| 216 | |
| 217 | QXmlSchema schema; |
| 218 | QVERIFY(!schema.load(url)); |
| 219 | */ |
| 220 | } |
| 221 | |
| 222 | void tst_QXmlSchema::loadSchemaUrlFail() const |
| 223 | { |
| 224 | const QUrl url("http://notavailable/" ); |
| 225 | |
| 226 | QXmlSchema schema; |
| 227 | QVERIFY(!schema.load(url)); |
| 228 | } |
| 229 | |
| 230 | void tst_QXmlSchema::loadSchemaDeviceSuccess() const |
| 231 | { |
| 232 | QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" |
| 233 | "<xsd:schema" |
| 234 | " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" |
| 235 | " xmlns=\"http://www.qt-project.org/xmlschematest\"" |
| 236 | " targetNamespace=\"http://www.qt-project.org/xmlschematest\"" |
| 237 | " version=\"1.0\"" |
| 238 | " elementFormDefault=\"qualified\">" |
| 239 | "</xsd:schema>" ); |
| 240 | |
| 241 | QBuffer buffer(&data); |
| 242 | buffer.open(openMode: QIODevice::ReadOnly); |
| 243 | |
| 244 | QXmlSchema schema; |
| 245 | QVERIFY(schema.load(&buffer)); |
| 246 | } |
| 247 | |
| 248 | void tst_QXmlSchema::loadSchemaDeviceFail() const |
| 249 | { |
| 250 | QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" |
| 251 | "<xsd:schema" |
| 252 | " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" |
| 253 | " xmlns=\"http://www.qt-project.org/xmlschematest\"" |
| 254 | " targetNamespace=\"http://www.qt-project.org/xmlschematest\"" |
| 255 | " version=\"1.0\"" |
| 256 | " elementFormDefault=\"qualified\">" |
| 257 | "</xsd:schema>" ); |
| 258 | |
| 259 | QBuffer buffer(&data); |
| 260 | // a closed device can not be loaded |
| 261 | |
| 262 | QXmlSchema schema; |
| 263 | QVERIFY(!schema.load(&buffer)); |
| 264 | } |
| 265 | |
| 266 | void tst_QXmlSchema::loadSchemaDataSuccess() const |
| 267 | { |
| 268 | const QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" |
| 269 | "<xsd:schema" |
| 270 | " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" |
| 271 | " xmlns=\"http://www.qt-project.org/xmlschematest\"" |
| 272 | " targetNamespace=\"http://www.qt-project.org/xmlschematest\"" |
| 273 | " version=\"1.0\"" |
| 274 | " elementFormDefault=\"qualified\">" |
| 275 | "</xsd:schema>" ); |
| 276 | QXmlSchema schema; |
| 277 | QVERIFY(schema.load(data)); |
| 278 | } |
| 279 | |
| 280 | void tst_QXmlSchema::loadSchemaDataFail() const |
| 281 | { |
| 282 | // empty schema can not be loaded |
| 283 | const QByteArray data; |
| 284 | |
| 285 | QXmlSchema schema; |
| 286 | QVERIFY(!schema.load(data)); |
| 287 | } |
| 288 | |
| 289 | |
| 290 | void tst_QXmlSchema::networkAccessManagerSignature() const |
| 291 | { |
| 292 | /* Const object. */ |
| 293 | const QXmlSchema schema; |
| 294 | |
| 295 | /* The function should be const. */ |
| 296 | schema.networkAccessManager(); |
| 297 | } |
| 298 | |
| 299 | void tst_QXmlSchema::networkAccessManagerDefaultValue() const |
| 300 | { |
| 301 | /* Test that the default value of network access manager is not empty. */ |
| 302 | { |
| 303 | QXmlSchema schema; |
| 304 | QVERIFY(schema.networkAccessManager() != static_cast<QNetworkAccessManager*>(0)); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | void tst_QXmlSchema::networkAccessManager() const |
| 309 | { |
| 310 | /* Test that we return the network manager that was set. */ |
| 311 | { |
| 312 | QNetworkAccessManager manager; |
| 313 | QXmlSchema schema; |
| 314 | schema.setNetworkAccessManager(&manager); |
| 315 | QCOMPARE(schema.networkAccessManager(), &manager); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | void tst_QXmlSchema::messageHandlerSignature() const |
| 320 | { |
| 321 | /* Const object. */ |
| 322 | const QXmlSchema schema; |
| 323 | |
| 324 | /* The function should be const. */ |
| 325 | schema.messageHandler(); |
| 326 | } |
| 327 | |
| 328 | void tst_QXmlSchema::messageHandlerDefaultValue() const |
| 329 | { |
| 330 | /* Test that the default value of message handler is not empty. */ |
| 331 | { |
| 332 | QXmlSchema schema; |
| 333 | QVERIFY(schema.messageHandler() != static_cast<QAbstractMessageHandler*>(0)); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | void tst_QXmlSchema::messageHandler() const |
| 338 | { |
| 339 | /* Test that we return the message handler that was set. */ |
| 340 | { |
| 341 | MessageSilencer handler; |
| 342 | |
| 343 | QXmlSchema schema; |
| 344 | schema.setMessageHandler(&handler); |
| 345 | QCOMPARE(schema.messageHandler(), static_cast<QAbstractMessageHandler *>(&handler)); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | void tst_QXmlSchema::uriResolverSignature() const |
| 350 | { |
| 351 | /* Const object. */ |
| 352 | const QXmlSchema schema; |
| 353 | |
| 354 | /* The function should be const. */ |
| 355 | schema.uriResolver(); |
| 356 | |
| 357 | /* Const object. */ |
| 358 | const TestURIResolver resolver; |
| 359 | |
| 360 | /* This should compile */ |
| 361 | QXmlSchema schema2; |
| 362 | schema2.setUriResolver(&resolver); |
| 363 | } |
| 364 | |
| 365 | void tst_QXmlSchema::uriResolverDefaultValue() const |
| 366 | { |
| 367 | /* Test that the default value of uri resolver is empty. */ |
| 368 | { |
| 369 | QXmlSchema schema; |
| 370 | QVERIFY(schema.uriResolver() == static_cast<QAbstractUriResolver*>(0)); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | void tst_QXmlSchema::uriResolver() const |
| 375 | { |
| 376 | /* Test that we return the uri resolver that was set. */ |
| 377 | { |
| 378 | TestURIResolver resolver; |
| 379 | |
| 380 | QXmlSchema schema; |
| 381 | schema.setUriResolver(&resolver); |
| 382 | QCOMPARE(schema.uriResolver(), static_cast<const QAbstractUriResolver *>(&resolver)); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | QTEST_MAIN(tst_QXmlSchema) |
| 387 | |
| 388 | #include "tst_qxmlschema.moc" |
| 389 | |