1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the test suite module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <QtTest/QtTest>
44 #include <QAbstractMessageHandler>
45 #include <QAbstractUriResolver>
46 #include <QtNetwork/QNetworkAccessManager>
49 #include <QXmlSchemaValidator>
51 #include "../qabstracturiresolver/TestURIResolver.h"
52 #include "../qxmlquery/MessageSilencer.h"
55 \class tst_QXmlSchemaValidatorValidator
57 \brief Tests class QXmlSchemaValidator.
59 This test is not intended for testing the engine, but the functionality specific
60 to the QXmlSchemaValidator class.
62 class tst_QXmlSchemaValidator : public QObject
67 void defaultConstructor() const;
68 void constructorQXmlNamePool() const;
69 void propertyInitialization() const;
70 void resetSchemaNamePool() const;
72 void loadInstanceUrlSuccess() const;
73 void loadInstanceUrlFail() const;
74 void loadInstanceDeviceSuccess() const;
75 void loadInstanceDeviceFail() const;
76 void loadInstanceDataSuccess() const;
77 void loadInstanceDataFail() const;
79 void networkAccessManagerSignature() const;
80 void networkAccessManagerDefaultValue() const;
81 void networkAccessManager() const;
83 void messageHandlerSignature() const;
84 void messageHandlerDefaultValue() const;
85 void messageHandler() const;
87 void uriResolverSignature() const;
88 void uriResolverDefaultValue() const;
89 void uriResolver() const;
92 static QXmlSchema createValidSchema()
94 const QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
96 " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
97 " xmlns=\"http://qt.nokia.com/xmlschematest\""
98 " targetNamespace=\"http://qt.nokia.com/xmlschematest\""
100 " elementFormDefault=\"qualified\">"
101 " <xsd:element name=\"myRoot\" type=\"xsd:string\"/>"
104 const QUrl documentUri("http://qt.nokia.com/xmlschematest");
107 schema.load(data, documentUri);
112 void tst_QXmlSchemaValidator::defaultConstructor() const
114 /* Allocate instance in different orders. */
117 QXmlSchemaValidator validator(schema);
124 QXmlSchemaValidator validator1(schema1);
125 QXmlSchemaValidator validator2(schema2);
131 QXmlSchemaValidator validator1(schema);
132 QXmlSchemaValidator validator2(schema);
136 void tst_QXmlSchemaValidator::propertyInitialization() const
138 /* Verify that properties set in the schema are used as default values for the validator */
140 MessageSilencer handler;
141 TestURIResolver resolver;
142 QNetworkAccessManager manager;
145 schema.setMessageHandler(&handler);
146 schema.setUriResolver(&resolver);
147 schema.setNetworkAccessManager(&manager);
149 QXmlSchemaValidator validator(schema);
150 QCOMPARE(validator.messageHandler(), static_cast<QAbstractMessageHandler *>(&handler));
151 QCOMPARE(validator.uriResolver(), static_cast<const QAbstractUriResolver *>(&resolver));
152 QCOMPARE(validator.networkAccessManager(), &manager);
156 void tst_QXmlSchemaValidator::constructorQXmlNamePool() const
158 // test that the name pool from the schema is used by
159 // the schema validator as well
162 QXmlNamePool np = schema.namePool();
164 const QXmlName name(np, QLatin1String("localName"),
165 QLatin1String("http://example.com/"),
166 QLatin1String("prefix"));
168 QXmlSchemaValidator validator(schema);
170 QXmlNamePool np2(validator.namePool());
171 QCOMPARE(name.namespaceUri(np2), QString::fromLatin1("http://example.com/"));
172 QCOMPARE(name.localName(np2), QString::fromLatin1("localName"));
173 QCOMPARE(name.prefix(np2), QString::fromLatin1("prefix"));
175 // make sure namePool() is const
176 const QXmlSchemaValidator constValidator(schema);
177 np = constValidator.namePool();
180 void tst_QXmlSchemaValidator::resetSchemaNamePool() const
183 QXmlNamePool np1 = schema1.namePool();
185 const QXmlName name1(np1, QLatin1String("localName"),
186 QLatin1String("http://example.com/"),
187 QLatin1String("prefix"));
189 QXmlSchemaValidator validator(schema1);
192 QXmlNamePool compNamePool(validator.namePool());
193 QCOMPARE(name1.namespaceUri(compNamePool), QString::fromLatin1("http://example.com/"));
194 QCOMPARE(name1.localName(compNamePool), QString::fromLatin1("localName"));
195 QCOMPARE(name1.prefix(compNamePool), QString::fromLatin1("prefix"));
199 QXmlNamePool np2 = schema2.namePool();
201 const QXmlName name2(np2, QLatin1String("remoteName"),
202 QLatin1String("http://example.com/"),
203 QLatin1String("suffix"));
205 // make sure that after re-setting the schema, the new namepool is used
206 validator.setSchema(schema2);
209 QXmlNamePool compNamePool(validator.namePool());
210 QCOMPARE(name2.namespaceUri(compNamePool), QString::fromLatin1("http://example.com/"));
211 QCOMPARE(name2.localName(compNamePool), QString::fromLatin1("remoteName"));
212 QCOMPARE(name2.prefix(compNamePool), QString::fromLatin1("suffix"));
216 void tst_QXmlSchemaValidator::loadInstanceUrlSuccess() const
219 TODO: put valid schema file on given url and enable test
220 const QXmlSchema schema(createValidSchema());
221 const QUrl url("http://notavailable/");
223 QXmlSchemaValidator validator(schema);
224 QVERIFY(!validator.validate(url));
228 void tst_QXmlSchemaValidator::loadInstanceUrlFail() const
230 const QXmlSchema schema(createValidSchema());
231 const QUrl url("http://notavailable/");
233 QXmlSchemaValidator validator(schema);
234 QVERIFY(!validator.validate(url));
237 void tst_QXmlSchemaValidator::loadInstanceDeviceSuccess() const
239 const QXmlSchema schema(createValidSchema());
241 QByteArray data( "<myRoot xmlns=\"http://qt.nokia.com/xmlschematest\">Testme</myRoot>" );
242 QBuffer buffer(&data);
243 buffer.open(QIODevice::ReadOnly);
245 QXmlSchemaValidator validator(schema);
246 QVERIFY(validator.validate(&buffer));
249 void tst_QXmlSchemaValidator::loadInstanceDeviceFail() const
251 const QXmlSchema schema(createValidSchema());
253 QByteArray data( "<myRoot xmlns=\"http://qt.nokia.com/xmlschematest\">Testme</myRoot>" );
254 QBuffer buffer(&data);
255 // a closed device can not be loaded
257 QXmlSchemaValidator validator(schema);
258 QVERIFY(!validator.validate(&buffer));
261 void tst_QXmlSchemaValidator::loadInstanceDataSuccess() const
263 const QXmlSchema schema(createValidSchema());
265 const QByteArray data( "<myRoot xmlns=\"http://qt.nokia.com/xmlschematest\">Testme</myRoot>" );
267 QXmlSchemaValidator validator(schema);
268 QVERIFY(validator.validate(data));
271 void tst_QXmlSchemaValidator::loadInstanceDataFail() const
273 const QXmlSchema schema(createValidSchema());
275 // empty instance can not be loaded
276 const QByteArray data;
278 QXmlSchemaValidator validator(schema);
279 QVERIFY(!validator.validate(data));
282 void tst_QXmlSchemaValidator::networkAccessManagerSignature() const
284 const QXmlSchema schema;
287 const QXmlSchemaValidator validator(schema);
289 /* The function should be const. */
290 validator.networkAccessManager();
293 void tst_QXmlSchemaValidator::networkAccessManagerDefaultValue() const
295 /* Test that the default value of network access manager is equal to the one from the schema. */
297 const QXmlSchema schema;
298 const QXmlSchemaValidator validator(schema);
299 QVERIFY(validator.networkAccessManager() == schema.networkAccessManager());
302 /* Test that the default value of network access manager is equal to the one from the schema. */
306 QNetworkAccessManager manager;
307 schema.setNetworkAccessManager(&manager);
309 const QXmlSchemaValidator validator(schema);
310 QVERIFY(validator.networkAccessManager() == schema.networkAccessManager());
314 void tst_QXmlSchemaValidator::networkAccessManager() const
316 /* Test that we return the network access manager that was set. */
318 QNetworkAccessManager manager;
320 const QXmlSchema schema;
321 QXmlSchemaValidator validator(schema);
323 validator.setNetworkAccessManager(&manager);
324 QCOMPARE(validator.networkAccessManager(), &manager);
327 /* Test that we return the network access manager that was set, even if the schema changed in between. */
329 QNetworkAccessManager manager;
331 const QXmlSchema schema;
332 QXmlSchemaValidator validator(schema);
334 validator.setNetworkAccessManager(&manager);
336 const QXmlSchema schema2;
337 validator.setSchema(schema2);
339 QCOMPARE(validator.networkAccessManager(), &manager);
343 void tst_QXmlSchemaValidator::messageHandlerSignature() const
345 const QXmlSchema schema;
348 const QXmlSchemaValidator validator(schema);
350 /* The function should be const. */
351 validator.messageHandler();
354 void tst_QXmlSchemaValidator::messageHandlerDefaultValue() const
356 /* Test that the default value of message handler is equal to the one from the schema. */
358 const QXmlSchema schema;
359 const QXmlSchemaValidator validator(schema);
360 QVERIFY(validator.messageHandler() == schema.messageHandler());
363 /* Test that the default value of network access manager is equal to the one from the schema. */
367 MessageSilencer handler;
368 schema.setMessageHandler(&handler);
370 const QXmlSchemaValidator validator(schema);
371 QVERIFY(validator.messageHandler() == schema.messageHandler());
375 void tst_QXmlSchemaValidator::messageHandler() const
377 /* Test that we return the message handler that was set. */
379 MessageSilencer handler;
381 const QXmlSchema schema;
382 QXmlSchemaValidator validator(schema);
384 validator.setMessageHandler(&handler);
385 QCOMPARE(validator.messageHandler(), static_cast<QAbstractMessageHandler *>(&handler));
388 /* Test that we return the message handler that was set, even if the schema changed in between. */
390 MessageSilencer handler;
392 const QXmlSchema schema;
393 QXmlSchemaValidator validator(schema);
395 validator.setMessageHandler(&handler);
397 const QXmlSchema schema2;
398 validator.setSchema(schema2);
400 QCOMPARE(validator.messageHandler(), static_cast<QAbstractMessageHandler *>(&handler));
404 void tst_QXmlSchemaValidator::uriResolverSignature() const
406 const QXmlSchema schema;
409 const QXmlSchemaValidator validator(schema);
411 /* The function should be const. */
412 validator.uriResolver();
415 const TestURIResolver resolver;
417 /* This should compile */
419 schema2.setUriResolver(&resolver);
422 void tst_QXmlSchemaValidator::uriResolverDefaultValue() const
424 /* Test that the default value of uri resolver is equal to the one from the schema. */
426 const QXmlSchema schema;
427 const QXmlSchemaValidator validator(schema);
428 QVERIFY(validator.uriResolver() == schema.uriResolver());
431 /* Test that the default value of uri resolver is equal to the one from the schema. */
435 TestURIResolver resolver;
436 schema.setUriResolver(&resolver);
438 const QXmlSchemaValidator validator(schema);
439 QVERIFY(validator.uriResolver() == schema.uriResolver());
443 void tst_QXmlSchemaValidator::uriResolver() const
445 /* Test that we return the uri resolver that was set. */
447 TestURIResolver resolver;
449 const QXmlSchema schema;
450 QXmlSchemaValidator validator(schema);
452 validator.setUriResolver(&resolver);
453 QCOMPARE(validator.uriResolver(), static_cast<const QAbstractUriResolver *>(&resolver));
456 /* Test that we return the uri resolver that was set, even if the schema changed in between. */
458 TestURIResolver resolver;
460 const QXmlSchema schema;
461 QXmlSchemaValidator validator(schema);
463 validator.setUriResolver(&resolver);
465 const QXmlSchema schema2;
466 validator.setSchema(schema2);
468 QCOMPARE(validator.uriResolver(), static_cast<const QAbstractUriResolver *>(&resolver));
472 QTEST_MAIN(tst_QXmlSchemaValidator)
474 #include "tst_qxmlschemavalidator.moc"