|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the test suite of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 |
|
43 #include <QDomDocument> |
|
44 #include <qthread.h> |
|
45 #include <qtcpserver.h> |
|
46 #include <qtcpsocket.h> |
|
47 #include <QtTest/QtTest> |
|
48 #include <qfile.h> |
|
49 #include <qstring.h> |
|
50 #include <qdir.h> |
|
51 #include <qbuffer.h> |
|
52 #include "parser/parser.h" |
|
53 |
|
54 static const char *const inputString = "<!DOCTYPE inferno [<!ELEMENT inferno (circle+)><!ELEMENT circle (#PCDATA)>]><inferno><circle /><circle /></inferno>"; |
|
55 static const char *const refString = "setDocumentLocator(locator={columnNumber=1, lineNumber=1})\nstartDocument()\nstartDTD(name=\"inferno\", publicId=\"\", systemId=\"\")\nendDTD()\nstartElement(namespaceURI=\"\", localName=\"inferno\", qName=\"inferno\", atts=[])\nstartElement(namespaceURI=\"\", localName=\"circle\", qName=\"circle\", atts=[])\nendElement(namespaceURI=\"\", localName=\"circle\", qName=\"circle\")\nstartElement(namespaceURI=\"\", localName=\"circle\", qName=\"circle\", atts=[])\nendElement(namespaceURI=\"\", localName=\"circle\", qName=\"circle\")\nendElement(namespaceURI=\"\", localName=\"inferno\", qName=\"inferno\")\nendDocument()\n"; |
|
56 |
|
57 //TESTED_CLASS= |
|
58 //TESTED_FILES= |
|
59 |
|
60 #define TEST_PORT 1088 |
|
61 |
|
62 class XmlServer : public QThread |
|
63 { |
|
64 Q_OBJECT |
|
65 public: |
|
66 XmlServer(); |
|
67 bool quit_soon; |
|
68 |
|
69 protected: |
|
70 virtual void run(); |
|
71 }; |
|
72 |
|
73 XmlServer::XmlServer() |
|
74 { |
|
75 quit_soon = false; |
|
76 } |
|
77 |
|
78 #define CHUNK_SIZE 1 |
|
79 |
|
80 void XmlServer::run() |
|
81 { |
|
82 QTcpServer srv; |
|
83 |
|
84 if (!srv.listen(QHostAddress::Any, TEST_PORT)) |
|
85 return; |
|
86 |
|
87 for (;;) { |
|
88 srv.waitForNewConnection(100); |
|
89 |
|
90 if (QTcpSocket *sock = srv.nextPendingConnection()) { |
|
91 QByteArray fileName; |
|
92 for (;;) { |
|
93 char c; |
|
94 if (sock->getChar(&c)) { |
|
95 if (c == '\n') |
|
96 break; |
|
97 fileName.append(c); |
|
98 } else { |
|
99 if (!sock->waitForReadyRead(-1)) |
|
100 break; |
|
101 } |
|
102 } |
|
103 |
|
104 QFile file(QString::fromLocal8Bit(fileName)); |
|
105 if (!file.open(QIODevice::ReadOnly)) { |
|
106 qWarning() << "XmlServer::run(): could not open" << fileName; |
|
107 sock->abort(); |
|
108 delete sock; |
|
109 continue; |
|
110 } |
|
111 |
|
112 QByteArray data = file.readAll(); |
|
113 for (int i = 0; i < data.size();) { |
|
114 // sock->putChar(data.at(i)); |
|
115 int cnt = qMin(CHUNK_SIZE, data.size() - i); |
|
116 sock->write(data.constData() + i, cnt); |
|
117 i += cnt; |
|
118 sock->flush(); |
|
119 QTest::qSleep(1); |
|
120 |
|
121 if (quit_soon) { |
|
122 sock->abort(); |
|
123 break; |
|
124 } |
|
125 } |
|
126 |
|
127 sock->disconnectFromHost(); |
|
128 delete sock; |
|
129 } |
|
130 |
|
131 if (quit_soon) |
|
132 break; |
|
133 } |
|
134 |
|
135 srv.close(); |
|
136 } |
|
137 |
|
138 class tst_QXmlSimpleReader : public QObject |
|
139 { |
|
140 Q_OBJECT |
|
141 |
|
142 public: |
|
143 tst_QXmlSimpleReader(); |
|
144 ~tst_QXmlSimpleReader(); |
|
145 |
|
146 private slots: |
|
147 |
|
148 void testGoodXmlFile(); |
|
149 void testGoodXmlFile_data(); |
|
150 void testBadXmlFile(); |
|
151 void testBadXmlFile_data(); |
|
152 void testIncrementalParsing(); |
|
153 void testIncrementalParsing_data(); |
|
154 void setDataQString(); |
|
155 void inputFromQIODevice(); |
|
156 void inputFromString(); |
|
157 void inputFromSocket_data(); |
|
158 void inputFromSocket(); |
|
159 |
|
160 void idsInParseException1(); |
|
161 void idsInParseException2(); |
|
162 void preserveCharacterReferences() const; |
|
163 void reportNamespace() const; |
|
164 void reportNamespace_data() const; |
|
165 void roundtripWithNamespaces() const; |
|
166 |
|
167 private: |
|
168 static QDomDocument fromByteArray(const QString &title, const QByteArray &ba); |
|
169 XmlServer *server; |
|
170 }; |
|
171 |
|
172 tst_QXmlSimpleReader::tst_QXmlSimpleReader() |
|
173 { |
|
174 server = new XmlServer(); |
|
175 server->setParent(this); |
|
176 server->start(); |
|
177 QTest::qSleep(1000); |
|
178 } |
|
179 |
|
180 tst_QXmlSimpleReader::~tst_QXmlSimpleReader() |
|
181 { |
|
182 server->quit_soon = true; |
|
183 server->wait(); |
|
184 } |
|
185 |
|
186 class MyErrorHandler : public QXmlErrorHandler |
|
187 { |
|
188 public: |
|
189 QString publicId; |
|
190 QString systemId; |
|
191 |
|
192 virtual bool error(const QXmlParseException &) |
|
193 { |
|
194 return false; |
|
195 } |
|
196 |
|
197 virtual QString errorString() const |
|
198 { |
|
199 return QString(); |
|
200 } |
|
201 |
|
202 virtual bool fatalError(const QXmlParseException &exception) |
|
203 { |
|
204 publicId = exception.publicId(); |
|
205 systemId = exception.systemId(); |
|
206 return true; |
|
207 } |
|
208 |
|
209 virtual bool warning(const QXmlParseException &) |
|
210 { |
|
211 return true; |
|
212 } |
|
213 |
|
214 }; |
|
215 |
|
216 void tst_QXmlSimpleReader::idsInParseException1() |
|
217 { |
|
218 MyErrorHandler handler; |
|
219 QXmlSimpleReader reader; |
|
220 |
|
221 reader.setErrorHandler(&handler); |
|
222 |
|
223 /* A non-wellformed XML document with PUBLIC and SYSTEM. */ |
|
224 QByteArray input("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" " |
|
225 "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" |
|
226 "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">" |
|
227 "<head>" |
|
228 "<a/><a/><a/>" |
|
229 "<head/>"); |
|
230 |
|
231 QBuffer buff(&input); |
|
232 QXmlInputSource source(&buff); |
|
233 |
|
234 /* Yes, parsing should be reported as a failure. */ |
|
235 QVERIFY(!reader.parse(source)); |
|
236 |
|
237 QCOMPARE(handler.publicId, QString::fromLatin1("-//W3C//DTD XHTML 1.0 Strict//EN")); |
|
238 QCOMPARE(handler.systemId, QString::fromLatin1("http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd")); |
|
239 } |
|
240 |
|
241 void tst_QXmlSimpleReader::idsInParseException2() |
|
242 { |
|
243 MyErrorHandler handler; |
|
244 QXmlSimpleReader reader; |
|
245 |
|
246 reader.setErrorHandler(&handler); |
|
247 |
|
248 /* A non-wellformed XML document with only SYSTEM. */ |
|
249 QByteArray input("<!DOCTYPE html SYSTEM \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" |
|
250 "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">" |
|
251 "<head>" |
|
252 "<a/><a/><a/>" |
|
253 "<head/>"); |
|
254 |
|
255 QBuffer buff(&input); |
|
256 QXmlInputSource source(&buff); |
|
257 |
|
258 /* Yes, parsing should be reported as a failure. */ |
|
259 QVERIFY(!reader.parse(source)); |
|
260 |
|
261 QCOMPARE(handler.publicId, QString()); |
|
262 QCOMPARE(handler.systemId, QString::fromLatin1("http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd")); |
|
263 } |
|
264 |
|
265 static QStringList findXmlFiles(QString dir_name) |
|
266 { |
|
267 QStringList result; |
|
268 |
|
269 QDir dir(dir_name); |
|
270 QFileInfoList file_list = dir.entryInfoList(QStringList("*.xml"), QDir::Files, QDir::Name); |
|
271 |
|
272 QFileInfoList::const_iterator it = file_list.begin(); |
|
273 for (; it != file_list.end(); ++it) { |
|
274 const QFileInfo &file_info = *it; |
|
275 result.append(file_info.filePath()); |
|
276 } |
|
277 |
|
278 return result; |
|
279 } |
|
280 |
|
281 |
|
282 void tst_QXmlSimpleReader::testGoodXmlFile_data() |
|
283 { |
|
284 const char * const good_data_dirs[] = { |
|
285 "xmldocs/valid/sa", |
|
286 "xmldocs/valid/not-sa", |
|
287 "xmldocs/valid/ext-sa", |
|
288 0 |
|
289 }; |
|
290 const char * const *d = good_data_dirs; |
|
291 |
|
292 QStringList good_file_list; |
|
293 for (; *d != 0; ++d) |
|
294 good_file_list += findXmlFiles(*d); |
|
295 |
|
296 QTest::addColumn<QString>("file_name"); |
|
297 QStringList::const_iterator it = good_file_list.begin(); |
|
298 for (; it != good_file_list.end(); ++it) |
|
299 QTest::newRow((*it).toLatin1()) << *it; |
|
300 } |
|
301 |
|
302 void tst_QXmlSimpleReader::testGoodXmlFile() |
|
303 { |
|
304 QFETCH(QString, file_name); |
|
305 QFile file(file_name); |
|
306 QVERIFY(file.open(QIODevice::ReadOnly)); |
|
307 QString content = file.readAll(); |
|
308 file.close(); |
|
309 QVERIFY(file.open(QIODevice::ReadOnly)); |
|
310 Parser parser; |
|
311 |
|
312 // static int i = 0; |
|
313 // qWarning("Test nr: " + QString::number(i)); ++i; |
|
314 QEXPECT_FAIL("xmldocs/valid/sa/089.xml", "", Continue); |
|
315 QVERIFY(parser.parseFile(&file)); |
|
316 |
|
317 QFile ref_file(file_name + ".ref"); |
|
318 QVERIFY(ref_file.open(QIODevice::ReadOnly | QIODevice::Text)); |
|
319 QTextStream ref_stream(&ref_file); |
|
320 ref_stream.setCodec("UTF-8"); |
|
321 QString ref_file_contents = ref_stream.readAll(); |
|
322 |
|
323 QEXPECT_FAIL("xmldocs/valid/sa/089.xml", "", Continue); |
|
324 QCOMPARE(parser.result(), ref_file_contents); |
|
325 } |
|
326 |
|
327 void tst_QXmlSimpleReader::testBadXmlFile_data() |
|
328 { |
|
329 const char * const bad_data_dirs[] = { |
|
330 "xmldocs/not-wf/sa", |
|
331 0 |
|
332 }; |
|
333 const char * const *d = bad_data_dirs; |
|
334 |
|
335 QStringList bad_file_list; |
|
336 for (; *d != 0; ++d) |
|
337 bad_file_list += findXmlFiles(*d); |
|
338 |
|
339 QTest::addColumn<QString>("file_name"); |
|
340 QStringList::const_iterator it = bad_file_list.begin(); |
|
341 for (; it != bad_file_list.end(); ++it) |
|
342 QTest::newRow((*it).toLatin1()) << *it; |
|
343 } |
|
344 |
|
345 void tst_QXmlSimpleReader::testBadXmlFile() |
|
346 { |
|
347 QFETCH(QString, file_name); |
|
348 QFile file(file_name); |
|
349 QVERIFY(file.open(QIODevice::ReadOnly)); |
|
350 Parser parser; |
|
351 |
|
352 // static int i = 0; |
|
353 // qWarning("Test nr: " + QString::number(++i)); |
|
354 QEXPECT_FAIL("xmldocs/not-wf/sa/030.xml", "", Continue); |
|
355 QEXPECT_FAIL("xmldocs/not-wf/sa/031.xml", "", Continue); |
|
356 QEXPECT_FAIL("xmldocs/not-wf/sa/032.xml", "", Continue); |
|
357 QEXPECT_FAIL("xmldocs/not-wf/sa/033.xml", "", Continue); |
|
358 QEXPECT_FAIL("xmldocs/not-wf/sa/038.xml", "", Continue); |
|
359 QEXPECT_FAIL("xmldocs/not-wf/sa/072.xml", "", Continue); |
|
360 QEXPECT_FAIL("xmldocs/not-wf/sa/073.xml", "", Continue); |
|
361 QEXPECT_FAIL("xmldocs/not-wf/sa/074.xml", "", Continue); |
|
362 QEXPECT_FAIL("xmldocs/not-wf/sa/076.xml", "", Continue); |
|
363 QEXPECT_FAIL("xmldocs/not-wf/sa/077.xml", "", Continue); |
|
364 QEXPECT_FAIL("xmldocs/not-wf/sa/078.xml", "", Continue); |
|
365 QEXPECT_FAIL("xmldocs/not-wf/sa/085.xml", "", Continue); |
|
366 QEXPECT_FAIL("xmldocs/not-wf/sa/086.xml", "", Continue); |
|
367 QEXPECT_FAIL("xmldocs/not-wf/sa/087.xml", "", Continue); |
|
368 QEXPECT_FAIL("xmldocs/not-wf/sa/101.xml", "", Continue); |
|
369 QEXPECT_FAIL("xmldocs/not-wf/sa/102.xml", "", Continue); |
|
370 QEXPECT_FAIL("xmldocs/not-wf/sa/104.xml", "", Continue); |
|
371 QEXPECT_FAIL("xmldocs/not-wf/sa/116.xml", "", Continue); |
|
372 QEXPECT_FAIL("xmldocs/not-wf/sa/117.xml", "", Continue); |
|
373 QEXPECT_FAIL("xmldocs/not-wf/sa/119.xml", "", Continue); |
|
374 QEXPECT_FAIL("xmldocs/not-wf/sa/122.xml", "", Continue); |
|
375 QEXPECT_FAIL("xmldocs/not-wf/sa/132.xml", "", Continue); |
|
376 QEXPECT_FAIL("xmldocs/not-wf/sa/142.xml", "", Continue); |
|
377 QEXPECT_FAIL("xmldocs/not-wf/sa/143.xml", "", Continue); |
|
378 QEXPECT_FAIL("xmldocs/not-wf/sa/144.xml", "", Continue); |
|
379 QEXPECT_FAIL("xmldocs/not-wf/sa/145.xml", "", Continue); |
|
380 QEXPECT_FAIL("xmldocs/not-wf/sa/146.xml", "", Abort); |
|
381 QEXPECT_FAIL("xmldocs/not-wf/sa/160.xml", "", Continue); |
|
382 QEXPECT_FAIL("xmldocs/not-wf/sa/162.xml", "", Continue); |
|
383 QEXPECT_FAIL("xmldocs/not-wf/sa/166.xml", "", Continue); |
|
384 QEXPECT_FAIL("xmldocs/not-wf/sa/167.xml", "", Continue); |
|
385 QEXPECT_FAIL("xmldocs/not-wf/sa/168.xml", "", Continue); |
|
386 QEXPECT_FAIL("xmldocs/not-wf/sa/169.xml", "", Continue); |
|
387 QEXPECT_FAIL("xmldocs/not-wf/sa/170.xml", "", Continue); |
|
388 QEXPECT_FAIL("xmldocs/not-wf/sa/171.xml", "", Abort); |
|
389 QEXPECT_FAIL("xmldocs/not-wf/sa/172.xml", "", Abort); |
|
390 QEXPECT_FAIL("xmldocs/not-wf/sa/173.xml", "", Abort); |
|
391 QEXPECT_FAIL("xmldocs/not-wf/sa/174.xml", "", Abort); |
|
392 QEXPECT_FAIL("xmldocs/not-wf/sa/175.xml", "", Abort); |
|
393 QEXPECT_FAIL("xmldocs/not-wf/sa/177.xml", "", Abort); |
|
394 QEXPECT_FAIL("xmldocs/not-wf/sa/180.xml", "", Continue); |
|
395 QEXPECT_FAIL("xmldocs/not-wf/sa/181.xml", "", Continue); |
|
396 QEXPECT_FAIL("xmldocs/not-wf/sa/182.xml", "", Continue); |
|
397 QEXPECT_FAIL("xmldocs/not-wf/sa/185.xml", "", Continue); |
|
398 QEXPECT_FAIL("xmldocs/not-wf/sa/186.xml", "", Continue); |
|
399 |
|
400 QVERIFY(!parser.parseFile(&file)); |
|
401 |
|
402 QFile ref_file(file_name + ".ref"); |
|
403 QVERIFY(ref_file.open(QIODevice::ReadOnly | QIODevice::Text)); |
|
404 QTextStream ref_stream(&ref_file); |
|
405 ref_stream.setCodec("UTF-8"); |
|
406 QString ref_file_contents = ref_stream.readAll(); |
|
407 |
|
408 QEXPECT_FAIL("xmldocs/not-wf/sa/144.xml", "", Continue); |
|
409 QEXPECT_FAIL("xmldocs/not-wf/sa/145.xml", "", Continue); |
|
410 QEXPECT_FAIL("xmldocs/not-wf/sa/146.xml", "", Continue); |
|
411 QEXPECT_FAIL("xmldocs/not-wf/sa/167.xml", "", Continue); |
|
412 QEXPECT_FAIL("xmldocs/not-wf/sa/166.xml", "", Continue); |
|
413 QEXPECT_FAIL("xmldocs/not-wf/sa/170.xml", "", Continue); |
|
414 |
|
415 QCOMPARE(parser.result(), ref_file_contents); |
|
416 } |
|
417 |
|
418 void tst_QXmlSimpleReader::testIncrementalParsing_data() |
|
419 { |
|
420 QTest::addColumn<QString>("file_name"); |
|
421 QTest::addColumn<int>("chunkSize"); |
|
422 |
|
423 const char * const good_data_dirs[] = { |
|
424 "xmldocs/valid/sa", |
|
425 "xmldocs/valid/not-sa", |
|
426 "xmldocs/valid/ext-sa", |
|
427 0 |
|
428 }; |
|
429 const char * const *d = good_data_dirs; |
|
430 |
|
431 QStringList good_file_list; |
|
432 for (; *d != 0; ++d) |
|
433 good_file_list += findXmlFiles(*d); |
|
434 |
|
435 for (int i=1; i<10; ++i) { |
|
436 QStringList::const_iterator it = good_file_list.begin(); |
|
437 for (; it != good_file_list.end(); ++it) { |
|
438 if ( *it == "xmldocs/valid/sa/089.xml" ) |
|
439 continue;// TODO: fails at the moment -- don't bother |
|
440 if ( i==1 && ( |
|
441 *it == "xmldocs/valid/sa/049.xml" || |
|
442 *it == "xmldocs/valid/sa/050.xml" || |
|
443 *it == "xmldocs/valid/sa/051.xml" || |
|
444 *it == "xmldocs/valid/sa/052.xml" ) ) { |
|
445 continue; // TODO: fails at the moment -- don't bother |
|
446 } |
|
447 QTest::newRow(QString("%1 %2").arg(*it).arg(i).toLatin1()) << *it << i; |
|
448 } |
|
449 } |
|
450 } |
|
451 |
|
452 void tst_QXmlSimpleReader::testIncrementalParsing() |
|
453 { |
|
454 QFETCH(QString, file_name); |
|
455 QFETCH(int, chunkSize); |
|
456 |
|
457 QFile file(file_name); |
|
458 QVERIFY(file.open(QIODevice::ReadOnly)); |
|
459 |
|
460 Parser parser; |
|
461 QXmlInputSource source; |
|
462 bool first = true; |
|
463 while (!file.atEnd()) { |
|
464 source.setData(file.read(chunkSize)); |
|
465 if(first) { |
|
466 QVERIFY(parser.parse(&source, true)); |
|
467 first = false; |
|
468 } else { |
|
469 QVERIFY(parser.parseContinue()); |
|
470 } |
|
471 } |
|
472 // detect end of document |
|
473 QVERIFY(parser.parseContinue()); |
|
474 // parsing should fail after the end of the document was reached |
|
475 QVERIFY(!parser.parseContinue()); |
|
476 |
|
477 QFile ref_file(file_name + ".ref"); |
|
478 QVERIFY(ref_file.open(QIODevice::ReadOnly | QIODevice::Text)); |
|
479 QTextStream ref_stream(&ref_file); |
|
480 ref_stream.setCodec("UTF-8"); |
|
481 QString ref_file_contents = ref_stream.readAll(); |
|
482 |
|
483 QCOMPARE(parser.result(), ref_file_contents); |
|
484 } |
|
485 |
|
486 void tst_QXmlSimpleReader::setDataQString() |
|
487 { |
|
488 QString input = inputString; |
|
489 QString ref = refString; |
|
490 |
|
491 QXmlInputSource source; |
|
492 Parser parser; |
|
493 |
|
494 source.setData(input); |
|
495 QVERIFY(parser.parse(&source,false)); |
|
496 |
|
497 QBuffer resultBuffer; |
|
498 resultBuffer.setData(parser.result().toLatin1()); |
|
499 |
|
500 QBuffer refBuffer; |
|
501 refBuffer.setData(ref.toLatin1()); |
|
502 |
|
503 resultBuffer.open(QIODevice::ReadOnly); |
|
504 refBuffer.open(QIODevice::ReadOnly); |
|
505 |
|
506 bool success = true; |
|
507 while (resultBuffer.canReadLine()) { |
|
508 if (!refBuffer.canReadLine()) { |
|
509 success = false; break ; |
|
510 } |
|
511 if (resultBuffer.readLine().simplified() != refBuffer.readLine().simplified()) { |
|
512 success = false; break ; |
|
513 } |
|
514 } |
|
515 QVERIFY(success); |
|
516 } |
|
517 |
|
518 void tst_QXmlSimpleReader::inputFromQIODevice() |
|
519 { |
|
520 QBuffer inputBuffer; |
|
521 inputBuffer.setData(inputString); |
|
522 |
|
523 QXmlInputSource source(&inputBuffer); |
|
524 Parser parser; |
|
525 |
|
526 QVERIFY(parser.parse(&source,false)); |
|
527 |
|
528 QBuffer resultBuffer; |
|
529 resultBuffer.setData(parser.result().toLatin1()); |
|
530 |
|
531 QBuffer refBuffer; |
|
532 refBuffer.setData(refString); |
|
533 |
|
534 resultBuffer.open(QIODevice::ReadOnly); |
|
535 refBuffer.open(QIODevice::ReadOnly); |
|
536 |
|
537 bool success = true; |
|
538 while (resultBuffer.canReadLine()) { |
|
539 if (!refBuffer.canReadLine()) { |
|
540 success = false; break ; |
|
541 } |
|
542 if (resultBuffer.readLine().simplified() != refBuffer.readLine().simplified()) { |
|
543 success = false; break ; |
|
544 } |
|
545 } |
|
546 QVERIFY(success); |
|
547 } |
|
548 |
|
549 void tst_QXmlSimpleReader::inputFromString() |
|
550 { |
|
551 QString str = "<foo><bar>kake</bar><bar>ja</bar></foo>"; |
|
552 QBuffer buff; |
|
553 buff.setData((char*)str.utf16(), str.size()*sizeof(ushort)); |
|
554 |
|
555 QXmlInputSource input(&buff); |
|
556 |
|
557 QXmlSimpleReader reader; |
|
558 QXmlDefaultHandler handler; |
|
559 reader.setContentHandler(&handler); |
|
560 |
|
561 QVERIFY(reader.parse(&input)); |
|
562 } |
|
563 |
|
564 void tst_QXmlSimpleReader::inputFromSocket_data() |
|
565 { |
|
566 QStringList files = findXmlFiles(QLatin1String("encodings")); |
|
567 QVERIFY(files.count() > 0); |
|
568 |
|
569 QTest::addColumn<QString>("file_name"); |
|
570 |
|
571 foreach (const QString &file_name, files) |
|
572 QTest::newRow(file_name.toLatin1()) << file_name; |
|
573 } |
|
574 |
|
575 void tst_QXmlSimpleReader::inputFromSocket() |
|
576 { |
|
577 QFETCH(QString, file_name); |
|
578 |
|
579 #if defined(Q_OS_SYMBIAN) |
|
580 QSKIP("Symbian: Skipped due to problems in Open C and QtNetwork", SkipAll); |
|
581 #endif |
|
582 |
|
583 #if defined(Q_OS_WIN32) && (defined(Q_CC_INTEL) || defined(Q_CC_MINGW) || defined(Q_CC_MSVC_NET)) |
|
584 QSKIP("Regression caused by QHOstInfo change 294548, see task 202231.", SkipAll); |
|
585 #endif |
|
586 QTcpSocket sock; |
|
587 sock.connectToHost(QHostAddress::LocalHost, TEST_PORT); |
|
588 |
|
589 const bool connectionSuccess = sock.waitForConnected(); |
|
590 if(!connectionSuccess) { |
|
591 QTextStream out(stderr); |
|
592 out << "QTcpSocket::errorString()" << sock.errorString(); |
|
593 } |
|
594 |
|
595 QVERIFY(connectionSuccess); |
|
596 |
|
597 sock.write(file_name.toLocal8Bit() + "\n"); |
|
598 QVERIFY(sock.waitForBytesWritten()); |
|
599 |
|
600 QXmlInputSource input(&sock); |
|
601 |
|
602 QXmlSimpleReader reader; |
|
603 QXmlDefaultHandler handler; |
|
604 reader.setContentHandler(&handler); |
|
605 |
|
606 QVERIFY(reader.parse(&input)); |
|
607 |
|
608 // qDebug() << "tst_QXmlSimpleReader::inputFromSocket(): success" << file_name; |
|
609 } |
|
610 |
|
611 void tst_QXmlSimpleReader::preserveCharacterReferences() const |
|
612 { |
|
613 class Handler : public QXmlDefaultHandler |
|
614 { |
|
615 public: |
|
616 virtual bool characters(const QString &chars) |
|
617 { |
|
618 received = chars; |
|
619 return true; |
|
620 } |
|
621 |
|
622 QString received; |
|
623 }; |
|
624 |
|
625 { |
|
626 QByteArray input("<e>A    A</e>"); |
|
627 |
|
628 QBuffer buff(&input); |
|
629 QXmlInputSource source(&buff); |
|
630 |
|
631 Handler h; |
|
632 QXmlSimpleReader reader; |
|
633 reader.setContentHandler(&h); |
|
634 QVERIFY(reader.parse(&source, false)); |
|
635 |
|
636 QCOMPARE(h.received, QLatin1Char('A') + QString(4, QChar(160)) + QLatin1Char('A')); |
|
637 } |
|
638 |
|
639 { |
|
640 QByteArray input("<e>    </e>"); |
|
641 |
|
642 QBuffer buff(&input); |
|
643 QXmlInputSource source(&buff); |
|
644 |
|
645 Handler h; |
|
646 QXmlSimpleReader reader; |
|
647 reader.setContentHandler(&h); |
|
648 QVERIFY(reader.parse(&source, false)); |
|
649 |
|
650 QCOMPARE(h.received, QString(4, QChar(160))); |
|
651 } |
|
652 } |
|
653 |
|
654 void tst_QXmlSimpleReader::reportNamespace() const |
|
655 { |
|
656 class Handler : public QXmlDefaultHandler |
|
657 { |
|
658 public: |
|
659 virtual bool startElement(const QString &namespaceURI, |
|
660 const QString &localName, |
|
661 const QString &qName, |
|
662 const QXmlAttributes &) |
|
663 { |
|
664 startNamespaceURI = namespaceURI; |
|
665 startLocalName = localName; |
|
666 startQName = qName; |
|
667 |
|
668 return true; |
|
669 } |
|
670 |
|
671 virtual bool endElement(const QString &namespaceURI, |
|
672 const QString &localName, |
|
673 const QString &qName) |
|
674 { |
|
675 endNamespaceURI = namespaceURI; |
|
676 endLocalName = localName; |
|
677 endQName = qName; |
|
678 |
|
679 return true; |
|
680 } |
|
681 |
|
682 QString startLocalName; |
|
683 QString startQName; |
|
684 QString startNamespaceURI; |
|
685 QString endLocalName; |
|
686 QString endQName; |
|
687 QString endNamespaceURI; |
|
688 }; |
|
689 |
|
690 QXmlSimpleReader reader; |
|
691 Handler handler; |
|
692 reader.setContentHandler(&handler); |
|
693 |
|
694 QFETCH(QByteArray, input); |
|
695 |
|
696 QBuffer buffer(&input); |
|
697 QVERIFY(buffer.open(QIODevice::ReadOnly)); |
|
698 |
|
699 QXmlInputSource source(&buffer); |
|
700 QVERIFY(reader.parse(source)); |
|
701 |
|
702 QFETCH(QString, expectedQName); |
|
703 QFETCH(QString, expectedLocalName); |
|
704 QFETCH(QString, expectedNamespace); |
|
705 |
|
706 QCOMPARE(handler.startNamespaceURI, expectedNamespace); |
|
707 QCOMPARE(handler.startLocalName, expectedLocalName); |
|
708 QCOMPARE(handler.startQName, expectedQName); |
|
709 |
|
710 QCOMPARE(handler.endNamespaceURI, expectedNamespace); |
|
711 QCOMPARE(handler.endLocalName, expectedLocalName); |
|
712 QCOMPARE(handler.endQName, expectedQName); |
|
713 } |
|
714 |
|
715 void tst_QXmlSimpleReader::reportNamespace_data() const |
|
716 { |
|
717 QTest::addColumn<QByteArray>("input"); |
|
718 QTest::addColumn<QString>("expectedQName"); |
|
719 QTest::addColumn<QString>("expectedLocalName"); |
|
720 QTest::addColumn<QString>("expectedNamespace"); |
|
721 |
|
722 QTest::newRow("default ns") << QByteArray("<element xmlns='http://example.com/'/>") |
|
723 << QString("element") |
|
724 << QString("element") |
|
725 << QString("http://example.com/"); |
|
726 |
|
727 QTest::newRow("with prefix") << QByteArray("<p:element xmlns:p='http://example.com/'/>") |
|
728 << QString("p:element") |
|
729 << QString("element") |
|
730 << QString("http://example.com/"); |
|
731 } |
|
732 |
|
733 QDomDocument tst_QXmlSimpleReader::fromByteArray(const QString &title, const QByteArray &ba) |
|
734 { |
|
735 QDomDocument doc(title); |
|
736 const bool ret = doc.setContent(ba, true); |
|
737 Q_ASSERT(ret); |
|
738 return doc; |
|
739 } |
|
740 |
|
741 void tst_QXmlSimpleReader::roundtripWithNamespaces() const |
|
742 { |
|
743 QEXPECT_FAIL("", "Known problem, see 154573. The fix happens to break uic.", Abort); |
|
744 |
|
745 const char *const expected = "<element b:attr=\"value\" xmlns:a=\"http://www.example.com/A\" xmlns:b=\"http://www.example.com/B\" />\n"; |
|
746 |
|
747 { |
|
748 const char *const xml = "<element xmlns:b=\"http://www.example.com/B\" b:attr=\"value\" xmlns:a=\"http://www.example.com/A\"/>"; |
|
749 |
|
750 const QDomDocument one(fromByteArray("document", xml)); |
|
751 const QDomDocument two(fromByteArray("document2", one.toByteArray(2))); |
|
752 |
|
753 QCOMPARE(expected, one.toByteArray().constData()); |
|
754 QCOMPARE(one.toByteArray(2).constData(), two.toByteArray(2).constData()); |
|
755 QCOMPARE(two.toByteArray(2).constData(), two.toByteArray(2).constData()); |
|
756 } |
|
757 |
|
758 { |
|
759 const char *const xml = "<element b:attr=\"value\" xmlns:b=\"http://www.example.com/B\" xmlns:a=\"http://www.example.com/A\"/>"; |
|
760 |
|
761 const QDomDocument one(fromByteArray("document", xml)); |
|
762 const QDomDocument two(fromByteArray("document2", one.toByteArray(2))); |
|
763 |
|
764 QCOMPARE(expected, one.toByteArray().constData()); |
|
765 QCOMPARE(one.toByteArray(2).constData(), two.toByteArray(2).constData()); |
|
766 QCOMPARE(two.toByteArray(2).constData(), two.toByteArray(2).constData()); |
|
767 } |
|
768 } |
|
769 |
|
770 QTEST_MAIN(tst_QXmlSimpleReader) |
|
771 #include "tst_qxmlsimplereader.moc" |