File indexing completed on 2022-10-04 14:00:25

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2009 Andrew Manson <g.real.ate@gmail.com>
0004 //
0005 
0006 #include <QObject>
0007 
0008 #include "GeoDataParser.h"
0009 #include "GeoDataDocument.h"
0010 #include "GeoDataColorStyle.h"
0011 #include "GeoWriter.h"
0012 #include <geodata/handlers/kml/KmlElementDictionary.h>
0013 
0014 #include <QDir>
0015 #include <QFile>
0016 #include <QTest>
0017 #include <QTextStream>
0018 #include <QBuffer>
0019 
0020 using namespace Marble;
0021 
0022 class TestGeoDataWriter : public QObject
0023 {
0024     Q_OBJECT
0025 private Q_SLOTS:
0026     void initTestCase();
0027     void countFeatures_data();
0028     void saveFile_data();
0029     void saveFile();
0030     void saveAndLoad_data();
0031     void saveAndLoad();
0032     void saveAndCompare_data();
0033     void saveAndCompare();
0034     void saveAndCompareEquality_data();
0035     void saveAndCompareEquality();
0036     void cleanupTestCase();
0037 private:
0038     QDir dataDir;
0039     QMap<QString, QSharedPointer<GeoDataParser> > parsers;
0040     QStringList m_testFiles;
0041 };
0042 
0043 Q_DECLARE_METATYPE( QSharedPointer<GeoDataParser> )
0044 
0045 void TestGeoDataWriter::initTestCase()
0046 {
0047     dataDir = QDir( TESTSRCDIR );
0048     dataDir.cd( "data" );
0049     //check there are files in the data dir
0050     QVERIFY( dataDir.count() > 0 );
0051 
0052     //test the loading of each file in the data dir
0053     foreach( const QString &filename, dataDir.entryList(QStringList() << "*.kml", QDir::Files) ){
0054         QFile file( dataDir.filePath(filename));
0055         QVERIFY( file.exists() );
0056 
0057         // Create parsers
0058         GeoDataParser* parser = new GeoDataParser( GeoData_KML );
0059         QSharedPointer<GeoDataParser>parserPointer ( parser );
0060 
0061         // Open the files and verify
0062         QVERIFY( file.open( QIODevice::ReadOnly ) );
0063         QVERIFY2(parser->read(&file), filename.toLatin1().constData());
0064 
0065         parsers.insert( filename, parserPointer );
0066         m_testFiles << filename;
0067         file.close();
0068     }
0069 }
0070 
0071 void TestGeoDataWriter::countFeatures_data()
0072 {
0073     QTest::addColumn<QSharedPointer<GeoDataParser> >("parser");
0074     foreach( const QString &file, m_testFiles ) {
0075         QTest::newRow(file.toStdString().c_str()) << parsers.value(file);
0076     }
0077 }
0078 
0079 void TestGeoDataWriter::saveFile_data()
0080 {
0081     QTest::addColumn<QSharedPointer<GeoDataParser> >( "parser" );
0082     foreach( const QString &file, m_testFiles ) {
0083         QTest::newRow(file.toStdString().c_str()) << parsers.value(file);
0084     }
0085 }
0086 
0087 void TestGeoDataWriter::saveFile()
0088 {
0089     QFETCH( QSharedPointer<GeoDataParser>, parser );
0090 
0091     //attempt to save a file using the GeoWriter
0092     QByteArray data;
0093     QBuffer buffer( &data );
0094 
0095     GeoWriter writer;
0096     //FIXME: a better way to do this?
0097     writer.setDocumentType( kml::kmlTag_nameSpaceOgc22 );
0098 
0099     // Open file in right mode
0100     QVERIFY( buffer.open( QIODevice::WriteOnly ) );
0101 
0102     QVERIFY( writer.write( &buffer, &(*dynamic_cast<GeoDataFeature*>(parser->activeDocument() ) ) ) );
0103 
0104 }
0105 
0106 void TestGeoDataWriter::saveAndLoad_data()
0107 {
0108     QTest::addColumn<QSharedPointer<GeoDataParser> >("parser");
0109     foreach( const QString &file, m_testFiles ) {
0110         QTest::newRow(file.toStdString().c_str()) << parsers.value(file);
0111     }
0112 }
0113 
0114 void TestGeoDataWriter::saveAndLoad()
0115 {
0116     //Save the file and then verify loading it again
0117     QFETCH( QSharedPointer<GeoDataParser>, parser );
0118 
0119     QByteArray data;
0120     QBuffer buffer( &data );
0121 
0122     GeoWriter writer;
0123     //FIXME: a better way to do this?
0124     writer.setDocumentType( kml::kmlTag_nameSpaceOgc22 );
0125 
0126     // Open file in right mode
0127     QVERIFY( buffer.open( QIODevice::ReadWrite ) );
0128 
0129     QVERIFY( writer.write( &buffer, &( *dynamic_cast<GeoDataFeature*>(parser->activeDocument() ) ) ) );
0130 
0131     GeoDataParser resultParser( GeoData_KML );
0132 
0133     buffer.reset();
0134     QVERIFY( resultParser.read( &buffer ) );
0135 }
0136 
0137 void TestGeoDataWriter::saveAndCompare_data()
0138 {
0139     QTest::addColumn<QSharedPointer<GeoDataParser> >("parser");
0140     QTest::addColumn<QString>("original");
0141 
0142     foreach( const QString &file, m_testFiles ) {
0143         QTest::newRow(file.toStdString().c_str()) << parsers.value(file) << file;
0144     }
0145 }
0146 
0147 void TestGeoDataWriter::saveAndCompare()
0148 {
0149     //save the file and compare it to the original
0150     QFETCH( QSharedPointer<GeoDataParser>, parser );
0151     QFETCH( QString, original );
0152 
0153     //attempt to save a file using the GeoWriter
0154     QByteArray data;
0155     QBuffer buffer( &data );
0156     buffer.open( QIODevice::ReadWrite );
0157 
0158     GeoWriter writer;
0159     //FIXME: a better way to do this?
0160     writer.setDocumentType( kml::kmlTag_nameSpaceOgc22 );
0161 
0162     QVERIFY( writer.write( &buffer, &( *dynamic_cast<GeoDataFeature*>(parser->activeDocument() ) ) ) );
0163 
0164     QFile file( dataDir.filePath( original ) );
0165     QVERIFY( file.open( QIODevice::ReadOnly ) );
0166     QVERIFY( buffer.reset() );
0167     QTextStream oldFile( &file );
0168     QTextStream newFile( &buffer );
0169 
0170     QCOMPARE( newFile.readAll().simplified(), oldFile.readAll().simplified() );
0171 }
0172 
0173 void TestGeoDataWriter::saveAndCompareEquality_data()
0174 {
0175     QTest::addColumn<QSharedPointer<GeoDataParser> >("parser");
0176     QTest::addColumn<QString>("original");
0177 
0178     foreach( const QString &file, m_testFiles ) {
0179         QTest::newRow(file.toStdString().c_str()) << parsers.value(file) << file;
0180     }
0181 }
0182 
0183 void TestGeoDataWriter::saveAndCompareEquality()
0184 {
0185     QFETCH( QSharedPointer<GeoDataParser>, parser );
0186     QFETCH( QString, original );
0187 
0188     QByteArray data;
0189     QBuffer buffer( &data );
0190     buffer.open( QIODevice::ReadWrite );
0191 
0192     GeoWriter writer;
0193     //FIXME: a better way to do this?
0194     writer.setDocumentType( kml::kmlTag_nameSpaceOgc22 );
0195 
0196     GeoDataDocument *initialDoc = dynamic_cast<GeoDataDocument*>( parser->activeDocument() );
0197     QVERIFY( writer.write( &buffer, initialDoc) );
0198 
0199     buffer.reset();
0200     GeoDataParser otherParser( GeoData_KML);
0201     QVERIFY( otherParser.read( &buffer ) );
0202 
0203     GeoDataDocument *otherDoc = dynamic_cast<GeoDataDocument*>( otherParser.activeDocument() );
0204     QVERIFY( *initialDoc == *otherDoc );
0205 }
0206 
0207 void TestGeoDataWriter::cleanupTestCase()
0208 {
0209     QMap<QString, QSharedPointer<GeoDataParser> >::iterator itpoint = parsers.begin();
0210     QMap<QString, QSharedPointer<GeoDataParser> >::iterator const endpoint = parsers.end();
0211     for (; itpoint != endpoint; ++itpoint ) {
0212         delete itpoint.value()->releaseDocument();
0213     }
0214 }
0215 
0216 QTEST_MAIN( TestGeoDataWriter )
0217 #include "TestGeoDataWriter.moc"
0218