summaryrefslogtreecommitdiffstats
path: root/tests/auto/qxmlstream
diff options
context:
space:
mode:
authorOswald Buddenhagen <oswald.buddenhagen@nokia.com>2010-12-16 19:32:21 (GMT)
committerOswald Buddenhagen <oswald.buddenhagen@nokia.com>2010-12-17 17:11:03 (GMT)
commit8789d4bd9eaba3a90fb3a94edb71ad0c1e01dc66 (patch)
tree4e7a9d94273ec8b09f1e8af00ff7d747f0f77fcf /tests/auto/qxmlstream
parent468328468904d116a5ff70fd4c71c34e453e8593 (diff)
downloadQt-8789d4bd9eaba3a90fb3a94edb71ad0c1e01dc66.zip
Qt-8789d4bd9eaba3a90fb3a94edb71ad0c1e01dc66.tar.gz
Qt-8789d4bd9eaba3a90fb3a94edb71ad0c1e01dc66.tar.bz2
add error handling to QXmlStreamWriter
introduce QXmlStreamWriter::hasError(). set the error flag when any write error occurs. ignore subsequent writes after the first error. Reviewed-by: denis
Diffstat (limited to 'tests/auto/qxmlstream')
-rw-r--r--tests/auto/qxmlstream/tst_qxmlstream.cpp82
1 files changed, 82 insertions, 0 deletions
diff --git a/tests/auto/qxmlstream/tst_qxmlstream.cpp b/tests/auto/qxmlstream/tst_qxmlstream.cpp
index 19e4b90..9e4f3ec 100644
--- a/tests/auto/qxmlstream/tst_qxmlstream.cpp
+++ b/tests/auto/qxmlstream/tst_qxmlstream.cpp
@@ -574,6 +574,7 @@ private slots:
void checkCommentIndentation() const;
void checkCommentIndentation_data() const;
void qtbug9196_crash() const;
+ void hasError() const;
private:
static QByteArray readFile(const QString &filename);
@@ -1560,5 +1561,86 @@ void tst_QXmlStream::qtbug9196_crash() const
}
}
+class FakeBuffer : public QBuffer
+{
+protected:
+ qint64 writeData(const char *c, qint64 i)
+ {
+ qint64 ai = qMin(m_capacity, i);
+ m_capacity -= ai;
+ return ai ? QBuffer::writeData(c, ai) : 0;
+ }
+public:
+ void setCapacity(int capacity) { m_capacity = capacity; }
+private:
+ qint64 m_capacity;
+};
+
+void tst_QXmlStream::hasError() const
+{
+ {
+ FakeBuffer fb;
+ QVERIFY(fb.open(QBuffer::ReadWrite));
+ fb.setCapacity(1000);
+ QXmlStreamWriter writer(&fb);
+ writer.writeStartDocument();
+ writer.writeEndDocument();
+ QVERIFY(!writer.hasError());
+ QCOMPARE(fb.data(), QByteArray("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"));
+ }
+
+ {
+ // Failure caused by write(QString)
+ FakeBuffer fb;
+ QVERIFY(fb.open(QBuffer::ReadWrite));
+ fb.setCapacity(strlen("<?xml version=\""));
+ QXmlStreamWriter writer(&fb);
+ writer.writeStartDocument();
+ QVERIFY(writer.hasError());
+ QCOMPARE(fb.data(), QByteArray("<?xml version=\""));
+ }
+
+ {
+ // Failure caused by write(char *)
+ FakeBuffer fb;
+ QVERIFY(fb.open(QBuffer::ReadWrite));
+ fb.setCapacity(strlen("<?xml version=\"1.0"));
+ QXmlStreamWriter writer(&fb);
+ writer.writeStartDocument();
+ QVERIFY(writer.hasError());
+ QCOMPARE(fb.data(), QByteArray("<?xml version=\"1.0"));
+ }
+
+ {
+ // Failure caused by write(QStringRef)
+ FakeBuffer fb;
+ QVERIFY(fb.open(QBuffer::ReadWrite));
+ fb.setCapacity(strlen("<?xml version=\"1.0\" encoding=\"UTF-8\"?><test xmlns:"));
+ QXmlStreamWriter writer(&fb);
+ writer.writeStartDocument();
+ writer.writeStartElement("test");
+ writer.writeNamespace("http://foo.bar", "foo");
+ QVERIFY(writer.hasError());
+ QCOMPARE(fb.data(), QByteArray("<?xml version=\"1.0\" encoding=\"UTF-8\"?><test xmlns:"));
+ }
+
+ {
+ // Refusal to write after 1st failure
+ FakeBuffer fb;
+ QVERIFY(fb.open(QBuffer::ReadWrite));
+ fb.setCapacity(10);
+ QXmlStreamWriter writer(&fb);
+ writer.writeStartDocument();
+ QVERIFY(writer.hasError());
+ QCOMPARE(fb.data(), QByteArray("<?xml vers"));
+ fb.setCapacity(1000);
+ writer.writeStartElement("test"); // literal & qstring
+ writer.writeNamespace("http://foo.bar", "foo"); // literal & qstringref
+ QVERIFY(writer.hasError());
+ QCOMPARE(fb.data(), QByteArray("<?xml vers"));
+ }
+
+}
+
#include "tst_qxmlstream.moc"
// vim: et:ts=4:sw=4:sts=4