Statistics
| Revision:

root / trunk / libraries / libGPE / src-test / org / gvsig / gpe / writers / GPEWriterBaseTest.java @ 11265

History | View | Annotate | Download (7.52 KB)

1
package org.gvsig.gpe.writers;
2

    
3
import java.io.File;
4
import java.util.ArrayList;
5

    
6
import junit.framework.TestCase;
7

    
8
import org.gvsig.gpe.GPEContentHandler;
9
import org.gvsig.gpe.GPEContentHandlerTest;
10
import org.gvsig.gpe.GPEDefaults;
11
import org.gvsig.gpe.GPEErrorHandler;
12
import org.gvsig.gpe.GPEErrorHandlerTest;
13
import org.gvsig.gpe.GPEParser;
14
import org.gvsig.gpe.GPERegister;
15
import org.gvsig.gpe.containers.Layer;
16

    
17
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
18
 *
19
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
20
 *
21
 * This program is free software; you can redistribute it and/or
22
 * modify it under the terms of the GNU General Public License
23
 * as published by the Free Software Foundation; either version 2
24
 * of the License, or (at your option) any later version.
25
 *
26
 * This program is distributed in the hope that it will be useful,
27
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29
 * GNU General Public License for more details.
30
 *
31
 * You should have received a copy of the GNU General Public License
32
 * along with this program; if not, write to the Free Software
33
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
34
 *
35
 * For more information, contact:
36
 *
37
 *  Generalitat Valenciana
38
 *   Conselleria d'Infraestructures i Transport
39
 *   Av. Blasco Ib??ez, 50
40
 *   46010 VALENCIA
41
 *   SPAIN
42
 *
43
 *      +34 963862235
44
 *   gvsig@gva.es
45
 *      www.gvsig.gva.es
46
 *
47
 *    or
48
 *
49
 *   IVER T.I. S.A
50
 *   Salamanca 50
51
 *   46005 Valencia
52
 *   Spain
53
 *
54
 *   +34 963163400
55
 *   dac@iver.es
56
 */
57
/* CVS MESSAGES:
58
 *
59
 * $Id: GPEWriterBaseTest.java 11265 2007-04-19 11:56:03Z csanchez $
60
 * $Log$
61
 * Revision 1.5  2007-04-19 11:50:20  csanchez
62
 * Actualizacion protoripo libGPE
63
 *
64
 * Revision 1.4  2007/04/19 07:23:20  jorpiell
65
 * Add the add methods to teh contenhandler and change the register mode
66
 *
67
 * Revision 1.3  2007/04/17 06:27:20  jorpiell
68
 * Changed the default filename
69
 *
70
 * Revision 1.2  2007/04/14 16:06:35  jorpiell
71
 * Add the container classes
72
 *
73
 * Revision 1.1  2007/04/13 07:17:54  jorpiell
74
 * Add the writting tests for the simple geometries
75
 *
76
 *
77
 */
78
/**
79
 * This class must be implementend by all the classes that
80
 * implements a GPE writer Parser. It creates a writer, write some
81
 * features and then uses a reader to compare the writting 
82
 * process
83
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
84
 */
85
public abstract class GPEWriterBaseTest extends TestCase{
86
        private String filename = "testdata/FILETEMP";
87
        private GPEWriterHandler writerHandler = null;
88
        private GPEContentHandler contenHandler = null;
89
        private GPEErrorHandler errorHandler = null;
90
        private GPEParser parser = null;
91
        
92
        /**
93
         * Register the driver and gets the handler
94
         * @throws Exception 
95
         */
96
        public void setUp() throws Exception{
97
                GPEDefaults.setProperty(GPEDefaults.DEFAULT_FILE_NAME, filename);
98
                //Register the parser
99
                GPERegister.addGpeParser(getGPEParserName(), 
100
                                getGPEParserDescription(), 
101
                                getGPEParserClass());
102
                //Register the writer
103
                GPERegister.addGpeWriterHandler(getGPEWriterHandlerName(),
104
                                getGPEWriterHandlerDescription(),
105
                                getGPEWriterHandlerClass());
106
        }
107
        
108
        /**
109
         * Delete the file
110
         */
111
        public void tearDown(){
112
                //fileTemp.delete();
113
        }
114
        
115
        /**
116
         * This test writes some objects into the file and then
117
         * try to read the created file. It compare that the written
118
         * objects are the same that the read them
119
         * @throws Exception 
120
         */
121
        public void testWriter() throws Exception{
122
                writeObjects();
123
                File outputFile = writerHandler.getOutputFile();
124
                //The file must exist
125
                parser = GPERegister.createParser(outputFile);
126
                parser.parse(getContenHandler(),getErrorHandler() ,outputFile);
127
                readObjects();
128
        }
129
        
130
        /**
131
         * This method write somo objects into the writer handler
132
         */
133
        public abstract void writeObjects();
134
        
135
        /**
136
         * It read the objects and make all the comparations
137
         *
138
         */
139
        public abstract void readObjects();
140
                        
141
        /**
142
         * Each test must to return its parser name
143
         * to register it before to start the parsing
144
         * process
145
         */
146
        public String getGPEWriterHandlerName(){
147
                return "FORMAT VERSION";
148
        }
149
        
150
        /**
151
         * Each test must to return its parser description
152
         * to register it before to start the parsing
153
         * process
154
         */
155
        public String getGPEWriterHandlerDescription(){
156
                return "default writer handler description";
157
        }
158
        
159
        /**
160
         * Each test must to return its parser class
161
         * that will be used to create new parsers.
162
         */
163
        public abstract Class getGPEWriterHandlerClass();
164
        
165
        
166
        /**
167
         * Each test must to return its parser name
168
         * to register it before to start the parsing
169
         * process
170
         */
171
        public String getGPEParserName(){
172
                return "FORMAT VERSION";
173
        }
174
        
175
        /**
176
         * Each test must to return its parser description
177
         * to register it before to start the parsing
178
         * process
179
         */
180
        public String getGPEParserDescription(){
181
                return "default parser description";
182
        }
183
        
184
        /**
185
         * Each test must to return its parser class
186
         * that will be used to create new parsers.
187
         */
188
        public abstract Class getGPEParserClass();
189
        
190
        /**
191
         * Gets the file format. The deafult writer
192
         * format will be used by default
193
         * @return
194
         */
195
        public String getFormat(){
196
                return null;
197
        }
198
        
199
        /**
200
         * Gets the file format. The deafult writer
201
         * version will be used by default
202
         * @return
203
         */
204
        public String getVersion(){
205
                return null;
206
        }
207
        /**
208
         * It creates a Random bbox coordinates. It return 
209
         * 10 coordinates 
210
         * @return
211
         */
212
        protected double[] generateRandomCoordinates(){
213
                return generateRandomCoordinates(10);
214
        }
215
        
216
        /**
217
         * It creates a Random bbox coordinates
218
         * @param length
219
         * The number of coordinates
220
         * @return
221
         */
222
        protected double[] generateRandomCoordinates(int length){
223
                double[] coord = new double[length];
224
                for (int i=0 ; i<coord.length ; i++){
225
                        coord[i] = Math.random();
226
                }
227
                return coord;
228
        }
229
        
230
        /**
231
         * It creates a Random linear ring. It return 
232
         * 10 coordinates 
233
         * @return
234
         */
235
        protected double[] generateRandomLinearRing(){
236
                return generateRandomLinearRing(10);
237
        }
238
        
239
        
240
        /**
241
         * It creates a random linear ring
242
         * @param length
243
         * @return
244
         */
245
        protected double[] generateRandomLinearRing(int length){
246
                double[] coord = new double[length];
247
                for (int i=0 ; i<coord.length-1 ; i++){
248
                        coord[i] = Math.random();
249
                }
250
                coord[length-1] = coord[0];
251
                return coord;
252
        }
253
        
254
        /**
255
         * It creates a Random bbox coordinates
256
         * @return
257
         */
258
        protected double[] generateRandomBBox(){
259
                double[] coord = new double[2];
260
                for (int i=0 ; i<coord.length ; i++){
261
                        coord[i] = Math.random();
262
                }
263
                return coord;
264
        }
265

    
266
        /**
267
         * @return the handler
268
         */
269
        public GPEWriterHandler getWriterHandler() {
270
                if (writerHandler == null){
271
                        try {
272
                                writerHandler = GPERegister.createWriter(getGPEWriterHandlerName());
273
                                writerHandler.setFormat(getFormat());
274
                                writerHandler.setVersion(getVersion());
275
                                writerHandler.setErrorHandler(getErrorHandler());
276
                        } catch (Exception e) {
277
                                //never throwed
278
                                e.printStackTrace();
279
                        }                        
280
                }
281
                return writerHandler;
282
        }
283

    
284
        /**
285
         * @return the contenHandler
286
         */
287
        public GPEContentHandler getContenHandler() {
288
                if (contenHandler == null){
289
                        contenHandler = new GPEContentHandlerTest();
290
                }
291
                return contenHandler;
292
        }
293

    
294
        /**
295
         * @return the errorHandler
296
         */
297
        public GPEErrorHandler getErrorHandler() {
298
                if (errorHandler == null){
299
                        errorHandler = new GPEErrorHandlerTest();
300
                }
301
                return errorHandler;
302
        }
303
        
304
        /**
305
         * Gets a list of parsed layers
306
         * @return
307
         */
308
        public Layer[] getLayers(){
309
                ArrayList layers = ((GPEContentHandlerTest)parser.getContentHandler()).getLayers();
310
                Layer[] aLayers = new Layer[layers.size()];
311
                for (int i=0 ; i<layers.size() ; i++){
312
                        aLayers[i] = (Layer)layers.get(i);
313
                }
314
                return aLayers;
315
        }
316
}