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