tests/auto/xmlpatternssdk/TestSuite.cpp
branchRCL_3
changeset 8 3f74d0d4af4c
equal deleted inserted replaced
6:dee5afe5301f 8:3f74d0d4af4c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 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 <QFileInfo>
       
    43 #include <QVariant>
       
    44 #include <QXmlInputSource>
       
    45 #include <QXmlSimpleReader>
       
    46 #include <QtDebug>
       
    47 
       
    48 #include "Global.h"
       
    49 #include "TestSuiteHandler.h"
       
    50 #include "TestSuiteResult.h"
       
    51 #include "XMLWriter.h"
       
    52 #include "XSLTTestSuiteHandler.h"
       
    53 #include "XSDTestSuiteHandler.h"
       
    54 #include "qdebug_p.h"
       
    55 
       
    56 #include "TestSuite.h"
       
    57 
       
    58 using namespace QPatternistSDK;
       
    59 using namespace QPatternist;
       
    60 
       
    61 TestSuite::TestSuite()
       
    62 {
       
    63 }
       
    64 
       
    65 QVariant TestSuite::data(const Qt::ItemDataRole role, int column) const
       
    66 {
       
    67     if(role != Qt::DisplayRole)
       
    68         return QVariant();
       
    69 
       
    70     switch(column)
       
    71     {
       
    72         case 0:
       
    73             return title();
       
    74         case 1:
       
    75             return QString();
       
    76         default:
       
    77         {
       
    78             Q_ASSERT(false);
       
    79             return QString();
       
    80         }
       
    81     }
       
    82 }
       
    83 
       
    84 TestSuiteResult *TestSuite::runSuite()
       
    85 {
       
    86     const QDate date(QDate::currentDate());
       
    87     TestResult::List result(execute(CompileAndRun, this));
       
    88 
       
    89     return new TestSuiteResult(version(), date, result);
       
    90 }
       
    91 
       
    92 TestSuite *TestSuite::openCatalog(const QUrl &catalogURI,
       
    93                                   QString &errorMsg,
       
    94                                   const bool useExclusionList,
       
    95                                   SuiteType suiteType)
       
    96 {
       
    97     pDebug() << "Opening catalog:" << catalogURI.toString();
       
    98     QFile ts(catalogURI.toLocalFile());
       
    99     Q_ASSERT(catalogURI.isValid());
       
   100 
       
   101     if(!ts.exists())
       
   102     {
       
   103         errorMsg = QString::fromLatin1("The test suite catalog \"%1\" could not be found.\n")
       
   104                                        .arg(ts.fileName());
       
   105         return 0;
       
   106     }
       
   107 
       
   108     const QFileInfo info(ts);
       
   109 
       
   110     if(!info.isReadable())
       
   111     {
       
   112         errorMsg = QString::fromLatin1("Cannot read the test suite catalog.\n");
       
   113         return 0;
       
   114     }
       
   115     else if(!info.isFile())
       
   116     {
       
   117         errorMsg = QString::fromLatin1("The specified test suite catalog \"%1\" is not a file. "
       
   118                                        "The test suite catalog must be a file, it cannot be "
       
   119                                        "a directory, for example.\n")
       
   120                                        .arg(ts.fileName());
       
   121         return 0;
       
   122     }
       
   123     else if(!ts.open(QIODevice::ReadOnly | QIODevice::Text))
       
   124     {
       
   125         errorMsg = QString::fromLatin1("Failed to open the test suite catalog, \"%1\".\n")
       
   126                                        .arg(ts.fileName());
       
   127         return 0;
       
   128     }
       
   129 
       
   130     return openCatalog(&ts, errorMsg, catalogURI, useExclusionList, suiteType);
       
   131 }
       
   132 
       
   133 TestSuite *TestSuite::openCatalog(QIODevice *input,
       
   134                                   QString &errorMsg,
       
   135                                   const QUrl &fileName,
       
   136                                   const bool useExclusionList,
       
   137                                   SuiteType suiteType)
       
   138 {
       
   139     Q_ASSERT(input);
       
   140 
       
   141     QXmlSimpleReader reader;
       
   142     typedef QPatternist::AutoPtr<QXmlDefaultHandler> HandlerPtr;
       
   143 
       
   144     HandlerPtr loader;
       
   145 
       
   146     switch (suiteType) {
       
   147         case XQuerySuite: loader = HandlerPtr(new TestSuiteHandler(fileName, useExclusionList)); break;
       
   148         case XsltSuite: loader = HandlerPtr(new XSLTTestSuiteHandler(fileName)); break;
       
   149         case XsdSuite: loader = HandlerPtr(new XSDTestSuiteHandler(fileName)); break;
       
   150         default: Q_ASSERT(false); break;
       
   151     }
       
   152 
       
   153     reader.setContentHandler(loader.data());
       
   154 
       
   155     QXmlInputSource source(input);
       
   156 
       
   157     if(!reader.parse(source))
       
   158     {
       
   159         errorMsg = QString::fromLatin1("Couldn't parse %1").arg(fileName.toString());
       
   160         return 0;
       
   161     }
       
   162 
       
   163     TestSuite *suite = 0;
       
   164     switch (suiteType) {
       
   165         case XQuerySuite: suite = static_cast<TestSuiteHandler *>(loader.data())->testSuite(); break;
       
   166         case XsltSuite: suite = static_cast<XSLTTestSuiteHandler *>(loader.data())->testSuite(); break;
       
   167         case XsdSuite: suite = static_cast<XSDTestSuiteHandler *>(loader.data())->testSuite(); break;
       
   168         default: Q_ASSERT(false); break;
       
   169     }
       
   170 
       
   171     if(suite)
       
   172         return suite;
       
   173 
       
   174     errorMsg = QString::fromLatin1("Failed to load \"%1\". "
       
   175                                    "It appears to have no test-suite element.\n").arg(fileName.toString());
       
   176     return 0;
       
   177 }
       
   178 
       
   179 void TestSuite::toXML(XMLWriter &receiver, TestCase *const tc) const
       
   180 {
       
   181     // TODO startElement() endElement() calls can be simplified.
       
   182 
       
   183     Q_ASSERT(tc);
       
   184 
       
   185     receiver.startDocument();
       
   186     /* <test-suite> */
       
   187     QXmlAttributes test_suiteAtts;
       
   188     test_suiteAtts.append(QLatin1String("CatalogDesignDate"), QString(),
       
   189                           QLatin1String("CatalogDesignDate"), m_designDate.toString(Qt::ISODate));
       
   190     test_suiteAtts.append(QLatin1String("version"), QString(),
       
   191                           QLatin1String("version"), m_version);
       
   192     test_suiteAtts.append(QLatin1String("SourceOffsetPath"), QString(),
       
   193                           QLatin1String("SourceOffsetPath"), QString());
       
   194     test_suiteAtts.append(QLatin1String("ResultOffsetPath"), QString(),
       
   195                           QLatin1String("ResultOffsetPath"), QString());
       
   196     test_suiteAtts.append(QLatin1String("XQueryQueryOffsetPath"), QString(),
       
   197                           QLatin1String("XQueryQueryOffsetPath"), QString());
       
   198     test_suiteAtts.append(QLatin1String("QueryXQueryOffsetPath"), QString(),
       
   199                           QLatin1String("QueryXQueryOffsetPath"), QString());
       
   200     test_suiteAtts.append(QLatin1String("XQueryFileExtension"), QString(),
       
   201                           QLatin1String("XQueryFileExtension"), QString());
       
   202     test_suiteAtts.append(QLatin1String("XQueryXFileExtension"), QString(),
       
   203                           QLatin1String("XQueryXFileExtension"), QString());
       
   204 
       
   205     receiver.startPrefixMapping(QString(), Global::xqtsCatalogNS);
       
   206     receiver.startElement(QLatin1String("test-suite"), test_suiteAtts);
       
   207     receiver.endPrefixMapping(QString());
       
   208 
       
   209     /* <test-group> */
       
   210     QXmlAttributes test_groupAtts;
       
   211     test_groupAtts.append(QLatin1String("GeneratedGroupByPatternistSDKRunSuite"), QString(),
       
   212                           QLatin1String("GeneratedGroupByPatternistSDKRunSuite"), QString());
       
   213     receiver.startElement(QLatin1String("test-group"), test_groupAtts);
       
   214 
       
   215     /* <GroupInfo> */
       
   216     receiver.startElement(QLatin1String("GroupInfo"), test_groupAtts);
       
   217 
       
   218     /* <title> */
       
   219     receiver.startElement(QLatin1String("title"), test_groupAtts);
       
   220     receiver.characters(QLatin1String("Contains the test case generated by PatternistSDKRunSuite."));
       
   221 
       
   222     /* </title> */
       
   223     receiver.endElement(QLatin1String("title"));
       
   224 
       
   225     /* <description> */
       
   226     receiver.startElement(QLatin1String("description"), test_groupAtts);
       
   227     /* </description> */
       
   228     receiver.endElement(QLatin1String("description"));
       
   229 
       
   230     /* </GroupInfo> */
       
   231     receiver.endElement(QLatin1String("GroupInfo"));
       
   232 
       
   233     /* <test-case> */
       
   234     tc->toXML(receiver);
       
   235     /* </test-case> */
       
   236 
       
   237     /* </test-group> */
       
   238     receiver.endElement(QLatin1String("test-group"));
       
   239 
       
   240     /* </test-suite> */
       
   241     receiver.endElement(QLatin1String("test-suite"));
       
   242 }
       
   243 
       
   244 QString TestSuite::version() const
       
   245 {
       
   246     return m_version;
       
   247 }
       
   248 
       
   249 QDate TestSuite::designDate() const
       
   250 {
       
   251     return m_designDate;
       
   252 }
       
   253 
       
   254 void TestSuite::setVersion(const QString &ver)
       
   255 {
       
   256     m_version = ver;
       
   257 }
       
   258 
       
   259 void TestSuite::setDesignDate(const QDate &date)
       
   260 {
       
   261     m_designDate = date;
       
   262 }
       
   263 
       
   264 TestContainer *TestSuite::parent() const
       
   265 {
       
   266     return 0;
       
   267 }
       
   268 
       
   269 // vim: et:ts=4:sw=4:sts=4