Change copyrights from Nokia to Digia
[profile/ivi/qtxmlpatterns.git] / tests / auto / xmlpatternsxqts / tst_suitetest.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the test suite 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 Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42
43 #include <QtTest/QtTest>
44
45 #include <QProcess>
46 #include "TestSuite.h"
47 #include "TestSuiteResult.h"
48 #include "XMLWriter.h"
49 #include "ExitCode.h"
50 #include "Worker.h"
51 #include "private/qautoptr_p.h"
52 #include "tst_suitetest.h"
53
54 using namespace QPatternistSDK;
55
56 tst_SuiteTest::tst_SuiteTest(const SuiteType suiteType,
57                              const bool alwaysRun) : m_existingBaseline(QFINDTESTDATA("Baseline.xml"))
58                                                    , m_candidateBaseline(QDir::current().filePath("CandidateBaseline.xml"))
59                                                    , m_abortRun(!alwaysRun && !QFile::exists(QLatin1String("runTests")))
60                                                    , m_suiteType(suiteType)
61 {
62 }
63
64 /*!
65  Returns an absolute path to the XQTS catalog, or flags a failure using
66  QTestLib's mechanisms.
67  */
68 void tst_SuiteTest::initTestCase()
69 {
70     catalogPath(m_catalogPath);
71 }
72
73 /*!
74   Just runs the test suite and writes the result to m_candidateBaseline.
75  */
76 void tst_SuiteTest::runTestSuite() const
77 {
78     if(m_abortRun)
79         QSKIP("The test suite is not available, no tests are run.");
80
81     QString errMsg;
82     const QFileInfo fi(m_catalogPath);
83     const QUrl catalogPath(QUrl::fromLocalFile(fi.absoluteFilePath()));
84
85     TestSuite::SuiteType suiteType(TestSuite::XQuerySuite);
86     switch (m_suiteType) {
87     case XQuerySuite:
88         suiteType = TestSuite::XQuerySuite;
89         break;
90     case XsltSuite:
91         suiteType = TestSuite::XsltSuite;
92         break;
93     case XsdSuite:
94         suiteType = TestSuite::XsdSuite;
95         break;
96     default:
97         break;
98     }
99
100     TestSuite *const ts = TestSuite::openCatalog(catalogPath, errMsg, true, suiteType);
101
102     QVERIFY2(ts, qPrintable(QString::fromLatin1("Failed to open the catalog, maybe it doesn't exist or is broken: %1").arg(errMsg)));
103
104     /* Run the tests, and serialize the result(as according to XQTSResult.xsd) to standard out. */
105     TestSuiteResult *const result = ts->runSuite();
106     QVERIFY(result);
107
108     QFile out(m_candidateBaseline);
109     QVERIFY(out.open(QIODevice::WriteOnly));
110
111     XMLWriter serializer(&out);
112     result->toXML(serializer);
113
114     delete result;
115     delete ts;
116 }
117
118 void tst_SuiteTest::checkTestSuiteResult() const
119 {
120     if(m_abortRun)
121         QSKIP("This test takes too long time to run on the majority of platforms.");
122
123     typedef QList<QFileInfo> QFileInfoList;
124
125     const QFileInfo baseline(m_existingBaseline);
126     const QFileInfo result(m_candidateBaseline);
127     QFileInfoList list;
128     list.append(baseline);
129     list.append(result);
130
131     const QFileInfoList::const_iterator end(list.constEnd());
132
133     QEventLoop eventLoop;
134     const QPatternist::AutoPtr<Worker> worker(new Worker(eventLoop, m_existingBaseline, result));
135
136     /* Passed to ResultThreader so it knows what kind of file it is handling. */
137     ResultThreader::Type type = ResultThreader::Baseline;
138
139     for(QFileInfoList::const_iterator it(list.constBegin()); it != end; ++it)
140     {
141         QFileInfo i(*it);
142         i.makeAbsolute();
143
144         QVERIFY2(i.exists(), qPrintable(QString::fromLatin1("File %1 does not exist.")
145                                                             .arg(i.fileName())));
146
147         QFile *const file = new QFile(i.absoluteFilePath(), worker.data());
148
149         QVERIFY2(file->open(QIODevice::ReadOnly), qPrintable(QString::fromLatin1("Could not open file %1 for reading.")
150                                                              .arg(i.fileName())));
151
152         ResultThreader *handler = new ResultThreader(eventLoop, file, type, worker.data());
153
154         QObject::connect(handler, SIGNAL(finished()), worker.data(), SLOT(threadFinished()));
155
156         handler->start(); /* Start the thread. It now parses the file
157                              and emits threadFinished() when done. */
158         type = ResultThreader::Result;
159     }
160
161     const int exitCode = eventLoop.exec();
162
163     QCOMPARE(exitCode, 0);
164 }
165
166 bool tst_SuiteTest::dontRun() const
167 {
168     return m_abortRun;
169 }
170
171 // vim: et:ts=4:sw=4:sts=4