Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dataFile / src-test / org / gvsig / fmap / data / feature / file / DataStoreTest.java @ 23303

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

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

    
46
public class DataStoreTest extends TestCase {
47

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

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

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

    
64
        }
65

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

    
71
                        DataManager dsm = DataManager.getManager();
72

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

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

    
87
                        //                        fs.open();
88

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

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

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

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

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

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

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

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

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

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

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

    
155
                        it = fc.iterator();
156

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

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

    
193
                        /// CON  EDICION
194

    
195
                        fs.startEditing();
196

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

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

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

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

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

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

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

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

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

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

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

    
291
                        it = fc.iterator();
292

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

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

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

    
347
                        fs.cancelEditing();
348

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

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

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

    
363
        }
364

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

    
371
                        DataManager dsm = DataManager.getManager();
372

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

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

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

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

    
391
                        //                        fs.open();
392

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

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

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

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

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

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

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

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

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

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

    
460
                        it = fc.iterator();
461

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

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

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

    
515
                        /// CON  EDICION
516

    
517
                        fs.startEditing();
518

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

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

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

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

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

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

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

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

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

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

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

    
611
                        it = fc.iterator();
612

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

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

    
655
                        fs.cancelEditing();
656

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

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

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

    
676
                        DataManager dsm = DataManager.getManager();
677

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

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

    
694
                        //                        fs.open();
695

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

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

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

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

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

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

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

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

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

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

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

    
760
                        it = fc.iterator();
761

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

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

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

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

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

    
819
                        DataManager dsm = DataManager.getManager();
820

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

    
834
                        //                        fs.open();
835

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

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

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

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

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

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

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

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

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

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

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

    
903
                        it = fc.iterator();
904

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

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

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

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

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

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

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

    
972
        //        }
973

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

    
978

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

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

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

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

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

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

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

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

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

    
1067
                featureCollection.dispose();
1068

    
1069
                if (fs.isEditable() && testEdit) {
1070
                        try {
1071
                                fs.finishEditing();
1072
                        } catch (WriteException e) {
1073
                                e.printStackTrace();
1074
                                fail("Exception: " + e);
1075
                        } catch (ReadException e) {
1076
                                e.printStackTrace();
1077
                                fail("Exception: " + e);
1078
                        }
1079
                }
1080
                //                try {
1081
                //                        fs.close();
1082
                //                } catch (CloseException e) {
1083
                //                        e.printStackTrace();
1084
                //                        fail("Exception: "+e);
1085
                //                }
1086
                try {
1087
                        fs.dispose();
1088
                } catch (CloseException e) {
1089
                        // TODO Auto-generated catch block
1090
                        e.printStackTrace();
1091
                }
1092
                persistenceTest(dp);
1093
        }
1094

    
1095
        public static void doFileResourceTest(FileStoreParameters params) {
1096
                doFileResourceTest(params, true);
1097
        }
1098

    
1099
        public static void doFileResourceTest(FileStoreParameters params,
1100
                        boolean testEdit) {
1101
                DataManager manager = DataManager.getManager();
1102

    
1103
                ResourceManager resMan = ResourceManager.getResourceManager();
1104

    
1105
                AbstractFeatureStore store = null;
1106
                AbstractFeatureStore store2 = null;
1107
                AbstractFeatureStore store3 = null;
1108
                try {
1109
                        store = (AbstractFeatureStore) manager.createDataStore(params);
1110
                        store2 = (AbstractFeatureStore) manager.createDataStore(params);
1111
                        store3 = (AbstractFeatureStore) manager.createDataStore(params);
1112
                } catch (InitializeException e1) {
1113
                        e1.printStackTrace();
1114
                        fail();
1115
                }
1116

    
1117
                int i = 0;
1118
                Resource res = null;
1119
                Resource tmpRes = null;
1120
                Object obj = null;
1121

    
1122
                Iterator iter = resMan.iterator();
1123
                while (iter.hasNext()) {
1124
                        obj = iter.next();
1125
                        if (obj instanceof FileResource) {
1126
                                tmpRes = (Resource) obj;
1127
                                if (((FileResource) tmpRes).getFile().getAbsoluteFile().equals(
1128
                                                params.getFile().getAbsoluteFile())) {
1129
                                        i++;
1130
                                        res = tmpRes;
1131
                                }
1132
                        }
1133
                }
1134

    
1135
                assertEquals(1, i);
1136

    
1137
                assertEquals(3, res.getRefencesCount());
1138

    
1139
                try {
1140
                        store.close();
1141
                } catch (CloseException e1) {
1142
                        e1.printStackTrace();
1143
                        fail();
1144
                }
1145

    
1146
                assertEquals(false, res.isOpen());
1147

    
1148
                DataCollection coll = null;
1149

    
1150
                try {
1151
                        coll = store.getDataCollection();
1152
                } catch (ReadException e1) {
1153
                        e1.printStackTrace();
1154
                        fail();
1155
                }
1156

    
1157
                coll.iterator().next();
1158
                //                coll.size();
1159

    
1160
                assertEquals(true, res.isOpen());
1161

    
1162
                coll.dispose();
1163

    
1164
                if (store.isEditable() && testEdit) {
1165
                        /* Test edition notification */
1166

    
1167
                        int fCountOrg = 0;
1168
                        int fCountFin = 0;
1169
                        try {
1170
                                fCountOrg = store.getDataCollection().size();
1171
                        } catch (ReadException e2) {
1172
                                // TODO Auto-generated catch block
1173
                                e2.printStackTrace();
1174
                                fail("Exception: " + e2);
1175
                        }
1176
                        try {
1177
                                store.startEditing();
1178
                        } catch (ReadException e1) {
1179
                                e1.printStackTrace();
1180
                                fail("Exception: " + e1);
1181
                        }
1182

    
1183
                        try {
1184
                                coll = store2.getDataCollection();
1185
                        } catch (ReadException e1) {
1186
                                e1.printStackTrace();
1187
                                fail("Exception: " + e1);
1188
                        }
1189

    
1190
                        try {
1191
                                store.finishEditing();
1192
                        } catch (WriteException e1) {
1193
                                e1.printStackTrace();
1194
                                fail("Exception: " + e1);
1195
                        } catch (ReadException e1) {
1196
                                e1.printStackTrace();
1197
                                fail("Exception: " + e1);
1198
                        }
1199

    
1200
                        try {
1201
                                fCountFin = store.getDataCollection().size();
1202
                        } catch (ReadException e2) {
1203
                                // TODO Auto-generated catch block
1204
                                e2.printStackTrace();
1205
                                fail("Exception: " + e2);
1206
                        }
1207

    
1208
                        assertEquals(fCountOrg, fCountFin);
1209

    
1210
                        boolean isOk = false;
1211
                        try {
1212
                                coll.iterator().next();
1213
                        } catch (Exception e) {
1214
                                isOk = true;
1215
                        }
1216
                        assertTrue("Resource Changed Notification fails", isOk);
1217

    
1218
                        coll.dispose();
1219

    
1220
                        long time = System.currentTimeMillis();
1221
                        while ((System.currentTimeMillis() - time) < 1000) {
1222
                                //sleep
1223
                        }
1224

    
1225
                        ((FileResource) res).getFile().setLastModified(
1226
                                        System.currentTimeMillis());
1227

    
1228
                        isOk = false;
1229
                        try {
1230
                                store.getDataCollection().iterator().next();
1231
                        } catch (Exception e) {
1232
                                isOk = true;
1233
                        }
1234
                        assertTrue("Resource Changed Notification fails", isOk);
1235

    
1236
                        /* Test edition notification END */
1237

    
1238
                }
1239

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

    
1247
                assertEquals(2, res.getRefencesCount());
1248

    
1249
                try {
1250
                        store2.dispose();
1251
                } catch (CloseException e1) {
1252
                        e1.printStackTrace();
1253
                        fail();
1254
                }
1255

    
1256
                assertEquals(1, res.getRefencesCount());
1257

    
1258
                try {
1259
                        store.dispose();
1260
                } catch (CloseException e1) {
1261
                        e1.printStackTrace();
1262
                        fail();
1263
                }
1264

    
1265
                assertEquals(0, res.getRefencesCount());
1266
                res = null;
1267

    
1268
                i = 0;
1269
                iter = resMan.iterator();
1270
                while (iter.hasNext()) {
1271
                        obj = iter.next();
1272
                        if (obj instanceof FileResource) {
1273
                                tmpRes = (Resource) obj;
1274
                                if (((FileResource) tmpRes).getFile().getAbsoluteFile().equals(
1275
                                                params.getFile().getAbsoluteFile())) {
1276
                                        i++;
1277
                                        res = tmpRes;
1278
                                }
1279
                        }
1280
                }
1281

    
1282
                assertEquals(0, i);
1283

    
1284
                doPrepareFileResourceTest(params);
1285

    
1286
        }
1287

    
1288
        public static void doPrepareFileResourceTest(FileStoreParameters params) {
1289
                DataManager manager = DataManager.getManager();
1290

    
1291
                ResourceManager resMan = ResourceManager.getResourceManager();
1292

    
1293
                File originalFile = params.getFile();
1294
                params.setFile(new File(originalFile.getName()));
1295

    
1296
                Observer obs = new PrepareResourceObserver(originalFile);
1297

    
1298
                resMan.addObserver(obs);
1299

    
1300
                AbstractFeatureStore store = null;
1301
                try {
1302
                        store = (AbstractFeatureStore) manager.createDataStore(params);
1303
                } catch (InitializeException e1) {
1304
                        e1.printStackTrace();
1305
                        fail();
1306
                } catch (Exception e2) {
1307
                        e2.printStackTrace();
1308
                        fail();
1309
                }
1310

    
1311
                try {
1312
                        store.getDataCollection().iterator().next();
1313
                } catch (ReadException e) {
1314
                        // TODO Auto-generated catch block
1315
                        e.printStackTrace();
1316
                        fail();
1317
                }
1318

    
1319
                try {
1320
                        store.close();
1321
                } catch (CloseException e) {
1322
                        // TODO Auto-generated catch block
1323
                        e.printStackTrace();
1324
                        fail();
1325
                }
1326
                try {
1327
                        store.dispose();
1328
                } catch (CloseException e) {
1329
                        // TODO Auto-generated catch block
1330
                        e.printStackTrace();
1331
                        fail();
1332
                }
1333

    
1334
        }
1335

    
1336
        private void persistenceTest(DataStoreParameters params) {
1337
                DataManager manager = DataManager.getManager();
1338

    
1339
                DataStoreParameters params2 = null;
1340
                XMLEntity paramsXML = null;
1341
                XMLEntity params2XML = null;
1342

    
1343
                paramsXML = params.getXMLEntity();
1344

    
1345
                try {
1346
                        params2 = manager.createDataStoreParameters(paramsXML);
1347
                } catch (InitializeException e) {
1348
                        e.printStackTrace();
1349
                        fail();
1350
                }
1351

    
1352
                params2XML = params2.getXMLEntity();
1353

    
1354
                assertEquals(paramsXML.toString(), params2XML.toString());
1355

    
1356
                FeatureStore store = null;
1357
                XMLEntity storeXML = null;
1358
                FeatureStore store2 = null;
1359
                XMLEntity store2XML = null;
1360

    
1361
                try {
1362
                        store = (FeatureStore) manager.createDataStore(params);
1363
                } catch (InitializeException e) {
1364
                        e.printStackTrace();
1365
                        fail();
1366
                }
1367

    
1368
                try {
1369
                        storeXML = store.getXMLEntity();
1370
                } catch (XMLException e1) {
1371
                        e1.printStackTrace();
1372
                        fail();
1373
                }
1374

    
1375
                assertNotNull(storeXML);
1376

    
1377
                try {
1378
                        store2 = (FeatureStore) manager.createDataStore(storeXML);
1379
                } catch (InitializeException e) {
1380
                        e.printStackTrace();
1381
                        fail();
1382
                }
1383

    
1384
                try {
1385
                        store2XML = store2.getXMLEntity();
1386
                } catch (XMLException e1) {
1387
                        e1.printStackTrace();
1388
                        fail();
1389
                }
1390

    
1391
                assertEquals(storeXML.toString(), store2XML.toString());
1392

    
1393
                FeatureCollection coll = null;
1394
                FeatureCollection coll2 = null;
1395
                Iterator iter = null;
1396
                Iterator iter2 = null;
1397
                Feature feature=null;
1398
                Feature feature2 = null;
1399
                Comparable v1 = null;
1400
                Comparable v2 = null;
1401
                Handler h = null;
1402
                Handler h2 = null;
1403

    
1404

    
1405
                try {
1406
                        coll = (FeatureCollection) store.getDataCollection();
1407
                        coll2 = (FeatureCollection) store2.getDataCollection();
1408
                } catch (ReadException e) {
1409
                        e.printStackTrace();
1410
                        fail();
1411
                }
1412

    
1413
                assertEquals(coll.size(), coll2.size());
1414

    
1415
                iter = coll.iterator();
1416
                iter2 = coll2.iterator();
1417
                int i;
1418
                int featureSize = store.getDefaultFeatureType().size();
1419

    
1420
                try {
1421
                        while (iter.hasNext()) {
1422
                                feature = (Feature) iter.next();
1423
                                feature2 = (Feature) iter2.next();
1424
                                for (i = 0; i < featureSize; i++) {
1425
                                        v1 = (Comparable) feature.get(i);
1426
                                        v2 = (Comparable) feature2.get(i);
1427
                                        if (v1 == null) {
1428
                                                assertNull(v2);
1429
                                        } else if (v1 instanceof Geometry) {
1430
                                                assertTrue(v2 instanceof Geometry);
1431
                                                assertEquals(((Geometry) v1).getType(), ((Geometry) v2)
1432
                                                                .getType());
1433
                                                assertEquals(
1434
                                                                (((Geometry) v1)
1435
                                                                                .getHandlers(Geometry.SELECTHANDLER)).length,
1436
                                                                (((Geometry) v2)
1437
                                                                                .getHandlers(Geometry.SELECTHANDLER)).length);
1438
                                                h = (((Geometry) v1)
1439
                                                                .getHandlers(Geometry.SELECTHANDLER))[0];
1440
                                                h2 = (((Geometry) v2)
1441
                                                                .getHandlers(Geometry.SELECTHANDLER))[0];
1442

    
1443
                                                assertEquals(0.0, h.getPoint().distance(h2.getPoint()),
1444
                                                                0.000001);
1445

    
1446
                                                h = (((Geometry) v1)
1447
                                                                .getHandlers(Geometry.SELECTHANDLER))[(((Geometry) v1)
1448
                                                                .getHandlers(Geometry.SELECTHANDLER)).length - 1];
1449
                                                h2 = (((Geometry) v2)
1450
                                                                .getHandlers(Geometry.SELECTHANDLER))[(((Geometry) v2)
1451
                                                                .getHandlers(Geometry.SELECTHANDLER)).length - 1];
1452

    
1453
                                                assertEquals(0.0, h.getPoint().distance(h2.getPoint()),
1454
                                                                0.000001);
1455

    
1456
                                        } else {
1457
                                                assertEquals(0, v1.compareTo(v2));
1458
                                        }
1459
                                }
1460

    
1461

    
1462
                        }
1463
                } catch (Exception e) {
1464
                        e.printStackTrace();
1465
                        fail();
1466
                }
1467

    
1468
                coll.dispose();
1469
                coll2.dispose();
1470

    
1471
                try {
1472
                        store.dispose();
1473
                        store2.dispose();
1474
                } catch (CloseException e) {
1475
                        e.printStackTrace();
1476
                        fail();
1477
                }
1478

    
1479
        }
1480

    
1481
        public void cloneFeatureTypeTest() {
1482

    
1483
                org.gvsig.fmap.data.feature.file.dbf.Register.selfRegister();
1484

    
1485
                DataManager dsm = DataManager.getManager();
1486

    
1487
                DataStoreParameters dp;
1488
                try {
1489
                        dp = dsm.createDataStoreParameters(DBFStore.DATASTORE_NAME);
1490
                } catch (InitializeException e1) {
1491
                        e1.printStackTrace();
1492
                        fail();
1493
                        return;
1494
                }
1495
                ((DBFStoreParameters) dp).setFile(dbffile);
1496

    
1497
                FeatureStore fs = null;
1498
                try {
1499
                        fs = (FeatureStore) dsm.createDataStore(dp);
1500
                } catch (InitializeException e) {
1501
                        e.printStackTrace();
1502
                        fail("Exception:" + e);
1503
                        return;
1504
                }
1505
                assertNotNull("Can't create Feature Store", fs);
1506

    
1507
                compareFeatureType(fs.getDefaultFeatureType(), fs
1508
                                .getDefaultFeatureType()
1509
                                .cloneFeatureType());
1510

    
1511
                FeatureCollection fc = null;
1512

    
1513
                try {
1514
                        fc = (FeatureCollection) fs.getDataCollection();
1515
                } catch (ReadException e1) {
1516
                        e1.printStackTrace();
1517
                        fail();
1518
                        return;
1519
                }
1520

    
1521

    
1522
                compareFeatureType(fs.getDefaultFeatureType(), fc.getFeatureType());
1523

    
1524

    
1525

    
1526
                fc.dispose();
1527

    
1528
                try {
1529
                        fs.addEvaluatedAttribute(null, "eva1",
1530
                                        FeatureAttributeDescriptor.TYPE_STRING, "lower(' ')", null);
1531
                } catch (ReadException e1) {
1532
                        e1.printStackTrace();
1533
                        fail();
1534
                        return;
1535
                }
1536

    
1537

    
1538
                compareFeatureType(fs.getDefaultFeatureType(), fs
1539
                                .getDefaultFeatureType().cloneFeatureType());
1540

    
1541
                try {
1542
                        fs.dispose();
1543
                } catch (CloseException e) {
1544
                        e.printStackTrace();
1545
                        fail();
1546
                        return;
1547
                }
1548
        }
1549

    
1550
        private void compareFeatureType(FeatureType original, FeatureType copy) {
1551
                AttributeDescriptor attr1, attr2;
1552

    
1553
                assertEquals(original.size(), copy.size());
1554
                assertEquals(original.getDefaultGeometry(), copy.getDefaultGeometry());
1555

    
1556
                int i = 0;
1557

    
1558
                for (i = 0; i < original.size(); i++) {
1559
                        attr1 = (AttributeDescriptor) original.get(i);
1560
                        attr2 = (AttributeDescriptor) copy.get(i);
1561

    
1562
                        assertEquals(attr1.getName(), attr2.getName());
1563
                        assertEquals(attr1.getDataType(), attr2.getDataType());
1564
                        assertEquals(attr1.getDefaultValue(), attr2.getDefaultValue());
1565
                        assertEquals(attr1.ordinal(), attr2.ordinal());
1566
                        assertEquals(attr1.originalPosition(), attr2.originalPosition());
1567
                        assertEquals(attr1.isEvaluated(), attr2.isEvaluated());
1568
                        assertEquals(attr1.getGeometryType(), attr2.getGeometryType());
1569
                        assertEquals(attr1.getSize(), attr2.getSize());
1570
                        assertEquals(attr1.getSRS(), attr2.getSRS());
1571
                        assertEquals(attr1.getPrecision(), attr2.getPrecision());
1572
                        assertEquals(attr1.getExpression(), attr2.getExpression());
1573
                        assertEquals(attr1.isAllowNull(), attr2.isAllowNull());
1574
                        assertEquals(attr1.isPrimaryKey(), attr2.isPrimaryKey());
1575
                        assertEquals(attr1.isReadOnly(), attr2.isReadOnly());
1576
                }
1577

    
1578
        }
1579
}