Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dataFile / src-test / org / gvsig / fmap / data / feature / joinstore / JoinStoreTest.java @ 24250

History | View | Annotate | Download (14.4 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.DataManager;
36
import org.gvsig.fmap.data.exceptions.CloseException;
37
import org.gvsig.fmap.data.exceptions.InitializeException;
38
import org.gvsig.fmap.data.exceptions.ReadException;
39
import org.gvsig.fmap.data.feature.AttributeDescriptor;
40
import org.gvsig.fmap.data.feature.Feature;
41
import org.gvsig.fmap.data.feature.FeatureSet;
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.fmap.data.impl.DefaultDataManager;
50
import org.gvsig.tools.exception.BaseException;
51

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

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

    
65
        protected JoinFeatureStoreParameters getDefaultParameters() {
66

    
67
                DataManager dm = DefaultDataManager.getManager();
68

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

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

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

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

    
95
                return join_params;
96
        }
97

    
98

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

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

    
111
                FeatureType fType = join.getDefaultFeatureType();
112

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

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

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

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

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

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

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

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

    
156
                FeatureSet fColl = null;
157
                try {
158
                        fColl = (FeatureSet) join.getDataSet();
159
                } catch (ReadException e) {
160
                        e.printStackTrace();
161
                        fail();
162
                }
163

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

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

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

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

    
189
                assertEquals(2, fColl.getDefaultFeatureType().size());
190

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

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

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

    
211
        }
212

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

    
218

    
219
                FeatureSet fColl = null;
220

    
221

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
350
        }
351

    
352

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

    
358

    
359
                FeatureSet fColl = null;
360

    
361

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

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

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

    
383
                iter = fColl.iterator();
384

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

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

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

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

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

    
420

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
515
                assertEquals(10, join_params.size());
516

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

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

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

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

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

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

    
562
                assertEquals(10, join_params.values().size());
563
                assertEquals(10, join_params.keySet().size());
564

    
565
                join_params.clear();
566
                assertEquals(8, join_params.values().size());
567
                assertEquals(8, join_params.keySet().size());
568

    
569
        }
570

    
571
}