33be13a7089e4412db3a6cfffe0d92b86d8c7aa6
[profile/ivi/qtxmlpatterns.git] / tests / auto / xmlpatternssdk / TestSuiteHandler.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtDebug>
43
44 #include <private/qacceltreeresourceloader_p.h>
45 #include <private/qnetworkaccessdelegator_p.h>
46
47 #include "Global.h"
48 #include "TestBaseLine.h"
49 #include "TestGroup.h"
50
51 #include "TestSuiteHandler.h"
52
53 using namespace QPatternistSDK;
54
55 QNetworkAccessManager s_networkManager;
56
57 TestSuiteHandler::TestSuiteHandler(const QUrl &catalogFile,
58                                    const bool useEList) : m_ts(0)
59                                                         , m_container(0)
60                                                         , m_tc(0)
61                                                         , m_baseLine(0)
62                                                         , m_catalogFile(catalogFile)
63                                                         , m_exclusionList(readExclusionList(useEList))
64                                                         , m_isExcluding(false)
65 {
66     Q_ASSERT(!m_catalogFile.isRelative());
67 }
68
69 QStringList TestSuiteHandler::readExclusionList(const bool useExclusionList) const
70 {
71     if(!useExclusionList)
72         return QStringList();
73
74     QStringList avoid;
75
76     /* These test groups are for features we don't support.
77      *
78      * Originally these were stored in a text file pulled in with Qt resources, but
79      * it was not possible to get it to link on some HP-UX and Intel-icc platforms. */
80
81     avoid << "SchemaImport";        // The schema import feature
82     avoid << "SchemaValidation";    // The validate expression(requires schema import)
83     avoid << "StaticTyping";        // Pessimistic static typing checking
84     avoid << "TrivialEmbedding";    // XQueryX inside XQuery
85     avoid << "XMark";               // We're currently too buggy for running these tests.
86
87     return avoid;
88 }
89
90 bool TestSuiteHandler::startElement(const QString &namespaceURI,
91                                     const QString &localName,
92                                     const QString &/*qName*/,
93                                     const QXmlAttributes &atts)
94 {
95     if(namespaceURI != Global::xqtsCatalogNS)
96         return true;
97     else if(m_isExcluding)
98     {
99         if(localName == QLatin1String("test-group"))
100         {
101             m_testGroupName.push(atts.value(QLatin1String("name")));
102             return true;
103         }
104         else
105             return true;
106     }
107
108     /* The elements are handled roughly in the order of highest occurrence in the catalog file. */
109     if(localName == QLatin1String("test-case"))
110     {
111         XQTSTestCase *const c = new XQTSTestCase(
112                 TestCase::scenarioFromString(atts.value(QLatin1String("scenario"))), m_container);
113
114         c->setName(atts.value(QLatin1String("name")));
115         c->setCreator(atts.value(QLatin1String("Creator")));
116         c->setIsXPath(Global::readBoolean(atts.value(QLatin1String("is-XPath2"))));
117         c->setLastModified(QDate::fromString(atts.value(QLatin1String("version-drop")), Qt::ISODate));
118         Q_ASSERT(c->lastModified().isNull() || c->lastModified().isValid());
119
120         m_currentQueryPath = m_queryOffset.resolved(QUrl(atts.value(QLatin1String("FilePath"))));
121         m_currentBaselinePath = m_baselineOffset.resolved(QUrl(atts.value(QLatin1String("FilePath"))));
122
123         m_container->appendChild(c);
124         m_tc = c;
125      }
126     else if(localName == QLatin1String("query"))
127     {
128         m_tc->setQueryPath(m_currentQueryPath.resolved(atts.value(QLatin1String("name")) +
129                                                        m_xqueryFileExtension));
130     }
131     else if(localName == QLatin1String("input-file") ||
132             localName == QLatin1String("input-URI"))
133     {
134         m_currentInputVariable = atts.value(QLatin1String("variable"));
135     }
136     else if(localName == QLatin1String("output-file"))
137     {
138         m_baseLine = new TestBaseLine(TestBaseLine::identifierFromString(atts.value(QLatin1String("compare"))));
139     }
140     else if(localName == QLatin1String("expected-error"))
141     {
142         m_baseLine = new TestBaseLine(TestBaseLine::ExpectedError);
143     }
144     else if(localName == QLatin1String("test-group"))
145     {
146         m_testGroupName.push(atts.value(QLatin1String("name")));
147
148         if(m_exclusionList.contains(m_testGroupName.top()))
149         {
150             /* Ok, this group is supposed to be excluded, we don't
151              * insert it into the tree. */
152             m_isExcluding = true;
153             return true;
154         }
155         else
156         {
157             Q_ASSERT(m_container);
158             TestGroup *const newGroup = new TestGroup(m_container);
159             m_container->appendChild(newGroup);
160             m_container = newGroup;
161         }
162     }
163     else if(localName == QLatin1String("source"))
164     {
165         m_sourceMap.insert(atts.value(QLatin1String("ID")),
166                            m_sourceOffset.resolved(QUrl(atts.value(QLatin1String("FileName")))));
167     }
168     else if(localName == QLatin1String("test-suite"))
169     {
170         m_ts = new TestSuite();
171         m_ts->setVersion(atts.value(QLatin1String("version")));
172         m_ts->setDesignDate(QDate::fromString(atts.value(QLatin1String("CatalogDesignDate")), Qt::ISODate));
173         Q_ASSERT(m_ts->designDate().isValid());
174         m_container = m_ts;
175
176         m_xqueryFileExtension   = atts.value(QLatin1String("XQueryFileExtension"));
177         m_queryOffset           = m_catalogFile.resolved(atts.value(QLatin1String("XQueryQueryOffsetPath")));
178         m_baselineOffset        = m_catalogFile.resolved(atts.value(QLatin1String("ResultOffsetPath")));
179         m_sourceOffset          = m_catalogFile.resolved(atts.value(QLatin1String("SourceOffsetPath")));
180     }
181     else if(localName == QLatin1String("input-query"))
182     {
183         m_tcSourceInputs.insert(atts.value(QLatin1String("variable")),
184                                 ExternalSourceLoader::VariableValue(m_currentQueryPath.resolved(atts.value(QLatin1String("name")) + m_xqueryFileExtension),
185                                                                     ExternalSourceLoader::Query));
186     }
187
188     return true;
189 }
190
191 bool TestSuiteHandler::endElement(const QString &namespaceURI,
192                                   const QString &localName,
193                                   const QString &/*qName*/)
194 {
195     if(namespaceURI != Global::xqtsCatalogNS)
196         return true;
197
198     if(m_isExcluding)
199     {
200         if(localName == QLatin1String("test-group"))
201         {
202             const QString myName(m_testGroupName.pop());
203
204             if(m_exclusionList.contains(myName))
205             {
206                 /* This test-group is being excluded and now we're exiting from it. */
207                 m_isExcluding = false;
208             }
209         }
210
211         return true;
212     }
213
214     /* The elements are handled roughly in the order of highest occurrence in the catalog file. */
215     if(localName == QLatin1String("description"))
216     {
217         if(m_tc)
218         {
219             /* We're inside a <test-case>, so the <description> belongs
220              * to the test-case. */
221             m_tc->setDescription(m_ch.simplified());
222         }
223         else
224             m_container->setDescription(m_ch.simplified());
225     }
226     else if(localName == QLatin1String("test-case"))
227     {
228         Q_ASSERT(m_tc->baseLines().count() >= 1);
229         Q_ASSERT(m_resourceLoader);
230         m_tc->setExternalVariableLoader(QPatternist::ExternalVariableLoader::Ptr
231                                                 (new ExternalSourceLoader(m_tcSourceInputs,
232                                                                           m_resourceLoader)));
233         m_tcSourceInputs.clear();
234
235         if(!m_contextItemSource.isEmpty())
236         {
237             m_tc->setContextItemSource(QUrl(m_sourceMap.value(m_contextItemSource)));
238             m_contextItemSource.clear();
239         }
240
241         m_tc = 0;
242     }
243     else if(localName == QLatin1String("output-file"))
244     {
245         m_baseLine->setDetails(m_currentBaselinePath.resolved(m_ch).toString());
246         m_tc->addBaseLine(m_baseLine);
247     }
248     else if(localName == QLatin1String("input-file"))
249     {
250         m_tcSourceInputs.insert(m_currentInputVariable, ExternalSourceLoader::VariableValue(m_sourceMap.value(m_ch),
251                                                                                             ExternalSourceLoader::Document));
252     }
253     else if(localName == QLatin1String("expected-error"))
254     {
255         m_baseLine->setDetails(m_ch);
256         m_tc->addBaseLine(m_baseLine);
257     }
258     else if(localName == QLatin1String("title"))
259     {
260         /* A bit dangerous, the only element with name title in the vocabulary
261          * is the the child of GroupInfo */
262         m_container->setTitle(m_ch.simplified());
263     }
264     else if(localName == QLatin1String("test-group"))
265     {
266         m_testGroupName.pop();
267         Q_ASSERT(m_container);
268         m_container = static_cast<TestContainer *>(m_container->parent());
269         Q_ASSERT(m_container);
270     }
271     else if(localName == QLatin1String("test-suite"))
272     {
273         Q_ASSERT(m_container);
274         m_container = static_cast<TestContainer *>(m_container->parent());
275     }
276     else if(localName == QLatin1String("sources"))
277     {
278         const QPatternist::NetworkAccessDelegator::Ptr networkDelegator(new QPatternist::NetworkAccessDelegator(&s_networkManager, &s_networkManager));
279
280         m_resourceLoader = QPatternist::ResourceLoader::Ptr(new QPatternist::AccelTreeResourceLoader(Global::namePool(),
281                                                                                                      networkDelegator));
282
283         const ExternalSourceLoader::SourceMap::const_iterator end(m_sourceMap.constEnd());
284         ExternalSourceLoader::SourceMap::const_iterator it(m_sourceMap.constBegin());
285
286         for(; it != end; ++it)
287             m_resourceLoader->announceDocument(it.value(), QPatternist::ResourceLoader::WillUse);
288     }
289     else if(localName == QLatin1String("input-URI"))
290     {
291         m_tcSourceInputs.insert(m_currentInputVariable, ExternalSourceLoader::VariableValue(m_sourceMap.value(m_ch),
292                                                                                             ExternalSourceLoader::URI));
293     }
294     else if(localName == QLatin1String("contextItem"))
295         m_contextItemSource = m_ch;
296
297     return true;
298 }
299
300 bool TestSuiteHandler::characters(const QString &ch)
301 {
302     m_ch = ch;
303     return true;
304 }
305
306 TestSuite *TestSuiteHandler::testSuite() const
307 {
308     return m_ts;
309 }
310
311 // vim: et:ts=4:sw=4:sts=4
312