summaryrefslogtreecommitdiffstats
path: root/tests/auto/qxml/tst_qxml.cpp
blob: 9e15c8f4d87eb717a8437916b993031e14d5ff60 (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
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** 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, Nokia gives you certain additional
** rights. These rights are described in the Nokia 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.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/


#include <QtTest/QtTest>

#include <qcoreapplication.h>
#include <qdebug.h>
#include <qxml.h>


//TESTED_CLASS=
//TESTED_FILES=

class tst_QXml : public QObject
{
Q_OBJECT

private slots:
    void getSetCheck();
    void interpretedAs0D() const;
    void exception();
};

class MyXmlEntityResolver : public QXmlEntityResolver
{
public:
    MyXmlEntityResolver() : QXmlEntityResolver() {}
    QString errorString() const { return QString(); } 
    bool resolveEntity(const QString &, const QString &, QXmlInputSource *&) { return false; }
};

class MyXmlContentHandler : public QXmlContentHandler
{
public:
    MyXmlContentHandler() : QXmlContentHandler() {}
    bool characters(const QString &) { return false; }
    bool endDocument() { return false; }
    bool endElement(const QString &, const QString &, const QString &) { return false; }
    bool endPrefixMapping(const QString &) { return false; }
    QString errorString() const { return QString(); }
    bool ignorableWhitespace(const QString &) { return false; }
    bool processingInstruction(const QString &, const QString &) { return false; }
    void setDocumentLocator(QXmlLocator *) { }
    bool skippedEntity(const QString &) { return false; }
    bool startDocument() { return false; }
    bool startElement(const QString &, const QString &, const QString &, const QXmlAttributes &) { return false; }
    bool startPrefixMapping(const QString &, const QString &) { return false; }
};

class MyXmlErrorHandler : public QXmlErrorHandler
{
public:
    MyXmlErrorHandler() : QXmlErrorHandler() {}
    QString errorString() const { return QString(); } 
    bool error(const QXmlParseException &) { return false; }
    bool fatalError(const QXmlParseException &) { return false; }
    bool warning(const QXmlParseException &) { return false; }
};

class MyXmlLexicalHandler : public QXmlLexicalHandler
{
public:
    MyXmlLexicalHandler() : QXmlLexicalHandler() {}
    bool comment(const QString &) { return false; }
    bool endCDATA() { return false; }
    bool endDTD() { return false; }
    bool endEntity(const QString &) { return false; }
    QString errorString() const { return QString(); }
    bool startCDATA() { return false; }
    bool startDTD(const QString &, const QString &, const QString &) { return false; }
    bool startEntity(const QString &) { return false; }
};

class MyXmlDeclHandler : public QXmlDeclHandler
{
public:
    MyXmlDeclHandler() : QXmlDeclHandler() {}
    bool attributeDecl(const QString &, const QString &, const QString &, const QString &, const QString &) { return false; }
    QString errorString() const { return QString(); }
    bool externalEntityDecl(const QString &, const QString &, const QString &) { return false; }
    bool internalEntityDecl(const QString &, const QString &) { return false; }
};

// Testing get/set functions
void tst_QXml::getSetCheck()
{
    QXmlSimpleReader obj1;
    // QXmlEntityResolver* QXmlSimpleReader::entityResolver()
    // void QXmlSimpleReader::setEntityResolver(QXmlEntityResolver*)
    MyXmlEntityResolver *var1 = new MyXmlEntityResolver;
    obj1.setEntityResolver(var1);
    QCOMPARE(static_cast<QXmlEntityResolver *>(var1), obj1.entityResolver());
    obj1.setEntityResolver((QXmlEntityResolver *)0);
    QCOMPARE((QXmlEntityResolver *)0, obj1.entityResolver());
    delete var1;

    // QXmlContentHandler* QXmlSimpleReader::contentHandler()
    // void QXmlSimpleReader::setContentHandler(QXmlContentHandler*)
    MyXmlContentHandler *var2 = new MyXmlContentHandler;
    obj1.setContentHandler(var2);
    QCOMPARE(static_cast<QXmlContentHandler *>(var2), obj1.contentHandler());
    obj1.setContentHandler((QXmlContentHandler *)0);
    QCOMPARE((QXmlContentHandler *)0, obj1.contentHandler());
    delete var2;

    // QXmlErrorHandler* QXmlSimpleReader::errorHandler()
    // void QXmlSimpleReader::setErrorHandler(QXmlErrorHandler*)
    MyXmlErrorHandler *var3 = new MyXmlErrorHandler;
    obj1.setErrorHandler(var3);
    QCOMPARE(static_cast<QXmlErrorHandler *>(var3), obj1.errorHandler());
    obj1.setErrorHandler((QXmlErrorHandler *)0);
    QCOMPARE((QXmlErrorHandler *)0, obj1.errorHandler());
    delete var3;

    // QXmlLexicalHandler* QXmlSimpleReader::lexicalHandler()
    // void QXmlSimpleReader::setLexicalHandler(QXmlLexicalHandler*)
    MyXmlLexicalHandler *var4 = new MyXmlLexicalHandler;
    obj1.setLexicalHandler(var4);
    QCOMPARE(static_cast<QXmlLexicalHandler *>(var4), obj1.lexicalHandler());
    obj1.setLexicalHandler((QXmlLexicalHandler *)0);
    QCOMPARE((QXmlLexicalHandler *)0, obj1.lexicalHandler());
    delete var4;

    // QXmlDeclHandler* QXmlSimpleReader::declHandler()
    // void QXmlSimpleReader::setDeclHandler(QXmlDeclHandler*)
    MyXmlDeclHandler *var5 = new MyXmlDeclHandler;
    obj1.setDeclHandler(var5);
    QCOMPARE(static_cast<QXmlDeclHandler *>(var5), obj1.declHandler());
    obj1.setDeclHandler((QXmlDeclHandler *)0);
    QCOMPARE((QXmlDeclHandler *)0, obj1.declHandler());
    delete var5;
}

void tst_QXml::interpretedAs0D() const
{
    /* See task 172632. */

    class MyHandler : public QXmlDefaultHandler
    {
    public:
        virtual bool startElement(const QString &namespaceURI,
                                  const QString &localName,
                                  const QString &qName,
                                  const QXmlAttributes &atts)
        {
            Q_UNUSED(namespaceURI);
            Q_UNUSED(localName);
            Q_UNUSED(qName);
            attrName = atts.qName(0);
            attrCount = atts.count();
            return true;
        }

        QString attrName;
        int     attrCount;
    };

    const QString document(QLatin1String("<element ") +
                           QChar(0x010D) + 
                           QLatin1String("reated-by=\"an attr value\"/>"));

    QFile f("0x010D.xml");
    QVERIFY(f.open(QIODevice::ReadOnly));
    QXmlInputSource data(&f);

    QXmlSimpleReader reader;

    MyHandler myHandler;
    reader.setContentHandler(&myHandler);
    reader.setErrorHandler(&myHandler);

    QVERIFY(reader.parse(&data));

    QCOMPARE(myHandler.attrCount, 1);
    QCOMPARE(myHandler.attrName, QChar(0x010D) + QString::fromLatin1("reated-by"));
}

void tst_QXml::exception()
{
#ifndef QT_NO_EXCEPTIONS
    QString message = QString::fromLatin1("message");
    int column = 3;
    int line = 2;
    QString publicId = QString::fromLatin1("publicId");
    QString systemId = QString::fromLatin1("systemId");

    try {
        QXmlParseException e(message, column, line, publicId, systemId);
        throw e;
    }
    catch (QXmlParseException e) {
        QCOMPARE(e.message(), message);
        QCOMPARE(e.columnNumber(), column);
        QCOMPARE(e.lineNumber(), line);
        QCOMPARE(e.publicId(), publicId);
        QCOMPARE(e.systemId(), systemId);
    }
#else
    QSKIP("Exceptions not available", SkipAll);
#endif
}

QTEST_MAIN(tst_QXml)
#include "tst_qxml.moc"