Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libGPE / src-test / org / gvsig / gpe / writer / GPEWriterBaseTest.java @ 28113

History | View | Annotate | Download (9.21 KB)

1
package org.gvsig.gpe.writer;
2

    
3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileNotFoundException;
6
import java.io.FileOutputStream;
7
import java.io.InputStream;
8
import java.io.OutputStream;
9
import java.util.ArrayList;
10

    
11
import junit.framework.TestCase;
12

    
13
import org.gvsig.gpe.GPELocator;
14
import org.gvsig.gpe.GPEManager;
15
import org.gvsig.gpe.containers.Layer;
16
import org.gvsig.gpe.parser.GPEContentHandler;
17
import org.gvsig.gpe.parser.GPEContentHandlerTest;
18
import org.gvsig.gpe.parser.GPEErrorHandler;
19
import org.gvsig.gpe.parser.GPEErrorHandlerTest;
20
import org.gvsig.gpe.parser.GPELibraryTest;
21
import org.gvsig.gpe.parser.GPEParser;
22

    
23
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
24
 *
25
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
26
 *
27
 * This program is free software; you can redistribute it and/or
28
 * modify it under the terms of the GNU General Public License
29
 * as published by the Free Software Foundation; either version 2
30
 * of the License, or (at your option) any later version.
31
 *
32
 * This program is distributed in the hope that it will be useful,
33
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35
 * GNU General Public License for more details.
36
 *
37
 * You should have received a copy of the GNU General Public License
38
 * along with this program; if not, write to the Free Software
39
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
40
 *
41
 * For more information, contact:
42
 *
43
 *  Generalitat Valenciana
44
 *   Conselleria d'Infraestructures i Transport
45
 *   Av. Blasco Ib??ez, 50
46
 *   46010 VALENCIA
47
 *   SPAIN
48
 *
49
 *      +34 963862235
50
 *   gvsig@gva.es
51
 *      www.gvsig.gva.es
52
 *
53
 *    or
54
 *
55
 *   IVER T.I. S.A
56
 *   Salamanca 50
57
 *   46005 Valencia
58
 *   Spain
59
 *
60
 *   +34 963163400
61
 *   dac@iver.es
62
 */
63
/* CVS MESSAGES:
64
 *
65
 * $Id: GPEWriterBaseTest.java 262 2007-12-14 08:11:39Z jpiera $
66
 * $Log$
67
 * Revision 1.14  2007/06/22 12:38:59  jorpiell
68
 * The typeNotFoundException has been deleted. It never was thrown
69
 *
70
 * Revision 1.12  2007/06/07 14:52:28  jorpiell
71
 * Add the schema support
72
 *
73
 * Revision 1.11  2007/05/16 09:28:19  jorpiell
74
 * The polygons has to be closed
75
 *
76
 * Revision 1.10  2007/05/15 12:10:44  jorpiell
77
 * Outpout file deleted
78
 *
79
 * Revision 1.9  2007/05/15 09:52:00  jorpiell
80
 * The namespace is deleted from the element name
81
 *
82
 * Revision 1.8  2007/05/09 06:54:07  jorpiell
83
 * Change the File by URI
84
 *
85
 * Revision 1.7  2007/05/02 11:46:07  jorpiell
86
 * Writing tests updated
87
 *
88
 * Revision 1.6  2007/04/26 14:39:12  jorpiell
89
 * Add some tests
90
 *
91
 * Revision 1.5  2007/04/19 11:50:20  csanchez
92
 * Actualizacion protoripo libGPE
93
 *
94
 * Revision 1.4  2007/04/19 07:23:20  jorpiell
95
 * Add the add methods to teh contenhandler and change the register mode
96
 *
97
 * Revision 1.3  2007/04/17 06:27:20  jorpiell
98
 * Changed the default filename
99
 *
100
 * Revision 1.2  2007/04/14 16:06:35  jorpiell
101
 * Add the container classes
102
 *
103
 * Revision 1.1  2007/04/13 07:17:54  jorpiell
104
 * Add the writting tests for the simple geometries
105
 *
106
 *
107
 */
108
/**
109
 * This class must be implementend by all the classes that
110
 * implements a GPE writer Parser. It creates a writer, write some
111
 * features and then uses a reader to compare the writting 
112
 * process
113
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
114
 */
115
public abstract class GPEWriterBaseTest extends TestCase{
116
        private GPEWriterHandler writerHandler = null;
117
        private GPEContentHandler contenHandler = null;
118
        private GPEErrorHandler errorHandler = null;
119
        private GPEParser parser = null;
120
        private File outputFile = null;
121
        private GPEManager gpeManager = null;
122

    
123
        /**
124
         * Register the driver and gets the handler
125
         * @throws Exception 
126
         */
127
        public void setUp() throws Exception{
128
                GPELibraryTest lib = new GPELibraryTest();
129
                lib.initialize();
130
                lib.postInitialize();        
131
                gpeManager = GPELocator.getGPEManager();
132
                outputFile = new File(this.getClass().getName() + Math.random());
133
        }        
134
        
135
        /**
136
         * @return the gpeManager
137
         */
138
        public GPEManager getGpeManager() {
139
                return gpeManager;
140
        }
141
        
142
        /**
143
         * Delete the file
144
         */
145
        public void tearDown() throws Exception{
146
                System.out.println("******** WARNINGS ********");
147
                for (int i=0 ; i<getErrorHandler().getWarningsSize(); i++){
148
                        System.out.println(getErrorHandler().getWarningAt(i));
149
                }        
150
        }
151
        
152
        /**
153
         * This test writes some objects into the file and then
154
         * try to read the created file. It compare that the written
155
         * objects are the same that the read them
156
         * @throws Exception 
157
         */
158
        public void testWriter() throws Exception{
159
                OutputStream os = createOutputStream(outputFile);
160
                getWriterHandler().setOutputStream(os);
161
                writeObjects();                
162
                
163
                parser = gpeManager.createParserByClass(getGPEParserClass().getName());
164
                InputStream is = createInputStream(outputFile);
165
                parser.parse(getContenHandler(),getErrorHandler() ,is);
166
                readObjects();
167
                
168
                outputFile.delete();                
169
        }
170
        
171
        protected OutputStream createOutputStream(File file) throws FileNotFoundException{
172
                return new FileOutputStream(file);
173
        }
174
        
175
        protected InputStream createInputStream(File file) throws FileNotFoundException{
176
                return new FileInputStream(file);
177
        }        
178
        
179
        /**
180
         * This method write somo objects into the writer handler
181
         */
182
        public abstract void writeObjects();
183
        
184
        /**
185
         * It read the objects and make all the comparations
186
         *
187
         */
188
        public abstract void readObjects();
189
                        
190
        /**
191
         * Each test must to return its parser name
192
         * to register it before to start the parsing
193
         * process
194
         */
195
        public String getGPEWriterHandlerName(){
196
                return "FORMAT VERSION";
197
        }
198
        
199
        /**
200
         * Each test must to return its parser description
201
         * to register it before to start the parsing
202
         * process
203
         */
204
        public String getGPEWriterHandlerDescription(){
205
                return "default writer handler description";
206
        }
207
        
208
        /**
209
         * Each test must to return its parser class
210
         * that will be used to create new parsers.
211
         */
212
        public abstract Class getGPEWriterHandlerClass();
213
        
214
        
215
        /**
216
         * Each test must to return its parser name
217
         * to register it before to start the parsing
218
         * process
219
         */
220
        public String getGPEParserName(){
221
                return "FORMAT VERSION";
222
        }
223
        
224
        /**
225
         * Each test must to return its parser description
226
         * to register it before to start the parsing
227
         * process
228
         */
229
        public String getGPEParserDescription(){
230
                return "default parser description";
231
        }
232
        
233
        /**
234
         * Each test must to return its parser class
235
         * that will be used to create new parsers.
236
         */
237
        public abstract Class getGPEParserClass();
238
        
239
        /**
240
         * Gets the file format. The deafult writer
241
         * format will be used by default
242
         * @return
243
         */
244
        public String getFormat(){
245
                return null;
246
        }
247
        
248
        /**
249
         * It creates a random point
250
         * @param length
251
         * @return
252
         */
253
        protected double generateRandomPoint(){
254
                return Math.random();                
255
        }
256
        
257
        /**
258
         * It creates a Random bbox coordinates. It return 
259
         * 10 coordinates 
260
         * @return
261
         */
262
        protected double[] generateRandomCoordinates(){
263
                return generateRandomCoordinates(10);
264
        }
265
        
266
        /**
267
         * It creates a Random bbox coordinates
268
         * @param length
269
         * The number of coordinates
270
         * @return
271
         */
272
        protected double[] generateRandomCoordinates(int length){
273
                double[] coord = new double[length];
274
                for (int i=0 ; i<coord.length ; i++){
275
                        coord[i] = generateRandomPoint();
276
                }
277
                return coord;
278
        }
279
        
280
        /**
281
         * It creates a Random linear ring. It return 
282
         * 10 coordinates 
283
         * @return
284
         */
285
        protected double[] generateRandomLinearRing(){
286
                return generateRandomLinearRing(10);
287
        }
288
        
289
        /**
290
         * It creates a random linear ring
291
         * @param length
292
         * @return
293
         */
294
        protected double[] generateRandomLinearRing(int length){
295
                double[] coord = new double[length];
296
                for (int i=0 ; i<coord.length-1 ; i++){
297
                        coord[i] = generateRandomPoint();
298
                }
299
                coord[length-1] = coord[0];
300
                return coord;
301
        }
302
        
303
        /**
304
         * It creates a Random bbox coordinates
305
         * @return
306
         */
307
        protected double[] generateRandomBBox(){
308
                double[] coord = new double[2];
309
                for (int i=0 ; i<coord.length ; i++){
310
                        coord[i] = generateRandomPoint();
311
                }
312
                return coord;
313
        }
314
        
315
        /**
316
         * Closes a polygon
317
         * @param x
318
         * Polygon coordinates
319
         * @return
320
         * A closed polygon
321
         */
322
        protected double[] closePolygon(double[] x){
323
                double[] xClosed = new double[x.length + 1];
324
                System.arraycopy(x, 0, xClosed, 0, x.length);
325
                xClosed[xClosed.length -1] = x[0];
326
                return xClosed;
327
        }
328

    
329
        /**
330
         * @return the handler
331
         */
332
        public GPEWriterHandler getWriterHandler() {
333
                if (writerHandler == null){
334
                        try {
335
                                writerHandler = gpeManager.createWriterByClass(getGPEWriterHandlerClass().getName());
336
                                writerHandler.setErrorHandler(getErrorHandler());
337
                        } catch (Exception e) {
338
                                //never throwed
339
                                e.printStackTrace();
340
                        }                        
341
                }
342
                return writerHandler;
343
        }
344

    
345
        /**
346
         * @return the contenHandler
347
         */
348
        public GPEContentHandler getContenHandler() {
349
                if (contenHandler == null){
350
                        contenHandler = new GPEContentHandlerTest();
351
                }
352
                return contenHandler;
353
        }
354

    
355
        /**
356
         * @return the errorHandler
357
         */
358
        public GPEErrorHandler getErrorHandler() {
359
                if (errorHandler == null){
360
                        errorHandler = new GPEErrorHandlerTest();
361
                }
362
                return errorHandler;
363
        }
364
        
365
        /**
366
         * Gets a list of parsed layers
367
         * @return
368
         */
369
        public Layer[] getLayers(){
370
                ArrayList layers = ((GPEContentHandlerTest)parser.getContentHandler()).getLayers();
371
                Layer[] aLayers = new Layer[layers.size()];
372
                for (int i=0 ; i<layers.size() ; i++){
373
                        aLayers[i] = (Layer)layers.get(i);
374
                }
375
                return aLayers;
376
        }
377
}