1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the autotests of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
48 #include "XSDTSTestCase.h"
50 #include "qxmlschema.h"
51 #include "qxmlschemavalidator.h"
53 using namespace QPatternistSDK;
54 using namespace QPatternist;
56 XSDTSTestCase::XSDTSTestCase(const Scenario scen, TreeItem *p, TestType testType)
59 , m_testType(testType)
63 XSDTSTestCase::~XSDTSTestCase()
65 qDeleteAll(m_baseLines);
68 TestResult::List XSDTSTestCase::execute(const ExecutionStage, TestSuite*)
70 ErrorHandler errHandler;
71 ErrorHandler::installQtMessageHandler(&errHandler);
73 TestResult::List retval;
74 TestResult::Status resultStatus = TestResult::Unknown;
77 if (m_testType == SchemaTest) {
78 executeSchemaTest(resultStatus, serialized, &errHandler);
80 executeInstanceTest(resultStatus, serialized, &errHandler);
83 resultStatus = TestBaseLine::scan(serialized, baseLines());
84 Q_ASSERT(resultStatus != TestResult::Unknown);
86 m_result = new TestResult(name(), resultStatus, 0, errHandler.messages(),
87 QPatternist::Item::List(), serialized);
88 retval.append(m_result);
89 ErrorHandler::installQtMessageHandler(0);
94 void XSDTSTestCase::executeSchemaTest(TestResult::Status &resultStatus, QString &serialized, QAbstractMessageHandler *handler)
96 QFile file(m_schemaUri.path());
97 if (!file.open(QIODevice::ReadOnly)) {
98 resultStatus = TestResult::Fail;
99 serialized = QString();
104 schema.setMessageHandler(handler);
105 schema.load(&file, m_schemaUri);
107 if (schema.isValid()) {
108 resultStatus = TestResult::Pass;
109 serialized = QString::fromLatin1("true");
111 resultStatus = TestResult::Pass;
112 serialized = QString::fromLatin1("false");
116 void XSDTSTestCase::executeInstanceTest(TestResult::Status &resultStatus, QString &serialized, QAbstractMessageHandler *handler)
118 QFile instanceFile(m_instanceUri.path());
119 if (!instanceFile.open(QIODevice::ReadOnly)) {
120 resultStatus = TestResult::Fail;
121 serialized = QString();
126 if (m_schemaUri.isValid()) {
127 QFile file(m_schemaUri.path());
128 if (!file.open(QIODevice::ReadOnly)) {
129 resultStatus = TestResult::Fail;
130 serialized = QString();
134 schema.setMessageHandler(handler);
135 schema.load(&file, m_schemaUri);
137 if (!schema.isValid()) {
138 resultStatus = TestResult::Pass;
139 serialized = QString::fromLatin1("false");
144 QXmlSchemaValidator validator(schema);
145 validator.setMessageHandler(handler);
147 qDebug("check %s", qPrintable(m_instanceUri.path()));
148 if (validator.validate(&instanceFile, m_instanceUri)) {
149 resultStatus = TestResult::Pass;
150 serialized = QString::fromLatin1("true");
152 resultStatus = TestResult::Pass;
153 serialized = QString::fromLatin1("false");
157 QVariant XSDTSTestCase::data(const Qt::ItemDataRole role, int column) const
159 if(role == Qt::DisplayRole)
164 const TestResult *const tr = testResult();
168 return TestResult::displayName(TestResult::NotTested);
172 const TestResult::Status status = tr->status();
177 return status == TestResult::Pass ? QString(QChar::fromLatin1('1'))
178 : QString(QChar::fromLatin1('0'));
180 return status == TestResult::Fail ? QString(QChar::fromLatin1('1'))
181 : QString(QChar::fromLatin1('0'));
187 if(role != Qt::BackgroundRole)
190 const TestResult *const tr = testResult();
200 const TestResult::Status status = tr->status();
202 if(status == TestResult::NotTested || status == TestResult::Unknown)
208 return status == TestResult::Pass ? Qt::green : QVariant();
210 return status == TestResult::Fail ? Qt::red : QVariant();
216 QString XSDTSTestCase::sourceCode(bool &ok) const
218 QFile file((m_testType == SchemaTest ? m_schemaUri : m_instanceUri).toLocalFile());
223 err = QString::fromLatin1("Error: %1 does not exist.").arg(file.fileName());
224 else if(!QFileInfo(file.fileName()).isFile())
225 err = QString::fromLatin1("Error: %1 is not a file, cannot display it.").arg(file.fileName());
226 else if(!file.open(QIODevice::ReadOnly))
227 err = QString::fromLatin1("Error: Could not open %1. Likely a permission error.")
228 .arg(file.fileName());
230 if(err.isNull()) /* No errors. */
233 /* Scary, we assume the query is stored in UTF-8. */
234 return QString::fromUtf8(file.readAll());
243 int XSDTSTestCase::columnCount() const
248 void XSDTSTestCase::addBaseLine(TestBaseLine *line)
250 m_baseLines.append(line);
253 QString XSDTSTestCase::name() const
258 QString XSDTSTestCase::creator() const
263 QString XSDTSTestCase::description() const
265 return m_description;
268 QDate XSDTSTestCase::lastModified() const
270 return m_lastModified;
273 bool XSDTSTestCase::isXPath() const
278 TestCase::Scenario XSDTSTestCase::scenario() const
283 void XSDTSTestCase::setName(const QString &n)
288 void XSDTSTestCase::setCreator(const QString &ctor)
293 void XSDTSTestCase::setDescription(const QString &descriptionP)
295 m_description = descriptionP;
298 void XSDTSTestCase::setLastModified(const QDate &date)
300 m_lastModified = date;
303 void XSDTSTestCase::setSchemaUri(const QUrl &uri)
308 void XSDTSTestCase::setInstanceUri(const QUrl &uri)
313 TreeItem *XSDTSTestCase::parent() const
318 QString XSDTSTestCase::title() const
323 TestBaseLine::List XSDTSTestCase::baseLines() const
325 Q_ASSERT_X(!m_baseLines.isEmpty(), Q_FUNC_INFO,
326 qPrintable(QString::fromLatin1("The test %1 has no base lines, it should have at least one.").arg(name())));
330 QUrl XSDTSTestCase::schemaUri() const
335 QUrl XSDTSTestCase::instanceUri() const
337 return m_instanceUri;
340 void XSDTSTestCase::setContextItemSource(const QUrl &uri)
342 m_contextItemSource = uri;
345 QUrl XSDTSTestCase::contextItemSource() const
347 return m_contextItemSource;
350 void XSDTSTestCase::setParent(TreeItem *const p)
355 QPatternist::ExternalVariableLoader::Ptr XSDTSTestCase::externalVariableLoader() const
357 return QPatternist::ExternalVariableLoader::Ptr();
360 TestResult *XSDTSTestCase::testResult() const
365 TestItem::ResultSummary XSDTSTestCase::resultSummary() const
368 return ResultSummary(m_result->status() == TestResult::Pass ? 1 : 0,
371 return ResultSummary(0, 1);
374 // vim: et:ts=4:sw=4:sts=4