tests/auto/qxmlschema/tst_qxmlschema.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     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 
       
    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 
       
    96 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 
       
   115 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 
       
   141 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 
       
   161 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 
       
   201 void tst_QXmlSchema::isValid() const
       
   202 {
       
   203     /* Check default value. */
       
   204     QXmlSchema schema;
       
   205     QVERIFY(!schema.isValid());
       
   206 }
       
   207 
       
   208 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 
       
   226 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 
       
   237 void tst_QXmlSchema::loadSchemaUrlFail() const
       
   238 {
       
   239     const QUrl url("http://notavailable/");
       
   240 
       
   241     QXmlSchema schema;
       
   242     QVERIFY(!schema.load(url));
       
   243 }
       
   244 
       
   245 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 
       
   263 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 
       
   281 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 
       
   295 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 
       
   305 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 
       
   314 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 
       
   323 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 
       
   334 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 
       
   343 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 
       
   352 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(), &handler);
       
   361     }
       
   362 }
       
   363 
       
   364 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 
       
   380 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 
       
   389 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(), &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