Statistics
| Revision:

root / branches / Mobile1.0 / org.gvsig.gpe / src-test / org / gvsig / gpe / writer / GPEWriterBaseTest.java @ 79

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

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

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

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