summaryrefslogtreecommitdiff
path: root/tests/auto/xmlpatternssdk/TestSuite.cpp
blob: a29d784ea2f43b857074b55cf7997fd595a3e21a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QFileInfo>
#include <QVariant>
#include <QXmlInputSource>
#include <QXmlSimpleReader>
#include <QtDebug>

#include "Global.h"
#include "TestSuiteHandler.h"
#include "TestSuiteResult.h"
#include "XMLWriter.h"
#include "XSLTTestSuiteHandler.h"
#include "XSDTestSuiteHandler.h"
#include <private/qdebug_p.h>

#include "TestSuite.h"

using namespace QPatternistSDK;
using namespace QPatternist;

TestSuite::TestSuite()
{
}

QVariant TestSuite::data(const Qt::ItemDataRole role, int column) const
{
    if(role != Qt::DisplayRole)
        return QVariant();

    switch(column)
    {
        case 0:
            return title();
        case 1:
            return QString();
        default:
        {
            Q_ASSERT(false);
            return QString();
        }
    }
}

TestSuiteResult *TestSuite::runSuite()
{
    const QDate date(QDate::currentDate());
    TestResult::List result(execute(CompileAndRun, this));

    return new TestSuiteResult(version(), date, result);
}

TestSuite *TestSuite::openCatalog(const QUrl &catalogURI,
                                  QString &errorMsg,
                                  const bool useExclusionList,
                                  SuiteType suiteType)
{
    pDebug() << "Opening catalog:" << catalogURI.toString();
    QFile ts(catalogURI.toLocalFile());
    Q_ASSERT(catalogURI.isValid());

    if(!ts.exists())
    {
        errorMsg = QString::fromLatin1("The test suite catalog \"%1\" could not be found.\n")
                                       .arg(ts.fileName());
        return 0;
    }

    const QFileInfo info(ts);

    if(!info.isReadable())
    {
        errorMsg = QString::fromLatin1("Cannot read the test suite catalog.\n");
        return 0;
    }
    else if(!info.isFile())
    {
        errorMsg = QString::fromLatin1("The specified test suite catalog \"%1\" is not a file. "
                                       "The test suite catalog must be a file, it cannot be "
                                       "a directory, for example.\n")
                                       .arg(ts.fileName());
        return 0;
    }
    else if(!ts.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        errorMsg = QString::fromLatin1("Failed to open the test suite catalog, \"%1\".\n")
                                       .arg(ts.fileName());
        return 0;
    }

    return openCatalog(&ts, errorMsg, catalogURI, useExclusionList, suiteType);
}

TestSuite *TestSuite::openCatalog(QIODevice *input,
                                  QString &errorMsg,
                                  const QUrl &fileName,
                                  const bool useExclusionList,
                                  SuiteType suiteType)
{
    Q_ASSERT(input);

    QXmlSimpleReader reader;
    typedef QPatternist::AutoPtr<QXmlDefaultHandler> HandlerPtr;

    HandlerPtr loader;

    switch (suiteType) {
        case XQuerySuite: loader = HandlerPtr(new TestSuiteHandler(fileName, useExclusionList)); break;
        case XsltSuite: loader = HandlerPtr(new XSLTTestSuiteHandler(fileName)); break;
        case XsdSuite: loader = HandlerPtr(new XSDTestSuiteHandler(fileName)); break;
        default: Q_ASSERT(false); break;
    }

    reader.setContentHandler(loader.data());

    QXmlInputSource source(input);

    if(!reader.parse(source))
    {
        errorMsg = QString::fromLatin1("Couldn't parse %1").arg(fileName.toString());
        return 0;
    }

    TestSuite *suite = 0;
    switch (suiteType) {
        case XQuerySuite: suite = static_cast<TestSuiteHandler *>(loader.data())->testSuite(); break;
        case XsltSuite: suite = static_cast<XSLTTestSuiteHandler *>(loader.data())->testSuite(); break;
        case XsdSuite: suite = static_cast<XSDTestSuiteHandler *>(loader.data())->testSuite(); break;
        default: Q_ASSERT(false); break;
    }

    if(suite)
        return suite;

    errorMsg = QString::fromLatin1("Failed to load \"%1\". "
                                   "It appears to have no test-suite element.\n").arg(fileName.toString());
    return 0;
}

void TestSuite::toXML(XMLWriter &receiver, TestCase *const tc) const
{
    // TODO startElement() endElement() calls can be simplified.

    Q_ASSERT(tc);

    receiver.startDocument();
    /* <test-suite> */
    QXmlAttributes test_suiteAtts;
    test_suiteAtts.append(QLatin1String("CatalogDesignDate"), QString(),
                          QLatin1String("CatalogDesignDate"), m_designDate.toString(Qt::ISODate));
    test_suiteAtts.append(QLatin1String("version"), QString(),
                          QLatin1String("version"), m_version);
    test_suiteAtts.append(QLatin1String("SourceOffsetPath"), QString(),
                          QLatin1String("SourceOffsetPath"), QString());
    test_suiteAtts.append(QLatin1String("ResultOffsetPath"), QString(),
                          QLatin1String("ResultOffsetPath"), QString());
    test_suiteAtts.append(QLatin1String("XQueryQueryOffsetPath"), QString(),
                          QLatin1String("XQueryQueryOffsetPath"), QString());
    test_suiteAtts.append(QLatin1String("QueryXQueryOffsetPath"), QString(),
                          QLatin1String("QueryXQueryOffsetPath"), QString());
    test_suiteAtts.append(QLatin1String("XQueryFileExtension"), QString(),
                          QLatin1String("XQueryFileExtension"), QString());
    test_suiteAtts.append(QLatin1String("XQueryXFileExtension"), QString(),
                          QLatin1String("XQueryXFileExtension"), QString());

    receiver.startPrefixMapping(QString(), Global::xqtsCatalogNS);
    receiver.startElement(QLatin1String("test-suite"), test_suiteAtts);
    receiver.endPrefixMapping(QString());

    /* <test-group> */
    QXmlAttributes test_groupAtts;
    test_groupAtts.append(QLatin1String("GeneratedGroupByPatternistSDKRunSuite"), QString(),
                          QLatin1String("GeneratedGroupByPatternistSDKRunSuite"), QString());
    receiver.startElement(QLatin1String("test-group"), test_groupAtts);

    /* <GroupInfo> */
    receiver.startElement(QLatin1String("GroupInfo"), test_groupAtts);

    /* <title> */
    receiver.startElement(QLatin1String("title"), test_groupAtts);
    receiver.characters(QLatin1String("Contains the test case generated by PatternistSDKRunSuite."));

    /* </title> */
    receiver.endElement(QLatin1String("title"));

    /* <description> */
    receiver.startElement(QLatin1String("description"), test_groupAtts);
    /* </description> */
    receiver.endElement(QLatin1String("description"));

    /* </GroupInfo> */
    receiver.endElement(QLatin1String("GroupInfo"));

    /* <test-case> */
    tc->toXML(receiver);
    /* </test-case> */

    /* </test-group> */
    receiver.endElement(QLatin1String("test-group"));

    /* </test-suite> */
    receiver.endElement(QLatin1String("test-suite"));
}

QString TestSuite::version() const
{
    return m_version;
}

QDate TestSuite::designDate() const
{
    return m_designDate;
}

void TestSuite::setVersion(const QString &ver)
{
    m_version = ver;
}

void TestSuite::setDesignDate(const QDate &date)
{
    m_designDate = date;
}

TestContainer *TestSuite::parent() const
{
    return 0;
}

// vim: et:ts=4:sw=4:sts=4