root / branches / v2_0_0_prep / libraries / libFMap_dataFile / src-test / org / gvsig / fmap / data / feature / file / DataStoreTest.java @ 24250
History | View | Annotate | Download (44 KB)
1 |
package org.gvsig.fmap.data.feature.file; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.util.Iterator; |
5 |
|
6 |
import junit.framework.TestCase; |
7 |
|
8 |
import org.cresques.cts.IProjection; |
9 |
import org.gvsig.fmap.crs.CRSFactory; |
10 |
import org.gvsig.fmap.data.DataSet; |
11 |
import org.gvsig.fmap.data.DataManager; |
12 |
import org.gvsig.fmap.data.DataParameters; |
13 |
import org.gvsig.fmap.data.DataStoreParameters; |
14 |
import org.gvsig.fmap.data.exceptions.CloseException; |
15 |
import org.gvsig.fmap.data.exceptions.DataException; |
16 |
import org.gvsig.fmap.data.exceptions.InitializeException; |
17 |
import org.gvsig.fmap.data.exceptions.OpenException; |
18 |
import org.gvsig.fmap.data.exceptions.ReadException; |
19 |
import org.gvsig.fmap.data.exceptions.WriteException; |
20 |
import org.gvsig.fmap.data.feature.AttributeDescriptor; |
21 |
import org.gvsig.fmap.data.feature.Feature; |
22 |
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor; |
23 |
import org.gvsig.fmap.data.feature.FeatureSet; |
24 |
import org.gvsig.fmap.data.feature.FeatureStore; |
25 |
import org.gvsig.fmap.data.feature.FeatureType; |
26 |
import org.gvsig.fmap.data.feature.MemoryFeature; |
27 |
import org.gvsig.fmap.data.feature.file.dbf.DBFStore; |
28 |
import org.gvsig.fmap.data.feature.file.dbf.DBFStoreParameters; |
29 |
import org.gvsig.fmap.data.feature.file.dgn.DGNStore; |
30 |
import org.gvsig.fmap.data.feature.file.dgn.DGNStoreParameters; |
31 |
import org.gvsig.fmap.data.feature.file.dxf.DXFStore; |
32 |
import org.gvsig.fmap.data.feature.file.dxf.DXFStoreParameters; |
33 |
import org.gvsig.fmap.data.feature.file.shp.SHPStore; |
34 |
import org.gvsig.fmap.data.feature.file.shp.SHPStoreParameters; |
35 |
import org.gvsig.fmap.data.feature.impl.AbstractFeatureStore; |
36 |
import org.gvsig.fmap.data.feature.visitor.PrintlnFeaturesVisitor; |
37 |
import org.gvsig.fmap.data.impl.DefaultDataManager; |
38 |
import org.gvsig.fmap.data.resource.ResourceManager; |
39 |
import org.gvsig.fmap.data.resource.impl.DefaultResourceManager; |
40 |
import org.gvsig.fmap.data.resource.spi.AbstractResource; |
41 |
import org.gvsig.fmap.geom.Geometry; |
42 |
import org.gvsig.fmap.geom.handler.Handler; |
43 |
import org.gvsig.tools.exception.BaseException; |
44 |
import org.gvsig.tools.observer.Observer; |
45 |
|
46 |
import com.iver.utiles.XMLEntity; |
47 |
import com.iver.utiles.XMLException; |
48 |
|
49 |
public class DataStoreTest extends TestCase { |
50 |
|
51 |
private File dbffile = new File(DataStoreTest.class.getResource( |
52 |
"data/prueba.dbf").getFile());
|
53 |
private File shpfile = new File(DataStoreTest.class.getResource( |
54 |
"data/prueba.shp").getFile());
|
55 |
private File dxffile = new File(DataStoreTest.class.getResource( |
56 |
"data/prueba.dxf").getFile());
|
57 |
private File dgnfile = new File(DataStoreTest.class.getResource( |
58 |
"data/prueba.dgn").getFile());
|
59 |
|
60 |
public static void main(String[] args) { |
61 |
junit.textui.TestRunner.run(DataStoreTest.class); |
62 |
} |
63 |
|
64 |
protected void setUp() throws Exception { |
65 |
super.setUp();
|
66 |
|
67 |
} |
68 |
|
69 |
public void testDBF() { |
70 |
try {
|
71 |
System.out.println("======= DBF =============="); |
72 |
org.gvsig.fmap.data.feature.file.dbf.Register.selfRegister(); |
73 |
|
74 |
DataManager dsm = DefaultDataManager.getManager(); |
75 |
|
76 |
DataStoreParameters dp = dsm |
77 |
.createStoreParameters(DBFStore.DATASTORE_NAME); |
78 |
((DBFStoreParameters) dp).setFile(dbffile); |
79 |
|
80 |
driverTest(dp, null, null, true); |
81 |
FeatureStore fs = null;
|
82 |
try {
|
83 |
fs = (FeatureStore) dsm.createStore(dp); |
84 |
} catch (InitializeException e) {
|
85 |
e.printStackTrace(); |
86 |
fail("Exception:" + e);
|
87 |
} |
88 |
assertNotNull("Can't create Feature Store", fs);
|
89 |
|
90 |
// fs.open();
|
91 |
|
92 |
Iterator it;
|
93 |
FeatureSet fc; |
94 |
Comparable v1, v2;
|
95 |
Feature feature, pfeature; |
96 |
long count;
|
97 |
|
98 |
fc = (FeatureSet) fs.getDataSet(); |
99 |
|
100 |
assertEquals(9, fc.size());
|
101 |
|
102 |
fc = (FeatureSet) fs.getDataCollection(fs |
103 |
.getDefaultFeatureType(), "lower(NOMBRE) like 'b%'", null); |
104 |
|
105 |
assertEquals(2, fc.size());
|
106 |
|
107 |
it = fc.iterator(); |
108 |
count = 0;
|
109 |
while (it.hasNext()) {
|
110 |
feature = (Feature) it.next(); |
111 |
assertTrue("Filter error", feature.getString("NOMBRE") |
112 |
.toLowerCase().startsWith("b"));
|
113 |
count++; |
114 |
} |
115 |
assertEquals("Iteration error", 2, count); |
116 |
|
117 |
fc = (FeatureSet) fs.getDataCollection(fs |
118 |
.getDefaultFeatureType(), null, "NOMBRE ASC"); |
119 |
assertEquals(9, fc.size());
|
120 |
it = fc.iterator(); |
121 |
count = 0;
|
122 |
pfeature = (Feature) it.next(); |
123 |
count++; |
124 |
while (it.hasNext()) {
|
125 |
feature = (Feature) it.next(); |
126 |
v1 = (Comparable) pfeature.get("NOMBRE"); |
127 |
v2 = (Comparable) feature.get("NOMBRE"); |
128 |
pfeature = feature; |
129 |
assertTrue("Short error", (v1.compareTo(v1) <= 0)); |
130 |
count++; |
131 |
} |
132 |
assertEquals("Iteration error", 9, count); |
133 |
|
134 |
fc = (FeatureSet) fs.getDataCollection(fs |
135 |
.getDefaultFeatureType(), null, "NOMBRE DESC"); |
136 |
assertEquals(9, fc.size());
|
137 |
it = fc.iterator(); |
138 |
|
139 |
count = 0;
|
140 |
pfeature = (Feature) it.next(); |
141 |
count++; |
142 |
while (it.hasNext()) {
|
143 |
feature = (Feature) it.next(); |
144 |
v1 = (Comparable) pfeature.get("NOMBRE"); |
145 |
v2 = (Comparable) feature.get("NOMBRE"); |
146 |
pfeature = feature; |
147 |
assertTrue("Short error", (v1.compareTo(v1) >= 0)); |
148 |
count++; |
149 |
} |
150 |
assertEquals("Iteration error", 9, count); |
151 |
|
152 |
fc = (FeatureSet) fs.getDataCollection(fs |
153 |
.getDefaultFeatureType(), "lower(NOMBRE) like 'b%'",
|
154 |
"NOMBRE");
|
155 |
|
156 |
assertEquals(2, fc.size());
|
157 |
|
158 |
it = fc.iterator(); |
159 |
|
160 |
count = 0;
|
161 |
pfeature = (Feature) it.next(); |
162 |
assertTrue(pfeature.getString("NOMBRE").toLowerCase().startsWith(
|
163 |
"b"));
|
164 |
count++; |
165 |
while (it.hasNext()) {
|
166 |
feature = (Feature) it.next(); |
167 |
assertTrue("Filter error", feature.getString("NOMBRE") |
168 |
.toLowerCase().startsWith("b"));
|
169 |
v1 = (Comparable) pfeature.get("NOMBRE"); |
170 |
v2 = (Comparable) feature.get("NOMBRE"); |
171 |
pfeature = feature; |
172 |
assertTrue("Short error", (v1.compareTo(v1) <= 0)); |
173 |
count++; |
174 |
} |
175 |
assertEquals("Iteration error", 2, count); |
176 |
|
177 |
fc = (FeatureSet) fs.getDataCollection(fs |
178 |
.getDefaultFeatureType(), null, "Tipo,lower(NOMBRE) Desc"); |
179 |
assertEquals(9, fc.size());
|
180 |
it = fc.iterator(); |
181 |
count = 0;
|
182 |
pfeature = (Feature) it.next(); |
183 |
System.out.println(pfeature.getString("NOMBRE")); |
184 |
count++; |
185 |
while (it.hasNext()) {
|
186 |
feature = (Feature) it.next(); |
187 |
v1 = ((String) pfeature.get("NOMBRE")).toLowerCase(); |
188 |
v2 = ((String) feature.get("NOMBRE")).toLowerCase(); |
189 |
pfeature = feature; |
190 |
assertTrue("Short error", (v1.compareTo(v2) >= 0)); |
191 |
System.out.println(pfeature.getString("NOMBRE")); |
192 |
count++; |
193 |
} |
194 |
assertEquals("Iteration error", 9, count); |
195 |
|
196 |
/// CON EDICION
|
197 |
|
198 |
fs.edit(); |
199 |
|
200 |
Feature newFeature = fs.createNewFeature(true);
|
201 |
newFeature.editing(); |
202 |
newFeature.setAttribute("NOMBRE", "BuRjaSOT"); |
203 |
newFeature.setAttribute("TIPO", "MUNICIPIO"); |
204 |
fs.insert(newFeature); |
205 |
|
206 |
try {
|
207 |
fc = (FeatureSet) fs.getDataSet(); |
208 |
} catch (ReadException e1) {
|
209 |
e1.printStackTrace(); |
210 |
fail(); |
211 |
} |
212 |
|
213 |
assertEquals(10, fc.size());
|
214 |
|
215 |
try {
|
216 |
fc = (FeatureSet) fs.getDataCollection(fs |
217 |
.getDefaultFeatureType(), "lower(NOMBRE) like 'b%'",
|
218 |
null);
|
219 |
} catch (ReadException e1) {
|
220 |
e1.printStackTrace(); |
221 |
fail(); |
222 |
} |
223 |
|
224 |
assertEquals(3, fc.size());
|
225 |
|
226 |
it = fc.iterator(); |
227 |
count = 0;
|
228 |
while (it.hasNext()) {
|
229 |
feature = (Feature) it.next(); |
230 |
assertTrue("Filter error", feature.getString("NOMBRE") |
231 |
.toLowerCase().startsWith("b"));
|
232 |
count++; |
233 |
} |
234 |
assertEquals("Iteration error", 3, count); |
235 |
|
236 |
try {
|
237 |
fc = (FeatureSet) fs.getDataCollection(fs |
238 |
.getDefaultFeatureType(), null, "NOMBRE ASC"); |
239 |
} catch (ReadException e1) {
|
240 |
e1.printStackTrace(); |
241 |
fail(); |
242 |
} |
243 |
assertEquals(10, fc.size());
|
244 |
it = fc.iterator(); |
245 |
count = 0;
|
246 |
pfeature = (Feature) it.next(); |
247 |
count++; |
248 |
while (it.hasNext()) {
|
249 |
feature = (Feature) it.next(); |
250 |
v1 = (Comparable) pfeature.get("NOMBRE"); |
251 |
v2 = (Comparable) feature.get("NOMBRE"); |
252 |
pfeature = feature; |
253 |
assertTrue("Short error", (v1.compareTo(v1) <= 0)); |
254 |
count++; |
255 |
} |
256 |
assertEquals("Iteration error", 10, count); |
257 |
|
258 |
try {
|
259 |
fc = (FeatureSet) fs.getDataCollection(fs |
260 |
.getDefaultFeatureType(), null, "NOMBRE DESC"); |
261 |
} catch (ReadException e1) {
|
262 |
e1.printStackTrace(); |
263 |
fail(); |
264 |
} |
265 |
assertEquals(10, fc.size());
|
266 |
it = fc.iterator(); |
267 |
|
268 |
count = 0;
|
269 |
pfeature = (Feature) it.next(); |
270 |
count++; |
271 |
while (it.hasNext()) {
|
272 |
feature = (Feature) it.next(); |
273 |
v1 = (Comparable) pfeature.get("NOMBRE"); |
274 |
v2 = (Comparable) feature.get("NOMBRE"); |
275 |
pfeature = feature; |
276 |
if (v1 != null && v2 != null) { |
277 |
assertTrue("Short error", (v1.compareTo(v1) >= 0)); |
278 |
} |
279 |
count++; |
280 |
} |
281 |
assertEquals("Iteration error", 10, count); |
282 |
|
283 |
try {
|
284 |
fc = (FeatureSet) fs.getDataCollection( |
285 |
new String[] { "NOMBRE" }, "lower(NOMBRE) like 'b%'", |
286 |
"NOMBRE");
|
287 |
} catch (ReadException e1) {
|
288 |
e1.printStackTrace(); |
289 |
fail(); |
290 |
} |
291 |
|
292 |
assertEquals(3, fc.size());
|
293 |
|
294 |
it = fc.iterator(); |
295 |
|
296 |
count = 0;
|
297 |
pfeature = (Feature) it.next(); |
298 |
assertTrue(pfeature.getString("NOMBRE").toLowerCase().startsWith(
|
299 |
"b"));
|
300 |
count++; |
301 |
while (it.hasNext()) {
|
302 |
feature = (Feature) it.next(); |
303 |
if (!(feature instanceof MemoryFeature)) { //FIXME OJO con los featureType de los Feature en memoria |
304 |
assertEquals(1, feature.getType().size());
|
305 |
} |
306 |
assertTrue("Filter error", feature.getString("NOMBRE") |
307 |
.toLowerCase().startsWith("b"));
|
308 |
v1 = (Comparable) pfeature.get("NOMBRE"); |
309 |
v2 = (Comparable) feature.get("NOMBRE"); |
310 |
pfeature = feature; |
311 |
assertTrue("Short error", (v1.compareTo(v1) <= 0)); |
312 |
count++; |
313 |
} |
314 |
assertEquals("Iteration error", 3, count); |
315 |
|
316 |
try {
|
317 |
fc = (FeatureSet) fs.getDataCollection(fs |
318 |
.getDefaultFeatureType(), null,
|
319 |
"Tipo,lower(NOMBRE) Desc");
|
320 |
} catch (ReadException e1) {
|
321 |
e1.printStackTrace(); |
322 |
fail(); |
323 |
} |
324 |
assertEquals(10, fc.size());
|
325 |
it = fc.iterator(); |
326 |
count = 0;
|
327 |
pfeature = (Feature) it.next(); |
328 |
System.out.println(pfeature.getString("TIPO") + "," |
329 |
+ pfeature.getString("NOMBRE"));
|
330 |
count++; |
331 |
while (it.hasNext()) {
|
332 |
feature = (Feature) it.next(); |
333 |
System.out.println(feature.getString("TIPO") + "," |
334 |
+ feature.getString("NOMBRE"));
|
335 |
v1 = ((String) pfeature.get("TIPO")).toLowerCase(); |
336 |
v2 = ((String) feature.get("TIPO")).toLowerCase(); |
337 |
assertTrue("Short error", (v2.compareTo(v1) >= 0)); |
338 |
if (v1.compareTo(v2) == 0) { |
339 |
v1 = ((String) pfeature.get("NOMBRE")).toLowerCase(); |
340 |
v2 = ((String) feature.get("NOMBRE")).toLowerCase(); |
341 |
assertTrue("Short error", (v1.compareTo(v2) >= 0)); |
342 |
} |
343 |
pfeature = feature; |
344 |
|
345 |
// System.out.println(pfeature.getString("TIPO")+","+pfeature.getString("NOMBRE"));
|
346 |
count++; |
347 |
} |
348 |
assertEquals("Iteration error", 10, count); |
349 |
|
350 |
fs.cancelEditing(); |
351 |
|
352 |
// try {
|
353 |
// fs.close();
|
354 |
// } catch (CloseException e) {
|
355 |
// e.printStackTrace();
|
356 |
// fail("Exception:" + e);
|
357 |
// }
|
358 |
|
359 |
System.out.println("======= /DBF =============="); |
360 |
|
361 |
} catch (DataException e) {
|
362 |
e.printStackTrace(); |
363 |
fail(); |
364 |
} |
365 |
|
366 |
} |
367 |
|
368 |
public void testSHP() { |
369 |
try {
|
370 |
System.out.println("======= SHP =============="); |
371 |
org.gvsig.fmap.data.feature.file.shp.Register.selfRegister(); |
372 |
org.gvsig.fmap.data.feature.file.dbf.Register.selfRegister(); |
373 |
|
374 |
DataManager dsm = DefaultDataManager.getManager(); |
375 |
|
376 |
DataStoreParameters dp = dsm |
377 |
.createStoreParameters(SHPStore.DATASTORE_NAME); |
378 |
((SHPStoreParameters) dp).setFile(shpfile); |
379 |
// ((SHPStoreParameters)dp).setSHXFile(SHP.getShxFile(shpfile));
|
380 |
// ((SHPStoreParameters)dp).setDBFFile(SHP.getDbfFile(shpfile));
|
381 |
|
382 |
driverTest(dp, null, null, true); |
383 |
|
384 |
FeatureStore fs = null;
|
385 |
try {
|
386 |
fs = (FeatureStore) dsm.createStore(dp); |
387 |
} catch (InitializeException e) {
|
388 |
e.printStackTrace(); |
389 |
fail("Exception:" + e);
|
390 |
} |
391 |
|
392 |
assertNotNull("Can't create Feature Store", fs);
|
393 |
|
394 |
// fs.open();
|
395 |
|
396 |
Iterator it;
|
397 |
FeatureSet fc; |
398 |
Comparable v1, v2;
|
399 |
Feature feature, pfeature; |
400 |
long count;
|
401 |
|
402 |
fc = (FeatureSet) fs.getDataSet(); |
403 |
|
404 |
assertEquals(9, fc.size());
|
405 |
|
406 |
fc = (FeatureSet) fs.getDataCollection(fs |
407 |
.getDefaultFeatureType(), "lower(NOMBRE) like 'b%'", null); |
408 |
|
409 |
assertEquals(2, fc.size());
|
410 |
|
411 |
it = fc.iterator(); |
412 |
count = 0;
|
413 |
while (it.hasNext()) {
|
414 |
feature = (Feature) it.next(); |
415 |
assertTrue("Filter error", feature.getString("NOMBRE") |
416 |
.toLowerCase().startsWith("b"));
|
417 |
count++; |
418 |
} |
419 |
assertEquals("Iteration error", 2, count); |
420 |
|
421 |
fc = (FeatureSet) fs.getDataCollection(fs |
422 |
.getDefaultFeatureType(), null, "NOMBRE"); |
423 |
assertEquals(9, fc.size());
|
424 |
it = fc.iterator(); |
425 |
count = 0;
|
426 |
pfeature = (Feature) it.next(); |
427 |
count++; |
428 |
while (it.hasNext()) {
|
429 |
feature = (Feature) it.next(); |
430 |
v1 = (Comparable) pfeature.get("NOMBRE"); |
431 |
v2 = (Comparable) feature.get("NOMBRE"); |
432 |
pfeature = feature; |
433 |
assertTrue("Short error", (v1.compareTo(v2) <= 0)); |
434 |
count++; |
435 |
} |
436 |
assertEquals("Iteration error", 9, count); |
437 |
|
438 |
fc = (FeatureSet) fs.getDataCollection(fs |
439 |
.getDefaultFeatureType(), null, "NOMBRE DESC"); |
440 |
assertEquals(9, fc.size());
|
441 |
it = fc.iterator(); |
442 |
count = 0;
|
443 |
pfeature = (Feature) it.next(); |
444 |
System.out.println(pfeature.getString("NOMBRE")); |
445 |
count++; |
446 |
while (it.hasNext()) {
|
447 |
feature = (Feature) it.next(); |
448 |
v1 = (Comparable) pfeature.get("NOMBRE"); |
449 |
v2 = (Comparable) feature.get("NOMBRE"); |
450 |
pfeature = feature; |
451 |
assertTrue("Short error", (v1.compareTo(v2) >= 0)); |
452 |
System.out.println(pfeature.getString("NOMBRE")); |
453 |
count++; |
454 |
} |
455 |
assertEquals("Iteration error", 9, count); |
456 |
|
457 |
fc = (FeatureSet) fs.getDataCollection(fs |
458 |
.getDefaultFeatureType(), "lower(NOMBRE) like 'b%'",
|
459 |
"NOMBRE ASC");
|
460 |
|
461 |
assertEquals(2, fc.size());
|
462 |
|
463 |
it = fc.iterator(); |
464 |
|
465 |
count = 0;
|
466 |
pfeature = (Feature) it.next(); |
467 |
assertTrue(pfeature.getString("NOMBRE").toLowerCase().startsWith(
|
468 |
"b"));
|
469 |
count++; |
470 |
while (it.hasNext()) {
|
471 |
feature = (Feature) it.next(); |
472 |
assertTrue("Filter error", feature.getString("NOMBRE") |
473 |
.toLowerCase().startsWith("b"));
|
474 |
v1 = (Comparable) pfeature.get("NOMBRE"); |
475 |
v2 = (Comparable) feature.get("NOMBRE"); |
476 |
pfeature = feature; |
477 |
assertTrue("Short error", (v1.compareTo(v2) <= 0)); |
478 |
count++; |
479 |
} |
480 |
assertEquals("Iteration error", 2, count); |
481 |
|
482 |
fc = (FeatureSet) fs.getDataCollection(fs |
483 |
.getDefaultFeatureType(), null, "lower(NOMBRE) ASC"); |
484 |
assertEquals(9, fc.size());
|
485 |
it = fc.iterator(); |
486 |
count = 0;
|
487 |
pfeature = (Feature) it.next(); |
488 |
count++; |
489 |
while (it.hasNext()) {
|
490 |
feature = (Feature) it.next(); |
491 |
v1 = ((String) pfeature.get("NOMBRE")).toLowerCase(); |
492 |
v2 = ((String) feature.get("NOMBRE")).toLowerCase(); |
493 |
pfeature = feature; |
494 |
assertTrue("Short error", (v1.compareTo(v2) <= 0)); |
495 |
count++; |
496 |
} |
497 |
assertEquals("Iteration error", 9, count); |
498 |
|
499 |
fc = (FeatureSet) fs.getDataCollection(fs |
500 |
.getDefaultFeatureType(), null, "Tipo,lower(NOMBRE) Desc"); |
501 |
assertEquals(9, fc.size());
|
502 |
it = fc.iterator(); |
503 |
count = 0;
|
504 |
pfeature = (Feature) it.next(); |
505 |
System.out.println(pfeature.getString("NOMBRE")); |
506 |
count++; |
507 |
while (it.hasNext()) {
|
508 |
feature = (Feature) it.next(); |
509 |
v1 = ((String) pfeature.get("NOMBRE")).toLowerCase(); |
510 |
v2 = ((String) feature.get("NOMBRE")).toLowerCase(); |
511 |
pfeature = feature; |
512 |
assertTrue("Short error", (v1.compareTo(v2) >= 0)); |
513 |
System.out.println(pfeature.getString("NOMBRE")); |
514 |
count++; |
515 |
} |
516 |
assertEquals("Iteration error", 9, count); |
517 |
|
518 |
/// CON EDICION
|
519 |
|
520 |
fs.edit(); |
521 |
|
522 |
Feature newFeature = fs.createNewFeature(true);
|
523 |
newFeature.editing(); |
524 |
newFeature.setAttribute("NOMBRE", "BuRjaSOT"); |
525 |
newFeature.setAttribute("TIPO", "MUNICIPIO"); |
526 |
fs.insert(newFeature); |
527 |
|
528 |
try {
|
529 |
fc = (FeatureSet) fs.getDataSet(); |
530 |
} catch (ReadException e1) {
|
531 |
e1.printStackTrace(); |
532 |
fail(); |
533 |
} |
534 |
|
535 |
assertEquals(10, fc.size());
|
536 |
|
537 |
try {
|
538 |
fc = (FeatureSet) fs.getDataCollection(fs |
539 |
.getDefaultFeatureType(), "lower(NOMBRE) like 'b%'",
|
540 |
null);
|
541 |
} catch (ReadException e1) {
|
542 |
e1.printStackTrace(); |
543 |
fail(); |
544 |
} |
545 |
|
546 |
assertEquals(3, fc.size());
|
547 |
|
548 |
it = fc.iterator(); |
549 |
count = 0;
|
550 |
while (it.hasNext()) {
|
551 |
feature = (Feature) it.next(); |
552 |
assertTrue("Filter error", feature.getString("NOMBRE") |
553 |
.toLowerCase().startsWith("b"));
|
554 |
count++; |
555 |
} |
556 |
assertEquals("Iteration error", 3, count); |
557 |
|
558 |
try {
|
559 |
fc = (FeatureSet) fs.getDataCollection(fs |
560 |
.getDefaultFeatureType(), null, "NOMBRE ASC"); |
561 |
} catch (ReadException e1) {
|
562 |
e1.printStackTrace(); |
563 |
fail(); |
564 |
} |
565 |
assertEquals(10, fc.size());
|
566 |
it = fc.iterator(); |
567 |
count = 0;
|
568 |
pfeature = (Feature) it.next(); |
569 |
count++; |
570 |
while (it.hasNext()) {
|
571 |
feature = (Feature) it.next(); |
572 |
v1 = (Comparable) pfeature.get("NOMBRE"); |
573 |
v2 = (Comparable) feature.get("NOMBRE"); |
574 |
pfeature = feature; |
575 |
assertTrue("Short error", (v1.compareTo(v1) <= 0)); |
576 |
count++; |
577 |
} |
578 |
assertEquals("Iteration error", 10, count); |
579 |
|
580 |
try {
|
581 |
fc = (FeatureSet) fs.getDataCollection(fs |
582 |
.getDefaultFeatureType(), null, "NOMBRE DESC"); |
583 |
} catch (ReadException e1) {
|
584 |
e1.printStackTrace(); |
585 |
fail(); |
586 |
} |
587 |
assertEquals(10, fc.size());
|
588 |
it = fc.iterator(); |
589 |
|
590 |
count = 0;
|
591 |
pfeature = (Feature) it.next(); |
592 |
count++; |
593 |
while (it.hasNext()) {
|
594 |
feature = (Feature) it.next(); |
595 |
v1 = (Comparable) pfeature.get("NOMBRE"); |
596 |
v2 = (Comparable) feature.get("NOMBRE"); |
597 |
pfeature = feature; |
598 |
assertTrue("Short error", (v1.compareTo(v1) >= 0)); |
599 |
count++; |
600 |
} |
601 |
assertEquals("Iteration error", 10, count); |
602 |
|
603 |
try {
|
604 |
fc = (FeatureSet) fs.getDataCollection(fs |
605 |
.getDefaultFeatureType(), "lower(NOMBRE) like 'b%'",
|
606 |
"NOMBRE");
|
607 |
} catch (ReadException e1) {
|
608 |
e1.printStackTrace(); |
609 |
fail(); |
610 |
} |
611 |
|
612 |
assertEquals(3, fc.size());
|
613 |
|
614 |
it = fc.iterator(); |
615 |
|
616 |
count = 0;
|
617 |
pfeature = (Feature) it.next(); |
618 |
assertTrue(pfeature.getString("NOMBRE").toLowerCase().startsWith(
|
619 |
"b"));
|
620 |
count++; |
621 |
while (it.hasNext()) {
|
622 |
feature = (Feature) it.next(); |
623 |
assertTrue("Filter error", feature.getString("NOMBRE") |
624 |
.toLowerCase().startsWith("b"));
|
625 |
v1 = (Comparable) pfeature.get("NOMBRE"); |
626 |
v2 = (Comparable) feature.get("NOMBRE"); |
627 |
pfeature = feature; |
628 |
assertTrue("Short error", (v1.compareTo(v1) <= 0)); |
629 |
count++; |
630 |
} |
631 |
assertEquals("Iteration error", 3, count); |
632 |
|
633 |
try {
|
634 |
fc = (FeatureSet) fs.getDataCollection(fs |
635 |
.getDefaultFeatureType(), null,
|
636 |
"Tipo,lower(NOMBRE) Desc");
|
637 |
} catch (ReadException e1) {
|
638 |
e1.printStackTrace(); |
639 |
fail(); |
640 |
} |
641 |
assertEquals(10, fc.size());
|
642 |
it = fc.iterator(); |
643 |
count = 0;
|
644 |
pfeature = (Feature) it.next(); |
645 |
System.out.println(pfeature.getString("NOMBRE")); |
646 |
count++; |
647 |
while (it.hasNext()) {
|
648 |
feature = (Feature) it.next(); |
649 |
v1 = ((String) pfeature.get("NOMBRE")).toLowerCase(); |
650 |
v2 = ((String) feature.get("NOMBRE")).toLowerCase(); |
651 |
pfeature = feature; |
652 |
assertTrue("Short error", (v1.compareTo(v2) >= 0)); |
653 |
System.out.println(pfeature.getString("NOMBRE")); |
654 |
count++; |
655 |
} |
656 |
assertEquals("Iteration error", 10, count); |
657 |
|
658 |
fs.cancelEditing(); |
659 |
|
660 |
// try {
|
661 |
// fs.close();
|
662 |
// } catch (CloseException e) {
|
663 |
// e.printStackTrace();
|
664 |
// fail("Exception:" + e);
|
665 |
// }
|
666 |
|
667 |
System.out.println("======= /SHP =============="); |
668 |
} catch (DataException e) {
|
669 |
// TODO Auto-generated catch block
|
670 |
e.printStackTrace(); |
671 |
} |
672 |
} |
673 |
|
674 |
public void testDXF() { |
675 |
try {
|
676 |
System.out.println("======= DXF =============="); |
677 |
org.gvsig.fmap.data.feature.file.dxf.Register.selfRegister(); |
678 |
|
679 |
DataManager dsm = DefaultDataManager.getManager(); |
680 |
|
681 |
DataStoreParameters dp = dsm |
682 |
.createStoreParameters(DXFStore.DATASTORE_NAME); |
683 |
((DXFStoreParameters) dp).setFile(dxffile); |
684 |
IProjection proj = CRSFactory.getCRS("EPSG:23030");
|
685 |
((DXFStoreParameters) dp).setSRSID("EPSG:23030");
|
686 |
driverTest(dp, null, null, true); |
687 |
|
688 |
FeatureStore fs = null;
|
689 |
try {
|
690 |
fs = (FeatureStore) dsm.createStore(dp); |
691 |
} catch (InitializeException e) {
|
692 |
e.printStackTrace(); |
693 |
fail("Exception:" + e);
|
694 |
} |
695 |
assertNotNull("Can't create Feature Store", fs);
|
696 |
|
697 |
// fs.open();
|
698 |
|
699 |
Iterator it;
|
700 |
FeatureSet fc; |
701 |
Comparable v1, v2;
|
702 |
Feature feature, pfeature; |
703 |
long count;
|
704 |
|
705 |
fc = (FeatureSet) fs.getDataSet(); |
706 |
|
707 |
assertEquals(10, fc.size());
|
708 |
|
709 |
fc = (FeatureSet) fs.getDataCollection(fs |
710 |
.getDefaultFeatureType(), "Color > '7'", null); |
711 |
|
712 |
assertEquals(2, fc.size());
|
713 |
|
714 |
it = fc.iterator(); |
715 |
count = 0;
|
716 |
while (it.hasNext()) {
|
717 |
feature = (Feature) it.next(); |
718 |
assertTrue("Filter error", feature.getInt("Color") > 7); |
719 |
count++; |
720 |
} |
721 |
assertEquals("Iteration error", 2, count); |
722 |
|
723 |
fc = (FeatureSet) fs.getDataCollection(fs |
724 |
.getDefaultFeatureType(), null, "Layer ASC"); |
725 |
assertEquals(10, fc.size());
|
726 |
it = fc.iterator(); |
727 |
count = 0;
|
728 |
pfeature = (Feature) it.next(); |
729 |
count++; |
730 |
while (it.hasNext()) {
|
731 |
feature = (Feature) it.next(); |
732 |
v1 = (Comparable) pfeature.get("Layer"); |
733 |
v2 = (Comparable) feature.get("Layer"); |
734 |
pfeature = feature; |
735 |
assertTrue("Short error", (v1.compareTo(v1) <= 0)); |
736 |
count++; |
737 |
} |
738 |
assertEquals("Iteration error", 10, count); |
739 |
|
740 |
fc = (FeatureSet) fs.getDataCollection(fs |
741 |
.getDefaultFeatureType(), null, "Layer DESC"); |
742 |
assertEquals(10, fc.size());
|
743 |
it = fc.iterator(); |
744 |
|
745 |
count = 0;
|
746 |
pfeature = (Feature) it.next(); |
747 |
count++; |
748 |
while (it.hasNext()) {
|
749 |
feature = (Feature) it.next(); |
750 |
v1 = (Comparable) pfeature.get("Layer"); |
751 |
v2 = (Comparable) feature.get("Layer"); |
752 |
pfeature = feature; |
753 |
assertTrue("Short error", (v1.compareTo(v1) >= 0)); |
754 |
count++; |
755 |
} |
756 |
assertEquals("Iteration error", 10, count); |
757 |
|
758 |
fc = (FeatureSet) fs.getDataCollection(fs |
759 |
.getDefaultFeatureType(), "Color > '7'", "Layer ASC"); |
760 |
|
761 |
assertEquals(2, fc.size());
|
762 |
|
763 |
it = fc.iterator(); |
764 |
|
765 |
count = 0;
|
766 |
pfeature = (Feature) it.next(); |
767 |
assertTrue(pfeature.getInt("Color") > 7); |
768 |
count++; |
769 |
while (it.hasNext()) {
|
770 |
feature = (Feature) it.next(); |
771 |
assertTrue("Filter error", feature.getInt("Color") > 7); |
772 |
v1 = (Comparable) pfeature.get("Color"); |
773 |
v2 = (Comparable) feature.get("Color"); |
774 |
pfeature = feature; |
775 |
Integer i1 = (Integer) v1; |
776 |
Integer i2 = (Integer) v2; |
777 |
assertTrue("Short error", (i1.intValue() > i2.intValue()));
|
778 |
count++; |
779 |
} |
780 |
assertEquals("Iteration error", 2, count); |
781 |
|
782 |
// fc = (FeatureCollection)fs.getDataCollection(null,null,"Color,Layer Desc");
|
783 |
// assertEquals(10, fc.size());
|
784 |
// it = fc.iterator();
|
785 |
// count=0;
|
786 |
// pfeature = (Feature)it.next();
|
787 |
// System.out.println(pfeature.getString("Layer"));
|
788 |
// count++;
|
789 |
// while (it.hasNext()){
|
790 |
// feature = (Feature)it.next();
|
791 |
// v1 = (Comparable)((String)pfeature.get("Layer")).toLowerCase();
|
792 |
// v2 = (Comparable)((String)feature.get("Layer")).toLowerCase();
|
793 |
// pfeature=feature;
|
794 |
// assertTrue("Short error", (v1.compareTo(v2) >= 0));
|
795 |
// System.out.println(pfeature.getString("Layer"));
|
796 |
// count++;
|
797 |
// }
|
798 |
// assertEquals("Iteration error",10,count);
|
799 |
|
800 |
// try {
|
801 |
// fs.close();
|
802 |
// } catch (CloseException e) {
|
803 |
// e.printStackTrace();
|
804 |
// fail("Exception:" + e);
|
805 |
// }
|
806 |
|
807 |
System.out.println("======= /DXF =============="); |
808 |
} catch (OpenException e1) {
|
809 |
e1.printStackTrace(); |
810 |
fail(); |
811 |
} catch (ReadException e) {
|
812 |
// TODO Auto-generated catch block
|
813 |
e.printStackTrace(); |
814 |
} |
815 |
} |
816 |
|
817 |
public void testDGN() { |
818 |
try {
|
819 |
System.out.println("======= DGN =============="); |
820 |
org.gvsig.fmap.data.feature.file.dgn.Register.selfRegister(); |
821 |
|
822 |
DataManager dsm = DefaultDataManager.getManager(); |
823 |
|
824 |
DataStoreParameters dp = dsm |
825 |
.createStoreParameters(DGNStore.DATASTORE_NAME); |
826 |
((DGNStoreParameters) dp).setFile(dgnfile); |
827 |
driverTest(dp, null, null, true); |
828 |
FeatureStore fs = null;
|
829 |
try {
|
830 |
fs = (FeatureStore) dsm.createStore(dp); |
831 |
} catch (InitializeException e) {
|
832 |
e.printStackTrace(); |
833 |
fail("Exception:" + e);
|
834 |
} |
835 |
assertNotNull("Can't create Feature Store", fs);
|
836 |
|
837 |
// fs.open();
|
838 |
|
839 |
Iterator it;
|
840 |
FeatureSet fc; |
841 |
Comparable v1, v2;
|
842 |
Feature feature, pfeature; |
843 |
long count;
|
844 |
|
845 |
fc = (FeatureSet) fs.getDataSet(); |
846 |
|
847 |
assertEquals(57, fc.size());
|
848 |
|
849 |
fc = (FeatureSet) fs.getDataCollection(fs |
850 |
.getDefaultFeatureType(), "Color > '7'", null); |
851 |
|
852 |
assertEquals(45, fc.size());
|
853 |
|
854 |
it = fc.iterator(); |
855 |
count = 0;
|
856 |
while (it.hasNext()) {
|
857 |
feature = (Feature) it.next(); |
858 |
assertTrue("Filter error", feature.getInt("Color") >= 7); |
859 |
count++; |
860 |
} |
861 |
assertEquals("Iteration error", 45, count); |
862 |
|
863 |
fc = (FeatureSet) fs.getDataCollection(fs |
864 |
.getDefaultFeatureType(), null, "Layer ASC"); |
865 |
assertEquals(57, fc.size());
|
866 |
it = fc.iterator(); |
867 |
count = 0;
|
868 |
pfeature = (Feature) it.next(); |
869 |
count++; |
870 |
while (it.hasNext()) {
|
871 |
feature = (Feature) it.next(); |
872 |
v1 = (Comparable) pfeature.get("Layer"); |
873 |
v2 = (Comparable) feature.get("Layer"); |
874 |
if (v2 == null) { |
875 |
System.out.println("null"); |
876 |
} |
877 |
assertTrue("Short error", (v1.compareTo(v2) <= 0)); |
878 |
pfeature = feature; |
879 |
count++; |
880 |
} |
881 |
assertEquals("Iteration error", 57, count); |
882 |
|
883 |
fc = (FeatureSet) fs.getDataCollection(fs |
884 |
.getDefaultFeatureType(), null, "Layer DESC"); |
885 |
assertEquals(57, fc.size());
|
886 |
it = fc.iterator(); |
887 |
|
888 |
count = 0;
|
889 |
pfeature = (Feature) it.next(); |
890 |
count++; |
891 |
while (it.hasNext()) {
|
892 |
feature = (Feature) it.next(); |
893 |
v1 = (Comparable) pfeature.get("Layer"); |
894 |
v2 = (Comparable) feature.get("Layer"); |
895 |
assertTrue("Short error", (v1.compareTo(v2) >= 0)); |
896 |
pfeature = feature; |
897 |
count++; |
898 |
} |
899 |
assertEquals("Iteration error", 57, count); |
900 |
|
901 |
fc = (FeatureSet) fs.getDataCollection(fs |
902 |
.getDefaultFeatureType(), "Color > '7'", "Layer ASC"); |
903 |
|
904 |
assertEquals(45, fc.size());
|
905 |
|
906 |
it = fc.iterator(); |
907 |
|
908 |
count = 0;
|
909 |
pfeature = (Feature) it.next(); |
910 |
assertTrue(pfeature.getInt("Color") > 7); |
911 |
count++; |
912 |
while (it.hasNext()) {
|
913 |
feature = (Feature) it.next(); |
914 |
assertTrue("Filter error", feature.getInt("Color") > 7); |
915 |
v1 = (Comparable) pfeature.get("Layer"); |
916 |
v2 = (Comparable) feature.get("Layer"); |
917 |
pfeature = feature; |
918 |
Integer i1 = (Integer) v1; |
919 |
Integer i2 = (Integer) v2; |
920 |
assertTrue("Short error", (i1.intValue() <= i2.intValue()));
|
921 |
count++; |
922 |
} |
923 |
assertEquals("Iteration error", 45, count); |
924 |
|
925 |
// fc = (FeatureCollection)fs.getDataCollection(null,null,"Color,Layer Desc");
|
926 |
// assertEquals(10, fc.size());
|
927 |
// it = fc.iterator();
|
928 |
// count=0;
|
929 |
// pfeature = (Feature)it.next();
|
930 |
// System.out.println(pfeature.getString("Layer"));
|
931 |
// count++;
|
932 |
// while (it.hasNext()){
|
933 |
// feature = (Feature)it.next();
|
934 |
// v1 = (Comparable)((String)pfeature.get("Layer")).toLowerCase();
|
935 |
// v2 = (Comparable)((String)feature.get("Layer")).toLowerCase();
|
936 |
// pfeature=feature;
|
937 |
// assertTrue("Short error", (v1.compareTo(v2) >= 0));
|
938 |
// System.out.println(pfeature.getString("Layer"));
|
939 |
// count++;
|
940 |
// }
|
941 |
// assertEquals("Iteration error",10,count);
|
942 |
|
943 |
// try {
|
944 |
// fs.close();
|
945 |
// } catch (CloseException e) {
|
946 |
// e.printStackTrace();
|
947 |
// fail("Exception:" + e);
|
948 |
// }
|
949 |
|
950 |
System.out.println("======= /DGN =============="); |
951 |
} catch (OpenException e1) {
|
952 |
e1.printStackTrace(); |
953 |
fail(); |
954 |
} catch (ReadException e) {
|
955 |
// TODO Auto-generated catch block
|
956 |
e.printStackTrace(); |
957 |
} |
958 |
} |
959 |
|
960 |
// private FeatureStore createFeatureStore(IDriverParameters dp){
|
961 |
// DataSourceManager dsm=DataSourceManager.getManager();
|
962 |
|
963 |
// DataStoreParameters dsp=dsm.createDataStoreParameters(DriverStore.DATASTORE_NAME);
|
964 |
|
965 |
// ((IDriverStoreParameters)dsp).setDriverParameters(dp);
|
966 |
// FeatureStore fs=null;
|
967 |
// try {
|
968 |
// fs = (FeatureStore)dsm.createDataStore(dsp);
|
969 |
// } catch (InitializeException e) {
|
970 |
// e.printStackTrace();
|
971 |
// fail("Exception:" + e);
|
972 |
// }
|
973 |
// return fs;
|
974 |
|
975 |
// }
|
976 |
|
977 |
private void driverTest(DataStoreParameters dp, String filter, |
978 |
String order, boolean testEdit) { |
979 |
DataManager dsm = DefaultDataManager.getManager(); |
980 |
|
981 |
|
982 |
// DataStoreParameters dsp=dsm.createDataStoreParameters(DriverStore.DATASTORE_NAME);
|
983 |
|
984 |
// ((IDriverStoreParameters)dsp).setDriverParameters(dp);
|
985 |
// FeatureStore fs=createFeatureStore(dp);
|
986 |
FeatureStore fs = null;
|
987 |
try {
|
988 |
fs = (FeatureStore) dsm.createStore(dp); |
989 |
} catch (InitializeException e) {
|
990 |
e.printStackTrace(); |
991 |
fail("Exception:" + e);
|
992 |
} |
993 |
// try {
|
994 |
// fs.open();
|
995 |
// } catch (OpenException e2) {
|
996 |
// e2.printStackTrace();
|
997 |
// fail();
|
998 |
// }
|
999 |
|
1000 |
if (fs.allowWrite() && testEdit) {
|
1001 |
try {
|
1002 |
fs.edit(); |
1003 |
if (fs.canAlterFeatureType()) {
|
1004 |
FeatureType featureType = fs.getDefaultFeatureType(); |
1005 |
// DefaultAttributeDescriptor dad=new DefaultAttributeDescriptor();
|
1006 |
// dad.loading();
|
1007 |
// dad.setName("Prueba1");
|
1008 |
// dad.setDefaultValue("11p");
|
1009 |
// dad.setSize(10);
|
1010 |
// dad.setType(FeatureAttributeDescriptor.TYPE_STRING);
|
1011 |
// dad.stopLoading();
|
1012 |
// fs.insert(dad);
|
1013 |
// fs.delete((FeatureAttributeDescriptor)featureType.get(featureType.getFieldIndex("Prueba1")));
|
1014 |
AttributeDescriptor dad2 = (AttributeDescriptor) featureType |
1015 |
.get(featureType.getIndex("TIPO"));
|
1016 |
dad2.editing(); |
1017 |
// dad2.setName("TIPO");
|
1018 |
dad2.setDefaultValue("Tipop");
|
1019 |
// dad2.setSize(10);
|
1020 |
dad2.setType(FeatureAttributeDescriptor.STRING); |
1021 |
fs.update(dad2); |
1022 |
} |
1023 |
} catch (DataException e) {
|
1024 |
e.printStackTrace(); |
1025 |
fail(); |
1026 |
} |
1027 |
try {
|
1028 |
Feature feature1 = fs.createNewFeature(false);
|
1029 |
|
1030 |
Feature feature2 = fs.createNewFeature(false);
|
1031 |
Feature feature3 = fs.createNewFeature(false);
|
1032 |
|
1033 |
fs.insert(feature1); |
1034 |
fs.insert(feature2); |
1035 |
|
1036 |
feature1.editing(); |
1037 |
feature1.setAttribute(1, "hola"); |
1038 |
|
1039 |
fs.update(feature1); |
1040 |
fs.delete(feature3); |
1041 |
fs.delete(feature2); |
1042 |
} catch (DataException e) {
|
1043 |
// TODO Auto-generated catch block
|
1044 |
e.printStackTrace(); |
1045 |
} |
1046 |
} |
1047 |
|
1048 |
//Mostrar por consola todos los registros.
|
1049 |
FeatureType ft = fs.getDefaultFeatureType(); |
1050 |
FeatureSet featureCollection = null;
|
1051 |
// featureCollection = (FeatureCollection)fs.getDataCollection();
|
1052 |
// featureCollection = (FeatureCollection)fs.getDataCollection(ft,"NOMBRE = 'CALPE'",null);
|
1053 |
// featureCollection = (FeatureCollection)fs.getDataCollection(ft,"AREA > 3.2213163729E7 and AREA < 3.2213163749E7",null);
|
1054 |
try {
|
1055 |
featureCollection = (FeatureSet) fs.getDataCollection(ft, |
1056 |
filter, order); |
1057 |
} catch (ReadException e2) {
|
1058 |
// TODO Auto-generated catch block
|
1059 |
e2.printStackTrace(); |
1060 |
} |
1061 |
|
1062 |
PrintlnFeaturesVisitor visitor = new PrintlnFeaturesVisitor(ft);
|
1063 |
try {
|
1064 |
featureCollection.accept(visitor); |
1065 |
} catch (BaseException e1) {
|
1066 |
e1.printStackTrace(); |
1067 |
fail("Exception: " + e1);
|
1068 |
} |
1069 |
|
1070 |
featureCollection.dispose(); |
1071 |
|
1072 |
if (fs.allowWrite() && testEdit) {
|
1073 |
try {
|
1074 |
fs.finishEditing(); |
1075 |
} catch (WriteException e) {
|
1076 |
e.printStackTrace(); |
1077 |
fail("Exception: " + e);
|
1078 |
} catch (ReadException e) {
|
1079 |
e.printStackTrace(); |
1080 |
fail("Exception: " + e);
|
1081 |
} |
1082 |
long t1 = System.currentTimeMillis(); |
1083 |
while (System.currentTimeMillis() - t1 > 2000) { |
1084 |
//Esperamos 2 seg para no tener problemas con
|
1085 |
// los lastUpdate de los ficheros
|
1086 |
} |
1087 |
} |
1088 |
// try {
|
1089 |
// fs.close();
|
1090 |
// } catch (CloseException e) {
|
1091 |
// e.printStackTrace();
|
1092 |
// fail("Exception: "+e);
|
1093 |
// }
|
1094 |
try {
|
1095 |
fs.dispose(); |
1096 |
} catch (CloseException e) {
|
1097 |
// TODO Auto-generated catch block
|
1098 |
e.printStackTrace(); |
1099 |
} |
1100 |
persistenceTest(dp); |
1101 |
mapParamsBaseTest(dp); |
1102 |
|
1103 |
} |
1104 |
|
1105 |
public static void doFileResourceTest(FileStoreParameters params) { |
1106 |
doFileResourceTest(params, true);
|
1107 |
} |
1108 |
|
1109 |
public static void doFileResourceTest(FileStoreParameters params, |
1110 |
boolean testEdit) {
|
1111 |
DataManager manager = DefaultDataManager.getManager(); |
1112 |
|
1113 |
ResourceManager resMan = DefaultResourceManager.getResourceManager(); |
1114 |
|
1115 |
AbstractFeatureStore store = null;
|
1116 |
FeatureStore store2 = null;
|
1117 |
FeatureStore store3 = null;
|
1118 |
try {
|
1119 |
store = (AbstractFeatureStore) manager.createStore(params); |
1120 |
store2 = (FeatureStore) manager.createStore(params); |
1121 |
store3 = (FeatureStore) manager.createStore(params); |
1122 |
} catch (InitializeException e1) {
|
1123 |
e1.printStackTrace(); |
1124 |
fail(); |
1125 |
} |
1126 |
|
1127 |
AbstractResource res = getResource(params); |
1128 |
|
1129 |
assertNotNull(res); |
1130 |
|
1131 |
assertEquals(3, res.getRefencesCount());
|
1132 |
|
1133 |
try {
|
1134 |
store.close(); |
1135 |
} catch (CloseException e1) {
|
1136 |
e1.printStackTrace(); |
1137 |
fail(); |
1138 |
} |
1139 |
|
1140 |
assertEquals(false, res.isOpen());
|
1141 |
|
1142 |
DataSet coll = null;
|
1143 |
|
1144 |
try {
|
1145 |
coll = store.getDataSet(); |
1146 |
} catch (ReadException e1) {
|
1147 |
e1.printStackTrace(); |
1148 |
fail(); |
1149 |
} |
1150 |
|
1151 |
coll.iterator().next(); |
1152 |
// coll.size();
|
1153 |
|
1154 |
assertEquals(true, res.isOpen());
|
1155 |
|
1156 |
coll.dispose(); |
1157 |
|
1158 |
if (store.allowWrite() && testEdit) {
|
1159 |
/* Test edition notification */
|
1160 |
|
1161 |
int fCountOrg = 0; |
1162 |
int fCountFin = 0; |
1163 |
try {
|
1164 |
fCountOrg = store.getDataSet().size(); |
1165 |
} catch (ReadException e2) {
|
1166 |
// TODO Auto-generated catch block
|
1167 |
e2.printStackTrace(); |
1168 |
fail("Exception: " + e2);
|
1169 |
} |
1170 |
try {
|
1171 |
store.edit(); |
1172 |
} catch (ReadException e1) {
|
1173 |
e1.printStackTrace(); |
1174 |
fail("Exception: " + e1);
|
1175 |
} |
1176 |
|
1177 |
try {
|
1178 |
coll = store2.getDataSet(); |
1179 |
} catch (ReadException e1) {
|
1180 |
e1.printStackTrace(); |
1181 |
fail("Exception: " + e1);
|
1182 |
} |
1183 |
|
1184 |
try {
|
1185 |
store.finishEditing(); |
1186 |
} catch (WriteException e1) {
|
1187 |
e1.printStackTrace(); |
1188 |
fail("Exception: " + e1);
|
1189 |
} catch (ReadException e1) {
|
1190 |
e1.printStackTrace(); |
1191 |
fail("Exception: " + e1);
|
1192 |
} |
1193 |
|
1194 |
try {
|
1195 |
fCountFin = store.getDataSet().size(); |
1196 |
} catch (ReadException e2) {
|
1197 |
// TODO Auto-generated catch block
|
1198 |
e2.printStackTrace(); |
1199 |
fail("Exception: " + e2);
|
1200 |
} |
1201 |
|
1202 |
assertEquals(fCountOrg, fCountFin); |
1203 |
|
1204 |
boolean isOk = false; |
1205 |
try {
|
1206 |
coll.iterator().next(); |
1207 |
} catch (Exception e) { |
1208 |
isOk = true;
|
1209 |
} |
1210 |
assertTrue("Resource Changed Notification fails", isOk);
|
1211 |
|
1212 |
coll.dispose(); |
1213 |
|
1214 |
long time = System.currentTimeMillis(); |
1215 |
while ((System.currentTimeMillis() - time) < 1000) { |
1216 |
//sleep
|
1217 |
} |
1218 |
|
1219 |
((FileResource) res).getFile().setLastModified( |
1220 |
System.currentTimeMillis());
|
1221 |
|
1222 |
isOk = false;
|
1223 |
try {
|
1224 |
store.getDataSet().iterator().next(); |
1225 |
} catch (Exception e) { |
1226 |
isOk = true;
|
1227 |
} |
1228 |
assertTrue("Resource Changed Notification fails", isOk);
|
1229 |
|
1230 |
/* Test edition notification END */
|
1231 |
|
1232 |
} |
1233 |
|
1234 |
try {
|
1235 |
store3.dispose(); |
1236 |
} catch (CloseException e1) {
|
1237 |
e1.printStackTrace(); |
1238 |
fail(); |
1239 |
} |
1240 |
|
1241 |
assertEquals(2, res.getRefencesCount());
|
1242 |
|
1243 |
try {
|
1244 |
store2.dispose(); |
1245 |
} catch (CloseException e1) {
|
1246 |
e1.printStackTrace(); |
1247 |
fail(); |
1248 |
} |
1249 |
|
1250 |
assertEquals(1, res.getRefencesCount());
|
1251 |
|
1252 |
store = null;
|
1253 |
System.gc();
|
1254 |
System.gc();
|
1255 |
System.gc();
|
1256 |
|
1257 |
assertEquals(0, res.getRefencesCount());
|
1258 |
try {
|
1259 |
resMan.collectDeadResources(); |
1260 |
} catch (DataException e) {
|
1261 |
e.printStackTrace(); |
1262 |
fail(); |
1263 |
return;
|
1264 |
} |
1265 |
res = getResource(params); |
1266 |
|
1267 |
assertNull(res); |
1268 |
|
1269 |
doPrepareFileResourceTest(params); |
1270 |
|
1271 |
} |
1272 |
|
1273 |
public static void doPrepareFileResourceTest(FileStoreParameters params) { |
1274 |
DataManager manager = DefaultDataManager.getManager(); |
1275 |
|
1276 |
DefaultResourceManager resMan = DefaultResourceManager.getResourceManager(); |
1277 |
|
1278 |
File originalFile = params.getFile();
|
1279 |
params.setFile(new File(originalFile.getName())); |
1280 |
|
1281 |
Observer obs = new PrepareResourceObserver(originalFile); |
1282 |
|
1283 |
resMan.addObserver(obs); |
1284 |
|
1285 |
AbstractFeatureStore store = null;
|
1286 |
try {
|
1287 |
store = (AbstractFeatureStore) manager.createStore(params); |
1288 |
} catch (InitializeException e1) {
|
1289 |
e1.printStackTrace(); |
1290 |
fail(); |
1291 |
} catch (Exception e2) { |
1292 |
e2.printStackTrace(); |
1293 |
fail(); |
1294 |
} |
1295 |
|
1296 |
try {
|
1297 |
store.getDataSet().iterator().next(); |
1298 |
} catch (ReadException e) {
|
1299 |
// TODO Auto-generated catch block
|
1300 |
e.printStackTrace(); |
1301 |
fail(); |
1302 |
} |
1303 |
|
1304 |
try {
|
1305 |
store.close(); |
1306 |
} catch (CloseException e) {
|
1307 |
// TODO Auto-generated catch block
|
1308 |
e.printStackTrace(); |
1309 |
fail(); |
1310 |
} |
1311 |
try {
|
1312 |
store.dispose(); |
1313 |
} catch (CloseException e) {
|
1314 |
// TODO Auto-generated catch block
|
1315 |
e.printStackTrace(); |
1316 |
fail(); |
1317 |
} |
1318 |
|
1319 |
} |
1320 |
|
1321 |
private void persistenceTest(DataStoreParameters params) { |
1322 |
DataManager manager = DefaultDataManager.getManager(); |
1323 |
|
1324 |
DataStoreParameters params2 = null;
|
1325 |
XMLEntity paramsXML = null;
|
1326 |
XMLEntity params2XML = null;
|
1327 |
|
1328 |
paramsXML = params.getXMLEntity(); |
1329 |
|
1330 |
try {
|
1331 |
params2 = manager.createStoreParameters(paramsXML); |
1332 |
} catch (InitializeException e) {
|
1333 |
e.printStackTrace(); |
1334 |
fail(); |
1335 |
} |
1336 |
|
1337 |
params2XML = params2.getXMLEntity(); |
1338 |
|
1339 |
assertEquals(paramsXML.toString(), params2XML.toString()); |
1340 |
|
1341 |
FeatureStore store = null;
|
1342 |
XMLEntity storeXML = null;
|
1343 |
FeatureStore store2 = null;
|
1344 |
XMLEntity store2XML = null;
|
1345 |
|
1346 |
try {
|
1347 |
store = (FeatureStore) manager.createStore(params); |
1348 |
} catch (InitializeException e) {
|
1349 |
e.printStackTrace(); |
1350 |
fail(); |
1351 |
} |
1352 |
|
1353 |
try {
|
1354 |
storeXML = store.getXMLEntity(); |
1355 |
} catch (XMLException e1) {
|
1356 |
e1.printStackTrace(); |
1357 |
fail(); |
1358 |
} |
1359 |
|
1360 |
assertNotNull(storeXML); |
1361 |
|
1362 |
try {
|
1363 |
store2 = (FeatureStore) manager.createStore(storeXML); |
1364 |
} catch (InitializeException e) {
|
1365 |
e.printStackTrace(); |
1366 |
fail(); |
1367 |
} |
1368 |
|
1369 |
try {
|
1370 |
store2XML = store2.getXMLEntity(); |
1371 |
} catch (XMLException e1) {
|
1372 |
e1.printStackTrace(); |
1373 |
fail(); |
1374 |
} |
1375 |
|
1376 |
assertEquals(storeXML.toString(), store2XML.toString()); |
1377 |
|
1378 |
FeatureSet coll = null;
|
1379 |
FeatureSet coll2 = null;
|
1380 |
Iterator iter = null; |
1381 |
Iterator iter2 = null; |
1382 |
Feature feature=null;
|
1383 |
Feature feature2 = null;
|
1384 |
Comparable v1 = null; |
1385 |
Comparable v2 = null; |
1386 |
Handler h = null; |
1387 |
Handler h2 = null; |
1388 |
|
1389 |
|
1390 |
try {
|
1391 |
coll = (FeatureSet) store.getDataSet(); |
1392 |
coll2 = (FeatureSet) store2.getDataSet(); |
1393 |
} catch (ReadException e) {
|
1394 |
e.printStackTrace(); |
1395 |
fail(); |
1396 |
} |
1397 |
|
1398 |
assertEquals(coll.size(), coll2.size()); |
1399 |
|
1400 |
iter = coll.iterator(); |
1401 |
iter2 = coll2.iterator(); |
1402 |
int i;
|
1403 |
int featureSize = store.getDefaultFeatureType().size();
|
1404 |
|
1405 |
try {
|
1406 |
while (iter.hasNext()) {
|
1407 |
feature = (Feature) iter.next(); |
1408 |
feature2 = (Feature) iter2.next(); |
1409 |
for (i = 0; i < featureSize; i++) { |
1410 |
v1 = (Comparable) feature.get(i);
|
1411 |
v2 = (Comparable) feature2.get(i);
|
1412 |
if (v1 == null) { |
1413 |
assertNull(v2); |
1414 |
} else if (v1 instanceof Geometry) { |
1415 |
assertTrue(v2 instanceof Geometry);
|
1416 |
assertEquals(((Geometry) v1).getType(), ((Geometry) v2) |
1417 |
.getType()); |
1418 |
assertEquals( |
1419 |
(((Geometry) v1) |
1420 |
.getHandlers(Geometry.SELECTHANDLER)).length, |
1421 |
(((Geometry) v2) |
1422 |
.getHandlers(Geometry.SELECTHANDLER)).length); |
1423 |
h = (((Geometry) v1) |
1424 |
.getHandlers(Geometry.SELECTHANDLER))[0];
|
1425 |
h2 = (((Geometry) v2) |
1426 |
.getHandlers(Geometry.SELECTHANDLER))[0];
|
1427 |
|
1428 |
assertEquals(0.0, h.getPoint().distance(h2.getPoint()),
|
1429 |
0.000001);
|
1430 |
|
1431 |
h = (((Geometry) v1) |
1432 |
.getHandlers(Geometry.SELECTHANDLER))[(((Geometry) v1) |
1433 |
.getHandlers(Geometry.SELECTHANDLER)).length - 1];
|
1434 |
h2 = (((Geometry) v2) |
1435 |
.getHandlers(Geometry.SELECTHANDLER))[(((Geometry) v2) |
1436 |
.getHandlers(Geometry.SELECTHANDLER)).length - 1];
|
1437 |
|
1438 |
assertEquals(0.0, h.getPoint().distance(h2.getPoint()),
|
1439 |
0.000001);
|
1440 |
|
1441 |
} else {
|
1442 |
assertEquals(0, v1.compareTo(v2));
|
1443 |
} |
1444 |
} |
1445 |
|
1446 |
|
1447 |
} |
1448 |
} catch (Exception e) { |
1449 |
e.printStackTrace(); |
1450 |
fail(); |
1451 |
} |
1452 |
|
1453 |
coll.dispose(); |
1454 |
coll2.dispose(); |
1455 |
|
1456 |
try {
|
1457 |
store.dispose(); |
1458 |
store2.dispose(); |
1459 |
} catch (CloseException e) {
|
1460 |
e.printStackTrace(); |
1461 |
fail(); |
1462 |
} |
1463 |
|
1464 |
} |
1465 |
|
1466 |
public void cloneFeatureTypeTest() { |
1467 |
|
1468 |
org.gvsig.fmap.data.feature.file.dbf.Register.selfRegister(); |
1469 |
|
1470 |
DataManager dsm = DefaultDataManager.getManager(); |
1471 |
|
1472 |
DataStoreParameters dp; |
1473 |
try {
|
1474 |
dp = dsm.createStoreParameters(DBFStore.DATASTORE_NAME); |
1475 |
} catch (InitializeException e1) {
|
1476 |
e1.printStackTrace(); |
1477 |
fail(); |
1478 |
return;
|
1479 |
} |
1480 |
((DBFStoreParameters) dp).setFile(dbffile); |
1481 |
|
1482 |
FeatureStore fs = null;
|
1483 |
try {
|
1484 |
fs = (FeatureStore) dsm.createStore(dp); |
1485 |
} catch (InitializeException e) {
|
1486 |
e.printStackTrace(); |
1487 |
fail("Exception:" + e);
|
1488 |
return;
|
1489 |
} |
1490 |
assertNotNull("Can't create Feature Store", fs);
|
1491 |
|
1492 |
compareFeatureType(fs.getDefaultFeatureType(), fs |
1493 |
.getDefaultFeatureType() |
1494 |
.getCopy()); |
1495 |
|
1496 |
FeatureSet fc = null;
|
1497 |
|
1498 |
try {
|
1499 |
fc = (FeatureSet) fs.getDataSet(); |
1500 |
} catch (ReadException e1) {
|
1501 |
e1.printStackTrace(); |
1502 |
fail(); |
1503 |
return;
|
1504 |
} |
1505 |
|
1506 |
|
1507 |
compareFeatureType(fs.getDefaultFeatureType(), fc.getDefaultFeatureType()); |
1508 |
|
1509 |
|
1510 |
|
1511 |
fc.dispose(); |
1512 |
|
1513 |
try {
|
1514 |
fs.addEvaluatedAttribute(null, "eva1", |
1515 |
FeatureAttributeDescriptor.STRING, "lower(' ')", null); |
1516 |
} catch (ReadException e1) {
|
1517 |
e1.printStackTrace(); |
1518 |
fail(); |
1519 |
return;
|
1520 |
} |
1521 |
|
1522 |
|
1523 |
compareFeatureType(fs.getDefaultFeatureType(), fs |
1524 |
.getDefaultFeatureType().getCopy()); |
1525 |
|
1526 |
try {
|
1527 |
fs.dispose(); |
1528 |
} catch (CloseException e) {
|
1529 |
e.printStackTrace(); |
1530 |
fail(); |
1531 |
return;
|
1532 |
} |
1533 |
} |
1534 |
|
1535 |
private void compareFeatureType(FeatureType original, FeatureType copy) { |
1536 |
AttributeDescriptor attr1, attr2; |
1537 |
|
1538 |
assertEquals(original.size(), copy.size()); |
1539 |
assertEquals(original.getDefaultGeometryAttributeName(), copy.getDefaultGeometryAttributeName()); |
1540 |
|
1541 |
int i = 0; |
1542 |
|
1543 |
for (i = 0; i < original.size(); i++) { |
1544 |
attr1 = (AttributeDescriptor) original.get(i); |
1545 |
attr2 = (AttributeDescriptor) copy.get(i); |
1546 |
|
1547 |
assertEquals(attr1.getName(), attr2.getName()); |
1548 |
assertEquals(attr1.getDataType(), attr2.getDataType()); |
1549 |
assertEquals(attr1.getDefaultValue(), attr2.getDefaultValue()); |
1550 |
assertEquals(attr1.ordinal(), attr2.ordinal()); |
1551 |
assertEquals(attr1.originalPosition(), attr2.originalPosition()); |
1552 |
assertEquals(attr1.isEvaluated(), attr2.isEvaluated()); |
1553 |
assertEquals(attr1.getGeometryType(), attr2.getGeometryType()); |
1554 |
assertEquals(attr1.getSize(), attr2.getSize()); |
1555 |
assertEquals(attr1.getSRS(), attr2.getSRS()); |
1556 |
assertEquals(attr1.getPrecision(), attr2.getPrecision()); |
1557 |
assertEquals(attr1.getExpression(), attr2.getExpression()); |
1558 |
assertEquals(attr1.isAllowNull(), attr2.isAllowNull()); |
1559 |
assertEquals(attr1.isPrimaryKey(), attr2.isPrimaryKey()); |
1560 |
assertEquals(attr1.isReadOnly(), attr2.isReadOnly()); |
1561 |
} |
1562 |
|
1563 |
} |
1564 |
|
1565 |
protected static AbstractResource getResource(FileStoreParameters params) { |
1566 |
AbstractResource tmpRes = null;
|
1567 |
Object obj = null; |
1568 |
|
1569 |
ResourceManager resMan = DefaultResourceManager.getResourceManager(); |
1570 |
|
1571 |
Iterator iter = resMan.iterator();
|
1572 |
while (iter.hasNext()) {
|
1573 |
obj = iter.next(); |
1574 |
if (obj instanceof FileResource) { |
1575 |
tmpRes = (AbstractResource) obj; |
1576 |
if (((FileResource) tmpRes).getFile().getAbsoluteFile().equals(
|
1577 |
params.getFile().getAbsoluteFile())) { |
1578 |
return tmpRes;
|
1579 |
} |
1580 |
} |
1581 |
} |
1582 |
return null; |
1583 |
} |
1584 |
|
1585 |
private void mapParamsBaseTest(DataParameters params) { |
1586 |
Exception e = null; |
1587 |
try {
|
1588 |
params.put(new Integer(0), "GEOMETRY_x"); |
1589 |
} catch (Exception e1) { |
1590 |
e = e1; |
1591 |
} |
1592 |
assertNull(e); |
1593 |
|
1594 |
e = null;
|
1595 |
try {
|
1596 |
params.put("kk", "GEOMETRY_x"); |
1597 |
} catch (IllegalArgumentException e1) { |
1598 |
e = e1; |
1599 |
} |
1600 |
assertNull(e); |
1601 |
|
1602 |
e = null;
|
1603 |
try {
|
1604 |
params.entrySet(); |
1605 |
} catch (UnsupportedOperationException e1) { |
1606 |
e = e1; |
1607 |
} |
1608 |
assertNull(e); |
1609 |
|
1610 |
e = null;
|
1611 |
try {
|
1612 |
params.keySet().add("hola");
|
1613 |
} catch (UnsupportedOperationException e1) { |
1614 |
e = e1; |
1615 |
} |
1616 |
assertNotNull(e); |
1617 |
|
1618 |
e = null;
|
1619 |
try {
|
1620 |
params.keySet().remove("storeSecondary");
|
1621 |
} catch (UnsupportedOperationException e1) { |
1622 |
e = e1; |
1623 |
} |
1624 |
assertNotNull(e); |
1625 |
|
1626 |
e = null;
|
1627 |
try {
|
1628 |
params.values().remove("GEOMETRY_x");
|
1629 |
} catch (UnsupportedOperationException e1) { |
1630 |
e = e1; |
1631 |
} |
1632 |
assertNotNull(e); |
1633 |
|
1634 |
e = null;
|
1635 |
try {
|
1636 |
params.values().add("GEOMETRY_x");
|
1637 |
} catch (UnsupportedOperationException e1) { |
1638 |
e = e1; |
1639 |
} |
1640 |
assertNotNull(e); |
1641 |
|
1642 |
} |
1643 |
|
1644 |
} |