[Ktutorial-commits] SF.net SVN: ktutorial:[197] trunk/ktutorial/ktutorial-editor
Status: Alpha
Brought to you by:
danxuliu
|
From: <dan...@us...> - 2010-03-26 19:41:02
|
Revision: 197
http://ktutorial.svn.sourceforge.net/ktutorial/?rev=197&view=rev
Author: danxuliu
Date: 2010-03-26 19:40:54 +0000 (Fri, 26 Mar 2010)
Log Message:
-----------
Add loadTutorial and saveTutorial methods to Serialization facade.
Modified Paths:
--------------
trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.cpp
trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.h
trunk/ktutorial/ktutorial-editor/tests/unit/serialization/SerializationTest.cpp
Modified: trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.cpp 2010-03-26 19:34:21 UTC (rev 196)
+++ trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.cpp 2010-03-26 19:40:54 UTC (rev 197)
@@ -24,9 +24,41 @@
#include <KIO/NetAccess>
#include "JavascriptExporter.h"
+#include "TutorialReader.h"
+#include "TutorialWriter.h"
//public:
+Tutorial* Serialization::loadTutorial(const KUrl& url)
+throw (DeserializationException) {
+ Q_ASSERT(url.isValid());
+
+ QString temporaryFileName;
+ if (!KIO::NetAccess::download(url, temporaryFileName, 0)) {
+ return 0;
+ }
+
+ QFile temporaryFile(temporaryFileName);
+ temporaryFile.open(QIODevice::ReadOnly | QIODevice::Text);
+
+ QString data;
+ QTextStream in(&temporaryFile);
+ while (!in.atEnd()) {
+ data += in.readAll();
+ }
+ temporaryFile.close();
+
+ return TutorialReader().readTutorial(data);
+}
+
+bool Serialization::saveTutorial(Tutorial* tutorial, const KUrl& url) {
+ Q_ASSERT(tutorial);
+ Q_ASSERT(url.isValid());
+
+ QString serializedTutorial = TutorialWriter().writeTutorial(tutorial);
+ return writeFile(serializedTutorial, url);
+}
+
QString Serialization::availableExporterTypes() {
QStringList typeList = availableExporterTypeList();
@@ -51,14 +83,7 @@
Q_ASSERT(false);
}
- KTemporaryFile temporaryFile;
- temporaryFile.open();
- QTextStream out(&temporaryFile);
- out << exportedCode;
- out.flush();
- temporaryFile.close();
-
- return KIO::NetAccess::upload(temporaryFile.fileName(), url, 0);
+ return writeFile(exportedCode, url);
}
//private:
@@ -69,3 +94,14 @@
"*.js|Javascript file");
return types;
}
+
+bool Serialization::writeFile(const QString& data, const KUrl& url) {
+ KTemporaryFile temporaryFile;
+ temporaryFile.open();
+ QTextStream out(&temporaryFile);
+ out << data;
+ out.flush();
+ temporaryFile.close();
+
+ return KIO::NetAccess::upload(temporaryFile.fileName(), url, 0);
+}
Modified: trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.h
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.h 2010-03-26 19:34:21 UTC (rev 196)
+++ trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.h 2010-03-26 19:40:54 UTC (rev 197)
@@ -21,18 +21,55 @@
#include <QStringList>
+#include "DeserializationException.h"
+
class KUrl;
class Tutorial;
/**
* Facade for serialization system.
- * It provides the methods needed to know the available exporters and export a
- * tutorial using one of them.
+ * It provides the methods needed to save and load a tutorial to a XML file, and
+ * to know the available exporters and export a tutorial to a script file using
+ * one of them.
*/
class Serialization {
public:
/**
+ * 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.
+ *
+ * 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.
+ * @see TutorialReader
+ */
+ static Tutorial* loadTutorial(const KUrl& url)
+ throw (DeserializationException);
+
+ /**
+ * 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.
+ *
+ * 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.
+ * @see TutorialWriter
+ */
+ static bool saveTutorial(Tutorial* tutorial, const KUrl& url);
+
+ /**
* Returns the available exporter types.
* The string has the format expected by KFileDialog setFilter method, that
* is, "*.fileExtension1|Human readable name1\n*.fileExtension2|Human
@@ -71,6 +108,17 @@
*/
static QStringList availableExporterTypeList();
+ /**
+ * 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.
+ *
+ * @param data The data to write to the file.
+ * @return True if the data was saved, false otherwise.
+ */
+ static bool writeFile(const QString& data, const KUrl& url);
+
};
#endif
Modified: trunk/ktutorial/ktutorial-editor/tests/unit/serialization/SerializationTest.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/tests/unit/serialization/SerializationTest.cpp 2010-03-26 19:34:21 UTC (rev 196)
+++ trunk/ktutorial/ktutorial-editor/tests/unit/serialization/SerializationTest.cpp 2010-03-26 19:40:54 UTC (rev 197)
@@ -36,6 +36,16 @@
void init();
void cleanup();
+ void testSaveAndLoad();
+
+ void testLoadFromUnreadableUrl();
+ void testLoadNotAnXmlFile();
+ void testLoadXmlNotWellFormed();
+ void testLoadXmlWithoutRootTutorialElement();
+
+ void testSaveToExistingUrl();
+ void testSaveToUnwritableUrl();
+
void testAvailableExporterTypes();
void testExportJavascript();
@@ -45,7 +55,7 @@
private:
- QFile* mExportedFile;
+ QFile* mFile;
QString readFile(QFile* file);
void writeFile(QFile* file, const QString& contents);
@@ -53,16 +63,120 @@
};
void SerializationTest::init() {
- mExportedFile = 0;
+ mFile = 0;
}
void SerializationTest::cleanup() {
- if (mExportedFile) {
- mExportedFile->remove();
+ if (mFile) {
+ mFile->remove();
}
- delete mExportedFile;
+ delete mFile;
}
+void SerializationTest::testSaveAndLoad() {
+ Tutorial tutorial;
+ tutorial.setName("The name");
+ tutorial.setDescription("The description");
+
+ KUrl url = KGlobal::dirs()->saveLocation("tmp") + "/SerializationTest.xml";
+
+ QVERIFY(Serialization::saveTutorial(&tutorial, url));
+
+ QScopedPointer<Tutorial> loadedTutorial(Serialization::loadTutorial(url));
+
+ QVERIFY(loadedTutorial);
+ QCOMPARE(loadedTutorial->name(), tutorial.name());
+ QCOMPARE(loadedTutorial->description(), tutorial.description());
+}
+
+void SerializationTest::testLoadFromUnreadableUrl() {
+ KUrl url = KGlobal::dirs()->saveLocation("tmp") + "/SerializationTest.xml";
+ mFile = new QFile(url.toLocalFile());
+ mFile->open(QIODevice::WriteOnly | QIODevice::Text);
+ writeFile(mFile, "<tutorial name=\"The name\"></tutorial>");
+ QVERIFY(QFile::setPermissions(mFile->fileName(), 0));
+
+ QVERIFY(!Serialization::loadTutorial(url));
+}
+
+void SerializationTest::testLoadNotAnXmlFile() {
+ KUrl url = KGlobal::dirs()->saveLocation("tmp") + "/SerializationTest.txt";
+ mFile = new QFile(url.toLocalFile());
+ 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) {
+ }
+}
+
+void SerializationTest::testLoadXmlNotWellFormed() {
+ KUrl url = KGlobal::dirs()->saveLocation("tmp") + "/SerializationTest.xml";
+ mFile = new QFile(url.toLocalFile());
+ 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) {
+ }
+}
+
+void SerializationTest::testLoadXmlWithoutRootTutorialElement() {
+ KUrl url = KGlobal::dirs()->saveLocation("tmp") + "/SerializationTest.xml";
+ mFile = new QFile(url.toLocalFile());
+ 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) {
+ }
+}
+
+void SerializationTest::testSaveToExistingUrl() {
+ Tutorial tutorial;
+ tutorial.setName("The name");
+ tutorial.setDescription("The description");
+
+ KUrl url = KGlobal::dirs()->saveLocation("tmp") + "/SerializationTest.xml";
+ mFile = new QFile(url.toLocalFile());
+ mFile->open(QIODevice::WriteOnly | QIODevice::Text);
+ writeFile(mFile, "Hello world!");
+
+ QVERIFY(Serialization::saveTutorial(&tutorial, url));
+
+ QVERIFY(mFile->exists());
+ QVERIFY(mFile->open(QIODevice::ReadOnly | QIODevice::Text));
+
+ QString actualContents = readFile(mFile);
+ QString expectedContents =
+"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
+"<tutorial name=\"The name\">\n"
+" <description>The description</description>\n"
+"</tutorial>\n";
+
+ QCOMPARE(actualContents, expectedContents);
+}
+
+void SerializationTest::testSaveToUnwritableUrl() {
+ Tutorial tutorial;
+ tutorial.setName("The name");
+ tutorial.setDescription("The description");
+
+ KUrl url = KGlobal::dirs()->saveLocation("tmp") + "/SerializationTest.xml";
+ mFile = new QFile(url.toLocalFile());
+ mFile->open(QIODevice::WriteOnly | QIODevice::Text);
+ writeFile(mFile, "Hello world!");
+ QVERIFY(QFile::setPermissions(mFile->fileName(), 0));
+
+ QVERIFY(!Serialization::saveTutorial(&tutorial, url));
+}
+
void SerializationTest::testAvailableExporterTypes() {
QString types = Serialization::availableExporterTypes();
@@ -79,11 +193,11 @@
QVERIFY(Serialization::exportTutorial(&tutorial, "*.js", url));
- mExportedFile = new QFile(url.toLocalFile());
- QVERIFY(mExportedFile->exists());
- QVERIFY(mExportedFile->open(QIODevice::ReadOnly | QIODevice::Text));
+ mFile = new QFile(url.toLocalFile());
+ QVERIFY(mFile->exists());
+ QVERIFY(mFile->open(QIODevice::ReadOnly | QIODevice::Text));
- QString actualContents = readFile(mExportedFile);
+ QString actualContents = readFile(mFile);
QString expectedContents =
"t = Kross.module(\"kdetranslation\");\n"
"\n"
@@ -101,16 +215,16 @@
tutorial.setDescription("The description");
KUrl url = KGlobal::dirs()->saveLocation("tmp") + "/SerializationTest.js";
- mExportedFile = new QFile(url.toLocalFile());
- mExportedFile->open(QIODevice::WriteOnly | QIODevice::Text);
- writeFile(mExportedFile, "Hello world!");
+ mFile = new QFile(url.toLocalFile());
+ mFile->open(QIODevice::WriteOnly | QIODevice::Text);
+ writeFile(mFile, "Hello world!");
QVERIFY(Serialization::exportTutorial(&tutorial, "*.js", url));
- QVERIFY(mExportedFile->exists());
- QVERIFY(mExportedFile->open(QIODevice::ReadOnly | QIODevice::Text));
+ QVERIFY(mFile->exists());
+ QVERIFY(mFile->open(QIODevice::ReadOnly | QIODevice::Text));
- QString actualContents = readFile(mExportedFile);
+ QString actualContents = readFile(mFile);
QString expectedContents =
"t = Kross.module(\"kdetranslation\");\n"
"\n"
@@ -128,10 +242,10 @@
tutorial.setDescription("The description");
KUrl url = KGlobal::dirs()->saveLocation("tmp") + "/SerializationTest.js";
- mExportedFile = new QFile(url.toLocalFile());
- mExportedFile->open(QIODevice::WriteOnly | QIODevice::Text);
- writeFile(mExportedFile, "Hello world!");
- QVERIFY(QFile::setPermissions(mExportedFile->fileName(), 0));
+ mFile = new QFile(url.toLocalFile());
+ mFile->open(QIODevice::WriteOnly | QIODevice::Text);
+ writeFile(mFile, "Hello world!");
+ QVERIFY(QFile::setPermissions(mFile->fileName(), 0));
QVERIFY(!Serialization::exportTutorial(&tutorial, "*.js", url));
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|