Statistics
| Revision:

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

History | View | Annotate | Download (6.31 KB)

1
package org.gvsig.gpe.parser;
2

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

    
9
import junit.framework.TestCase;
10

    
11
import org.gvsig.gpe.GPELocator;
12
import org.gvsig.gpe.GPEManager;
13
import org.gvsig.gpe.containers.Layer;
14

    
15
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
16
 *
17
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
18
 *
19
 * This program is free software; you can redistribute it and/or
20
 * modify it under the terms of the GNU General Public License
21
 * as published by the Free Software Foundation; either version 2
22
 * of the License, or (at your option) any later version.
23
 *
24
 * This program is distributed in the hope that it will be useful,
25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27
 * GNU General Public License for more details.
28
 *
29
 * You should have received a copy of the GNU General Public License
30
 * along with this program; if not, write to the Free Software
31
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
32
 *
33
 * For more information, contact:
34
 *
35
 *  Generalitat Valenciana
36
 *   Conselleria d'Infraestructures i Transport
37
 *   Av. Blasco Ib??ez, 50
38
 *   46010 VALENCIA
39
 *   SPAIN
40
 *
41
 *      +34 963862235
42
 *   gvsig@gva.es
43
 *      www.gvsig.gva.es
44
 *
45
 *    or
46
 *
47
 *   IVER T.I. S.A
48
 *   Salamanca 50
49
 *   46005 Valencia
50
 *   Spain
51
 *
52
 *   +34 963163400
53
 *   dac@iver.es
54
 */
55
/* CVS MESSAGES:
56
 *
57
 * $Id: GPEReaderBaseTest.java 173 2007-11-06 12:10:57Z jpiera $
58
 * $Log$
59
 * Revision 1.7  2007/05/09 06:54:07  jorpiell
60
 * Change the File by URI
61
 *
62
 * Revision 1.6  2007/04/20 12:04:10  csanchez
63
 * Actualizacion protoripo libGPE, A?adidos test para el parser, parseo con XSOM
64
 *
65
 * Revision 1.5  2007/04/19 11:50:20  csanchez
66
 * Actualizacion protoripo libGPE
67
 *
68
 * Revision 1.4  2007/04/19 07:23:20  jorpiell
69
 * Add the add methods to teh contenhandler and change the register mode
70
 *
71
 * Revision 1.3  2007/04/14 16:06:35  jorpiell
72
 * Add the container classes
73
 *
74
 * Revision 1.2  2007/04/13 13:14:55  jorpiell
75
 * Created the base tests and add some methods to the content handler
76
 *
77
 * Revision 1.1  2007/04/13 07:17:54  jorpiell
78
 * Add the writting tests for the simple geometries
79
 *
80
 *
81
 */
82
/**
83
 * This class must be implementend by all the classes that
84
 * implements a GPE reader Parser. 
85
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
86
 */
87
public abstract class GPEReaderBaseTest extends TestCase {
88
        private File file = null;
89
        private GPEParser parser = null;
90
        private GPEContentHandler contenHandler = null;
91
        private GPEErrorHandler errorHandler = null;
92
        private String parserName="FORMAT VERSION";
93
        private String parserDescription="default parser description";
94
        private GPEManager gpeManager = null;
95

    
96
        public void setUp() throws Exception{
97
                gpeManager = GPELocator.getGPEManager();        
98
        }        
99
        
100
        /**
101
         * @return the gpeManager
102
         */
103
        public GPEManager getGpeManager() {
104
                return gpeManager;
105
        }
106
        
107
        /**
108
         * This test parses the file and make the 
109
         * asserts.
110
         * @throws Exception
111
         */
112
        public void testParse() throws Exception{
113
                System.out.println("INFO: Parser registrado");
114
                file = new File(getFile());
115
                System.out.println("INFO: Abriendo Fichero: "+file.getName());
116
                //assertEquals(gpeManager.accept(file.toURI()),true);
117
                System.out.println("INFO: Existe parser registrado para el fomato ");
118
                parser = gpeManager.createParserByClass(getGPEParserClass().getName());
119
                System.out.println("INFO: Creado el parser ");
120
                assertNotNull(parser);
121
                parseFile();
122
                parseInputStream();                
123
        }
124
        
125
        /**
126
         * Parses the file
127
         */
128
        private void parseFile(){
129
                System.out.println("INFO: PARSING THE FILE...");
130
                parser.parse(getContenHandler() , getErrorHandler(), file.toURI());
131
                System.out.println("INFO: ??? SUCCESS !!!");
132
                makeAsserts();
133
        }
134
        
135
        /**
136
         * Parses the file like an inputstream
137
         * @throws FileNotFoundException
138
         */
139
        private void parseInputStream() throws Exception{
140
                InputStream is = getInputStream();
141
                errorHandler = null;
142
                contenHandler = null;
143
                System.out.println("INFO: PARSING THE INPUTSTREAM...");
144
                parser.parse(getContenHandler() , getErrorHandler(), is);
145
                System.out.println("INFO: ??? SUCCESS !!!");
146
                makeAsserts();
147
        }        
148
        
149
        /**
150
         * This method has to be override by the tests that use
151
         * other InputStream (KMZ...)
152
         * @return
153
         * @throws FileNotFoundException
154
         */
155
        public InputStream getInputStream() throws Exception{
156
                return new FileInputStream(new File(getFile()));
157
        }
158
        
159
        /**
160
         * This method must be used by the subclasses
161
         * to make the comparations. With getLayers
162
         * it can retrieve the parsed layers
163
         */
164
        public abstract void makeAsserts();
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 parserName;
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 parserDescription ;
182
        }
183
        /**
184
         * Each test must to return its parser name
185
         * to register it before to start the parsing
186
         * process
187
         */
188
        public void setGPEParserName(String name){
189
                parserName=name;
190
        }
191
        
192
        /**
193
         * Each test must to return its parser description
194
         * to register it before to start the parsing
195
         * process
196
         */
197
        public void setGPEParserDescription(String description){
198
                parserDescription=description;
199
        }
200
        /**
201
         * Each test must to return its parser class
202
         * that will be used to create new parsers.
203
         */
204
        public abstract Class getGPEParserClass();
205
        
206
        /**
207
         * Gets the GML file to open
208
         * @return
209
         */
210
        public abstract String getFile();
211
        
212
        /**
213
         * Gets a list of parsed layers
214
         * @return
215
         */
216
        public Layer[] getLayers(){
217
                ArrayList layers = ((GPEContentHandlerTest)parser.getContentHandler()).getLayers();
218
                Layer[] aLayers = new Layer[layers.size()];
219
                for (int i=0 ; i<layers.size() ; i++){
220
                        aLayers[i] = (Layer)layers.get(i);
221
                }
222
                return aLayers;
223
        }
224
        
225
        /**
226
         * @return the contenHandler
227
         */
228
        public GPEContentHandler getContenHandler() {
229
                if (contenHandler == null){
230
                        contenHandler = new GPEContentHandlerTest();
231
                }
232
                return contenHandler;
233
        }
234

    
235
        /**
236
         * @return the errorHandler
237
         */
238
        public GPEErrorHandler getErrorHandler() {
239
                if (errorHandler == null){
240
                        errorHandler = new GPEErrorHandlerTest();
241
                }
242
                return errorHandler;
243
        }
244
        
245
}