1
+ /* **************************************************************************
2
+ testqgsvectorfilewriter.cpp
3
+ --------------------------------------
4
+ Date : Sun Sep 16 12:22:54 AKDT 2007
5
+ Copyright : (C) 2007 by Tim Sutton
6
+ Email : tim @ linfiniti.com
7
+ ***************************************************************************
8
+ * *
9
+ * This program is free software; you can redistribute it and/or modify *
10
+ * it under the terms of the GNU General Public License as published by *
11
+ * the Free Software Foundation; either version 2 of the License, or *
12
+ * (at your option) any later version. *
13
+ * *
14
+ ***************************************************************************/
15
+ #include < QtTest>
16
+ #include < QObject>
17
+ #include < QString>
18
+ #include < QStringList>
19
+ #include < QObject>
20
+ #include < QPainter>
21
+ #include < QSettings>
22
+ #include < QTime>
23
+ #include < iostream>
24
+
25
+ #include < QApplication>
26
+ #include < QDesktopServices>
27
+
28
+ // qgis includes...
29
+ #include < qgsvectorlayer.h> // defines QgsFieldMap
30
+ #include < qgsvectorfilewriter.h> // logic for writing shpfiles
31
+ #include < qgsfeature.h> // we will need to pass a bunch of these for each rec
32
+ #include < qgsgeometry.h> // each feature needs a geometry
33
+ #include < qgspoint.h> // we will use point geometry
34
+ #include < qgsspatialrefsys.h> // needed for creating a srs
35
+ #include < qgsapplication.h> // search path for srs.db
36
+ #include < qgsfield.h>
37
+ #include < qgis.h> // defines GEOWKT
38
+ #include < qgsproviderregistry.h>
39
+
40
+
41
+ /* * \ingroup UnitTests
42
+ * This is a unit test for the QgsMapRender class.
43
+ * It will do some performance testing too
44
+ *
45
+ */
46
+ class Regression1141 : public QObject
47
+ {
48
+ Q_OBJECT;
49
+ private slots:
50
+ void initTestCase ();// will be called before the first testfunction is executed.
51
+ void cleanupTestCase ();// will be called after the last testfunction was executed.
52
+ void init (){};// will be called before each testfunction is executed.
53
+ void cleanup (){};// will be called after every testfunction.
54
+
55
+ /* * This method tests that we can create a shpfile with diacriticals in its name
56
+ * and with fields that have diacriticals in their names*/
57
+ void diacriticalTest ();
58
+
59
+ private:
60
+ QString mEncoding ;
61
+ QgsVectorFileWriter::WriterError mError ;
62
+ QgsSpatialRefSys mSRS ;
63
+ QgsFieldMap mFields ;
64
+ QString mFileName ;
65
+ };
66
+
67
+ void Regression1141::initTestCase ()
68
+ {
69
+ //
70
+ // Runs once before any tests are run
71
+ //
72
+ // init QGIS's paths - true means that all path will be inited from prefix
73
+ QString qgisPath = QCoreApplication::applicationDirPath ();
74
+ QgsApplication::setPrefixPath (INSTALL_PREFIX, true );
75
+ QgsApplication::showSettings ();
76
+ // Instantiate the plugin directory so that providers are loaded
77
+ QgsProviderRegistry::instance (QgsApplication::pluginPath ());
78
+ // compute our test file name:
79
+ QString myTmpDir = QDir::tempPath () + QDir::separator () ;
80
+ mFileName = myTmpDir + " ąęćń.shp" ;
81
+ }
82
+
83
+
84
+ void Regression1141::cleanupTestCase ()
85
+ {
86
+ //
87
+ // Runs after all tests are done
88
+ //
89
+ }
90
+
91
+
92
+
93
+ void Regression1141::diacriticalTest ()
94
+ {
95
+ // create some objects that will be used in all tests...
96
+ mEncoding = " UTF-8" ;
97
+ QgsField myField ( " ąęćń" , QVariant::Int, " int" , 10 , 0 , " Value on lon" );
98
+ mFields .insert ( 0 , myField );
99
+ mSRS = QgsSpatialRefSys ( GEOWKT );
100
+
101
+ qDebug ( " Checking test dataset exists..." );
102
+ qDebug ( mFileName .toLocal8Bit () );
103
+
104
+ if ( !QFile::exists ( mFileName ) )
105
+ {
106
+ qDebug ( " Creating test dataset: " );
107
+
108
+ QgsVectorFileWriter myWriter ( mFileName ,
109
+ mEncoding ,
110
+ mFields ,
111
+ QGis::WKBPolygon,
112
+ &mSRS );
113
+
114
+ QgsPoint myPoint = QgsPoint ( 10.0 , 10.0 );
115
+ // NOTE: dont delete this pointer again -
116
+ // ownership is passed to the feature which will
117
+ // delete it in its dtor!
118
+ QgsGeometry * mypPointGeometry = QgsGeometry::fromPoint ( myPoint );
119
+ QgsFeature myFeature;
120
+ myFeature.setTypeName ( " WKBPoint" );
121
+ myFeature.setGeometry ( mypPointGeometry );
122
+ myFeature.addAttribute ( 0 , 10 );
123
+ //
124
+ // Write the feature to the filewriter
125
+ // and check for errors
126
+ //
127
+ QVERIFY ( myWriter.addFeature ( myFeature ) );
128
+ mError = myWriter.hasError ();
129
+
130
+ if ( mError == QgsVectorFileWriter::ErrDriverNotFound )
131
+ {
132
+ std::cout << " Driver not found error" << std::endl;
133
+ }
134
+ else if ( mError == QgsVectorFileWriter::ErrCreateDataSource )
135
+ {
136
+ std::cout << " Create data source error" << std::endl;
137
+ }
138
+ else if ( mError == QgsVectorFileWriter::ErrCreateLayer )
139
+ {
140
+ std::cout << " Create layer error" << std::endl;
141
+ }
142
+
143
+ QVERIFY ( mError == QgsVectorFileWriter::NoError );
144
+ // Now check we can delete it again ok
145
+ QVERIFY (QgsVectorFileWriter::deleteShapeFile (mFileName ));
146
+
147
+ } // file exists
148
+ }
149
+
150
+
151
+ QTEST_MAIN (Regression1141)
152
+
153
+ #include " moc_regression1141.cxx"
154
+
0 commit comments