547549c1ea33e3e937c2e9637ce8525d88dc5fd4
[profile/ivi/qtxmlpatterns.git] / tests / auto / qxmlschemavalidator / tst_qxmlschemavalidator.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the test suite module of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtTest/QtTest>
43
44 #include <QAbstractMessageHandler>
45 #include <QAbstractUriResolver>
46 #include <QtNetwork/QNetworkAccessManager>
47 #include <QXmlName>
48 #include <QXmlSchema>
49 #include <QXmlSchemaValidator>
50
51 #include "../qabstracturiresolver/TestURIResolver.h"
52 #include "../qxmlquery/MessageSilencer.h"
53
54 /*!
55  \class tst_QXmlSchemaValidatorValidator
56  \internal
57  \brief Tests class QXmlSchemaValidator.
58
59  This test is not intended for testing the engine, but the functionality specific
60  to the QXmlSchemaValidator class.
61  */
62 class tst_QXmlSchemaValidator : public QObject
63 {
64     Q_OBJECT
65
66 private Q_SLOTS:
67     void defaultConstructor() const;
68     void constructorQXmlNamePool() const;
69     void propertyInitialization() const;
70     void resetSchemaNamePool() const;
71
72     void loadInstanceUrlSuccess() const;
73     void loadInstanceUrlFail() const;
74     void loadInstanceDeviceSuccess() const;
75     void loadInstanceDeviceFail() const;
76     void loadInstanceDataSuccess() const;
77     void loadInstanceDataFail() const;
78
79     void networkAccessManagerSignature() const;
80     void networkAccessManagerDefaultValue() const;
81     void networkAccessManager() const;
82
83     void messageHandlerSignature() const;
84     void messageHandlerDefaultValue() const;
85     void messageHandler() const;
86
87     void uriResolverSignature() const;
88     void uriResolverDefaultValue() const;
89     void uriResolver() const;
90 };
91
92 static QXmlSchema createValidSchema()
93 {
94     const QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
95                            "<xsd:schema"
96                            "        xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
97                            "        xmlns=\"http://qt.nokia.com/xmlschematest\""
98                            "        targetNamespace=\"http://qt.nokia.com/xmlschematest\""
99                            "        version=\"1.0\""
100                            "        elementFormDefault=\"qualified\">"
101                            "  <xsd:element name=\"myRoot\" type=\"xsd:string\"/>"
102                            "</xsd:schema>" );
103
104     const QUrl documentUri("http://qt.nokia.com/xmlschematest");
105
106     QXmlSchema schema;
107     schema.load(data, documentUri);
108
109     return schema;
110 }
111
112 void tst_QXmlSchemaValidator::defaultConstructor() const
113 {
114     /* Allocate instance in different orders. */
115     {
116         QXmlSchema schema;
117         QXmlSchemaValidator validator(schema);
118     }
119
120     {
121         QXmlSchema schema1;
122         QXmlSchema schema2;
123
124         QXmlSchemaValidator validator1(schema1);
125         QXmlSchemaValidator validator2(schema2);
126     }
127
128     {
129         QXmlSchema schema;
130
131         QXmlSchemaValidator validator1(schema);
132         QXmlSchemaValidator validator2(schema);
133     }
134 }
135
136 void tst_QXmlSchemaValidator::propertyInitialization() const
137 {
138     /* Verify that properties set in the schema are used as default values for the validator */
139     {
140         MessageSilencer handler;
141         TestURIResolver resolver;
142         QNetworkAccessManager manager;
143
144         QXmlSchema schema;
145         schema.setMessageHandler(&handler);
146         schema.setUriResolver(&resolver);
147         schema.setNetworkAccessManager(&manager);
148
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);
153     }
154 }
155
156 void tst_QXmlSchemaValidator::constructorQXmlNamePool() const
157 {
158     // test that the name pool from the schema is used by
159     // the schema validator as well
160     QXmlSchema schema;
161
162     QXmlNamePool np = schema.namePool();
163
164     const QXmlName name(np, QLatin1String("localName"),
165                             QLatin1String("http://example.com/"),
166                             QLatin1String("prefix"));
167
168     QXmlSchemaValidator validator(schema);
169
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"));
174
175     // make sure namePool() is const
176     const QXmlSchemaValidator constValidator(schema);
177     np = constValidator.namePool();
178 }
179
180 void tst_QXmlSchemaValidator::resetSchemaNamePool() const
181 {
182     QXmlSchema schema1;
183     QXmlNamePool np1 = schema1.namePool();
184
185     const QXmlName name1(np1, QLatin1String("localName"),
186                               QLatin1String("http://example.com/"),
187                               QLatin1String("prefix"));
188
189     QXmlSchemaValidator validator(schema1);
190
191     {
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"));
196     }
197
198     QXmlSchema schema2;
199     QXmlNamePool np2 = schema2.namePool();
200
201     const QXmlName name2(np2, QLatin1String("remoteName"),
202                               QLatin1String("http://example.com/"),
203                               QLatin1String("suffix"));
204
205     // make sure that after re-setting the schema, the new namepool is used
206     validator.setSchema(schema2);
207
208     {
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"));
213     }
214 }
215
216 void tst_QXmlSchemaValidator::loadInstanceUrlSuccess() const
217 {
218 /*
219     TODO: put valid schema file on given url and enable test
220     const QXmlSchema schema(createValidSchema());
221     const QUrl url("http://notavailable/");
222
223     QXmlSchemaValidator validator(schema);
224     QVERIFY(!validator.validate(url));
225 */
226 }
227
228 void tst_QXmlSchemaValidator::loadInstanceUrlFail() const
229 {
230     const QXmlSchema schema(createValidSchema());
231     const QUrl url("http://notavailable/");
232
233     QXmlSchemaValidator validator(schema);
234     QVERIFY(!validator.validate(url));
235 }
236
237 void tst_QXmlSchemaValidator::loadInstanceDeviceSuccess() const
238 {
239     const QXmlSchema schema(createValidSchema());
240
241     QByteArray data( "<myRoot xmlns=\"http://qt.nokia.com/xmlschematest\">Testme</myRoot>" );
242     QBuffer buffer(&data);
243     buffer.open(QIODevice::ReadOnly);
244
245     QXmlSchemaValidator validator(schema);
246     QVERIFY(validator.validate(&buffer));
247 }
248
249 void tst_QXmlSchemaValidator::loadInstanceDeviceFail() const
250 {
251     const QXmlSchema schema(createValidSchema());
252
253     QByteArray data( "<myRoot xmlns=\"http://qt.nokia.com/xmlschematest\">Testme</myRoot>" );
254     QBuffer buffer(&data);
255     // a closed device can not be loaded
256
257     QXmlSchemaValidator validator(schema);
258     QVERIFY(!validator.validate(&buffer));
259 }
260
261 void tst_QXmlSchemaValidator::loadInstanceDataSuccess() const
262 {
263     const QXmlSchema schema(createValidSchema());
264
265     const QByteArray data( "<myRoot xmlns=\"http://qt.nokia.com/xmlschematest\">Testme</myRoot>" );
266
267     QXmlSchemaValidator validator(schema);
268     QVERIFY(validator.validate(data));
269 }
270
271 void tst_QXmlSchemaValidator::loadInstanceDataFail() const
272 {
273     const QXmlSchema schema(createValidSchema());
274
275     // empty instance can not be loaded
276     const QByteArray data;
277
278     QXmlSchemaValidator validator(schema);
279     QVERIFY(!validator.validate(data));
280 }
281
282 void tst_QXmlSchemaValidator::networkAccessManagerSignature() const
283 {
284     const QXmlSchema schema;
285
286     /* Const object. */
287     const QXmlSchemaValidator validator(schema);
288
289     /* The function should be const. */
290     validator.networkAccessManager();
291 }
292
293 void tst_QXmlSchemaValidator::networkAccessManagerDefaultValue() const
294 {
295     /* Test that the default value of network access manager is equal to the one from the schema. */
296     {
297         const QXmlSchema schema;
298         const QXmlSchemaValidator validator(schema);
299         QVERIFY(validator.networkAccessManager() == schema.networkAccessManager());
300     }
301
302     /* Test that the default value of network access manager is equal to the one from the schema. */
303     {
304         QXmlSchema schema;
305
306         QNetworkAccessManager manager;
307         schema.setNetworkAccessManager(&manager);
308
309         const QXmlSchemaValidator validator(schema);
310         QVERIFY(validator.networkAccessManager() == schema.networkAccessManager());
311     }
312 }
313
314 void tst_QXmlSchemaValidator::networkAccessManager() const
315 {
316     /* Test that we return the network access manager that was set. */
317     {
318         QNetworkAccessManager manager;
319
320         const QXmlSchema schema;
321         QXmlSchemaValidator validator(schema);
322
323         validator.setNetworkAccessManager(&manager);
324         QCOMPARE(validator.networkAccessManager(), &manager);
325     }
326
327     /* Test that we return the network access manager that was set, even if the schema changed in between. */
328     {
329         QNetworkAccessManager manager;
330
331         const QXmlSchema schema;
332         QXmlSchemaValidator validator(schema);
333
334         validator.setNetworkAccessManager(&manager);
335
336         const QXmlSchema schema2;
337         validator.setSchema(schema2);
338
339         QCOMPARE(validator.networkAccessManager(), &manager);
340     }
341 }
342
343 void tst_QXmlSchemaValidator::messageHandlerSignature() const
344 {
345     const QXmlSchema schema;
346
347     /* Const object. */
348     const QXmlSchemaValidator validator(schema);
349
350     /* The function should be const. */
351     validator.messageHandler();
352 }
353
354 void tst_QXmlSchemaValidator::messageHandlerDefaultValue() const
355 {
356     /* Test that the default value of message handler is equal to the one from the schema. */
357     {
358         const QXmlSchema schema;
359         const QXmlSchemaValidator validator(schema);
360         QVERIFY(validator.messageHandler() == schema.messageHandler());
361     }
362
363     /* Test that the default value of network access manager is equal to the one from the schema. */
364     {
365         QXmlSchema schema;
366
367         MessageSilencer handler;
368         schema.setMessageHandler(&handler);
369
370         const QXmlSchemaValidator validator(schema);
371         QVERIFY(validator.messageHandler() == schema.messageHandler());
372     }
373 }
374
375 void tst_QXmlSchemaValidator::messageHandler() const
376 {
377     /* Test that we return the message handler that was set. */
378     {
379         MessageSilencer handler;
380
381         const QXmlSchema schema;
382         QXmlSchemaValidator validator(schema);
383
384         validator.setMessageHandler(&handler);
385         QCOMPARE(validator.messageHandler(), static_cast<QAbstractMessageHandler *>(&handler));
386     }
387
388     /* Test that we return the message handler that was set, even if the schema changed in between. */
389     {
390         MessageSilencer handler;
391
392         const QXmlSchema schema;
393         QXmlSchemaValidator validator(schema);
394
395         validator.setMessageHandler(&handler);
396
397         const QXmlSchema schema2;
398         validator.setSchema(schema2);
399
400         QCOMPARE(validator.messageHandler(), static_cast<QAbstractMessageHandler *>(&handler));
401     }
402 }
403
404 void tst_QXmlSchemaValidator::uriResolverSignature() const
405 {
406     const QXmlSchema schema;
407
408     /* Const object. */
409     const QXmlSchemaValidator validator(schema);
410
411     /* The function should be const. */
412     validator.uriResolver();
413
414     /* Const object. */
415     const TestURIResolver resolver;
416
417     /* This should compile */
418     QXmlSchema schema2;
419     schema2.setUriResolver(&resolver);
420 }
421
422 void tst_QXmlSchemaValidator::uriResolverDefaultValue() const
423 {
424     /* Test that the default value of uri resolver is equal to the one from the schema. */
425     {
426         const QXmlSchema schema;
427         const QXmlSchemaValidator validator(schema);
428         QVERIFY(validator.uriResolver() == schema.uriResolver());
429     }
430
431     /* Test that the default value of uri resolver is equal to the one from the schema. */
432     {
433         QXmlSchema schema;
434
435         TestURIResolver resolver;
436         schema.setUriResolver(&resolver);
437
438         const QXmlSchemaValidator validator(schema);
439         QVERIFY(validator.uriResolver() == schema.uriResolver());
440     }
441 }
442
443 void tst_QXmlSchemaValidator::uriResolver() const
444 {
445     /* Test that we return the uri resolver that was set. */
446     {
447         TestURIResolver resolver;
448
449         const QXmlSchema schema;
450         QXmlSchemaValidator validator(schema);
451
452         validator.setUriResolver(&resolver);
453         QCOMPARE(validator.uriResolver(), static_cast<const QAbstractUriResolver *>(&resolver));
454     }
455
456     /* Test that we return the uri resolver that was set, even if the schema changed in between. */
457     {
458         TestURIResolver resolver;
459
460         const QXmlSchema schema;
461         QXmlSchemaValidator validator(schema);
462
463         validator.setUriResolver(&resolver);
464
465         const QXmlSchema schema2;
466         validator.setSchema(schema2);
467
468         QCOMPARE(validator.uriResolver(), static_cast<const QAbstractUriResolver *>(&resolver));
469     }
470 }
471
472 QTEST_MAIN(tst_QXmlSchemaValidator)
473
474 #include "tst_qxmlschemavalidator.moc"