svn-gvsig-desktop / branches / v2_0_0_prep / libraries / org.gvsig.geocoding / src-test / org / gvsig / geocoding / persistence / TestPersistence.java @ 32479
History | View | Annotate | Download (10.1 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2008 Prodevelop S.L main development
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.geocoding.persistence; |
29 |
|
30 |
import java.io.File; |
31 |
import java.io.FileOutputStream; |
32 |
import java.io.IOException; |
33 |
import java.io.OutputStream; |
34 |
import java.io.OutputStreamWriter; |
35 |
import java.util.ArrayList; |
36 |
import java.util.List; |
37 |
|
38 |
import org.exolab.castor.xml.MarshalException; |
39 |
import org.exolab.castor.xml.Marshaller; |
40 |
import org.exolab.castor.xml.ValidationException; |
41 |
import org.gvsig.fmap.dal.exception.DataException; |
42 |
import org.gvsig.geocoding.GeocodingTags; |
43 |
import org.gvsig.geocoding.address.Literal; |
44 |
import org.gvsig.geocoding.address.impl.DefaultLiteral; |
45 |
import org.gvsig.geocoding.pattern.GeocodingPattern; |
46 |
import org.gvsig.geocoding.pattern.GeocodingSource; |
47 |
import org.gvsig.geocoding.pattern.impl.DefaultGeocodingPattern; |
48 |
import org.gvsig.geocoding.pattern.impl.DefaultGeocodingSource; |
49 |
import org.gvsig.geocoding.styles.AbstractGeocodingStyle; |
50 |
import org.gvsig.geocoding.styles.impl.Composed; |
51 |
import org.gvsig.geocoding.styles.impl.DoubleRange; |
52 |
import org.gvsig.geocoding.styles.impl.SimpleCentroid; |
53 |
import org.gvsig.geocoding.styles.impl.SimpleRange; |
54 |
import org.gvsig.tools.persistence.PersistenceException; |
55 |
import org.gvsig.tools.persistence.PersistenceValidateExceptions; |
56 |
import org.gvsig.utils.XMLEntity; |
57 |
import org.gvsig.utils.XMLException; |
58 |
import org.slf4j.Logger; |
59 |
import org.slf4j.LoggerFactory; |
60 |
|
61 |
/**
|
62 |
* Test
|
63 |
*
|
64 |
* @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
|
65 |
* @author <a href="mailto:vsanjaime@prodevelop.es"> Vicent Sanjaime Calvet</a>
|
66 |
*/
|
67 |
|
68 |
public class TestPersistence extends |
69 |
org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase { |
70 |
|
71 |
private static String PROJECTENCODING = "UTF-8"; |
72 |
|
73 |
private static final Logger log = LoggerFactory |
74 |
.getLogger(TestPersistence.class); |
75 |
|
76 |
/**
|
77 |
* tearDown
|
78 |
*/
|
79 |
public void tearDown() { |
80 |
|
81 |
} |
82 |
|
83 |
/**
|
84 |
* Pattern SimpleCentroid persistence
|
85 |
*
|
86 |
* @throws IOException
|
87 |
* @throws IOException
|
88 |
* @throws XMLException
|
89 |
* @throws ValidationException
|
90 |
* @throws MarshalException
|
91 |
* @throws PersistenceException
|
92 |
* @throws PersistenceValidateExceptions
|
93 |
*
|
94 |
* @throws DataException
|
95 |
*/
|
96 |
public void testPersistPattern() throws IOException, XMLException, |
97 |
MarshalException, ValidationException {
|
98 |
|
99 |
// Get pattern
|
100 |
GeocodingPattern pat = getPattern(); |
101 |
|
102 |
File file = File.createTempFile("00_pat", ".xml"); |
103 |
log.debug(file.getAbsolutePath()); |
104 |
OutputStream fos = new FileOutputStream(file.getAbsolutePath()); |
105 |
OutputStreamWriter writer = new OutputStreamWriter(fos, PROJECTENCODING); |
106 |
Marshaller m = new Marshaller(writer);
|
107 |
m.setEncoding(PROJECTENCODING); |
108 |
|
109 |
XMLEntity xml = pat.getXMLEntity(); |
110 |
m.marshal(xml.getXmlTag()); |
111 |
|
112 |
} |
113 |
|
114 |
/**
|
115 |
* Pattern SimpleRange persistence
|
116 |
*
|
117 |
* @throws IOException
|
118 |
* @throws IOException
|
119 |
* @throws XMLException
|
120 |
* @throws ValidationException
|
121 |
* @throws MarshalException
|
122 |
* @throws PersistenceException
|
123 |
* @throws PersistenceValidateExceptions
|
124 |
*
|
125 |
* @throws DataException
|
126 |
*/
|
127 |
public void testPersistPatternSimpleRange() throws IOException, XMLException, |
128 |
MarshalException, ValidationException {
|
129 |
|
130 |
// Get pattern
|
131 |
GeocodingPattern pat = getPattern1(); |
132 |
|
133 |
File file = File.createTempFile("00_pat1", ".xml"); |
134 |
log.debug(file.getAbsolutePath()); |
135 |
|
136 |
OutputStream fos = new FileOutputStream(file.getAbsolutePath()); |
137 |
OutputStreamWriter writer = new OutputStreamWriter(fos, PROJECTENCODING); |
138 |
Marshaller m = new Marshaller(writer);
|
139 |
m.setEncoding(PROJECTENCODING); |
140 |
|
141 |
XMLEntity xml = pat.getXMLEntity(); |
142 |
m.marshal(xml.getXmlTag()); |
143 |
|
144 |
} |
145 |
|
146 |
/**
|
147 |
* Pattern DoubleRange persistence
|
148 |
*
|
149 |
* @throws IOException
|
150 |
* @throws IOException
|
151 |
* @throws XMLException
|
152 |
* @throws ValidationException
|
153 |
* @throws MarshalException
|
154 |
* @throws PersistenceException
|
155 |
* @throws PersistenceValidateExceptions
|
156 |
*
|
157 |
* @throws DataException
|
158 |
*/
|
159 |
public void testPersistPatternDoubleRange() throws IOException, XMLException, |
160 |
MarshalException, ValidationException {
|
161 |
|
162 |
|
163 |
// Get pattern
|
164 |
GeocodingPattern pat = getPattern2(); |
165 |
|
166 |
File file = File.createTempFile("00_pat2", ".xml"); |
167 |
log.debug(file.getAbsolutePath()); |
168 |
OutputStream fos = new FileOutputStream(file.getAbsolutePath()); |
169 |
OutputStreamWriter writer = new OutputStreamWriter(fos, PROJECTENCODING); |
170 |
Marshaller m = new Marshaller(writer);
|
171 |
m.setEncoding(PROJECTENCODING); |
172 |
|
173 |
XMLEntity xml = pat.getXMLEntity(); |
174 |
m.marshal(xml.getXmlTag()); |
175 |
|
176 |
} |
177 |
|
178 |
/**
|
179 |
* Pattern Composed persistence
|
180 |
*
|
181 |
* @throws IOException
|
182 |
* @throws IOException
|
183 |
* @throws XMLException
|
184 |
* @throws ValidationException
|
185 |
* @throws MarshalException
|
186 |
* @throws PersistenceException
|
187 |
* @throws PersistenceValidateExceptions
|
188 |
*
|
189 |
* @throws DataException
|
190 |
*/
|
191 |
public void testPersistPatternComposed() throws IOException, XMLException, |
192 |
MarshalException, ValidationException {
|
193 |
|
194 |
// Get pattern
|
195 |
GeocodingPattern pat = getPattern3(); |
196 |
|
197 |
File file = File.createTempFile("00_pat3", ".xml"); |
198 |
log.debug(file.getAbsolutePath()); |
199 |
OutputStream fos = new FileOutputStream(file.getAbsolutePath()); |
200 |
OutputStreamWriter writer = new OutputStreamWriter(fos, PROJECTENCODING); |
201 |
Marshaller m = new Marshaller(writer);
|
202 |
m.setEncoding(PROJECTENCODING); |
203 |
|
204 |
XMLEntity xml = pat.getXMLEntity(); |
205 |
m.marshal(xml.getXmlTag()); |
206 |
|
207 |
} |
208 |
|
209 |
/**
|
210 |
* get pattern
|
211 |
*
|
212 |
* @return
|
213 |
*/
|
214 |
private GeocodingPattern getPattern() {
|
215 |
|
216 |
GeocodingPattern pat = new DefaultGeocodingPattern();
|
217 |
|
218 |
pat.setPatternName("SimpleCentroid style");
|
219 |
|
220 |
GeocodingSource source = new DefaultGeocodingSource();
|
221 |
source.setLayerName("Layer");
|
222 |
source.setLayerProvider("SHP");
|
223 |
|
224 |
AbstractGeocodingStyle style = new SimpleCentroid();
|
225 |
Literal relations = new DefaultLiteral();
|
226 |
relations.put("Calle", "STREET_NAM"); |
227 |
relations.put("City", "CITY"); |
228 |
relations.put("Barrio", "STREET"); |
229 |
|
230 |
style.setRelationsLiteral(relations); |
231 |
source.setStyle(style); |
232 |
|
233 |
pat.setSource(source); |
234 |
|
235 |
return pat;
|
236 |
} |
237 |
|
238 |
/**
|
239 |
* get pattern
|
240 |
*
|
241 |
* @return
|
242 |
*/
|
243 |
private GeocodingPattern getPattern1() {
|
244 |
|
245 |
GeocodingPattern pat = new DefaultGeocodingPattern();
|
246 |
|
247 |
pat.setPatternName("SimpleRange style");
|
248 |
|
249 |
GeocodingSource source = new DefaultGeocodingSource();
|
250 |
source.setLayerName("Layer");
|
251 |
source.setLayerProvider("SHP");
|
252 |
|
253 |
AbstractGeocodingStyle style = new SimpleRange();
|
254 |
Literal relations = new DefaultLiteral();
|
255 |
relations.put("Calle", "STREET_NAM"); |
256 |
relations.put("City", "CITY"); |
257 |
relations.put("Barrio", "STREET"); |
258 |
relations.put(GeocodingTags.FIRSTNUMBER, "NUM");
|
259 |
relations.put(GeocodingTags.LASTNUMBER, "NUM2");
|
260 |
|
261 |
style.setRelationsLiteral(relations); |
262 |
((SimpleRange) style).setFirstNumber(GeocodingTags.FIRSTNUMBER); |
263 |
((SimpleRange) style).setLastNumber(GeocodingTags.LASTNUMBER); |
264 |
|
265 |
source.setStyle(style); |
266 |
|
267 |
pat.setSource(source); |
268 |
|
269 |
return pat;
|
270 |
} |
271 |
|
272 |
/**
|
273 |
* get pattern
|
274 |
*
|
275 |
* @return
|
276 |
*/
|
277 |
private GeocodingPattern getPattern2() {
|
278 |
|
279 |
GeocodingPattern pat = new DefaultGeocodingPattern();
|
280 |
|
281 |
pat.setPatternName("DoubleRange style");
|
282 |
|
283 |
GeocodingSource source = new DefaultGeocodingSource();
|
284 |
source.setLayerName("Layer");
|
285 |
source.setLayerProvider("SHP");
|
286 |
|
287 |
AbstractGeocodingStyle style = new DoubleRange();
|
288 |
Literal relations = new DefaultLiteral();
|
289 |
relations.put("Calle", "STREET_NAM"); |
290 |
relations.put("City", "CITY"); |
291 |
relations.put("Barrio", "STREET"); |
292 |
relations.put(GeocodingTags.FIRSTLEFTNUMBER, "NUM");
|
293 |
relations.put(GeocodingTags.FIRSTRIGHTNUMBER, "NUM2");
|
294 |
relations.put(GeocodingTags.LASTLEFTNUMBER, "NUM3");
|
295 |
relations.put(GeocodingTags.LASTRIGHTNUMBER, "NUM4");
|
296 |
|
297 |
style.setRelationsLiteral(relations); |
298 |
((DoubleRange) style).setFirstLeftNumber(GeocodingTags.FIRSTLEFTNUMBER); |
299 |
((DoubleRange) style).setFirstRightNumber(GeocodingTags.FIRSTRIGHTNUMBER); |
300 |
((DoubleRange) style).setLastLeftNumber(GeocodingTags.LASTLEFTNUMBER); |
301 |
((DoubleRange) style).setLastRightNumber(GeocodingTags.LASTRIGHTNUMBER); |
302 |
|
303 |
source.setStyle(style); |
304 |
|
305 |
pat.setSource(source); |
306 |
|
307 |
return pat;
|
308 |
} |
309 |
|
310 |
/**
|
311 |
* get pattern
|
312 |
*
|
313 |
* @return
|
314 |
*/
|
315 |
private GeocodingPattern getPattern3() {
|
316 |
|
317 |
GeocodingPattern pat = new DefaultGeocodingPattern();
|
318 |
|
319 |
pat.setPatternName("Composed style");
|
320 |
|
321 |
GeocodingSource source = new DefaultGeocodingSource();
|
322 |
source.setLayerName("Layer");
|
323 |
source.setLayerProvider("SHP");
|
324 |
|
325 |
AbstractGeocodingStyle style = new Composed();
|
326 |
Literal lit = new DefaultLiteral();
|
327 |
lit.put("Calle", "STREET_NAM"); |
328 |
lit.put("City", "CITY"); |
329 |
lit.put("Barrio", "STREET"); |
330 |
|
331 |
style.setRelationsLiteral(lit); |
332 |
|
333 |
Literal lit2 = new DefaultLiteral();
|
334 |
lit2.put("Calle", "STREET_NAM1"); |
335 |
lit2.put("City", "CITY1"); |
336 |
lit2.put("Barrio", "STREET1"); |
337 |
|
338 |
|
339 |
Literal lit3 = new DefaultLiteral();
|
340 |
lit3.put("Calle", "STREET_NAM2"); |
341 |
lit3.put("City", "CITY2"); |
342 |
lit3.put("Barrio", "STREET2"); |
343 |
|
344 |
List<Literal> intersectLit = new ArrayList<Literal>(); |
345 |
intersectLit.add(lit2); |
346 |
intersectLit.add(lit3); |
347 |
((Composed)style).setIntersectsLiterals(intersectLit); |
348 |
|
349 |
source.setStyle(style); |
350 |
|
351 |
pat.setSource(source); |
352 |
|
353 |
return pat;
|
354 |
} |
355 |
|
356 |
@Override
|
357 |
protected void doSetUp() throws Exception { |
358 |
|
359 |
|
360 |
|
361 |
} |
362 |
|
363 |
} |