Statistics
| Revision:

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

History | View | Annotate | Download (9.08 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.GPEParser;
21

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

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

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

    
341
        /**
342
         * @return the contenHandler
343
         */
344
        public GPEContentHandler getContenHandler() {
345
                if (contenHandler == null){
346
                        contenHandler = new GPEContentHandlerTest();
347
                }
348
                return contenHandler;
349
        }
350

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