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