Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataFile / src-test / org / gvsig / fmap / data / feature / joinstore / JoinStoreTest.java @ 23543

History | View | Annotate | Download (14.3 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22

    
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2008 IVER T.I. S.A.   {{Task}}
26
*/
27

    
28
package org.gvsig.fmap.data.feature.joinstore;
29

    
30
import java.io.File;
31
import java.util.Iterator;
32

    
33
import junit.framework.TestCase;
34

    
35
import org.gvsig.fmap.data.CloseException;
36
import org.gvsig.fmap.data.DataManager;
37
import org.gvsig.fmap.data.InitializeException;
38
import org.gvsig.fmap.data.ReadException;
39
import org.gvsig.fmap.data.feature.AttributeDescriptor;
40
import org.gvsig.fmap.data.feature.Feature;
41
import org.gvsig.fmap.data.feature.FeatureCollection;
42
import org.gvsig.fmap.data.feature.FeatureStore;
43
import org.gvsig.fmap.data.feature.FeatureType;
44
import org.gvsig.fmap.data.feature.file.dbf.DBFStore;
45
import org.gvsig.fmap.data.feature.file.dbf.DBFStoreParameters;
46
import org.gvsig.fmap.data.feature.file.shp.SHPStore;
47
import org.gvsig.fmap.data.feature.file.shp.SHPStoreParameters;
48
import org.gvsig.fmap.data.feature.visitor.PrintlnFeaturesVisitor;
49
import org.gvsig.tools.exception.BaseException;
50

    
51
public class JoinStoreTest extends TestCase {
52
        private File dbf_carrilbici = new File(JoinStoreTest.class.getResource(
53
                        "data/Tbcarrilbici.dbf").getFile());
54
        private File shp_carrilbici = new File(JoinStoreTest.class.getResource(
55
                        "data/carrilbici.shp").getFile());
56

    
57
        protected void setUp() throws Exception {
58
                super.setUp();
59
                Register.selfRegister();
60
                org.gvsig.fmap.data.feature.file.dbf.Register.selfRegister();
61
                org.gvsig.fmap.data.feature.file.shp.Register.selfRegister();
62
        }
63

    
64
        protected JoinFeatureStoreParameters getDefaultParameters() {
65

    
66
                DataManager dm = DataManager.getManager();
67

    
68
                DBFStoreParameters dbf_params = null;
69
                SHPStoreParameters shp_params = null;
70
                JoinFeatureStoreParameters join_params = null;
71

    
72
                try {
73
                        dbf_params = (DBFStoreParameters) dm
74
                                        .createDataStoreParameters(DBFStore.DATASTORE_NAME);
75
                        shp_params = (SHPStoreParameters) dm
76
                                        .createDataStoreParameters(SHPStore.DATASTORE_NAME);
77
                        join_params = (JoinFeatureStoreParameters) dm
78
                                        .createDataStoreParameters(JoinFeatureStore.DATASTORE_NAME);
79

    
80
                } catch (InitializeException e) {
81
                        e.printStackTrace();
82
                        fail();
83
                }
84
                shp_params.setFile(shp_carrilbici);
85
                dbf_params.setFile(dbf_carrilbici);
86

    
87
                join_params.setStorePrimary(shp_params);
88
                join_params.setStoreSecondary(dbf_params);
89
                join_params.setLinkFieldPrimary("CODIGO");
90
                join_params.setLinkFieldSecondary("CODIGO");
91
                join_params.setFieldNamePrefix("l_");
92
                join_params.setDefaultGeometry("GEOMETRY");
93

    
94
                return join_params;
95
        }
96

    
97

    
98
        public void testShp_Dbf_carrilbici_base() {
99
                DataManager dm = DataManager.getManager();
100
                JoinFeatureStoreParameters join_params = this.getDefaultParameters();
101
                FeatureStore join = null;
102

    
103
                try {
104
                        join = (FeatureStore) dm.createDataStore(join_params);
105
                } catch (InitializeException e) {
106
                        e.printStackTrace();
107
                        fail();
108
                }
109

    
110
                FeatureType fType = join.getDefaultFeatureType();
111

    
112
                // num columnas: shp 4(3+geo) + dbg 3 = 7
113
                assertEquals(7, fType.size());
114

    
115
                //Comprobaci?n de atributos
116
                AttributeDescriptor attr;
117
                // 0 - LEVEL (Int)
118
                attr = (AttributeDescriptor) fType.get(0);
119
                assertTrue(attr.getName().equals("LEVEL"));
120
                assertEquals(AttributeDescriptor.TYPE_INT, attr.getDataType());
121

    
122
                // 1 - COLOR (Int)
123
                attr = (AttributeDescriptor) fType.get(1);
124
                assertTrue(attr.getName().equals("COLOR"));
125
                assertEquals(AttributeDescriptor.TYPE_INT, attr.getDataType());
126

    
127
                // 2 - CODIGO (Int)
128
                attr = (AttributeDescriptor) fType.get(2);
129
                assertTrue(attr.getName().equals("CODIGO"));
130
                assertEquals(AttributeDescriptor.TYPE_INT, attr.getDataType());
131

    
132
                // 3 - GEOMETRY (geom)
133
                attr = (AttributeDescriptor) fType.get(3);
134
                assertTrue(attr.getName().equals("GEOMETRY"));
135
                assertEquals(AttributeDescriptor.TYPE_GEOMETRY, attr.getDataType());
136

    
137
                // 4 - {pref}CODIGO (int)
138
                attr = (AttributeDescriptor) fType.get(4);
139
                assertTrue(attr.getName().equals(
140
                                join_params.getFieldNamePrefix() + "CODIGO"));
141
                assertEquals(AttributeDescriptor.TYPE_INT, attr.getDataType());
142

    
143
                // 5 - {pref}CALLE (Str)
144
                attr = (AttributeDescriptor) fType.get(5);
145
                assertTrue(attr.getName().equals(
146
                                join_params.getFieldNamePrefix() + "CALLE"));
147
                assertEquals(AttributeDescriptor.TYPE_STRING, attr.getDataType());
148

    
149
                // 6 - {pref}EC_2000 (int)
150
                attr = (AttributeDescriptor) fType.get(6);
151
                assertTrue(attr.getName().equals(
152
                                join_params.getFieldNamePrefix() + "EC_2000"));
153
                assertEquals(AttributeDescriptor.TYPE_INT, attr.getDataType());
154

    
155
                FeatureCollection fColl = null;
156
                try {
157
                        fColl = (FeatureCollection) join.getDataCollection();
158
                } catch (ReadException e) {
159
                        e.printStackTrace();
160
                        fail();
161
                }
162

    
163
                // numero de elementos: shp 43
164
                assertEquals(43, fColl.size());
165
                try {
166
                        fColl.accept(new PrintlnFeaturesVisitor(fType));
167
                } catch (BaseException e) {
168
                        e.printStackTrace();
169
                        fail();
170
                }
171

    
172
                fColl.dispose();
173
                fColl = null;
174

    
175
                //Comprobar los coleccion de "subtipo"
176
                try {
177
                        fColl = (FeatureCollection) join.getDataCollection(new String[] {
178
                                        "CODIGO", join_params.getFieldNamePrefix() + "CALLE" },
179
                                        null, null);
180
                } catch (ReadException e) {
181
                        e.printStackTrace();
182
                        fail();
183
                }
184

    
185
                // numero de elementos: shp 43
186
                assertEquals(43, fColl.size());
187

    
188
                assertEquals(2, fColl.getFeatureType().size());
189

    
190
                // numero de elementos: shp 43
191
                assertEquals(43, fColl.size());
192
                try {
193
                        fColl.accept(new PrintlnFeaturesVisitor(fColl.getFeatureType()));
194
                } catch (BaseException e) {
195
                        e.printStackTrace();
196
                        fail();
197
                }
198

    
199
                fColl.dispose();
200
                fColl = null;
201

    
202
                try {
203
                        join.dispose();
204
                } catch (CloseException e) {
205
                        e.printStackTrace();
206
                        fail();
207
                        return;
208
                }
209

    
210
        }
211

    
212
        public void testShp_Dbf_carrilbici_order() {
213
                DataManager dm = DataManager.getManager();
214
                JoinFeatureStoreParameters join_params = this.getDefaultParameters();
215
                FeatureStore join = null;
216

    
217

    
218
                FeatureCollection fColl = null;
219

    
220

    
221
                try {
222
                        join = (FeatureStore) dm.createDataStore(join_params);
223
                } catch (InitializeException e) {
224
                        e.printStackTrace();
225
                        fail();
226
                }
227

    
228
                //Comprobar los coleccion con orden
229
                try {
230
                        fColl = (FeatureCollection) join.getDataCollection(join
231
                                        .getDefaultFeatureType(), null, "CODIGO");
232
                } catch (ReadException e) {
233
                        e.printStackTrace();
234
                        fail();
235
                }
236

    
237
                // numero de elementos: shp 43
238
                assertEquals(43, fColl.size());
239
                Comparable v1 = null;
240
                Comparable v2 = null;
241
                Iterator iter;
242
                boolean elPrimero;
243
                Feature feature;
244

    
245
                iter = fColl.iterator();
246
                elPrimero = true;
247

    
248
                while (iter.hasNext()) {
249
                        feature = (Feature) iter.next();
250
                        if (elPrimero) {
251
                                v1 = (Comparable) feature.get("CODIGO");
252
                                elPrimero = false;
253
                        } else {
254
                                v2 = (Comparable) feature.get("CODIGO");
255
                                if (v1.compareTo(v2) > 0) {
256
                                        fail();
257
                                }
258
                                v1 = v2;
259
                        }
260
                }
261

    
262
                fColl.dispose();
263
                fColl = null;
264

    
265
                //Comprobar los coleccion con orden de la segunda tabla
266
                try {
267
                        fColl = (FeatureCollection) join.getDataCollection(join
268
                                        .getDefaultFeatureType(), null, join_params
269
                                        .getFieldNamePrefix()
270
                                        + "CALLE");
271
                } catch (ReadException e) {
272
                        e.printStackTrace();
273
                        fail();
274
                }
275

    
276
                // numero de elementos: shp 43
277
                assertEquals(43, fColl.size());
278
                iter = fColl.iterator();
279
                elPrimero = true;
280

    
281
                while (iter.hasNext()) {
282
                        feature = (Feature) iter.next();
283
                        if (elPrimero) {
284
                                v1 = (Comparable) feature.get(join_params.getFieldNamePrefix()
285
                                                + "CALLE");
286
                                elPrimero = false;
287
                                System.out.println(v1);
288
                        } else {
289
                                v2 = (Comparable) feature.get(join_params.getFieldNamePrefix()
290
                                                + "CALLE");
291
                                System.out.println(v2);
292
                                if (v1 == null || v2 == null) {
293
                                        continue;
294
                                }
295
                                if (v1.compareTo(v2) > 0) {
296
                                        fail();
297
                                }
298
                                v1 = v2;
299
                        }
300
                }
301

    
302
                //Comprobar los coleccion con orden de la segunda tabla y subtipo
303
                try {
304
                        fColl = (FeatureCollection) join.getDataCollection(new String[] {
305
                                        "CODIGO", join_params.getFieldNamePrefix() + "CALLE" },
306
                                        null, join_params.getFieldNamePrefix() + "CALLE");
307
                } catch (ReadException e) {
308
                        e.printStackTrace();
309
                        fail();
310
                }
311

    
312
                // numero de elementos: shp 43
313
                assertEquals(43, fColl.size());
314
                iter = fColl.iterator();
315
                elPrimero = true;
316

    
317
                while (iter.hasNext()) {
318
                        feature = (Feature) iter.next();
319
                        if (elPrimero) {
320
                                v1 = (Comparable) feature.get(join_params.getFieldNamePrefix()
321
                                                + "CALLE");
322
                                elPrimero = false;
323
                                System.out.println(v1);
324
                        } else {
325
                                v2 = (Comparable) feature.get(join_params.getFieldNamePrefix()
326
                                                + "CALLE");
327
                                System.out.println(v2);
328
                                if (v1 == null || v2 == null) {
329
                                        continue;
330
                                }
331
                                if (v1.compareTo(v2) > 0) {
332
                                        fail();
333
                                }
334
                                v1 = v2;
335
                        }
336
                }
337

    
338
                fColl.dispose();
339
                fColl = null;
340

    
341
                try {
342
                        join.dispose();
343
                } catch (CloseException e) {
344
                        e.printStackTrace();
345
                        fail();
346
                        return;
347
                }
348

    
349
        }
350

    
351

    
352
        public void testShp_Dbf_carrilbici_filter() {
353
                DataManager dm = DataManager.getManager();
354
                JoinFeatureStoreParameters join_params = this.getDefaultParameters();
355
                FeatureStore join = null;
356

    
357

    
358
                FeatureCollection fColl = null;
359

    
360

    
361
                try {
362
                        join = (FeatureStore) dm.createDataStore(join_params);
363
                } catch (InitializeException e) {
364
                        e.printStackTrace();
365
                        fail();
366
                }
367

    
368
                //Filtro simple
369
                try {
370
                        fColl = (FeatureCollection) join.getDataCollection(join
371
                                        .getDefaultFeatureType(), "CODIGO < 10", null);
372
                } catch (ReadException e) {
373
                        e.printStackTrace();
374
                        fail();
375
                }
376

    
377
                // numero de elementos: shp 13 (codigo < 10)
378
                assertEquals(13, fColl.size());
379
                Iterator iter;
380
                Feature feature;
381

    
382
                iter = fColl.iterator();
383

    
384
                while (iter.hasNext()) {
385
                        feature = (Feature) iter.next();
386
                        if (feature.getInt("CODIGO") >= 10) {
387
                                fail();
388
                        }
389
                }
390

    
391
                fColl.dispose();
392
                fColl = null;
393

    
394
                //Filtro de campo de segunda tabla
395
                try {
396
                        fColl = (FeatureCollection) join.getDataCollection(join
397
                                        .getDefaultFeatureType(), "lower("
398
                                        + join_params
399
                                        .getFieldNamePrefix()
400
                                        + "CALLE) like 'a%'",
401
                                        null);
402
                } catch (ReadException e) {
403
                        e.printStackTrace();
404
                        fail();
405
                }
406

    
407
                // numero de elementos: shp 9 (lower(l_CALLE) like 'a%')
408
                assertEquals(9, fColl.size());
409
                iter = fColl.iterator();
410

    
411
                while (iter.hasNext()) {
412
                        feature = (Feature) iter.next();
413
                        if (!feature.getString(join_params.getFieldNamePrefix() + "CALLE")
414
                                        .toLowerCase().startsWith("a")) {
415
                                fail();
416
                        }
417
                }
418

    
419

    
420
                //Filtro de campo de segunda tabla con subtipo
421
                try {
422
                        fColl = (FeatureCollection) join.getDataCollection(new String[] {
423
                                        "CODIGO", join_params.getFieldNamePrefix() + "CALLE" },
424
                                        "lower(" + join_params.getFieldNamePrefix()
425
                                                        + "CALLE) like 'a%'", null);
426
                } catch (ReadException e) {
427
                        e.printStackTrace();
428
                        fail();
429
                }
430

    
431
                // numero de elementos: shp 9 (lower(l_CALLE) like 'a%')
432
                assertEquals(9, fColl.size());
433
                iter = fColl.iterator();
434

    
435
                while (iter.hasNext()) {
436
                        feature = (Feature) iter.next();
437
                        if (!feature.getString(join_params.getFieldNamePrefix() + "CALLE")
438
                                        .toLowerCase().startsWith("a")) {
439
                                fail();
440
                        }
441
                }
442

    
443
                fColl.dispose();
444
                fColl = null;
445

    
446
                try {
447
                        join.dispose();
448
                } catch (CloseException e) {
449
                        e.printStackTrace();
450
                        fail();
451
                        return;
452
                }
453
        }
454

    
455
        public void test_params_map() {
456
                DataManager dm = DataManager.getManager();
457

    
458
                DBFStoreParameters dbf_params = null;
459
                SHPStoreParameters shp_params = null;
460
                JoinFeatureStoreParameters join_params = null;
461

    
462
                try {
463
                        dbf_params = (DBFStoreParameters) dm
464
                                        .createDataStoreParameters(DBFStore.DATASTORE_NAME);
465
                        shp_params = (SHPStoreParameters) dm
466
                                        .createDataStoreParameters(SHPStore.DATASTORE_NAME);
467
                        join_params = (JoinFeatureStoreParameters) dm
468
                                        .createDataStoreParameters(JoinFeatureStore.DATASTORE_NAME);
469

    
470
                } catch (InitializeException e) {
471
                        e.printStackTrace();
472
                        fail();
473
                }
474
                shp_params.setFile(shp_carrilbici);
475
                dbf_params.setFile(dbf_carrilbici);
476

    
477
                join_params.setStorePrimary(shp_params);
478
                join_params.setStoreSecondary(dbf_params);
479
                join_params.setLinkFieldPrimary("CODIGO");
480
                join_params.setLinkFieldSecondary("CODIGO");
481
                join_params.setFieldNamePrefix("l_");
482
                join_params.setDefaultGeometry("GEOMETRY");
483

    
484
                assertEquals("l_", join_params.get("fieldNamePrefix"));
485
                assertEquals("GEOMETRY", join_params.get("defaultGeometry"));
486
                join_params.put("defaultGeometry", "GEOMETRY_x");
487
                assertEquals("GEOMETRY_x", join_params.get("defaultGeometry"));
488
                assertEquals("GEOMETRY_x", join_params.getDefaultGeometry());
489

    
490
                Exception e = null;
491
                try {
492
                        join_params.put(new Integer(0), "GEOMETRY_x");
493
                } catch (Exception e1) {
494
                        e = e1;
495
                }
496
                assertNotNull(e);
497

    
498
                e = null;
499
                try {
500
                        join_params.put("kk", "GEOMETRY_x");
501
                } catch (IllegalArgumentException e1) {
502
                        e = e1;
503
                }
504
                assertNotNull(e);
505

    
506
                e = null;
507
                try {
508
                        join_params.put("storeSecondary", "GEOMETRY_x");
509
                } catch (Exception e1) {
510
                        e = e1;
511
                }
512
                assertNotNull(e);
513

    
514
                assertEquals(8, join_params.size());
515

    
516
                e=null;
517
                try {
518
                        join_params.entrySet();
519
                } catch (UnsupportedOperationException e1) {
520
                        e = e1;
521
                }
522
                assertNotNull(e);
523

    
524
                e = null;
525
                try {
526
                        join_params.keySet().add("hola");
527
                } catch (UnsupportedOperationException e1) {
528
                        e = e1;
529
                }
530
                assertNotNull(e);
531

    
532
                e = null;
533
                try {
534
                        join_params.keySet().remove("storeSecondary");
535
                } catch (UnsupportedOperationException e1) {
536
                        e = e1;
537
                }
538
                assertNotNull(e);
539

    
540
                e = null;
541
                try {
542
                        join_params.values().remove("GEOMETRY_x");
543
                } catch (UnsupportedOperationException e1) {
544
                        e = e1;
545
                }
546
                assertNotNull(e);
547

    
548
                e = null;
549
                try {
550
                        join_params.values().add("GEOMETRY_x");
551
                } catch (UnsupportedOperationException e1) {
552
                        e = e1;
553
                }
554
                assertNotNull(e);
555

    
556
                assertTrue(join_params.containsValue("GEOMETRY_x"));
557
                assertTrue(join_params.values().contains("GEOMETRY_x"));
558
                assertTrue(join_params.keySet().contains("storeSecondary"));
559
                assertTrue(join_params.containsKey("storeSecondary"));
560

    
561
                assertEquals(8, join_params.values().size());
562
                assertEquals(8, join_params.keySet().size());
563

    
564
        }
565

    
566
}