Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataFile / src-test / org / gvsig / fmap / data / feature / file / DataStoreTest.java @ 23602

History | View | Annotate | Download (44.2 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.CloseException;
11
import org.gvsig.fmap.data.DataCollection;
12
import org.gvsig.fmap.data.DataException;
13
import org.gvsig.fmap.data.DataManager;
14
import org.gvsig.fmap.data.DataParameters;
15
import org.gvsig.fmap.data.DataStoreParameters;
16
import org.gvsig.fmap.data.InitializeException;
17
import org.gvsig.fmap.data.OpenException;
18
import org.gvsig.fmap.data.ReadException;
19
import org.gvsig.fmap.data.Resource;
20
import org.gvsig.fmap.data.ResourceManager;
21
import org.gvsig.fmap.data.WriteException;
22
import org.gvsig.fmap.data.feature.AbstractFeatureStore;
23
import org.gvsig.fmap.data.feature.AttributeDescriptor;
24
import org.gvsig.fmap.data.feature.Feature;
25
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor;
26
import org.gvsig.fmap.data.feature.FeatureCollection;
27
import org.gvsig.fmap.data.feature.FeatureStore;
28
import org.gvsig.fmap.data.feature.FeatureType;
29
import org.gvsig.fmap.data.feature.MemoryFeature;
30
import org.gvsig.fmap.data.feature.file.dbf.DBFStore;
31
import org.gvsig.fmap.data.feature.file.dbf.DBFStoreParameters;
32
import org.gvsig.fmap.data.feature.file.dgn.DGNStore;
33
import org.gvsig.fmap.data.feature.file.dgn.DGNStoreParameters;
34
import org.gvsig.fmap.data.feature.file.dxf.DXFStore;
35
import org.gvsig.fmap.data.feature.file.dxf.DXFStoreParameters;
36
import org.gvsig.fmap.data.feature.file.shp.SHPStore;
37
import org.gvsig.fmap.data.feature.file.shp.SHPStoreParameters;
38
import org.gvsig.fmap.data.feature.visitor.PrintlnFeaturesVisitor;
39
import org.gvsig.fmap.geom.Geometry;
40
import org.gvsig.fmap.geom.handler.Handler;
41
import org.gvsig.tools.exception.BaseException;
42
import org.gvsig.tools.observer.Observer;
43

    
44
import com.iver.utiles.XMLEntity;
45
import com.iver.utiles.XMLException;
46

    
47
public class DataStoreTest extends TestCase {
48

    
49
        private File dbffile = new File(DataStoreTest.class.getResource(
50
                        "data/prueba.dbf").getFile());
51
        private File shpfile = new File(DataStoreTest.class.getResource(
52
                        "data/prueba.shp").getFile());
53
        private File dxffile = new File(DataStoreTest.class.getResource(
54
                        "data/prueba.dxf").getFile());
55
        private File dgnfile = new File(DataStoreTest.class.getResource(
56
                        "data/prueba.dgn").getFile());
57

    
58
        public static void main(String[] args) {
59
                junit.textui.TestRunner.run(DataStoreTest.class);
60
        }
61

    
62
        protected void setUp() throws Exception {
63
                super.setUp();
64

    
65
        }
66

    
67
        public void testDBF() {
68
                try {
69
                        System.out.println("======= DBF ==============");
70
                        org.gvsig.fmap.data.feature.file.dbf.Register.selfRegister();
71

    
72
                        DataManager dsm = DataManager.getManager();
73

    
74
                        DataStoreParameters dp = dsm
75
                                        .createDataStoreParameters(DBFStore.DATASTORE_NAME);
76
                        ((DBFStoreParameters) dp).setFile(dbffile);
77

    
78
                        driverTest(dp, null, null, true);
79
                        FeatureStore fs = null;
80
                        try {
81
                                fs = (FeatureStore) dsm.createDataStore(dp);
82
                        } catch (InitializeException e) {
83
                                e.printStackTrace();
84
                                fail("Exception:" + e);
85
                        }
86
                        assertNotNull("Can't create Feature Store", fs);
87

    
88
                        //                        fs.open();
89

    
90
                        Iterator it;
91
                        FeatureCollection fc;
92
                        Comparable v1, v2;
93
                        Feature feature, pfeature;
94
                        long count;
95

    
96
                        fc = (FeatureCollection) fs.getDataCollection();
97

    
98
                        assertEquals(9, fc.size());
99

    
100
                        fc = (FeatureCollection) fs.getDataCollection(fs
101
                                        .getDefaultFeatureType(), "lower(NOMBRE) like 'b%'", null);
102

    
103
                        assertEquals(2, fc.size());
104

    
105
                        it = fc.iterator();
106
                        count = 0;
107
                        while (it.hasNext()) {
108
                                feature = (Feature) it.next();
109
                                assertTrue("Filter error", feature.getString("NOMBRE")
110
                                                .toLowerCase().startsWith("b"));
111
                                count++;
112
                        }
113
                        assertEquals("Iteration error", 2, count);
114

    
115
                        fc = (FeatureCollection) fs.getDataCollection(fs
116
                                        .getDefaultFeatureType(), null, "NOMBRE ASC");
117
                        assertEquals(9, fc.size());
118
                        it = fc.iterator();
119
                        count = 0;
120
                        pfeature = (Feature) it.next();
121
                        count++;
122
                        while (it.hasNext()) {
123
                                feature = (Feature) it.next();
124
                                v1 = (Comparable) pfeature.get("NOMBRE");
125
                                v2 = (Comparable) feature.get("NOMBRE");
126
                                pfeature = feature;
127
                                assertTrue("Short error", (v1.compareTo(v1) <= 0));
128
                                count++;
129
                        }
130
                        assertEquals("Iteration error", 9, count);
131

    
132
                        fc = (FeatureCollection) fs.getDataCollection(fs
133
                                        .getDefaultFeatureType(), null, "NOMBRE DESC");
134
                        assertEquals(9, fc.size());
135
                        it = fc.iterator();
136

    
137
                        count = 0;
138
                        pfeature = (Feature) it.next();
139
                        count++;
140
                        while (it.hasNext()) {
141
                                feature = (Feature) it.next();
142
                                v1 = (Comparable) pfeature.get("NOMBRE");
143
                                v2 = (Comparable) feature.get("NOMBRE");
144
                                pfeature = feature;
145
                                assertTrue("Short error", (v1.compareTo(v1) >= 0));
146
                                count++;
147
                        }
148
                        assertEquals("Iteration error", 9, count);
149

    
150
                        fc = (FeatureCollection) fs.getDataCollection(fs
151
                                        .getDefaultFeatureType(), "lower(NOMBRE) like 'b%'",
152
                                        "NOMBRE");
153

    
154
                        assertEquals(2, fc.size());
155

    
156
                        it = fc.iterator();
157

    
158
                        count = 0;
159
                        pfeature = (Feature) it.next();
160
                        assertTrue(pfeature.getString("NOMBRE").toLowerCase().startsWith(
161
                                        "b"));
162
                        count++;
163
                        while (it.hasNext()) {
164
                                feature = (Feature) it.next();
165
                                assertTrue("Filter error", feature.getString("NOMBRE")
166
                                                .toLowerCase().startsWith("b"));
167
                                v1 = (Comparable) pfeature.get("NOMBRE");
168
                                v2 = (Comparable) feature.get("NOMBRE");
169
                                pfeature = feature;
170
                                assertTrue("Short error", (v1.compareTo(v1) <= 0));
171
                                count++;
172
                        }
173
                        assertEquals("Iteration error", 2, count);
174

    
175
                        fc = (FeatureCollection) fs.getDataCollection(fs
176
                                        .getDefaultFeatureType(), null, "Tipo,lower(NOMBRE) Desc");
177
                        assertEquals(9, fc.size());
178
                        it = fc.iterator();
179
                        count = 0;
180
                        pfeature = (Feature) it.next();
181
                        System.out.println(pfeature.getString("NOMBRE"));
182
                        count++;
183
                        while (it.hasNext()) {
184
                                feature = (Feature) it.next();
185
                                v1 = ((String) pfeature.get("NOMBRE")).toLowerCase();
186
                                v2 = ((String) feature.get("NOMBRE")).toLowerCase();
187
                                pfeature = feature;
188
                                assertTrue("Short error", (v1.compareTo(v2) >= 0));
189
                                System.out.println(pfeature.getString("NOMBRE"));
190
                                count++;
191
                        }
192
                        assertEquals("Iteration error", 9, count);
193

    
194
                        /// CON  EDICION
195

    
196
                        fs.startEditing();
197

    
198
                        Feature newFeature = fs.createDefaultFeature(true);
199
                        newFeature.editing();
200
                        newFeature.set("NOMBRE", "BuRjaSOT");
201
                        newFeature.set("TIPO", "MUNICIPIO");
202
                        fs.insert(newFeature);
203

    
204
                        try {
205
                                fc = (FeatureCollection) fs.getDataCollection();
206
                        } catch (ReadException e1) {
207
                                e1.printStackTrace();
208
                                fail();
209
                        }
210

    
211
                        assertEquals(10, fc.size());
212

    
213
                        try {
214
                                fc = (FeatureCollection) fs.getDataCollection(fs
215
                                                .getDefaultFeatureType(), "lower(NOMBRE) like 'b%'",
216
                                                null);
217
                        } catch (ReadException e1) {
218
                                e1.printStackTrace();
219
                                fail();
220
                        }
221

    
222
                        assertEquals(3, fc.size());
223

    
224
                        it = fc.iterator();
225
                        count = 0;
226
                        while (it.hasNext()) {
227
                                feature = (Feature) it.next();
228
                                assertTrue("Filter error", feature.getString("NOMBRE")
229
                                                .toLowerCase().startsWith("b"));
230
                                count++;
231
                        }
232
                        assertEquals("Iteration error", 3, count);
233

    
234
                        try {
235
                                fc = (FeatureCollection) fs.getDataCollection(fs
236
                                                .getDefaultFeatureType(), null, "NOMBRE ASC");
237
                        } catch (ReadException e1) {
238
                                e1.printStackTrace();
239
                                fail();
240
                        }
241
                        assertEquals(10, fc.size());
242
                        it = fc.iterator();
243
                        count = 0;
244
                        pfeature = (Feature) it.next();
245
                        count++;
246
                        while (it.hasNext()) {
247
                                feature = (Feature) it.next();
248
                                v1 = (Comparable) pfeature.get("NOMBRE");
249
                                v2 = (Comparable) feature.get("NOMBRE");
250
                                pfeature = feature;
251
                                assertTrue("Short error", (v1.compareTo(v1) <= 0));
252
                                count++;
253
                        }
254
                        assertEquals("Iteration error", 10, count);
255

    
256
                        try {
257
                                fc = (FeatureCollection) fs.getDataCollection(fs
258
                                                .getDefaultFeatureType(), null, "NOMBRE DESC");
259
                        } catch (ReadException e1) {
260
                                e1.printStackTrace();
261
                                fail();
262
                        }
263
                        assertEquals(10, fc.size());
264
                        it = fc.iterator();
265

    
266
                        count = 0;
267
                        pfeature = (Feature) it.next();
268
                        count++;
269
                        while (it.hasNext()) {
270
                                feature = (Feature) it.next();
271
                                v1 = (Comparable) pfeature.get("NOMBRE");
272
                                v2 = (Comparable) feature.get("NOMBRE");
273
                                pfeature = feature;
274
                                if (v1 != null && v2 != null) {
275
                                        assertTrue("Short error", (v1.compareTo(v1) >= 0));
276
                                }
277
                                count++;
278
                        }
279
                        assertEquals("Iteration error", 10, count);
280

    
281
                        try {
282
                                fc = (FeatureCollection) fs.getDataCollection(
283
                                                new String[] { "NOMBRE" }, "lower(NOMBRE) like 'b%'",
284
                                                "NOMBRE");
285
                        } catch (ReadException e1) {
286
                                e1.printStackTrace();
287
                                fail();
288
                        }
289

    
290
                        assertEquals(3, fc.size());
291

    
292
                        it = fc.iterator();
293

    
294
                        count = 0;
295
                        pfeature = (Feature) it.next();
296
                        assertTrue(pfeature.getString("NOMBRE").toLowerCase().startsWith(
297
                                        "b"));
298
                        count++;
299
                        while (it.hasNext()) {
300
                                feature = (Feature) it.next();
301
                                if (!(feature instanceof MemoryFeature)) { //FIXME OJO con los featureType de los Feature en memoria
302
                                        assertEquals(1, feature.getType().size());
303
                                }
304
                                assertTrue("Filter error", feature.getString("NOMBRE")
305
                                                .toLowerCase().startsWith("b"));
306
                                v1 = (Comparable) pfeature.get("NOMBRE");
307
                                v2 = (Comparable) feature.get("NOMBRE");
308
                                pfeature = feature;
309
                                assertTrue("Short error", (v1.compareTo(v1) <= 0));
310
                                count++;
311
                        }
312
                        assertEquals("Iteration error", 3, count);
313

    
314
                        try {
315
                                fc = (FeatureCollection) fs.getDataCollection(fs
316
                                                .getDefaultFeatureType(), null,
317
                                                "Tipo,lower(NOMBRE) Desc");
318
                        } catch (ReadException e1) {
319
                                e1.printStackTrace();
320
                                fail();
321
                        }
322
                        assertEquals(10, fc.size());
323
                        it = fc.iterator();
324
                        count = 0;
325
                        pfeature = (Feature) it.next();
326
                        System.out.println(pfeature.getString("TIPO") + ","
327
                                        + pfeature.getString("NOMBRE"));
328
                        count++;
329
                        while (it.hasNext()) {
330
                                feature = (Feature) it.next();
331
                                System.out.println(feature.getString("TIPO") + ","
332
                                                + feature.getString("NOMBRE"));
333
                                v1 = ((String) pfeature.get("TIPO")).toLowerCase();
334
                                v2 = ((String) feature.get("TIPO")).toLowerCase();
335
                                assertTrue("Short error", (v2.compareTo(v1) >= 0));
336
                                if (v1.compareTo(v2) == 0) {
337
                                        v1 = ((String) pfeature.get("NOMBRE")).toLowerCase();
338
                                        v2 = ((String) feature.get("NOMBRE")).toLowerCase();
339
                                        assertTrue("Short error", (v1.compareTo(v2) >= 0));
340
                                }
341
                                pfeature = feature;
342

    
343
                                //                                System.out.println(pfeature.getString("TIPO")+","+pfeature.getString("NOMBRE"));
344
                                count++;
345
                        }
346
                        assertEquals("Iteration error", 10, count);
347

    
348
                        fs.cancelEditing();
349

    
350
                        //                        try {
351
                        //                                fs.close();
352
                        //                        } catch (CloseException e) {
353
                        //                                e.printStackTrace();
354
                        //                                fail("Exception:" + e);
355
                        //                        }
356

    
357
                        System.out.println("======= /DBF ==============");
358

    
359
                } catch (DataException e) {
360
                        e.printStackTrace();
361
                        fail();
362
                }
363

    
364
        }
365

    
366
        public void testSHP() {
367
                try {
368
                        System.out.println("======= SHP ==============");
369
                        org.gvsig.fmap.data.feature.file.shp.Register.selfRegister();
370
                        org.gvsig.fmap.data.feature.file.dbf.Register.selfRegister();
371

    
372
                        DataManager dsm = DataManager.getManager();
373

    
374
                        DataStoreParameters dp = dsm
375
                                        .createDataStoreParameters(SHPStore.DATASTORE_NAME);
376
                        ((SHPStoreParameters) dp).setFile(shpfile);
377
                        //                        ((SHPStoreParameters)dp).setSHXFile(SHP.getShxFile(shpfile));
378
                        //                        ((SHPStoreParameters)dp).setDBFFile(SHP.getDbfFile(shpfile));
379

    
380
                        driverTest(dp, null, null, true);
381

    
382
                        FeatureStore fs = null;
383
                        try {
384
                                fs = (FeatureStore) dsm.createDataStore(dp);
385
                        } catch (InitializeException e) {
386
                                e.printStackTrace();
387
                                fail("Exception:" + e);
388
                        }
389

    
390
                        assertNotNull("Can't create Feature Store", fs);
391

    
392
                        //                        fs.open();
393

    
394
                        Iterator it;
395
                        FeatureCollection fc;
396
                        Comparable v1, v2;
397
                        Feature feature, pfeature;
398
                        long count;
399

    
400
                        fc = (FeatureCollection) fs.getDataCollection();
401

    
402
                        assertEquals(9, fc.size());
403

    
404
                        fc = (FeatureCollection) fs.getDataCollection(fs
405
                                        .getDefaultFeatureType(), "lower(NOMBRE) like 'b%'", null);
406

    
407
                        assertEquals(2, fc.size());
408

    
409
                        it = fc.iterator();
410
                        count = 0;
411
                        while (it.hasNext()) {
412
                                feature = (Feature) it.next();
413
                                assertTrue("Filter error", feature.getString("NOMBRE")
414
                                                .toLowerCase().startsWith("b"));
415
                                count++;
416
                        }
417
                        assertEquals("Iteration error", 2, count);
418

    
419
                        fc = (FeatureCollection) fs.getDataCollection(fs
420
                                        .getDefaultFeatureType(), null, "NOMBRE");
421
                        assertEquals(9, fc.size());
422
                        it = fc.iterator();
423
                        count = 0;
424
                        pfeature = (Feature) it.next();
425
                        count++;
426
                        while (it.hasNext()) {
427
                                feature = (Feature) it.next();
428
                                v1 = (Comparable) pfeature.get("NOMBRE");
429
                                v2 = (Comparable) feature.get("NOMBRE");
430
                                pfeature = feature;
431
                                assertTrue("Short error", (v1.compareTo(v2) <= 0));
432
                                count++;
433
                        }
434
                        assertEquals("Iteration error", 9, count);
435

    
436
                        fc = (FeatureCollection) fs.getDataCollection(fs
437
                                        .getDefaultFeatureType(), null, "NOMBRE DESC");
438
                        assertEquals(9, fc.size());
439
                        it = fc.iterator();
440
                        count = 0;
441
                        pfeature = (Feature) it.next();
442
                        System.out.println(pfeature.getString("NOMBRE"));
443
                        count++;
444
                        while (it.hasNext()) {
445
                                feature = (Feature) it.next();
446
                                v1 = (Comparable) pfeature.get("NOMBRE");
447
                                v2 = (Comparable) feature.get("NOMBRE");
448
                                pfeature = feature;
449
                                assertTrue("Short error", (v1.compareTo(v2) >= 0));
450
                                System.out.println(pfeature.getString("NOMBRE"));
451
                                count++;
452
                        }
453
                        assertEquals("Iteration error", 9, count);
454

    
455
                        fc = (FeatureCollection) fs.getDataCollection(fs
456
                                        .getDefaultFeatureType(), "lower(NOMBRE) like 'b%'",
457
                                        "NOMBRE ASC");
458

    
459
                        assertEquals(2, fc.size());
460

    
461
                        it = fc.iterator();
462

    
463
                        count = 0;
464
                        pfeature = (Feature) it.next();
465
                        assertTrue(pfeature.getString("NOMBRE").toLowerCase().startsWith(
466
                                        "b"));
467
                        count++;
468
                        while (it.hasNext()) {
469
                                feature = (Feature) it.next();
470
                                assertTrue("Filter error", feature.getString("NOMBRE")
471
                                                .toLowerCase().startsWith("b"));
472
                                v1 = (Comparable) pfeature.get("NOMBRE");
473
                                v2 = (Comparable) feature.get("NOMBRE");
474
                                pfeature = feature;
475
                                assertTrue("Short error", (v1.compareTo(v2) <= 0));
476
                                count++;
477
                        }
478
                        assertEquals("Iteration error", 2, count);
479

    
480
                        fc = (FeatureCollection) fs.getDataCollection(fs
481
                                        .getDefaultFeatureType(), null, "lower(NOMBRE) ASC");
482
                        assertEquals(9, fc.size());
483
                        it = fc.iterator();
484
                        count = 0;
485
                        pfeature = (Feature) it.next();
486
                        count++;
487
                        while (it.hasNext()) {
488
                                feature = (Feature) it.next();
489
                                v1 = ((String) pfeature.get("NOMBRE")).toLowerCase();
490
                                v2 = ((String) feature.get("NOMBRE")).toLowerCase();
491
                                pfeature = feature;
492
                                assertTrue("Short error", (v1.compareTo(v2) <= 0));
493
                                count++;
494
                        }
495
                        assertEquals("Iteration error", 9, count);
496

    
497
                        fc = (FeatureCollection) fs.getDataCollection(fs
498
                                        .getDefaultFeatureType(), null, "Tipo,lower(NOMBRE) Desc");
499
                        assertEquals(9, fc.size());
500
                        it = fc.iterator();
501
                        count = 0;
502
                        pfeature = (Feature) it.next();
503
                        System.out.println(pfeature.getString("NOMBRE"));
504
                        count++;
505
                        while (it.hasNext()) {
506
                                feature = (Feature) it.next();
507
                                v1 = ((String) pfeature.get("NOMBRE")).toLowerCase();
508
                                v2 = ((String) feature.get("NOMBRE")).toLowerCase();
509
                                pfeature = feature;
510
                                assertTrue("Short error", (v1.compareTo(v2) >= 0));
511
                                System.out.println(pfeature.getString("NOMBRE"));
512
                                count++;
513
                        }
514
                        assertEquals("Iteration error", 9, count);
515

    
516
                        /// CON  EDICION
517

    
518
                        fs.startEditing();
519

    
520
                        Feature newFeature = fs.createDefaultFeature(true);
521
                        newFeature.editing();
522
                        newFeature.set("NOMBRE", "BuRjaSOT");
523
                        newFeature.set("TIPO", "MUNICIPIO");
524
                        fs.insert(newFeature);
525

    
526
                        try {
527
                                fc = (FeatureCollection) fs.getDataCollection();
528
                        } catch (ReadException e1) {
529
                                e1.printStackTrace();
530
                                fail();
531
                        }
532

    
533
                        assertEquals(10, fc.size());
534

    
535
                        try {
536
                                fc = (FeatureCollection) fs.getDataCollection(fs
537
                                                .getDefaultFeatureType(), "lower(NOMBRE) like 'b%'",
538
                                                null);
539
                        } catch (ReadException e1) {
540
                                e1.printStackTrace();
541
                                fail();
542
                        }
543

    
544
                        assertEquals(3, fc.size());
545

    
546
                        it = fc.iterator();
547
                        count = 0;
548
                        while (it.hasNext()) {
549
                                feature = (Feature) it.next();
550
                                assertTrue("Filter error", feature.getString("NOMBRE")
551
                                                .toLowerCase().startsWith("b"));
552
                                count++;
553
                        }
554
                        assertEquals("Iteration error", 3, count);
555

    
556
                        try {
557
                                fc = (FeatureCollection) fs.getDataCollection(fs
558
                                                .getDefaultFeatureType(), null, "NOMBRE ASC");
559
                        } catch (ReadException e1) {
560
                                e1.printStackTrace();
561
                                fail();
562
                        }
563
                        assertEquals(10, fc.size());
564
                        it = fc.iterator();
565
                        count = 0;
566
                        pfeature = (Feature) it.next();
567
                        count++;
568
                        while (it.hasNext()) {
569
                                feature = (Feature) it.next();
570
                                v1 = (Comparable) pfeature.get("NOMBRE");
571
                                v2 = (Comparable) feature.get("NOMBRE");
572
                                pfeature = feature;
573
                                assertTrue("Short error", (v1.compareTo(v1) <= 0));
574
                                count++;
575
                        }
576
                        assertEquals("Iteration error", 10, count);
577

    
578
                        try {
579
                                fc = (FeatureCollection) fs.getDataCollection(fs
580
                                                .getDefaultFeatureType(), null, "NOMBRE DESC");
581
                        } catch (ReadException e1) {
582
                                e1.printStackTrace();
583
                                fail();
584
                        }
585
                        assertEquals(10, fc.size());
586
                        it = fc.iterator();
587

    
588
                        count = 0;
589
                        pfeature = (Feature) it.next();
590
                        count++;
591
                        while (it.hasNext()) {
592
                                feature = (Feature) it.next();
593
                                v1 = (Comparable) pfeature.get("NOMBRE");
594
                                v2 = (Comparable) feature.get("NOMBRE");
595
                                pfeature = feature;
596
                                assertTrue("Short error", (v1.compareTo(v1) >= 0));
597
                                count++;
598
                        }
599
                        assertEquals("Iteration error", 10, count);
600

    
601
                        try {
602
                                fc = (FeatureCollection) fs.getDataCollection(fs
603
                                                .getDefaultFeatureType(), "lower(NOMBRE) like 'b%'",
604
                                                "NOMBRE");
605
                        } catch (ReadException e1) {
606
                                e1.printStackTrace();
607
                                fail();
608
                        }
609

    
610
                        assertEquals(3, fc.size());
611

    
612
                        it = fc.iterator();
613

    
614
                        count = 0;
615
                        pfeature = (Feature) it.next();
616
                        assertTrue(pfeature.getString("NOMBRE").toLowerCase().startsWith(
617
                                        "b"));
618
                        count++;
619
                        while (it.hasNext()) {
620
                                feature = (Feature) it.next();
621
                                assertTrue("Filter error", feature.getString("NOMBRE")
622
                                                .toLowerCase().startsWith("b"));
623
                                v1 = (Comparable) pfeature.get("NOMBRE");
624
                                v2 = (Comparable) feature.get("NOMBRE");
625
                                pfeature = feature;
626
                                assertTrue("Short error", (v1.compareTo(v1) <= 0));
627
                                count++;
628
                        }
629
                        assertEquals("Iteration error", 3, count);
630

    
631
                        try {
632
                                fc = (FeatureCollection) fs.getDataCollection(fs
633
                                                .getDefaultFeatureType(), null,
634
                                                "Tipo,lower(NOMBRE) Desc");
635
                        } catch (ReadException e1) {
636
                                e1.printStackTrace();
637
                                fail();
638
                        }
639
                        assertEquals(10, fc.size());
640
                        it = fc.iterator();
641
                        count = 0;
642
                        pfeature = (Feature) it.next();
643
                        System.out.println(pfeature.getString("NOMBRE"));
644
                        count++;
645
                        while (it.hasNext()) {
646
                                feature = (Feature) it.next();
647
                                v1 = ((String) pfeature.get("NOMBRE")).toLowerCase();
648
                                v2 = ((String) feature.get("NOMBRE")).toLowerCase();
649
                                pfeature = feature;
650
                                assertTrue("Short error", (v1.compareTo(v2) >= 0));
651
                                System.out.println(pfeature.getString("NOMBRE"));
652
                                count++;
653
                        }
654
                        assertEquals("Iteration error", 10, count);
655

    
656
                        fs.cancelEditing();
657

    
658
                        //                        try {
659
                        //                                fs.close();
660
                        //                        } catch (CloseException e) {
661
                        //                                e.printStackTrace();
662
                        //                                fail("Exception:" + e);
663
                        //                        }
664

    
665
                        System.out.println("======= /SHP ==============");
666
                } catch (DataException e) {
667
                        // TODO Auto-generated catch block
668
                        e.printStackTrace();
669
                }
670
        }
671

    
672
        public void testDXF() {
673
                try {
674
                        System.out.println("======= DXF ==============");
675
                        org.gvsig.fmap.data.feature.file.dxf.Register.selfRegister();
676

    
677
                        DataManager dsm = DataManager.getManager();
678

    
679
                        DataStoreParameters dp = dsm
680
                                        .createDataStoreParameters(DXFStore.DATASTORE_NAME);
681
                        ((DXFStoreParameters) dp).setFile(dxffile);
682
                        IProjection proj = CRSFactory.getCRS("EPSG:23030");
683
                        ((DXFStoreParameters) dp).setSRSID("EPSG:23030");
684
                        driverTest(dp, null, null, true);
685

    
686
                        FeatureStore fs = null;
687
                        try {
688
                                fs = (FeatureStore) dsm.createDataStore(dp);
689
                        } catch (InitializeException e) {
690
                                e.printStackTrace();
691
                                fail("Exception:" + e);
692
                        }
693
                        assertNotNull("Can't create Feature Store", fs);
694

    
695
                        //                        fs.open();
696

    
697
                        Iterator it;
698
                        FeatureCollection fc;
699
                        Comparable v1, v2;
700
                        Feature feature, pfeature;
701
                        long count;
702

    
703
                        fc = (FeatureCollection) fs.getDataCollection();
704

    
705
                        assertEquals(10, fc.size());
706

    
707
                        fc = (FeatureCollection) fs.getDataCollection(fs
708
                                        .getDefaultFeatureType(), "Color > '7'", null);
709

    
710
                        assertEquals(2, fc.size());
711

    
712
                        it = fc.iterator();
713
                        count = 0;
714
                        while (it.hasNext()) {
715
                                feature = (Feature) it.next();
716
                                assertTrue("Filter error", feature.getInt("Color") > 7);
717
                                count++;
718
                        }
719
                        assertEquals("Iteration error", 2, count);
720

    
721
                        fc = (FeatureCollection) fs.getDataCollection(fs
722
                                        .getDefaultFeatureType(), null, "Layer ASC");
723
                        assertEquals(10, fc.size());
724
                        it = fc.iterator();
725
                        count = 0;
726
                        pfeature = (Feature) it.next();
727
                        count++;
728
                        while (it.hasNext()) {
729
                                feature = (Feature) it.next();
730
                                v1 = (Comparable) pfeature.get("Layer");
731
                                v2 = (Comparable) feature.get("Layer");
732
                                pfeature = feature;
733
                                assertTrue("Short error", (v1.compareTo(v1) <= 0));
734
                                count++;
735
                        }
736
                        assertEquals("Iteration error", 10, count);
737

    
738
                        fc = (FeatureCollection) fs.getDataCollection(fs
739
                                        .getDefaultFeatureType(), null, "Layer DESC");
740
                        assertEquals(10, fc.size());
741
                        it = fc.iterator();
742

    
743
                        count = 0;
744
                        pfeature = (Feature) it.next();
745
                        count++;
746
                        while (it.hasNext()) {
747
                                feature = (Feature) it.next();
748
                                v1 = (Comparable) pfeature.get("Layer");
749
                                v2 = (Comparable) feature.get("Layer");
750
                                pfeature = feature;
751
                                assertTrue("Short error", (v1.compareTo(v1) >= 0));
752
                                count++;
753
                        }
754
                        assertEquals("Iteration error", 10, count);
755

    
756
                        fc = (FeatureCollection) fs.getDataCollection(fs
757
                                        .getDefaultFeatureType(), "Color > '7'", "Layer ASC");
758

    
759
                        assertEquals(2, fc.size());
760

    
761
                        it = fc.iterator();
762

    
763
                        count = 0;
764
                        pfeature = (Feature) it.next();
765
                        assertTrue(pfeature.getInt("Color") > 7);
766
                        count++;
767
                        while (it.hasNext()) {
768
                                feature = (Feature) it.next();
769
                                assertTrue("Filter error", feature.getInt("Color") > 7);
770
                                v1 = (Comparable) pfeature.get("Color");
771
                                v2 = (Comparable) feature.get("Color");
772
                                pfeature = feature;
773
                                Integer i1 = (Integer) v1;
774
                                Integer i2 = (Integer) v2;
775
                                assertTrue("Short error", (i1.intValue() > i2.intValue()));
776
                                count++;
777
                        }
778
                        assertEquals("Iteration error", 2, count);
779

    
780
                        //                        fc = (FeatureCollection)fs.getDataCollection(null,null,"Color,Layer Desc");
781
                        //                        assertEquals(10, fc.size());
782
                        //                        it = fc.iterator();
783
                        //                        count=0;
784
                        //                        pfeature = (Feature)it.next();
785
                        //                        System.out.println(pfeature.getString("Layer"));
786
                        //                        count++;
787
                        //                        while (it.hasNext()){
788
                        //                        feature = (Feature)it.next();
789
                        //                        v1 = (Comparable)((String)pfeature.get("Layer")).toLowerCase();
790
                        //                        v2 = (Comparable)((String)feature.get("Layer")).toLowerCase();
791
                        //                        pfeature=feature;
792
                        //                        assertTrue("Short error", (v1.compareTo(v2) >= 0));
793
                        //                        System.out.println(pfeature.getString("Layer"));
794
                        //                        count++;
795
                        //                        }
796
                        //                        assertEquals("Iteration error",10,count);
797

    
798
                        //                        try {
799
                        //                                fs.close();
800
                        //                        } catch (CloseException e) {
801
                        //                                e.printStackTrace();
802
                        //                                fail("Exception:" + e);
803
                        //                        }
804

    
805
                        System.out.println("======= /DXF ==============");
806
                } catch (OpenException e1) {
807
                        e1.printStackTrace();
808
                        fail();
809
                } catch (ReadException e) {
810
                        // TODO Auto-generated catch block
811
                        e.printStackTrace();
812
                }
813
        }
814

    
815
        public void testDGN() {
816
                try {
817
                        System.out.println("======= DGN ==============");
818
                        org.gvsig.fmap.data.feature.file.dgn.Register.selfRegister();
819

    
820
                        DataManager dsm = DataManager.getManager();
821

    
822
                        DataStoreParameters dp = dsm
823
                                        .createDataStoreParameters(DGNStore.DATASTORE_NAME);
824
                        ((DGNStoreParameters) dp).setFile(dgnfile);
825
                        driverTest(dp, null, null, true);
826
                        FeatureStore fs = null;
827
                        try {
828
                                fs = (FeatureStore) dsm.createDataStore(dp);
829
                        } catch (InitializeException e) {
830
                                e.printStackTrace();
831
                                fail("Exception:" + e);
832
                        }
833
                        assertNotNull("Can't create Feature Store", fs);
834

    
835
                        //                        fs.open();
836

    
837
                        Iterator it;
838
                        FeatureCollection fc;
839
                        Comparable v1, v2;
840
                        Feature feature, pfeature;
841
                        long count;
842

    
843
                        fc = (FeatureCollection) fs.getDataCollection();
844

    
845
                        assertEquals(57, fc.size());
846

    
847
                        fc = (FeatureCollection) fs.getDataCollection(fs
848
                                        .getDefaultFeatureType(), "Color > '7'", null);
849

    
850
                        assertEquals(45, fc.size());
851

    
852
                        it = fc.iterator();
853
                        count = 0;
854
                        while (it.hasNext()) {
855
                                feature = (Feature) it.next();
856
                                assertTrue("Filter error", feature.getInt("Color") >= 7);
857
                                count++;
858
                        }
859
                        assertEquals("Iteration error", 45, count);
860

    
861
                        fc = (FeatureCollection) fs.getDataCollection(fs
862
                                        .getDefaultFeatureType(), null, "Layer ASC");
863
                        assertEquals(57, fc.size());
864
                        it = fc.iterator();
865
                        count = 0;
866
                        pfeature = (Feature) it.next();
867
                        count++;
868
                        while (it.hasNext()) {
869
                                feature = (Feature) it.next();
870
                                v1 = (Comparable) pfeature.get("Layer");
871
                                v2 = (Comparable) feature.get("Layer");
872
                                if (v2 == null) {
873
                                        System.out.println("null");
874
                                }
875
                                assertTrue("Short error", (v1.compareTo(v2) <= 0));
876
                                pfeature = feature;
877
                                count++;
878
                        }
879
                        assertEquals("Iteration error", 57, count);
880

    
881
                        fc = (FeatureCollection) fs.getDataCollection(fs
882
                                        .getDefaultFeatureType(), null, "Layer DESC");
883
                        assertEquals(57, fc.size());
884
                        it = fc.iterator();
885

    
886
                        count = 0;
887
                        pfeature = (Feature) it.next();
888
                        count++;
889
                        while (it.hasNext()) {
890
                                feature = (Feature) it.next();
891
                                v1 = (Comparable) pfeature.get("Layer");
892
                                v2 = (Comparable) feature.get("Layer");
893
                                assertTrue("Short error", (v1.compareTo(v2) >= 0));
894
                                pfeature = feature;
895
                                count++;
896
                        }
897
                        assertEquals("Iteration error", 57, count);
898

    
899
                        fc = (FeatureCollection) fs.getDataCollection(fs
900
                                        .getDefaultFeatureType(), "Color > '7'", "Layer ASC");
901

    
902
                        assertEquals(45, fc.size());
903

    
904
                        it = fc.iterator();
905

    
906
                        count = 0;
907
                        pfeature = (Feature) it.next();
908
                        assertTrue(pfeature.getInt("Color") > 7);
909
                        count++;
910
                        while (it.hasNext()) {
911
                                feature = (Feature) it.next();
912
                                assertTrue("Filter error", feature.getInt("Color") > 7);
913
                                v1 = (Comparable) pfeature.get("Layer");
914
                                v2 = (Comparable) feature.get("Layer");
915
                                pfeature = feature;
916
                                Integer i1 = (Integer) v1;
917
                                Integer i2 = (Integer) v2;
918
                                assertTrue("Short error", (i1.intValue() <= i2.intValue()));
919
                                count++;
920
                        }
921
                        assertEquals("Iteration error", 45, count);
922

    
923
                        //                        fc = (FeatureCollection)fs.getDataCollection(null,null,"Color,Layer Desc");
924
                        //                        assertEquals(10, fc.size());
925
                        //                        it = fc.iterator();
926
                        //                        count=0;
927
                        //                        pfeature = (Feature)it.next();
928
                        //                        System.out.println(pfeature.getString("Layer"));
929
                        //                        count++;
930
                        //                        while (it.hasNext()){
931
                        //                        feature = (Feature)it.next();
932
                        //                        v1 = (Comparable)((String)pfeature.get("Layer")).toLowerCase();
933
                        //                        v2 = (Comparable)((String)feature.get("Layer")).toLowerCase();
934
                        //                        pfeature=feature;
935
                        //                        assertTrue("Short error", (v1.compareTo(v2) >= 0));
936
                        //                        System.out.println(pfeature.getString("Layer"));
937
                        //                        count++;
938
                        //                        }
939
                        //                        assertEquals("Iteration error",10,count);
940

    
941
                        //                        try {
942
                        //                                fs.close();
943
                        //                        } catch (CloseException e) {
944
                        //                                e.printStackTrace();
945
                        //                                fail("Exception:" + e);
946
                        //                        }
947

    
948
                        System.out.println("======= /DGN ==============");
949
                } catch (OpenException e1) {
950
                        e1.printStackTrace();
951
                        fail();
952
                } catch (ReadException e) {
953
                        // TODO Auto-generated catch block
954
                        e.printStackTrace();
955
                }
956
        }
957

    
958
        //        private FeatureStore createFeatureStore(IDriverParameters dp){
959
        //        DataSourceManager dsm=DataSourceManager.getManager();
960

    
961
        //        DataStoreParameters dsp=dsm.createDataStoreParameters(DriverStore.DATASTORE_NAME);
962

    
963
        //        ((IDriverStoreParameters)dsp).setDriverParameters(dp);
964
        //        FeatureStore fs=null;
965
        //        try {
966
        //        fs = (FeatureStore)dsm.createDataStore(dsp);
967
        //        } catch (InitializeException e) {
968
        //        e.printStackTrace();
969
        //        fail("Exception:" + e);
970
        //        }
971
        //        return fs;
972

    
973
        //        }
974

    
975
        private void driverTest(DataStoreParameters dp, String filter,
976
                        String order, boolean testEdit) {
977
                DataManager dsm = DataManager.getManager();
978

    
979

    
980
                //                DataStoreParameters dsp=dsm.createDataStoreParameters(DriverStore.DATASTORE_NAME);
981

    
982
                //                ((IDriverStoreParameters)dsp).setDriverParameters(dp);
983
                //                FeatureStore fs=createFeatureStore(dp);
984
                FeatureStore fs = null;
985
                try {
986
                        fs = (FeatureStore) dsm.createDataStore(dp);
987
                } catch (InitializeException e) {
988
                        e.printStackTrace();
989
                        fail("Exception:" + e);
990
                }
991
                //                try {
992
                //                        fs.open();
993
                //                } catch (OpenException e2) {
994
                //                        e2.printStackTrace();
995
                //                        fail();
996
                //                }
997

    
998
                if (fs.isEditable() && testEdit) {
999
                        try {
1000
                                fs.startEditing();
1001
                                if (fs.canAlterFeatureType()) {
1002
                                        FeatureType featureType = fs.getDefaultFeatureType();
1003
                                        //                                        DefaultAttributeDescriptor dad=new DefaultAttributeDescriptor();
1004
                                        //                                        dad.loading();
1005
                                        //                                        dad.setName("Prueba1");
1006
                                        //                                        dad.setDefaultValue("11p");
1007
                                        //                                        dad.setSize(10);
1008
                                        //                                        dad.setType(FeatureAttributeDescriptor.TYPE_STRING);
1009
                                        //                                        dad.stopLoading();
1010
                                        //                                        fs.insert(dad);
1011
                                        //                                        fs.delete((FeatureAttributeDescriptor)featureType.get(featureType.getFieldIndex("Prueba1")));
1012
                                        AttributeDescriptor dad2 = (AttributeDescriptor) featureType
1013
                                                        .get(featureType.getFieldIndex("TIPO"));
1014
                                        dad2.editing();
1015
                                        //                                        dad2.setName("TIPO");
1016
                                        dad2.setDefaultValue("Tipop");
1017
                                        //                                        dad2.setSize(10);
1018
                                        dad2.setType(FeatureAttributeDescriptor.TYPE_STRING);
1019
                                        fs.update(dad2);
1020
                                }
1021
                        } catch (DataException e) {
1022
                                e.printStackTrace();
1023
                                fail();
1024
                        }
1025
                        try {
1026
                                Feature feature1 = fs.createDefaultFeature(false);
1027

    
1028
                                Feature feature2 = fs.createDefaultFeature(false);
1029
                                Feature feature3 = fs.createDefaultFeature(false);
1030

    
1031
                                fs.insert(feature1);
1032
                                fs.insert(feature2);
1033

    
1034
                                feature1.editing();
1035
                                feature1.set(1, "hola");
1036

    
1037
                                fs.update(feature1);
1038
                                fs.delete(feature3);
1039
                                fs.delete(feature2);
1040
                        } catch (DataException e) {
1041
                                // TODO Auto-generated catch block
1042
                                e.printStackTrace();
1043
                        }
1044
                }
1045

    
1046
                //Mostrar por consola todos los registros.
1047
                FeatureType ft = fs.getDefaultFeatureType();
1048
                FeatureCollection featureCollection = null;
1049
                //                featureCollection = (FeatureCollection)fs.getDataCollection();
1050
                //                featureCollection = (FeatureCollection)fs.getDataCollection(ft,"NOMBRE = 'CALPE'",null);
1051
                //                featureCollection = (FeatureCollection)fs.getDataCollection(ft,"AREA > 3.2213163729E7 and AREA < 3.2213163749E7",null);
1052
                try {
1053
                        featureCollection = (FeatureCollection) fs.getDataCollection(ft,
1054
                                        filter, order);
1055
                } catch (ReadException e2) {
1056
                        // TODO Auto-generated catch block
1057
                        e2.printStackTrace();
1058
                }
1059

    
1060
                PrintlnFeaturesVisitor visitor = new PrintlnFeaturesVisitor(ft);
1061
                try {
1062
                        featureCollection.accept(visitor);
1063
                } catch (BaseException e1) {
1064
                        e1.printStackTrace();
1065
                        fail("Exception: " + e1);
1066
                }
1067

    
1068
                featureCollection.dispose();
1069

    
1070
                if (fs.isEditable() && testEdit) {
1071
                        try {
1072
                                fs.finishEditing();
1073
                        } catch (WriteException e) {
1074
                                e.printStackTrace();
1075
                                fail("Exception: " + e);
1076
                        } catch (ReadException e) {
1077
                                e.printStackTrace();
1078
                                fail("Exception: " + e);
1079
                        }
1080
                        long t1 = System.currentTimeMillis();
1081
                        while (System.currentTimeMillis() - t1 > 2000) {
1082
                                //Esperamos 2 seg para no tener problemas con
1083
                                // los lastUpdate de los ficheros
1084
                        }
1085
                }
1086
                //                try {
1087
                //                        fs.close();
1088
                //                } catch (CloseException e) {
1089
                //                        e.printStackTrace();
1090
                //                        fail("Exception: "+e);
1091
                //                }
1092
                try {
1093
                        fs.dispose();
1094
                } catch (CloseException e) {
1095
                        // TODO Auto-generated catch block
1096
                        e.printStackTrace();
1097
                }
1098
                persistenceTest(dp);
1099
                mapParamsBaseTest(dp);
1100

    
1101
        }
1102

    
1103
        public static void doFileResourceTest(FileStoreParameters params) {
1104
                doFileResourceTest(params, true);
1105
        }
1106

    
1107
        public static void doFileResourceTest(FileStoreParameters params,
1108
                        boolean testEdit) {
1109
                DataManager manager = DataManager.getManager();
1110

    
1111
                ResourceManager resMan = ResourceManager.getResourceManager();
1112

    
1113
                AbstractFeatureStore store = null;
1114
                AbstractFeatureStore store2 = null;
1115
                AbstractFeatureStore store3 = null;
1116
                try {
1117
                        store = (AbstractFeatureStore) manager.createDataStore(params);
1118
                        store2 = (AbstractFeatureStore) manager.createDataStore(params);
1119
                        store3 = (AbstractFeatureStore) manager.createDataStore(params);
1120
                } catch (InitializeException e1) {
1121
                        e1.printStackTrace();
1122
                        fail();
1123
                }
1124

    
1125
                Resource res = getResource(params);
1126

    
1127
                assertNotNull(res);
1128

    
1129
                assertEquals(3, res.getRefencesCount());
1130

    
1131
                try {
1132
                        store.close();
1133
                } catch (CloseException e1) {
1134
                        e1.printStackTrace();
1135
                        fail();
1136
                }
1137

    
1138
                assertEquals(false, res.isOpen());
1139

    
1140
                DataCollection coll = null;
1141

    
1142
                try {
1143
                        coll = store.getDataCollection();
1144
                } catch (ReadException e1) {
1145
                        e1.printStackTrace();
1146
                        fail();
1147
                }
1148

    
1149
                coll.iterator().next();
1150
                //                coll.size();
1151

    
1152
                assertEquals(true, res.isOpen());
1153

    
1154
                coll.dispose();
1155

    
1156
                if (store.isEditable() && testEdit) {
1157
                        /* Test edition notification */
1158

    
1159
                        int fCountOrg = 0;
1160
                        int fCountFin = 0;
1161
                        try {
1162
                                fCountOrg = store.getDataCollection().size();
1163
                        } catch (ReadException e2) {
1164
                                // TODO Auto-generated catch block
1165
                                e2.printStackTrace();
1166
                                fail("Exception: " + e2);
1167
                        }
1168
                        try {
1169
                                store.startEditing();
1170
                        } catch (ReadException e1) {
1171
                                e1.printStackTrace();
1172
                                fail("Exception: " + e1);
1173
                        }
1174

    
1175
                        try {
1176
                                coll = store2.getDataCollection();
1177
                        } catch (ReadException e1) {
1178
                                e1.printStackTrace();
1179
                                fail("Exception: " + e1);
1180
                        }
1181

    
1182
                        try {
1183
                                store.finishEditing();
1184
                        } catch (WriteException e1) {
1185
                                e1.printStackTrace();
1186
                                fail("Exception: " + e1);
1187
                        } catch (ReadException e1) {
1188
                                e1.printStackTrace();
1189
                                fail("Exception: " + e1);
1190
                        }
1191

    
1192
                        try {
1193
                                fCountFin = store.getDataCollection().size();
1194
                        } catch (ReadException e2) {
1195
                                // TODO Auto-generated catch block
1196
                                e2.printStackTrace();
1197
                                fail("Exception: " + e2);
1198
                        }
1199

    
1200
                        assertEquals(fCountOrg, fCountFin);
1201

    
1202
                        boolean isOk = false;
1203
                        try {
1204
                                coll.iterator().next();
1205
                        } catch (Exception e) {
1206
                                isOk = true;
1207
                        }
1208
                        assertTrue("Resource Changed Notification fails", isOk);
1209

    
1210
                        coll.dispose();
1211

    
1212
                        long time = System.currentTimeMillis();
1213
                        while ((System.currentTimeMillis() - time) < 1000) {
1214
                                //sleep
1215
                        }
1216

    
1217
                        ((FileResource) res).getFile().setLastModified(
1218
                                        System.currentTimeMillis());
1219

    
1220
                        isOk = false;
1221
                        try {
1222
                                store.getDataCollection().iterator().next();
1223
                        } catch (Exception e) {
1224
                                isOk = true;
1225
                        }
1226
                        assertTrue("Resource Changed Notification fails", isOk);
1227

    
1228
                        /* Test edition notification END */
1229

    
1230
                }
1231

    
1232
                try {
1233
                        store3.dispose();
1234
                } catch (CloseException e1) {
1235
                        e1.printStackTrace();
1236
                        fail();
1237
                }
1238

    
1239
                assertEquals(2, res.getRefencesCount());
1240

    
1241
                try {
1242
                        store2.dispose();
1243
                } catch (CloseException e1) {
1244
                        e1.printStackTrace();
1245
                        fail();
1246
                }
1247

    
1248
                assertEquals(1, res.getRefencesCount());
1249

    
1250
                store = null;
1251
                System.gc();
1252
                System.gc();
1253
                System.gc();
1254

    
1255
                assertEquals(0, res.getRefencesCount());
1256
                try {
1257
                        resMan.collectDeadResources();
1258
                } catch (DataException e) {
1259
                        e.printStackTrace();
1260
                        fail();
1261
                        return;
1262
                }
1263
                res = getResource(params);
1264

    
1265
                assertNull(res);
1266

    
1267
                doPrepareFileResourceTest(params);
1268

    
1269
        }
1270

    
1271
        public static void doPrepareFileResourceTest(FileStoreParameters params) {
1272
                DataManager manager = DataManager.getManager();
1273

    
1274
                ResourceManager resMan = ResourceManager.getResourceManager();
1275

    
1276
                File originalFile = params.getFile();
1277
                params.setFile(new File(originalFile.getName()));
1278

    
1279
                Observer obs = new PrepareResourceObserver(originalFile);
1280

    
1281
                resMan.addObserver(obs);
1282

    
1283
                AbstractFeatureStore store = null;
1284
                try {
1285
                        store = (AbstractFeatureStore) manager.createDataStore(params);
1286
                } catch (InitializeException e1) {
1287
                        e1.printStackTrace();
1288
                        fail();
1289
                } catch (Exception e2) {
1290
                        e2.printStackTrace();
1291
                        fail();
1292
                }
1293

    
1294
                try {
1295
                        store.getDataCollection().iterator().next();
1296
                } catch (ReadException e) {
1297
                        // TODO Auto-generated catch block
1298
                        e.printStackTrace();
1299
                        fail();
1300
                }
1301

    
1302
                try {
1303
                        store.close();
1304
                } catch (CloseException e) {
1305
                        // TODO Auto-generated catch block
1306
                        e.printStackTrace();
1307
                        fail();
1308
                }
1309
                try {
1310
                        store.dispose();
1311
                } catch (CloseException e) {
1312
                        // TODO Auto-generated catch block
1313
                        e.printStackTrace();
1314
                        fail();
1315
                }
1316

    
1317
        }
1318

    
1319
        private void persistenceTest(DataStoreParameters params) {
1320
                DataManager manager = DataManager.getManager();
1321

    
1322
                DataStoreParameters params2 = null;
1323
                XMLEntity paramsXML = null;
1324
                XMLEntity params2XML = null;
1325

    
1326
                paramsXML = params.getXMLEntity();
1327

    
1328
                try {
1329
                        params2 = manager.createDataStoreParameters(paramsXML);
1330
                } catch (InitializeException e) {
1331
                        e.printStackTrace();
1332
                        fail();
1333
                }
1334

    
1335
                params2XML = params2.getXMLEntity();
1336

    
1337
                assertEquals(paramsXML.toString(), params2XML.toString());
1338

    
1339
                FeatureStore store = null;
1340
                XMLEntity storeXML = null;
1341
                FeatureStore store2 = null;
1342
                XMLEntity store2XML = null;
1343

    
1344
                try {
1345
                        store = (FeatureStore) manager.createDataStore(params);
1346
                } catch (InitializeException e) {
1347
                        e.printStackTrace();
1348
                        fail();
1349
                }
1350

    
1351
                try {
1352
                        storeXML = store.getXMLEntity();
1353
                } catch (XMLException e1) {
1354
                        e1.printStackTrace();
1355
                        fail();
1356
                }
1357

    
1358
                assertNotNull(storeXML);
1359

    
1360
                try {
1361
                        store2 = (FeatureStore) manager.createDataStore(storeXML);
1362
                } catch (InitializeException e) {
1363
                        e.printStackTrace();
1364
                        fail();
1365
                }
1366

    
1367
                try {
1368
                        store2XML = store2.getXMLEntity();
1369
                } catch (XMLException e1) {
1370
                        e1.printStackTrace();
1371
                        fail();
1372
                }
1373

    
1374
                assertEquals(storeXML.toString(), store2XML.toString());
1375

    
1376
                FeatureCollection coll = null;
1377
                FeatureCollection coll2 = null;
1378
                Iterator iter = null;
1379
                Iterator iter2 = null;
1380
                Feature feature=null;
1381
                Feature feature2 = null;
1382
                Comparable v1 = null;
1383
                Comparable v2 = null;
1384
                Handler h = null;
1385
                Handler h2 = null;
1386

    
1387

    
1388
                try {
1389
                        coll = (FeatureCollection) store.getDataCollection();
1390
                        coll2 = (FeatureCollection) store2.getDataCollection();
1391
                } catch (ReadException e) {
1392
                        e.printStackTrace();
1393
                        fail();
1394
                }
1395

    
1396
                assertEquals(coll.size(), coll2.size());
1397

    
1398
                iter = coll.iterator();
1399
                iter2 = coll2.iterator();
1400
                int i;
1401
                int featureSize = store.getDefaultFeatureType().size();
1402

    
1403
                try {
1404
                        while (iter.hasNext()) {
1405
                                feature = (Feature) iter.next();
1406
                                feature2 = (Feature) iter2.next();
1407
                                for (i = 0; i < featureSize; i++) {
1408
                                        v1 = (Comparable) feature.get(i);
1409
                                        v2 = (Comparable) feature2.get(i);
1410
                                        if (v1 == null) {
1411
                                                assertNull(v2);
1412
                                        } else if (v1 instanceof Geometry) {
1413
                                                assertTrue(v2 instanceof Geometry);
1414
                                                assertEquals(((Geometry) v1).getType(), ((Geometry) v2)
1415
                                                                .getType());
1416
                                                assertEquals(
1417
                                                                (((Geometry) v1)
1418
                                                                                .getHandlers(Geometry.SELECTHANDLER)).length,
1419
                                                                (((Geometry) v2)
1420
                                                                                .getHandlers(Geometry.SELECTHANDLER)).length);
1421
                                                h = (((Geometry) v1)
1422
                                                                .getHandlers(Geometry.SELECTHANDLER))[0];
1423
                                                h2 = (((Geometry) v2)
1424
                                                                .getHandlers(Geometry.SELECTHANDLER))[0];
1425

    
1426
                                                assertEquals(0.0, h.getPoint().distance(h2.getPoint()),
1427
                                                                0.000001);
1428

    
1429
                                                h = (((Geometry) v1)
1430
                                                                .getHandlers(Geometry.SELECTHANDLER))[(((Geometry) v1)
1431
                                                                .getHandlers(Geometry.SELECTHANDLER)).length - 1];
1432
                                                h2 = (((Geometry) v2)
1433
                                                                .getHandlers(Geometry.SELECTHANDLER))[(((Geometry) v2)
1434
                                                                .getHandlers(Geometry.SELECTHANDLER)).length - 1];
1435

    
1436
                                                assertEquals(0.0, h.getPoint().distance(h2.getPoint()),
1437
                                                                0.000001);
1438

    
1439
                                        } else {
1440
                                                assertEquals(0, v1.compareTo(v2));
1441
                                        }
1442
                                }
1443

    
1444

    
1445
                        }
1446
                } catch (Exception e) {
1447
                        e.printStackTrace();
1448
                        fail();
1449
                }
1450

    
1451
                coll.dispose();
1452
                coll2.dispose();
1453

    
1454
                try {
1455
                        store.dispose();
1456
                        store2.dispose();
1457
                } catch (CloseException e) {
1458
                        e.printStackTrace();
1459
                        fail();
1460
                }
1461

    
1462
        }
1463

    
1464
        public void cloneFeatureTypeTest() {
1465

    
1466
                org.gvsig.fmap.data.feature.file.dbf.Register.selfRegister();
1467

    
1468
                DataManager dsm = DataManager.getManager();
1469

    
1470
                DataStoreParameters dp;
1471
                try {
1472
                        dp = dsm.createDataStoreParameters(DBFStore.DATASTORE_NAME);
1473
                } catch (InitializeException e1) {
1474
                        e1.printStackTrace();
1475
                        fail();
1476
                        return;
1477
                }
1478
                ((DBFStoreParameters) dp).setFile(dbffile);
1479

    
1480
                FeatureStore fs = null;
1481
                try {
1482
                        fs = (FeatureStore) dsm.createDataStore(dp);
1483
                } catch (InitializeException e) {
1484
                        e.printStackTrace();
1485
                        fail("Exception:" + e);
1486
                        return;
1487
                }
1488
                assertNotNull("Can't create Feature Store", fs);
1489

    
1490
                compareFeatureType(fs.getDefaultFeatureType(), fs
1491
                                .getDefaultFeatureType()
1492
                                .cloneFeatureType());
1493

    
1494
                FeatureCollection fc = null;
1495

    
1496
                try {
1497
                        fc = (FeatureCollection) fs.getDataCollection();
1498
                } catch (ReadException e1) {
1499
                        e1.printStackTrace();
1500
                        fail();
1501
                        return;
1502
                }
1503

    
1504

    
1505
                compareFeatureType(fs.getDefaultFeatureType(), fc.getFeatureType());
1506

    
1507

    
1508

    
1509
                fc.dispose();
1510

    
1511
                try {
1512
                        fs.addEvaluatedAttribute(null, "eva1",
1513
                                        FeatureAttributeDescriptor.TYPE_STRING, "lower(' ')", null);
1514
                } catch (ReadException e1) {
1515
                        e1.printStackTrace();
1516
                        fail();
1517
                        return;
1518
                }
1519

    
1520

    
1521
                compareFeatureType(fs.getDefaultFeatureType(), fs
1522
                                .getDefaultFeatureType().cloneFeatureType());
1523

    
1524
                try {
1525
                        fs.dispose();
1526
                } catch (CloseException e) {
1527
                        e.printStackTrace();
1528
                        fail();
1529
                        return;
1530
                }
1531
        }
1532

    
1533
        private void compareFeatureType(FeatureType original, FeatureType copy) {
1534
                AttributeDescriptor attr1, attr2;
1535

    
1536
                assertEquals(original.size(), copy.size());
1537
                assertEquals(original.getDefaultGeometry(), copy.getDefaultGeometry());
1538

    
1539
                int i = 0;
1540

    
1541
                for (i = 0; i < original.size(); i++) {
1542
                        attr1 = (AttributeDescriptor) original.get(i);
1543
                        attr2 = (AttributeDescriptor) copy.get(i);
1544

    
1545
                        assertEquals(attr1.getName(), attr2.getName());
1546
                        assertEquals(attr1.getDataType(), attr2.getDataType());
1547
                        assertEquals(attr1.getDefaultValue(), attr2.getDefaultValue());
1548
                        assertEquals(attr1.ordinal(), attr2.ordinal());
1549
                        assertEquals(attr1.originalPosition(), attr2.originalPosition());
1550
                        assertEquals(attr1.isEvaluated(), attr2.isEvaluated());
1551
                        assertEquals(attr1.getGeometryType(), attr2.getGeometryType());
1552
                        assertEquals(attr1.getSize(), attr2.getSize());
1553
                        assertEquals(attr1.getSRS(), attr2.getSRS());
1554
                        assertEquals(attr1.getPrecision(), attr2.getPrecision());
1555
                        assertEquals(attr1.getExpression(), attr2.getExpression());
1556
                        assertEquals(attr1.isAllowNull(), attr2.isAllowNull());
1557
                        assertEquals(attr1.isPrimaryKey(), attr2.isPrimaryKey());
1558
                        assertEquals(attr1.isReadOnly(), attr2.isReadOnly());
1559
                }
1560

    
1561
        }
1562

    
1563
        protected static Resource getResource(FileStoreParameters params) {
1564
                Resource tmpRes = null;
1565
                Object obj = null;
1566

    
1567
                ResourceManager resMan = ResourceManager.getResourceManager();
1568

    
1569
                Iterator iter = resMan.iterator();
1570
                while (iter.hasNext()) {
1571
                        obj = iter.next();
1572
                        if (obj instanceof FileResource) {
1573
                                tmpRes = (Resource) obj;
1574
                                if (((FileResource) tmpRes).getFile().getAbsoluteFile().equals(
1575
                                                params.getFile().getAbsoluteFile())) {
1576
                                        return tmpRes;
1577
                                }
1578
                        }
1579
                }
1580
                return null;
1581
        }
1582

    
1583
        private void mapParamsBaseTest(DataParameters params) {
1584
                Exception e = null;
1585
                try {
1586
                        params.put(new Integer(0), "GEOMETRY_x");
1587
                } catch (Exception e1) {
1588
                        e = e1;
1589
                }
1590
                assertNull(e);
1591

    
1592
                e = null;
1593
                try {
1594
                        params.put("kk", "GEOMETRY_x");
1595
                } catch (IllegalArgumentException e1) {
1596
                        e = e1;
1597
                }
1598
                assertNull(e);
1599

    
1600
                e = null;
1601
                try {
1602
                        params.entrySet();
1603
                } catch (UnsupportedOperationException e1) {
1604
                        e = e1;
1605
                }
1606
                assertNull(e);
1607

    
1608
                e = null;
1609
                try {
1610
                        params.keySet().add("hola");
1611
                } catch (UnsupportedOperationException e1) {
1612
                        e = e1;
1613
                }
1614
                assertNotNull(e);
1615

    
1616
                e = null;
1617
                try {
1618
                        params.keySet().remove("storeSecondary");
1619
                } catch (UnsupportedOperationException e1) {
1620
                        e = e1;
1621
                }
1622
                assertNotNull(e);
1623

    
1624
                e = null;
1625
                try {
1626
                        params.values().remove("GEOMETRY_x");
1627
                } catch (UnsupportedOperationException e1) {
1628
                        e = e1;
1629
                }
1630
                assertNotNull(e);
1631

    
1632
                e = null;
1633
                try {
1634
                        params.values().add("GEOMETRY_x");
1635
                } catch (UnsupportedOperationException e1) {
1636
                        e = e1;
1637
                }
1638
                assertNotNull(e);
1639

    
1640
        }
1641

    
1642
}