[Ktutorial-commits] SF.net SVN: ktutorial:[198] trunk/ktutorial/ktutorial-editor
Status: Alpha
Brought to you by:
danxuliu
|
From: <dan...@us...> - 2010-03-26 21:03:33
|
Revision: 198
http://ktutorial.svn.sourceforge.net/ktutorial/?rev=198&view=rev
Author: danxuliu
Date: 2010-03-26 21:03:26 +0000 (Fri, 26 Mar 2010)
Log Message:
-----------
-Use IOExceptions instead of return values to report errors.
-Create base class Exception to be inherited by all the exceptions. It is just a subclass of std::exception using QString for the messages.
Modified Paths:
--------------
trunk/ktutorial/ktutorial-editor/src/CMakeLists.txt
trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp
trunk/ktutorial/ktutorial-editor/src/serialization/CMakeLists.txt
trunk/ktutorial/ktutorial-editor/src/serialization/DeserializationException.cpp
trunk/ktutorial/ktutorial-editor/src/serialization/DeserializationException.h
trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.cpp
trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.h
trunk/ktutorial/ktutorial-editor/tests/unit/CMakeLists.txt
trunk/ktutorial/ktutorial-editor/tests/unit/serialization/CMakeLists.txt
trunk/ktutorial/ktutorial-editor/tests/unit/serialization/SerializationTest.cpp
Added Paths:
-----------
trunk/ktutorial/ktutorial-editor/src/Exception.cpp
trunk/ktutorial/ktutorial-editor/src/Exception.h
trunk/ktutorial/ktutorial-editor/src/serialization/IOException.cpp
trunk/ktutorial/ktutorial-editor/src/serialization/IOException.h
trunk/ktutorial/ktutorial-editor/tests/unit/ExceptionTest.cpp
trunk/ktutorial/ktutorial-editor/tests/unit/serialization/IOExceptionTest.cpp
Modified: trunk/ktutorial/ktutorial-editor/src/CMakeLists.txt
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/CMakeLists.txt 2010-03-26 19:40:54 UTC (rev 197)
+++ trunk/ktutorial/ktutorial-editor/src/CMakeLists.txt 2010-03-26 21:03:26 UTC (rev 198)
@@ -8,6 +8,7 @@
include_directories(${KDE4_INCLUDES})
set(ktutorial_editor_SRCS
+ Exception.cpp
KTutorialEditor.cpp
Reaction.cpp
Step.cpp
Added: trunk/ktutorial/ktutorial-editor/src/Exception.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/Exception.cpp (rev 0)
+++ trunk/ktutorial/ktutorial-editor/src/Exception.cpp 2010-03-26 21:03:26 UTC (rev 198)
@@ -0,0 +1,36 @@
+/***************************************************************************
+ * Copyright (C) 2010 by Daniel Calviño Sánchez *
+ * dan...@gm... *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 3 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; If not, see <http://www.gnu.org/licenses/>. *
+ ***************************************************************************/
+
+#include "Exception.h"
+
+//public:
+
+Exception::Exception(const QString& message): std::exception(),
+ mMessage(message) {
+}
+
+Exception::~Exception() throw() {
+}
+
+const char* Exception::what() const throw() {
+ return mMessage.toUtf8();
+}
+
+QString Exception::message() const throw() {
+ return mMessage;
+}
Property changes on: trunk/ktutorial/ktutorial-editor/src/Exception.cpp
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/ktutorial/ktutorial-editor/src/Exception.h
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/Exception.h (rev 0)
+++ trunk/ktutorial/ktutorial-editor/src/Exception.h 2010-03-26 21:03:26 UTC (rev 198)
@@ -0,0 +1,54 @@
+/***************************************************************************
+ * Copyright (C) 2010 by Daniel Calviño Sánchez *
+ * dan...@gm... *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 3 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; If not, see <http://www.gnu.org/licenses/>. *
+ ***************************************************************************/
+
+#ifndef EXCEPTION_H
+#define EXCEPTION_H
+
+#include <exception>
+#include <QString>
+
+/**
+ * Base class for exceptions.
+ */
+class Exception: public std::exception {
+public:
+
+ explicit Exception(const QString& message = QString());
+ virtual ~Exception() throw();
+
+ /**
+ * Returns the exception message.
+ *
+ * @return The exception message.
+ */
+ virtual const char* what() const throw();
+
+ /**
+ * Returns the exception message.
+ *
+ * @return The exception message.
+ */
+ QString message() const throw();
+
+private:
+
+ QString mMessage;
+
+};
+
+#endif
Property changes on: trunk/ktutorial/ktutorial-editor/src/Exception.h
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp 2010-03-26 19:40:54 UTC (rev 197)
+++ trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp 2010-03-26 21:03:26 UTC (rev 198)
@@ -301,10 +301,12 @@
return;
}
- if (!Serialization::exportTutorial(mTutorial, dialog->currentFilter(),
- dialog->selectedUrl())) {
+ try {
+ Serialization::exportTutorial(mTutorial, dialog->currentFilter(),
+ dialog->selectedUrl());
+ } catch (IOException e) {
QString text = i18nc("@label", "There was a problem when trying to "
-"save the exported tutorial:<nl/>%1", KIO::NetAccess::lastErrorString());
+"save the exported tutorial:<nl/>%1", e.message());
QString caption = i18nc("@title:window", "Exported tutorial could not "
"be saved");
KMessageBox::error(this, text, caption);
Modified: trunk/ktutorial/ktutorial-editor/src/serialization/CMakeLists.txt
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/serialization/CMakeLists.txt 2010-03-26 19:40:54 UTC (rev 197)
+++ trunk/ktutorial/ktutorial-editor/src/serialization/CMakeLists.txt 2010-03-26 21:03:26 UTC (rev 198)
@@ -2,6 +2,7 @@
set(ktutorial_editor_serialization_SRCS
DeserializationException.cpp
+ IOException.cpp
JavascriptExporter.cpp
Serialization.cpp
TutorialReader.cpp
Modified: trunk/ktutorial/ktutorial-editor/src/serialization/DeserializationException.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/serialization/DeserializationException.cpp 2010-03-26 19:40:54 UTC (rev 197)
+++ trunk/ktutorial/ktutorial-editor/src/serialization/DeserializationException.cpp 2010-03-26 21:03:26 UTC (rev 198)
@@ -21,17 +21,8 @@
//public:
DeserializationException::DeserializationException(const QString& message):
- std::exception(),
- mMessage(message) {
+ Exception(message) {
}
DeserializationException::~DeserializationException() throw() {
}
-
-const char* DeserializationException::what() const throw() {
- return mMessage.toUtf8();
-}
-
-QString DeserializationException::message() const throw() {
- return mMessage;
-}
Modified: trunk/ktutorial/ktutorial-editor/src/serialization/DeserializationException.h
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/serialization/DeserializationException.h 2010-03-26 19:40:54 UTC (rev 197)
+++ trunk/ktutorial/ktutorial-editor/src/serialization/DeserializationException.h 2010-03-26 21:03:26 UTC (rev 198)
@@ -19,37 +19,18 @@
#ifndef DESERIALIZATIONEXCEPTION_H
#define DESERIALIZATIONEXCEPTION_H
-#include <exception>
-#include <QString>
+#include "../Exception.h"
/**
* Thrown when the XML can't be deserialized (for example, when it isn't well
* formed).
*/
-class DeserializationException: public std::exception {
+class DeserializationException: public Exception {
public:
explicit DeserializationException(const QString& message = QString());
virtual ~DeserializationException() throw();
- /**
- * Returns the exception message.
- *
- * @return The exception message.
- */
- virtual const char* what() const throw();
-
- /**
- * Returns the exception message.
- *
- * @return The exception message.
- */
- QString message() const throw();
-
-private:
-
- QString mMessage;
-
};
#endif
Added: trunk/ktutorial/ktutorial-editor/src/serialization/IOException.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/serialization/IOException.cpp (rev 0)
+++ trunk/ktutorial/ktutorial-editor/src/serialization/IOException.cpp 2010-03-26 21:03:26 UTC (rev 198)
@@ -0,0 +1,27 @@
+/***************************************************************************
+ * Copyright (C) 2010 by Daniel Calviño Sánchez *
+ * dan...@gm... *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 3 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; If not, see <http://www.gnu.org/licenses/>. *
+ ***************************************************************************/
+
+#include "IOException.h"
+
+//public:
+
+IOException::IOException(const QString& message): Exception(message) {
+}
+
+IOException::~IOException() throw() {
+}
Property changes on: trunk/ktutorial/ktutorial-editor/src/serialization/IOException.cpp
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/ktutorial/ktutorial-editor/src/serialization/IOException.h
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/serialization/IOException.h (rev 0)
+++ trunk/ktutorial/ktutorial-editor/src/serialization/IOException.h 2010-03-26 21:03:26 UTC (rev 198)
@@ -0,0 +1,36 @@
+/***************************************************************************
+ * Copyright (C) 2010 by Daniel Calviño Sánchez *
+ * dan...@gm... *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 3 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; If not, see <http://www.gnu.org/licenses/>. *
+ ***************************************************************************/
+
+#ifndef IOEXCEPTION_H
+#define IOEXCEPTION_H
+
+#include "../Exception.h"
+
+/**
+ * Thrown when an input/ouput operation fails (for example, writing to an
+ * unwritable file, or reading from a file that does not exist).
+ */
+class IOException: public Exception {
+public:
+
+ explicit IOException(const QString& message = QString());
+ virtual ~IOException() throw();
+
+};
+
+#endif
Property changes on: trunk/ktutorial/ktutorial-editor/src/serialization/IOException.h
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.cpp 2010-03-26 19:40:54 UTC (rev 197)
+++ trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.cpp 2010-03-26 21:03:26 UTC (rev 198)
@@ -30,12 +30,12 @@
//public:
Tutorial* Serialization::loadTutorial(const KUrl& url)
-throw (DeserializationException) {
+throw (DeserializationException, IOException) {
Q_ASSERT(url.isValid());
QString temporaryFileName;
if (!KIO::NetAccess::download(url, temporaryFileName, 0)) {
- return 0;
+ throw IOException(KIO::NetAccess::lastErrorString());
}
QFile temporaryFile(temporaryFileName);
@@ -51,12 +51,13 @@
return TutorialReader().readTutorial(data);
}
-bool Serialization::saveTutorial(Tutorial* tutorial, const KUrl& url) {
+void Serialization::saveTutorial(Tutorial* tutorial, const KUrl& url)
+throw (IOException) {
Q_ASSERT(tutorial);
Q_ASSERT(url.isValid());
QString serializedTutorial = TutorialWriter().writeTutorial(tutorial);
- return writeFile(serializedTutorial, url);
+ writeFile(serializedTutorial, url);
}
QString Serialization::availableExporterTypes() {
@@ -71,8 +72,9 @@
return types;
}
-bool Serialization::exportTutorial(const Tutorial* tutorial,
- const QString& type, const KUrl& url) {
+void Serialization::exportTutorial(const Tutorial* tutorial,
+ const QString& type, const KUrl& url)
+throw (IOException) {
Q_ASSERT(tutorial);
Q_ASSERT(url.isValid());
@@ -83,7 +85,7 @@
Q_ASSERT(false);
}
- return writeFile(exportedCode, url);
+ writeFile(exportedCode, url);
}
//private:
@@ -95,7 +97,8 @@
return types;
}
-bool Serialization::writeFile(const QString& data, const KUrl& url) {
+void Serialization::writeFile(const QString& data, const KUrl& url)
+throw (IOException) {
KTemporaryFile temporaryFile;
temporaryFile.open();
QTextStream out(&temporaryFile);
@@ -103,5 +106,7 @@
out.flush();
temporaryFile.close();
- return KIO::NetAccess::upload(temporaryFile.fileName(), url, 0);
+ if (!KIO::NetAccess::upload(temporaryFile.fileName(), url, 0)) {
+ throw IOException(KIO::NetAccess::lastErrorString());
+ }
}
Modified: trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.h
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.h 2010-03-26 19:40:54 UTC (rev 197)
+++ trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.h 2010-03-26 21:03:26 UTC (rev 198)
@@ -22,6 +22,7 @@
#include <QStringList>
#include "DeserializationException.h"
+#include "IOException.h"
class KUrl;
class Tutorial;
@@ -38,36 +39,32 @@
/**
* Loads a tutorial from the file specified by the given url.
* The file must be a XML file that validates against the W3C Schema in
- * Tutorial.xsd.
+ * Tutorial.xsd. The url can be local or remote.
*
- * The url can be local or remote. If the file can't be saved, null is
- * returned. In that case, KIO::NetAccess can be asked for the error code
- * and string.
- *
* @param url The url to load the tutorial from.
* @return The loaded tutorial.
* @throw DeserializationException If there was a problem deserializing the
* tutorial.
+ * @throw IOException If there was a problem reading the contents from the
+ * file.
* @see TutorialReader
*/
static Tutorial* loadTutorial(const KUrl& url)
- throw (DeserializationException);
+ throw (DeserializationException, IOException);
/**
* Saves the tutorial to the file specified by the given url.
* The file will be a XML file that validates against the W3C Schema in
- * Tutorial.xsd.
+ * Tutorial.xsd. The url can be local or remote.
*
- * The url can be local or remote. If the file can't be saved, false is
- * returned. In that case, KIO::NetAccess can be asked for the error code
- * and string.
- *
* @param tutorial The tutorial to save.
* @param url The url to save the tutorial to.
- * @return True if the exported tutorial was saved, false otherwise.
+ * @throw IOException If there was a problem writing the contents to the
+ * file.
* @see TutorialWriter
*/
- static bool saveTutorial(Tutorial* tutorial, const KUrl& url);
+ static void saveTutorial(Tutorial* tutorial, const KUrl& url)
+ throw (IOException);
/**
* Returns the available exporter types.
@@ -86,18 +83,16 @@
* The type must be the extension fragment of one of the types returned by
* availableExporterTypes(). It is the value returned by KDialog
* currentFilter() method.
+ * The url can be local or remote.
*
- * The url can be local or remote. If the file can't be saved, false is
- * returned. In that case, KIO::NetAccess can be asked for the error code
- * and string.
- *
* @param tutorial The tutorial to export.
* @param type The type of the exporter to use.
* @param url The url to save the script file to.
- * @return True if the exported tutorial was saved, false otherwise.
+ * @throw IOException If there was a problem writing the contents to the
+ * file.
*/
- static bool exportTutorial(const Tutorial* tutorial, const QString& type,
- const KUrl& url);
+ static void exportTutorial(const Tutorial* tutorial, const QString& type,
+ const KUrl& url) throw (IOException);
private:
@@ -110,14 +105,15 @@
/**
* Writes the data to the file specified by the given url.
- * The url can be local or remote. If the file can't be saved, false is
- * returned. In that case, KIO::NetAccess can be asked for the error code
- * and string.
+ * The url can be local or remote.
*
* @param data The data to write to the file.
- * @return True if the data was saved, false otherwise.
+ * @param url The url of the file to save the data to.
+ * @throw IOException If there was a problem writing the contents to the
+ * file.
*/
- static bool writeFile(const QString& data, const KUrl& url);
+ static void writeFile(const QString& data, const KUrl& url)
+ throw (IOException);
};
Modified: trunk/ktutorial/ktutorial-editor/tests/unit/CMakeLists.txt
===================================================================
--- trunk/ktutorial/ktutorial-editor/tests/unit/CMakeLists.txt 2010-03-26 19:40:54 UTC (rev 197)
+++ trunk/ktutorial/ktutorial-editor/tests/unit/CMakeLists.txt 2010-03-26 21:03:26 UTC (rev 198)
@@ -16,6 +16,7 @@
ENDMACRO(UNIT_TESTS)
unit_tests(
+ Exception
Reaction
Step
Tutorial
@@ -33,6 +34,7 @@
ENDMACRO(MEM_TESTS)
mem_tests(
+ Exception
Reaction
Step
Tutorial
Added: trunk/ktutorial/ktutorial-editor/tests/unit/ExceptionTest.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/tests/unit/ExceptionTest.cpp (rev 0)
+++ trunk/ktutorial/ktutorial-editor/tests/unit/ExceptionTest.cpp 2010-03-26 21:03:26 UTC (rev 198)
@@ -0,0 +1,49 @@
+/***************************************************************************
+ * Copyright (C) 2010 by Daniel Calviño Sánchez *
+ * dan...@gm... *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 3 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; If not, see <http://www.gnu.org/licenses/>. *
+ ***************************************************************************/
+
+#include <QtTest>
+
+#include "Exception.h"
+
+class ExceptionTest: public QObject {
+Q_OBJECT
+
+private slots:
+
+ void testConstructor();
+ void testConstructorEmpty();
+
+};
+
+void ExceptionTest::testConstructor() {
+ Exception exception(QString("The message"));
+
+ QCOMPARE(exception.what(), "The message");
+ QCOMPARE(exception.message(), QString("The message"));
+}
+
+void ExceptionTest::testConstructorEmpty() {
+ Exception exception;
+
+ QCOMPARE(exception.what(), "");
+ QCOMPARE(exception.message(), QString(""));
+}
+
+QTEST_MAIN(ExceptionTest)
+
+#include "ExceptionTest.moc"
Property changes on: trunk/ktutorial/ktutorial-editor/tests/unit/ExceptionTest.cpp
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: trunk/ktutorial/ktutorial-editor/tests/unit/serialization/CMakeLists.txt
===================================================================
--- trunk/ktutorial/ktutorial-editor/tests/unit/serialization/CMakeLists.txt 2010-03-26 19:40:54 UTC (rev 197)
+++ trunk/ktutorial/ktutorial-editor/tests/unit/serialization/CMakeLists.txt 2010-03-26 21:03:26 UTC (rev 198)
@@ -13,6 +13,7 @@
unit_tests(
DeserializationException
+ IOException
JavascriptExporter
Serialization
TutorialReader
@@ -27,6 +28,7 @@
mem_tests(
DeserializationException
+ IOException
JavascriptExporter
Serialization
TutorialReader
Added: trunk/ktutorial/ktutorial-editor/tests/unit/serialization/IOExceptionTest.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/tests/unit/serialization/IOExceptionTest.cpp (rev 0)
+++ trunk/ktutorial/ktutorial-editor/tests/unit/serialization/IOExceptionTest.cpp 2010-03-26 21:03:26 UTC (rev 198)
@@ -0,0 +1,49 @@
+/***************************************************************************
+ * Copyright (C) 2010 by Daniel Calviño Sánchez *
+ * dan...@gm... *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 3 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; If not, see <http://www.gnu.org/licenses/>. *
+ ***************************************************************************/
+
+#include <QtTest>
+
+#include "IOException.h"
+
+class IOExceptionTest: public QObject {
+Q_OBJECT
+
+private slots:
+
+ void testConstructor();
+ void testConstructorEmpty();
+
+};
+
+void IOExceptionTest::testConstructor() {
+ IOException exception(QString("The message"));
+
+ QCOMPARE(exception.what(), "The message");
+ QCOMPARE(exception.message(), QString("The message"));
+}
+
+void IOExceptionTest::testConstructorEmpty() {
+ IOException exception;
+
+ QCOMPARE(exception.what(), "");
+ QCOMPARE(exception.message(), QString(""));
+}
+
+QTEST_MAIN(IOExceptionTest)
+
+#include "IOExceptionTest.moc"
Property changes on: trunk/ktutorial/ktutorial-editor/tests/unit/serialization/IOExceptionTest.cpp
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: trunk/ktutorial/ktutorial-editor/tests/unit/serialization/SerializationTest.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/tests/unit/serialization/SerializationTest.cpp 2010-03-26 19:40:54 UTC (rev 197)
+++ trunk/ktutorial/ktutorial-editor/tests/unit/serialization/SerializationTest.cpp 2010-03-26 21:03:26 UTC (rev 198)
@@ -28,6 +28,17 @@
#include "../Tutorial.h"
+#define EXPECT_EXCEPTION(statement, exception) \
+do {\
+ try {\
+ statement;\
+ QFAIL("Expected " #exception " not thrown");\
+ } catch (exception e) {\
+ } catch (Exception e) {\
+ QFAIL("Expected " #exception " not thrown");\
+ }\
+} while (0)
+
class SerializationTest: public QObject {
Q_OBJECT
@@ -80,8 +91,7 @@
KUrl url = KGlobal::dirs()->saveLocation("tmp") + "/SerializationTest.xml";
- QVERIFY(Serialization::saveTutorial(&tutorial, url));
-
+ Serialization::saveTutorial(&tutorial, url);
QScopedPointer<Tutorial> loadedTutorial(Serialization::loadTutorial(url));
QVERIFY(loadedTutorial);
@@ -96,7 +106,7 @@
writeFile(mFile, "<tutorial name=\"The name\"></tutorial>");
QVERIFY(QFile::setPermissions(mFile->fileName(), 0));
- QVERIFY(!Serialization::loadTutorial(url));
+ EXPECT_EXCEPTION(Serialization::loadTutorial(url), IOException);
}
void SerializationTest::testLoadNotAnXmlFile() {
@@ -105,11 +115,8 @@
mFile->open(QIODevice::WriteOnly | QIODevice::Text);
writeFile(mFile, "Not an XML file");
- try {
- QScopedPointer<Tutorial> tutorial(Serialization::loadTutorial(url));
- QFAIL("Expected DeserializationException not thrown");
- } catch (DeserializationException e) {
- }
+ EXPECT_EXCEPTION(Serialization::loadTutorial(url),
+ DeserializationException);
}
void SerializationTest::testLoadXmlNotWellFormed() {
@@ -118,11 +125,8 @@
mFile->open(QIODevice::WriteOnly | QIODevice::Text);
writeFile(mFile, "<tutorial><step></invalidEndElement></tutorial>");
- try {
- QScopedPointer<Tutorial> tutorial(Serialization::loadTutorial(url));
- QFAIL("Expected DeserializationException not thrown");
- } catch (DeserializationException e) {
- }
+ EXPECT_EXCEPTION(Serialization::loadTutorial(url),
+ DeserializationException);
}
void SerializationTest::testLoadXmlWithoutRootTutorialElement() {
@@ -131,11 +135,8 @@
mFile->open(QIODevice::WriteOnly | QIODevice::Text);
writeFile(mFile, "<unknownRootElement></unknownRootElement>");
- try {
- QScopedPointer<Tutorial> tutorial(Serialization::loadTutorial(url));
- QFAIL("Expected DeserializationException not thrown");
- } catch (DeserializationException e) {
- }
+ EXPECT_EXCEPTION(Serialization::loadTutorial(url),
+ DeserializationException);
}
void SerializationTest::testSaveToExistingUrl() {
@@ -148,7 +149,7 @@
mFile->open(QIODevice::WriteOnly | QIODevice::Text);
writeFile(mFile, "Hello world!");
- QVERIFY(Serialization::saveTutorial(&tutorial, url));
+ Serialization::saveTutorial(&tutorial, url);
QVERIFY(mFile->exists());
QVERIFY(mFile->open(QIODevice::ReadOnly | QIODevice::Text));
@@ -174,7 +175,7 @@
writeFile(mFile, "Hello world!");
QVERIFY(QFile::setPermissions(mFile->fileName(), 0));
- QVERIFY(!Serialization::saveTutorial(&tutorial, url));
+ EXPECT_EXCEPTION(Serialization::saveTutorial(&tutorial, url), IOException);
}
void SerializationTest::testAvailableExporterTypes() {
@@ -191,7 +192,7 @@
KUrl url = KGlobal::dirs()->saveLocation("tmp") + "/SerializationTest.js";
- QVERIFY(Serialization::exportTutorial(&tutorial, "*.js", url));
+ Serialization::exportTutorial(&tutorial, "*.js", url);
mFile = new QFile(url.toLocalFile());
QVERIFY(mFile->exists());
@@ -219,7 +220,7 @@
mFile->open(QIODevice::WriteOnly | QIODevice::Text);
writeFile(mFile, "Hello world!");
- QVERIFY(Serialization::exportTutorial(&tutorial, "*.js", url));
+ Serialization::exportTutorial(&tutorial, "*.js", url);
QVERIFY(mFile->exists());
QVERIFY(mFile->open(QIODevice::ReadOnly | QIODevice::Text));
@@ -247,7 +248,8 @@
writeFile(mFile, "Hello world!");
QVERIFY(QFile::setPermissions(mFile->fileName(), 0));
- QVERIFY(!Serialization::exportTutorial(&tutorial, "*.js", url));
+ EXPECT_EXCEPTION(Serialization::exportTutorial(&tutorial, "*.js", url),
+ IOException);
}
/////////////////////////////////// Helpers ////////////////////////////////////
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|