1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
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 #ifdef QTEST_XMLPATTERNS
46 #include <QAbstractMessageHandler>
47 #include <QAbstractUriResolver>
48 #include <QtNetwork/QNetworkAccessManager>
51 #include <QXmlSchemaValidator>
53 #include "../qabstracturiresolver/TestURIResolver.h"
54 #include "../qxmlquery/MessageSilencer.h"
57 \class tst_QXmlSchemaValidatorValidator
59 \brief Tests class QXmlSchemaValidator.
61 This test is not intended for testing the engine, but the functionality specific
62 to the QXmlSchemaValidator class.
64 class tst_QXmlSchemaValidator : public QObject
69 void defaultConstructor() const;
70 void constructorQXmlNamePool() const;
71 void propertyInitialization() const;
72 void resetSchemaNamePool() const;
74 void loadInstanceUrlSuccess() const;
75 void loadInstanceUrlFail() const;
76 void loadInstanceDeviceSuccess() const;
77 void loadInstanceDeviceFail() const;
78 void loadInstanceDataSuccess() const;
79 void loadInstanceDataFail() const;
81 void networkAccessManagerSignature() const;
82 void networkAccessManagerDefaultValue() const;
83 void networkAccessManager() const;
85 void messageHandlerSignature() const;
86 void messageHandlerDefaultValue() const;
87 void messageHandler() const;
89 void uriResolverSignature() const;
90 void uriResolverDefaultValue() const;
91 void uriResolver() const;
94 static QXmlSchema createValidSchema()
96 const QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
98 " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
99 " xmlns=\"http://qt.nokia.com/xmlschematest\""
100 " targetNamespace=\"http://qt.nokia.com/xmlschematest\""
102 " elementFormDefault=\"qualified\">"
103 " <xsd:element name=\"myRoot\" type=\"xsd:string\"/>"
106 const QUrl documentUri("http://qt.nokia.com/xmlschematest");
109 schema.load(data, documentUri);
114 void tst_QXmlSchemaValidator::defaultConstructor() const
116 /* Allocate instance in different orders. */
119 QXmlSchemaValidator validator(schema);
126 QXmlSchemaValidator validator1(schema1);
127 QXmlSchemaValidator validator2(schema2);
133 QXmlSchemaValidator validator1(schema);
134 QXmlSchemaValidator validator2(schema);
138 void tst_QXmlSchemaValidator::propertyInitialization() const
140 /* Verify that properties set in the schema are used as default values for the validator */
142 MessageSilencer handler;
143 TestURIResolver resolver;
144 QNetworkAccessManager manager;
147 schema.setMessageHandler(&handler);
148 schema.setUriResolver(&resolver);
149 schema.setNetworkAccessManager(&manager);
151 QXmlSchemaValidator validator(schema);
152 QCOMPARE(validator.messageHandler(), static_cast<QAbstractMessageHandler *>(&handler));
153 QCOMPARE(validator.uriResolver(), static_cast<const QAbstractUriResolver *>(&resolver));
154 QCOMPARE(validator.networkAccessManager(), &manager);
158 void tst_QXmlSchemaValidator::constructorQXmlNamePool() const
160 // test that the name pool from the schema is used by
161 // the schema validator as well
164 QXmlNamePool np = schema.namePool();
166 const QXmlName name(np, QLatin1String("localName"),
167 QLatin1String("http://example.com/"),
168 QLatin1String("prefix"));
170 QXmlSchemaValidator validator(schema);
172 QXmlNamePool np2(validator.namePool());
173 QCOMPARE(name.namespaceUri(np2), QString::fromLatin1("http://example.com/"));
174 QCOMPARE(name.localName(np2), QString::fromLatin1("localName"));
175 QCOMPARE(name.prefix(np2), QString::fromLatin1("prefix"));
177 // make sure namePool() is const
178 const QXmlSchemaValidator constValidator(schema);
179 np = constValidator.namePool();
182 void tst_QXmlSchemaValidator::resetSchemaNamePool() const
185 QXmlNamePool np1 = schema1.namePool();
187 const QXmlName name1(np1, QLatin1String("localName"),
188 QLatin1String("http://example.com/"),
189 QLatin1String("prefix"));
191 QXmlSchemaValidator validator(schema1);
194 QXmlNamePool compNamePool(validator.namePool());
195 QCOMPARE(name1.namespaceUri(compNamePool), QString::fromLatin1("http://example.com/"));
196 QCOMPARE(name1.localName(compNamePool), QString::fromLatin1("localName"));
197 QCOMPARE(name1.prefix(compNamePool), QString::fromLatin1("prefix"));
201 QXmlNamePool np2 = schema2.namePool();
203 const QXmlName name2(np2, QLatin1String("remoteName"),
204 QLatin1String("http://example.com/"),
205 QLatin1String("suffix"));
207 // make sure that after re-setting the schema, the new namepool is used
208 validator.setSchema(schema2);
211 QXmlNamePool compNamePool(validator.namePool());
212 QCOMPARE(name2.namespaceUri(compNamePool), QString::fromLatin1("http://example.com/"));
213 QCOMPARE(name2.localName(compNamePool), QString::fromLatin1("remoteName"));
214 QCOMPARE(name2.prefix(compNamePool), QString::fromLatin1("suffix"));
218 void tst_QXmlSchemaValidator::loadInstanceUrlSuccess() const
221 TODO: put valid schema file on given url and enable test
222 const QXmlSchema schema(createValidSchema());
223 const QUrl url("http://notavailable/");
225 QXmlSchemaValidator validator(schema);
226 QVERIFY(!validator.validate(url));
230 void tst_QXmlSchemaValidator::loadInstanceUrlFail() const
232 const QXmlSchema schema(createValidSchema());
233 const QUrl url("http://notavailable/");
235 QXmlSchemaValidator validator(schema);
236 QVERIFY(!validator.validate(url));
239 void tst_QXmlSchemaValidator::loadInstanceDeviceSuccess() const
241 const QXmlSchema schema(createValidSchema());
243 QByteArray data( "<myRoot xmlns=\"http://qt.nokia.com/xmlschematest\">Testme</myRoot>" );
244 QBuffer buffer(&data);
245 buffer.open(QIODevice::ReadOnly);
247 QXmlSchemaValidator validator(schema);
248 QVERIFY(validator.validate(&buffer));
251 void tst_QXmlSchemaValidator::loadInstanceDeviceFail() const
253 const QXmlSchema schema(createValidSchema());
255 QByteArray data( "<myRoot xmlns=\"http://qt.nokia.com/xmlschematest\">Testme</myRoot>" );
256 QBuffer buffer(&data);
257 // a closed device can not be loaded
259 QXmlSchemaValidator validator(schema);
260 QVERIFY(!validator.validate(&buffer));
263 void tst_QXmlSchemaValidator::loadInstanceDataSuccess() const
265 const QXmlSchema schema(createValidSchema());
267 const QByteArray data( "<myRoot xmlns=\"http://qt.nokia.com/xmlschematest\">Testme</myRoot>" );
269 QXmlSchemaValidator validator(schema);
270 QVERIFY(validator.validate(data));
273 void tst_QXmlSchemaValidator::loadInstanceDataFail() const
275 const QXmlSchema schema(createValidSchema());
277 // empty instance can not be loaded
278 const QByteArray data;
280 QXmlSchemaValidator validator(schema);
281 QVERIFY(!validator.validate(data));
284 void tst_QXmlSchemaValidator::networkAccessManagerSignature() const
286 const QXmlSchema schema;
289 const QXmlSchemaValidator validator(schema);
291 /* The function should be const. */
292 validator.networkAccessManager();
295 void tst_QXmlSchemaValidator::networkAccessManagerDefaultValue() const
297 /* Test that the default value of network access manager is equal to the one from the schema. */
299 const QXmlSchema schema;
300 const QXmlSchemaValidator validator(schema);
301 QVERIFY(validator.networkAccessManager() == schema.networkAccessManager());
304 /* Test that the default value of network access manager is equal to the one from the schema. */
308 QNetworkAccessManager manager;
309 schema.setNetworkAccessManager(&manager);
311 const QXmlSchemaValidator validator(schema);
312 QVERIFY(validator.networkAccessManager() == schema.networkAccessManager());
316 void tst_QXmlSchemaValidator::networkAccessManager() const
318 /* Test that we return the network access manager that was set. */
320 QNetworkAccessManager manager;
322 const QXmlSchema schema;
323 QXmlSchemaValidator validator(schema);
325 validator.setNetworkAccessManager(&manager);
326 QCOMPARE(validator.networkAccessManager(), &manager);
329 /* Test that we return the network access manager that was set, even if the schema changed in between. */
331 QNetworkAccessManager manager;
333 const QXmlSchema schema;
334 QXmlSchemaValidator validator(schema);
336 validator.setNetworkAccessManager(&manager);
338 const QXmlSchema schema2;
339 validator.setSchema(schema2);
341 QCOMPARE(validator.networkAccessManager(), &manager);
345 void tst_QXmlSchemaValidator::messageHandlerSignature() const
347 const QXmlSchema schema;
350 const QXmlSchemaValidator validator(schema);
352 /* The function should be const. */
353 validator.messageHandler();
356 void tst_QXmlSchemaValidator::messageHandlerDefaultValue() const
358 /* Test that the default value of message handler is equal to the one from the schema. */
360 const QXmlSchema schema;
361 const QXmlSchemaValidator validator(schema);
362 QVERIFY(validator.messageHandler() == schema.messageHandler());
365 /* Test that the default value of network access manager is equal to the one from the schema. */
369 MessageSilencer handler;
370 schema.setMessageHandler(&handler);
372 const QXmlSchemaValidator validator(schema);
373 QVERIFY(validator.messageHandler() == schema.messageHandler());
377 void tst_QXmlSchemaValidator::messageHandler() const
379 /* Test that we return the message handler that was set. */
381 MessageSilencer handler;
383 const QXmlSchema schema;
384 QXmlSchemaValidator validator(schema);
386 validator.setMessageHandler(&handler);
387 QCOMPARE(validator.messageHandler(), static_cast<QAbstractMessageHandler *>(&handler));
390 /* Test that we return the message handler that was set, even if the schema changed in between. */
392 MessageSilencer handler;
394 const QXmlSchema schema;
395 QXmlSchemaValidator validator(schema);
397 validator.setMessageHandler(&handler);
399 const QXmlSchema schema2;
400 validator.setSchema(schema2);
402 QCOMPARE(validator.messageHandler(), static_cast<QAbstractMessageHandler *>(&handler));
406 void tst_QXmlSchemaValidator::uriResolverSignature() const
408 const QXmlSchema schema;
411 const QXmlSchemaValidator validator(schema);
413 /* The function should be const. */
414 validator.uriResolver();
417 const TestURIResolver resolver;
419 /* This should compile */
421 schema2.setUriResolver(&resolver);
424 void tst_QXmlSchemaValidator::uriResolverDefaultValue() const
426 /* Test that the default value of uri resolver is equal to the one from the schema. */
428 const QXmlSchema schema;
429 const QXmlSchemaValidator validator(schema);
430 QVERIFY(validator.uriResolver() == schema.uriResolver());
433 /* Test that the default value of uri resolver is equal to the one from the schema. */
437 TestURIResolver resolver;
438 schema.setUriResolver(&resolver);
440 const QXmlSchemaValidator validator(schema);
441 QVERIFY(validator.uriResolver() == schema.uriResolver());
445 void tst_QXmlSchemaValidator::uriResolver() const
447 /* Test that we return the uri resolver that was set. */
449 TestURIResolver resolver;
451 const QXmlSchema schema;
452 QXmlSchemaValidator validator(schema);
454 validator.setUriResolver(&resolver);
455 QCOMPARE(validator.uriResolver(), static_cast<const QAbstractUriResolver *>(&resolver));
458 /* Test that we return the uri resolver that was set, even if the schema changed in between. */
460 TestURIResolver resolver;
462 const QXmlSchema schema;
463 QXmlSchemaValidator validator(schema);
465 validator.setUriResolver(&resolver);
467 const QXmlSchema schema2;
468 validator.setSchema(schema2);
470 QCOMPARE(validator.uriResolver(), static_cast<const QAbstractUriResolver *>(&resolver));
474 QTEST_MAIN(tst_QXmlSchemaValidator)
476 #include "tst_qxmlschemavalidator.moc"
477 #else //QTEST_PATTERNIST