1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
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 
52 #include "../qabstracturiresolver/TestURIResolver.h"
53 #include "../qxmlquery/MessageSilencer.h"
54 
55 /*!
56  \class tst_QXmlSchema
57  \internal
58  \brief Tests class QXmlSchema.
59 
60  This test is not intended for testing the engine, but the functionality specific
61  to the QXmlSchema class.
62  */
63 class tst_QXmlSchema : public QObject
64 {
65     Q_OBJECT
66 
67 private Q_SLOTS:
68     void defaultConstructor() const;
69     void copyConstructor() const;
70     void constructorQXmlNamePool() const;
71     void copyMutationTest() const;
72 
73     void isValid() const;
74     void documentUri() const;
75 
76     void loadSchemaUrlSuccess() const;
77     void loadSchemaUrlFail() const;
78     void loadSchemaDeviceSuccess() const;
79     void loadSchemaDeviceFail() const;
80     void loadSchemaDataSuccess() const;
81     void loadSchemaDataFail() const;
82 
83     void networkAccessManagerSignature() const;
84     void networkAccessManagerDefaultValue() const;
85     void networkAccessManager() const;
86 
87     void messageHandlerSignature() const;
88     void messageHandlerDefaultValue() const;
89     void messageHandler() const;
90 
91     void uriResolverSignature() const;
92     void uriResolverDefaultValue() const;
93     void uriResolver() const;
94 };
95 
defaultConstructor() const96 void tst_QXmlSchema::defaultConstructor() const
97 {
98     /* Allocate instance in different orders. */
99     {
100         QXmlSchema schema;
101     }
102 
103     {
104         QXmlSchema schema1;
105         QXmlSchema schema2;
106     }
107 
108     {
109         QXmlSchema schema1;
110         QXmlSchema schema2;
111         QXmlSchema schema3;
112     }
113 }
114 
copyConstructor() const115 void tst_QXmlSchema::copyConstructor() const
116 {
117     /* Verify that we can take a const reference, and simply do a copy of a default constructed object. */
118     {
119         const QXmlSchema schema1;
120         QXmlSchema schema2(schema1);
121     }
122 
123     /* Copy twice. */
124     {
125         const QXmlSchema schema1;
126         QXmlSchema schema2(schema1);
127         QXmlSchema schema3(schema2);
128     }
129 
130     /* Verify that copying default values works. */
131     {
132         const QXmlSchema schema1;
133         const QXmlSchema schema2(schema1);
134         QCOMPARE(schema2.messageHandler(), schema1.messageHandler());
135         QCOMPARE(schema2.uriResolver(), schema1.uriResolver());
136         QCOMPARE(schema2.networkAccessManager(), schema1.networkAccessManager());
137         QCOMPARE(schema2.isValid(), schema1.isValid());
138     }
139 }
140 
constructorQXmlNamePool() const141 void tst_QXmlSchema::constructorQXmlNamePool() const
142 {
143     QXmlSchema schema;
144 
145     QXmlNamePool np = schema.namePool();
146 
147     const QXmlName name(np, QLatin1String("localName"),
148                             QLatin1String("http://example.com/"),
149                             QLatin1String("prefix"));
150 
151     QXmlNamePool np2(schema.namePool());
152     QCOMPARE(name.namespaceUri(np2), QString::fromLatin1("http://example.com/"));
153     QCOMPARE(name.localName(np2), QString::fromLatin1("localName"));
154     QCOMPARE(name.prefix(np2), QString::fromLatin1("prefix"));
155 
156     // make sure namePool() is const
157     const QXmlSchema constSchema;
158     np = constSchema.namePool();
159 }
160 
copyMutationTest() const161 void tst_QXmlSchema::copyMutationTest() const
162 {
163     QXmlSchema schema1;
164     QXmlSchema schema2(schema1);
165 
166     // check that everything is equal
167     QVERIFY(schema2.messageHandler() == schema1.messageHandler());
168     QVERIFY(schema2.uriResolver() == schema1.uriResolver());
169     QVERIFY(schema2.networkAccessManager() == schema1.networkAccessManager());
170 
171     MessageSilencer handler;
172     const TestURIResolver resolver;
173     QNetworkAccessManager manager;
174 
175     // modify schema1
176     schema1.setMessageHandler(&handler);
177     schema1.setUriResolver(&resolver);
178     schema1.setNetworkAccessManager(&manager);
179 
180     // check that schema2 is not effected by the modifications of schema1
181     QVERIFY(schema2.messageHandler() != schema1.messageHandler());
182     QVERIFY(schema2.uriResolver() != schema1.uriResolver());
183     QVERIFY(schema2.networkAccessManager() != schema1.networkAccessManager());
184 
185     // modify schema1 further
186     const QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
187                            "<xsd:schema"
188                            "        xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
189                            "        xmlns=\"http://qt.nokia.com/xmlschematest\""
190                            "        targetNamespace=\"http://qt.nokia.com/xmlschematest\""
191                            "        version=\"1.0\""
192                            "        elementFormDefault=\"qualified\">"
193                            "</xsd:schema>" );
194 
195     const QUrl documentUri("http://qt.nokia.com/xmlschematest");
196     schema1.load(data, documentUri);
197 
198     QVERIFY(schema2.isValid() != schema1.isValid());
199 }
200 
isValid() const201 void tst_QXmlSchema::isValid() const
202 {
203     /* Check default value. */
204     QXmlSchema schema;
205     QVERIFY(!schema.isValid());
206 }
207 
documentUri() const208 void tst_QXmlSchema::documentUri() const
209 {
210     const QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
211                            "<xsd:schema"
212                            "        xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
213                            "        xmlns=\"http://qt.nokia.com/xmlschematest\""
214                            "        targetNamespace=\"http://qt.nokia.com/xmlschematest\""
215                            "        version=\"1.0\""
216                            "        elementFormDefault=\"qualified\">"
217                            "</xsd:schema>" );
218 
219     const QUrl documentUri("http://qt.nokia.com/xmlschematest");
220     QXmlSchema schema;
221     schema.load(data, documentUri);
222 
223     QCOMPARE(documentUri, schema.documentUri());
224 }
225 
loadSchemaUrlSuccess() const226 void tst_QXmlSchema::loadSchemaUrlSuccess() const
227 {
228 /**
229     TODO: put valid schema file on given url and enable test
230     const QUrl url("http://notavailable/");
231 
232     QXmlSchema schema;
233     QVERIFY(!schema.load(url));
234 */
235 }
236 
loadSchemaUrlFail() const237 void tst_QXmlSchema::loadSchemaUrlFail() const
238 {
239     const QUrl url("http://notavailable/");
240 
241     QXmlSchema schema;
242     QVERIFY(!schema.load(url));
243 }
244 
loadSchemaDeviceSuccess() const245 void tst_QXmlSchema::loadSchemaDeviceSuccess() const
246 {
247     QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
248                      "<xsd:schema"
249                      "        xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
250                      "        xmlns=\"http://qt.nokia.com/xmlschematest\""
251                      "        targetNamespace=\"http://qt.nokia.com/xmlschematest\""
252                      "        version=\"1.0\""
253                      "        elementFormDefault=\"qualified\">"
254                      "</xsd:schema>" );
255 
256     QBuffer buffer(&data);
257     buffer.open(QIODevice::ReadOnly);
258 
259     QXmlSchema schema;
260     QVERIFY(schema.load(&buffer));
261 }
262 
loadSchemaDeviceFail() const263 void tst_QXmlSchema::loadSchemaDeviceFail() const
264 {
265     QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
266                      "<xsd:schema"
267                      "        xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
268                      "        xmlns=\"http://qt.nokia.com/xmlschematest\""
269                      "        targetNamespace=\"http://qt.nokia.com/xmlschematest\""
270                      "        version=\"1.0\""
271                      "        elementFormDefault=\"qualified\">"
272                      "</xsd:schema>" );
273 
274     QBuffer buffer(&data);
275     // a closed device can not be loaded
276 
277     QXmlSchema schema;
278     QVERIFY(!schema.load(&buffer));
279 }
280 
loadSchemaDataSuccess() const281 void tst_QXmlSchema::loadSchemaDataSuccess() const
282 {
283     const QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
284                            "<xsd:schema"
285                            "        xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
286                            "        xmlns=\"http://qt.nokia.com/xmlschematest\""
287                            "        targetNamespace=\"http://qt.nokia.com/xmlschematest\""
288                            "        version=\"1.0\""
289                            "        elementFormDefault=\"qualified\">"
290                            "</xsd:schema>" );
291     QXmlSchema schema;
292     QVERIFY(schema.load(data));
293 }
294 
loadSchemaDataFail() const295 void tst_QXmlSchema::loadSchemaDataFail() const
296 {
297     // empty schema can not be loaded
298     const QByteArray data;
299 
300     QXmlSchema schema;
301     QVERIFY(!schema.load(data));
302 }
303 
304 
networkAccessManagerSignature() const305 void tst_QXmlSchema::networkAccessManagerSignature() const
306 {
307     /* Const object. */
308     const QXmlSchema schema;
309 
310     /* The function should be const. */
311     schema.networkAccessManager();
312 }
313 
networkAccessManagerDefaultValue() const314 void tst_QXmlSchema::networkAccessManagerDefaultValue() const
315 {
316     /* Test that the default value of network access manager is not empty. */
317     {
318         QXmlSchema schema;
319         QVERIFY(schema.networkAccessManager() != static_cast<QNetworkAccessManager*>(0));
320     }
321 }
322 
networkAccessManager() const323 void tst_QXmlSchema::networkAccessManager() const
324 {
325     /* Test that we return the network manager that was set. */
326     {
327         QNetworkAccessManager manager;
328         QXmlSchema schema;
329         schema.setNetworkAccessManager(&manager);
330         QCOMPARE(schema.networkAccessManager(), &manager);
331     }
332 }
333 
messageHandlerSignature() const334 void tst_QXmlSchema::messageHandlerSignature() const
335 {
336     /* Const object. */
337     const QXmlSchema schema;
338 
339     /* The function should be const. */
340     schema.messageHandler();
341 }
342 
messageHandlerDefaultValue() const343 void tst_QXmlSchema::messageHandlerDefaultValue() const
344 {
345     /* Test that the default value of message handler is not empty. */
346     {
347         QXmlSchema schema;
348         QVERIFY(schema.messageHandler() != static_cast<QAbstractMessageHandler*>(0));
349     }
350 }
351 
messageHandler() const352 void tst_QXmlSchema::messageHandler() const
353 {
354     /* Test that we return the message handler that was set. */
355     {
356         MessageSilencer handler;
357 
358         QXmlSchema schema;
359         schema.setMessageHandler(&handler);
360         QCOMPARE(schema.messageHandler(), static_cast<QAbstractMessageHandler *>(&handler));
361     }
362 }
363 
uriResolverSignature() const364 void tst_QXmlSchema::uriResolverSignature() const
365 {
366     /* Const object. */
367     const QXmlSchema schema;
368 
369     /* The function should be const. */
370     schema.uriResolver();
371 
372     /* Const object. */
373     const TestURIResolver resolver;
374 
375     /* This should compile */
376     QXmlSchema schema2;
377     schema2.setUriResolver(&resolver);
378 }
379 
uriResolverDefaultValue() const380 void tst_QXmlSchema::uriResolverDefaultValue() const
381 {
382     /* Test that the default value of uri resolver is empty. */
383     {
384         QXmlSchema schema;
385         QVERIFY(schema.uriResolver() == static_cast<QAbstractUriResolver*>(0));
386     }
387 }
388 
uriResolver() const389 void tst_QXmlSchema::uriResolver() const
390 {
391     /* Test that we return the uri resolver that was set. */
392     {
393         TestURIResolver resolver;
394 
395         QXmlSchema schema;
396         schema.setUriResolver(&resolver);
397         QCOMPARE(schema.uriResolver(), static_cast<const QAbstractUriResolver *>(&resolver));
398     }
399 }
400 
401 QTEST_MAIN(tst_QXmlSchema)
402 
403 #include "tst_qxmlschema.moc"
404 #else //QTEST_PATTERNIST
405 QTEST_NOOP_MAIN
406 #endif
407