Statistics
| Revision:

root / trunk / libraries / libFMap_dataDB / src-test / org / gvsig / data / datastores / vectorial / db / jdbc / postgresql / postgresqlTest.java @ 20874

History | View | Annotate | Download (21.1 KB)

1
package org.gvsig.data.datastores.vectorial.db.jdbc.postgresql;
2

    
3
import java.sql.Connection;
4
import java.sql.DriverManager;
5
import java.sql.Statement;
6
import java.util.Iterator;
7
import java.util.NoSuchElementException;
8

    
9
import org.gvsig.data.DataManager;
10
import org.gvsig.data.datastores.vectorial.db.DBAttributeDescriptor;
11
import org.gvsig.data.datastores.vectorial.db.DBTest;
12
import org.gvsig.data.exception.CloseException;
13
import org.gvsig.data.exception.DataException;
14
import org.gvsig.data.exception.InitializeException;
15
import org.gvsig.data.exception.OpenException;
16
import org.gvsig.data.exception.ReadException;
17
import org.gvsig.data.exception.WriteException;
18
import org.gvsig.data.vectorial.Feature;
19
import org.gvsig.data.vectorial.IFeature;
20
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor;
21
import org.gvsig.data.vectorial.IFeatureCollection;
22
import org.gvsig.data.vectorial.IFeatureStore;
23
import org.gvsig.data.vectorial.IsNotAttributeSettingException;
24

    
25
public class postgresqlTest extends DBTest {
26

    
27
        private static String SERVER_IP="192.168.0.66";
28
        private static String SERVER_PORT="5432";
29
        private static String SERVER_DBNAME="gis";
30
        private static String SERVER_SCHEMA="ds_test";
31
        private static String SERVER_USER="testCase";
32
        private static String SERVER_PASWD="testCase";
33

    
34

    
35
        private static String DS_NAME = PostgresqlStore.DATASTORE_NAME;
36

    
37
        private void preparar_prueba_table() throws Exception{
38
                String url;
39
                url = "jdbc:postgresql://"+SERVER_IP+":" + SERVER_PORT +"/"+SERVER_DBNAME;
40

    
41
                Connection conn = null;
42

    
43
                Class.forName("org.postgresql.Driver");
44
                conn = DriverManager.getConnection(url, SERVER_USER, SERVER_PASWD);
45
                conn.setAutoCommit(false);
46
                Statement st = conn.createStatement();
47
                st.execute("drop table "+SERVER_SCHEMA+".prueba");
48
                st.execute("CREATE TABLE "+SERVER_SCHEMA+".prueba "+
49
                "("+
50
                "  gid serial NOT NULL,"+
51
                "  area float8,"+
52
                "  perimeter float8,"+
53
                "  nombre text,"+
54
                "  ine text,"+
55
                "  tipo text,"+
56
                "  otro_idiom text,"+
57
                "  the_geom geometry,"+
58
                "  entero int4,"+
59
                "  bool bool,"+
60
                "  CONSTRAINT prueba_pkey PRIMARY KEY (gid),"+
61
                "  CONSTRAINT enforce_dims_the_geom CHECK (ndims(the_geom) = 2),"+
62
//                "  CONSTRAINT enforce_geotype_the_geom CHECK (geometrytype(the_geom) = 'MULTIPOLYGON'::text OR the_geom IS NULL),"+
63
//                "  CONSTRAINT enforce_srid_the_geom CHECK (srid(the_geom) = 23030)"+
64
                ") "+
65
                "WITHOUT OIDS");
66

    
67
                st.execute("ALTER TABLE ds_test.prueba OWNER TO \"testCase\"");
68
                st.execute("Insert into "+SERVER_SCHEMA+".prueba select * from "+SERVER_SCHEMA+".prueba_backup");
69
                st.execute("SELECT setval('"+SERVER_SCHEMA+".prueba_gid_seq', (select max(gid) from "+SERVER_SCHEMA+".prueba))");
70
                st.close();
71
                conn.commit();
72
                conn.close();
73

    
74

    
75

    
76

    
77
        }
78

    
79
        public void test1(){
80
                Register.selfRegister();
81

    
82
                try {
83
                        preparar_prueba_table();
84
                } catch (Exception e) {
85
                        e.printStackTrace();
86
                        fail("Inicializando tabla prueba");
87
                }
88
                DataManager manager = DataManager.getManager();
89

    
90

    
91
                PostgresqlStoreParameters dparam =
92
                        (PostgresqlStoreParameters)manager.createDataStoreParameters(DS_NAME);
93

    
94
                dparam.setHost(SERVER_IP);
95
                dparam.setPort(SERVER_PORT);
96
                dparam.setUser(SERVER_USER);
97
                dparam.setPassw(SERVER_PASWD);
98
                dparam.setSchema(SERVER_SCHEMA);
99
                dparam.setDb(SERVER_DBNAME);
100
                dparam.setTableName("prueba");
101
                dparam.setFieldId(new String[] {"gid"});
102
                dparam.setFields(new String[] {"*"});
103
                dparam.setDefaultGeometryField("the_geom");
104

    
105
                storeTest(dparam, null, null, false);
106

    
107
        }
108

    
109
        public void testSimpleIteration38K_regs(){
110
                Register.selfRegister();
111
                DataManager manager = DataManager.getManager();
112

    
113

    
114
                PostgresqlStoreParameters dparam =
115
                        (PostgresqlStoreParameters)manager.createDataStoreParameters(DS_NAME);
116

    
117
                dparam.setHost(SERVER_IP);
118
                dparam.setPort(SERVER_PORT);
119
                dparam.setUser(SERVER_USER);
120
                dparam.setPassw(SERVER_PASWD);
121
                dparam.setSchema(null);
122
                dparam.setDb(SERVER_DBNAME);
123
                dparam.setTableName("hidropol");
124
//                dparam.setTableName("ejes");
125
                dparam.setFieldId(new String[] {"gid"});
126
                dparam.setFields(new String[] {"*"});
127
                dparam.setDefaultGeometryField("the_geom");
128

    
129
//                storeTest(dparam, null, null, false);
130

    
131
                IFeatureStore fs=null;
132
                try {
133
                        fs = (IFeatureStore) manager.createDataStore(dparam);
134
                } catch (InitializeException e) {
135
                        e.printStackTrace();
136
                        fail();
137
                }
138
                IFeatureCollection fc=null;
139
                try {
140
                        fc = (IFeatureCollection) fs.getDataCollection();
141
                } catch (ReadException e) {
142
                        e.printStackTrace();
143
                        fail();
144
                }
145

    
146
                int i=0;
147
                int count = fc.size();
148
                Iterator iter = fc.iterator();
149
                while (true){
150
                        try{
151
                                iter.next();
152
                                i++;
153
                        } catch (NoSuchElementException e) {
154
                                break;
155
                        }
156
                }
157
                assertEquals(count, i);
158

    
159
                fc.dispose();
160

    
161
                try {
162
                        fs.close();
163
                } catch (CloseException e) {
164
                        e.printStackTrace();
165
                        fail("Exception: "+e);
166
                }
167
                try {
168
                        fs.dispose();
169
                } catch (CloseException e) {
170
                        // TODO Auto-generated catch block
171
                        e.printStackTrace();
172
                }
173

    
174

    
175
        }
176

    
177
        public void test__sqlMode(){
178
                Register.selfRegister();
179
                DataManager manager = DataManager.getManager();
180

    
181

    
182
                PostgresqlStoreParameters dparam =
183
                        (PostgresqlStoreParameters)manager.createDataStoreParameters(DS_NAME);
184

    
185
                dparam.setHost(SERVER_IP);
186
                dparam.setPort(SERVER_PORT);
187
                dparam.setUser(SERVER_USER);
188
                dparam.setPassw(SERVER_PASWD);
189
                dparam.setSchema(SERVER_SCHEMA);
190
                dparam.setDb(SERVER_DBNAME);
191
//                dparam.setTableName("prueba");
192
                dparam.setSqlSoure("Select * from "+ SERVER_SCHEMA+".prueba");
193
                dparam.setFieldId(new String[] {"gid"});
194
                dparam.setFields(new String[] {"*"});
195
                dparam.setDefaultGeometryField("the_geom");
196

    
197
                storeTest(dparam, null, null, false);
198

    
199
                Exception exc = null;
200

    
201
                IFeatureStore fs=null;
202
                try {
203
                        fs = (IFeatureStore)manager.createDataStore(dparam);
204
                } catch (InitializeException e) {
205
                        e.printStackTrace();
206
                        fail("Exception:" + e);
207
                }
208
                assertNotNull("Can't create Feature Store", fs);
209

    
210
                try {
211
                        fs.open();
212
                } catch (OpenException e2) {
213
                        e2.printStackTrace();
214
                        fail();
215
                }
216

    
217

    
218
                IFeatureCollection fc =null;
219

    
220
                try {
221
                        fc = (IFeatureCollection)fs.getDataCollection();
222
                } catch (ReadException e1) {
223
                        e1.printStackTrace();
224
                        fail();
225
                }
226

    
227
                assertEquals(9, fc.size());
228

    
229
                fc.dispose();
230

    
231
                assertFalse("Edition allowed in sqlSource mode", fs.isEditable());
232

    
233
                try {
234
                        fs.startEditing();
235
                } catch (ReadException e1) {
236
                        exc=e1;
237
                }
238
                assertNotNull("Edition allowed in sqlSource mode",exc);
239

    
240
                exc=null;
241
                try {
242
                        fc = (IFeatureCollection)fs.getDataCollection(null,"nombre like 'B%'",null);
243
                } catch (ReadException e1) {
244
                        exc=e1;
245
                }
246
                assertNotNull("Filter allowed in sqlSource mode",exc);
247

    
248
                exc=null;
249
                try {
250
                        fc = (IFeatureCollection)fs.getDataCollection(null,null,"nombre");
251
                } catch (ReadException e1) {
252
                        exc=e1;
253
                }
254
                assertNotNull("Order allowed in sqlSource mode",exc);
255

    
256

    
257
                try {
258
                        fs.close();
259
                } catch (CloseException e) {
260
                        e.printStackTrace();
261
                        fail("Exception:" + e);
262
                }
263

    
264
        }
265

    
266
        public void test2(){
267

    
268
                try {
269
                        preparar_prueba_table();
270
                } catch (Exception e) {
271
                        e.printStackTrace();
272
                        fail("Inicializando tabla prueba");
273
                }
274

    
275
                Register.selfRegister();
276
                DataManager manager = DataManager.getManager();
277

    
278

    
279
                PostgresqlStoreParameters dparam =
280
                        (PostgresqlStoreParameters)manager.createDataStoreParameters(DS_NAME);
281

    
282
                dparam.setHost(SERVER_IP);
283
                dparam.setPort(SERVER_PORT);
284
                dparam.setUser(SERVER_USER);
285
                dparam.setPassw(SERVER_PASWD);
286
                dparam.setSchema(SERVER_SCHEMA);
287
                dparam.setDb(SERVER_DBNAME);
288
                dparam.setTableName("prueba");
289
                dparam.setFieldId(new String[] {"gid"});
290
                dparam.setFields(new String[] {"*"});
291
                dparam.setDefaultGeometryField("the_geom");
292

    
293
//                storeTest(dparam, null, null, true);
294
                storeTest(dparam, null, null, false);
295

    
296
                IFeatureStore fs=null;
297
                try {
298
                        fs = (IFeatureStore)manager.createDataStore(dparam);
299
                } catch (InitializeException e) {
300
                        e.printStackTrace();
301
                        fail("Exception:" + e);
302
                }
303
                assertNotNull("Can't create Feature Store", fs);
304

    
305
                try {
306
                        fs.open();
307
                } catch (OpenException e2) {
308
                        e2.printStackTrace();
309
                        fail();
310
                }
311

    
312
                Iterator it;
313
                IFeatureCollection fc =null;
314
                Comparable v1,v2;
315
                IFeature feature=null,pfeature=null;
316
                long count;
317

    
318

    
319
                try {
320
                        fc = (IFeatureCollection)fs.getDataCollection();
321
                } catch (ReadException e1) {
322
                        e1.printStackTrace();
323
                        fail();
324
                }
325

    
326
                assertEquals(9, fc.size());
327

    
328
                fc.dispose();
329

    
330
                try {
331
                        fc = (IFeatureCollection)fs.getDataCollection(null,"lower(nombre) like 'b%'",null);
332
                } catch (ReadException e1) {
333
                        e1.printStackTrace();
334
                        fail();
335
                }
336

    
337
                assertEquals(2, fc.size());
338

    
339
                it = fc.iterator();
340
                count=0;
341
                while (it.hasNext()){
342
                        feature = (IFeature)it.next();
343
                        assertTrue("Filter error",feature.getString("nombre").toLowerCase().startsWith("b"));
344
                        count++;
345
                }
346
                assertEquals("Iteration error",2,count);
347

    
348
                fc.dispose();
349

    
350

    
351
                try {
352
                        fc = (IFeatureCollection)fs.getDataCollection(null,null,"nombre ASC");
353
                } catch (ReadException e1) {
354
                        e1.printStackTrace();
355
                        fail();
356
                }
357
                assertEquals(9, fc.size());
358
                it = fc.iterator();
359
                count=0;
360
                pfeature = (IFeature)it.next();
361
                count++;
362
                while (it.hasNext()){
363
                        feature = (IFeature)it.next();
364
                        v1 = (Comparable)pfeature.get("nombre");
365
                        v2 = (Comparable)feature.get("nombre");
366
                        pfeature=feature;
367
                        assertTrue("Short error", (v1.compareTo(v1) <= 0));
368
                        count++;
369
                }
370
                assertEquals("Iteration error",9,count);
371

    
372
                fc.dispose();
373

    
374

    
375
                try {
376
                        fc = (IFeatureCollection)fs.getDataCollection(null,null,"nombre DESC");
377
                } catch (ReadException e1) {
378
                        e1.printStackTrace();
379
                        fail();
380
                }
381
                assertEquals(9, fc.size());
382
                it = fc.iterator();
383

    
384
                count=0;
385
                pfeature = (IFeature)it.next();
386
                count++;
387
                while (it.hasNext()){
388
                        feature = (IFeature)it.next();
389
                        v1 = (Comparable)pfeature.get("nombre");
390
                        v2 = (Comparable)feature.get("nombre");
391
                        pfeature=feature;
392
                        assertTrue("Short error", (v1.compareTo(v1) >= 0));
393
                        count++;
394
                }
395
                assertEquals("Iteration error",9,count);
396

    
397
                fc.dispose();
398

    
399

    
400

    
401
                try {
402
                        fc = (IFeatureCollection)fs.getDataCollection(null,"lower(nombre) like 'b%'","nombre");
403
                } catch (ReadException e1) {
404
                        e1.printStackTrace();
405
                        fail();
406
                }
407

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

    
410
                it = fc.iterator();
411

    
412
                count=0;
413
                pfeature = (IFeature)it.next();
414
                assertTrue(pfeature.getString("nombre").toLowerCase().startsWith("b"));
415
                count++;
416
                while (it.hasNext()){
417
                        feature = (IFeature)it.next();
418
                        assertTrue("Filter error",feature.getString("nombre").toLowerCase().startsWith("b"));
419
                        v1 = (Comparable)pfeature.get("nombre");
420
                        v2 = (Comparable)feature.get("nombre");
421
                        pfeature=feature;
422
                        assertTrue("Short error", (v1.compareTo(v1) <= 0));
423
                        count++;
424
                }
425
                assertEquals("Iteration error",2,count);
426
                fc.dispose();
427

    
428

    
429

    
430
                try {
431
                        fc = (IFeatureCollection)fs.getDataCollection(null,null,"Tipo,lower(nombre) Desc");
432
                } catch (ReadException e1) {
433
                        e1.printStackTrace();
434
                        fail();
435
                }
436
                assertEquals(9, fc.size());
437
                it = fc.iterator();
438
                count=0;
439
                pfeature = (IFeature)it.next();
440
                System.out.println(pfeature.getString("nombre"));
441
                count++;
442
                while (it.hasNext()){
443
                        feature = (IFeature)it.next();
444
                        v1 = (Comparable)((String)pfeature.get("nombre")).toLowerCase();
445
                        v2 = (Comparable)((String)feature.get("nombre")).toLowerCase();
446
                        pfeature=feature;
447
                        assertTrue("Short error", (v1.compareTo(v2) >= 0));
448
                        System.out.println(pfeature.getString("nombre"));
449
                        count++;
450
                }
451
                assertEquals("Iteration error",9,count);
452
                fc.dispose();
453

    
454

    
455

    
456
                /// CON  EDICION
457
                try {
458
                        fs.startEditing();
459
                } catch (ReadException e1) {
460
                        e1.printStackTrace();
461
                        fail();
462
                }
463

    
464
                try{
465
                        IFeature newFeature = fs.createFeature(fs.getDefaultFeatureType());
466
                        newFeature.editing();
467
                        newFeature.set("nombre","BuRjaSOT");
468
                        newFeature.set("tipo","MUNICIPIO");
469
                        fs.insert(newFeature);
470
                }catch (DataException e) {
471
                        e.printStackTrace();
472
                        fail();
473
                }
474

    
475
                try {
476
                        fc = (IFeatureCollection)fs.getDataCollection();
477
                } catch (ReadException e1) {
478
                        e1.printStackTrace();
479
                        fail();
480
                }
481

    
482
                assertEquals(10, fc.size());
483
                fc.dispose();
484

    
485
                try {
486
                        fc = (IFeatureCollection)fs.getDataCollection(null,"lower(nombre) like 'b%'",null);
487
                } catch (ReadException e1) {
488
                        e1.printStackTrace();
489
                        fail();
490
                }
491

    
492
                assertEquals(3, fc.size());
493

    
494
                it = fc.iterator();
495
                count=0;
496
                while (it.hasNext()){
497
                        feature = (IFeature)it.next();
498
                        assertTrue("Filter error",feature.getString("nombre").toLowerCase().startsWith("b"));
499
                        count++;
500
                }
501
                assertEquals("Iteration error",3,count);
502
                fc.dispose();
503

    
504

    
505
                try {
506
                        fc = (IFeatureCollection)fs.getDataCollection(null,null,"nombre ASC");
507
                } catch (ReadException e1) {
508
                        e1.printStackTrace();
509
                        fail();
510
                }
511
                assertEquals(10, fc.size());
512
                it = fc.iterator();
513
                count=0;
514
                pfeature = (IFeature)it.next();
515
                count++;
516
                while (it.hasNext()){
517
                        feature = (IFeature)it.next();
518
                        v1 = (Comparable)pfeature.get("nombre");
519
                        v2 = (Comparable)feature.get("nombre");
520
                        pfeature=feature;
521
                        assertTrue("Short error", (v1.compareTo(v1) <= 0));
522
                        count++;
523
                }
524
                assertEquals("Iteration error",10,count);
525
                fc.dispose();
526

    
527

    
528
                try {
529
                        fc = (IFeatureCollection)fs.getDataCollection(null,null,"nombre DESC");
530
                } catch (ReadException e1) {
531
                        e1.printStackTrace();
532
                        fail();
533
                }
534
                assertEquals(10, fc.size());
535
                it = fc.iterator();
536

    
537
                count=0;
538
                pfeature = (IFeature)it.next();
539
                count++;
540
                while (it.hasNext()){
541
                        feature = (IFeature)it.next();
542
                        v1 = (Comparable)pfeature.get("nombre");
543
                        v2 = (Comparable)feature.get("nombre");
544
                        pfeature=feature;
545
                        assertTrue("Short error", (v1.compareTo(v1) >= 0));
546
                        count++;
547
                }
548
                assertEquals("Iteration error",10,count);
549
                fc.dispose();
550

    
551

    
552
                try {
553
                        fc = (IFeatureCollection)fs.getDataCollection(null,"lower(nombre) like 'b%'","nombre");
554
                } catch (ReadException e1) {
555
                        e1.printStackTrace();
556
                        fail();
557
                }
558

    
559
                assertEquals(3, fc.size());
560

    
561
                it = fc.iterator();
562

    
563
                count=0;
564
                pfeature = (IFeature)it.next();
565
                assertTrue(pfeature.getString("nombre").toLowerCase().startsWith("b"));
566
                count++;
567
                while (it.hasNext()){
568
                        feature = (IFeature)it.next();
569
                        assertTrue("Filter error",feature.getString("nombre").toLowerCase().startsWith("b"));
570
                        v1 = (Comparable)pfeature.get("nombre");
571
                        v2 = (Comparable)feature.get("nombre");
572
                        pfeature=feature;
573
                        assertTrue("Short error", (v1.compareTo(v1) <= 0));
574
                        count++;
575
                }
576
                assertEquals("Iteration error",3,count);
577
                fc.dispose();
578

    
579

    
580

    
581
                try {
582
                        fc = (IFeatureCollection)fs.getDataCollection(null,null,"Tipo,lower(nombre) Desc");
583
                } catch (ReadException e1) {
584
                        e1.printStackTrace();
585
                        fail();
586
                }
587
                assertEquals(10, fc.size());
588
                it = fc.iterator();
589
                count=0;
590
                pfeature = (IFeature)it.next();
591
                System.out.println(pfeature.getString("nombre"));
592
                count++;
593
                while (it.hasNext()){
594
                        feature = (IFeature)it.next();
595
                        v1 = (Comparable)((String)pfeature.get("nombre")).toLowerCase();
596
                        v2 = (Comparable)((String)feature.get("nombre")).toLowerCase();
597
                        pfeature=feature;
598
                        assertTrue("Short error", (v1.compareTo(v2) >= 0));
599
                        System.out.println(pfeature.getString("nombre"));
600
                        count++;
601
                }
602
                assertEquals("Iteration error",10,count);
603
                fc.dispose();
604

    
605

    
606
                fs.cancelEditing();
607

    
608

    
609
                //Insertar un elemento
610
                try{
611
                        fs.startEditing();
612
                } catch (ReadException e1) {
613
                        e1.printStackTrace();
614
                        fail();
615
                }
616
                try{
617
                feature = fs.createDefaultFeature(true);
618
                feature.editing();
619
                //                feature.set("id", 90000);
620
                feature.set("nombre","BurJASOT");
621
                feature.set("tipo", "OTRO");
622
                fs.insert(feature);
623
                }catch (DataException e) {
624
                        e.printStackTrace();
625
                        fail();
626
                }
627
                try {
628
                        fs.finishEditing();
629
                } catch (WriteException e) {
630
                        e.printStackTrace();
631
                        fail("Exception: "+e);
632
                } catch (ReadException e) {
633
                        e.printStackTrace();
634
                        fail("Exception: "+e);
635
                }
636

    
637

    
638
                try {
639
                        fc = (IFeatureCollection)fs.getDataCollection(null,null,"lower(nombre) Desc");
640
                } catch (ReadException e1) {
641
                        e1.printStackTrace();
642
                        fail();
643
                }
644
                assertEquals(10, fc.size());
645
                it = fc.iterator();
646
                count=0;
647
                pfeature = (IFeature)it.next();
648
                System.out.println(pfeature.getString("nombre"));
649
                count++;
650
                while (it.hasNext()){
651
                        feature = (IFeature)it.next();
652
                        v1 = (Comparable)((String)pfeature.get("nombre")).toLowerCase();
653
                        v2 = (Comparable)((String)feature.get("nombre")).toLowerCase();
654
                        pfeature=feature;
655
                        assertTrue("Short error: "+ v1.toString() + " >= " +v2.toString(), (v1.compareTo(v2) >= 0));
656
                        System.out.println(pfeature.getString("nombre"));
657
                        count++;
658
                }
659
                assertEquals("Iteration error",10,count);
660
                fc.dispose();
661

    
662

    
663

    
664
                //Actualizacion
665
                try{
666
                        fs.startEditing();
667
                } catch (ReadException e1) {
668
                        e1.printStackTrace();
669
                        fail();
670
                }
671

    
672

    
673
                try {
674
                        fc = (IFeatureCollection)fs.getDataCollection(null,"lower(nombre) = 'burjasot'",null);
675
                } catch (ReadException e1) {
676
                        e1.printStackTrace();
677
                        fail();
678
                }
679
                assertEquals(1, fc.size());
680
                feature =(Feature)fc.iterator().next();
681
                try {
682
                        feature.editing();
683
                        feature.set("nombre", feature.getString("nombre")+"__KK__");
684
                } catch (DataException e2) {
685
                        // TODO Auto-generated catch block
686
                        e2.printStackTrace();
687
                }
688
                try {
689
                        fs.update(feature);
690
                } catch (DataException e2) {
691
                        e2.printStackTrace();fail();
692
                }
693
                try {
694
                        fs.finishEditing();
695
                } catch (WriteException e) {
696
                        e.printStackTrace();
697
                        fail("Exception: "+e);
698
                } catch (ReadException e) {
699
                        e.printStackTrace();
700
                        fail("Exception: "+e);
701
                }
702

    
703
                try {
704
                        fc = (IFeatureCollection)fs.getDataCollection(null,"nombre like '%__KK__'",null);
705
                } catch (ReadException e1) {
706
                        e1.printStackTrace();
707
                        fail();
708
                }
709
                assertEquals(1, fc.size());
710
                fc.dispose();
711

    
712

    
713
                //Eliminacion
714
                try{
715
                        fs.startEditing();
716
                } catch (ReadException e1) {
717
                        e1.printStackTrace();
718
                        fail();
719
                }
720

    
721

    
722
                try {
723
                        fc = (IFeatureCollection)fs.getDataCollection(null,"nombre like '%__KK__'",null);
724
                } catch (ReadException e1) {
725
                        e1.printStackTrace();
726
                        fail();
727
                }
728
                assertEquals(1, fc.size());
729
                try {
730
                        fs.delete((IFeature)fc.iterator().next());
731
                } catch (DataException e2) {
732
                        e2.printStackTrace();fail();
733
                }
734
                fc.dispose();
735
                try {
736
                        fs.finishEditing();
737
                } catch (WriteException e) {
738
                        e.printStackTrace();
739
                        fail("Exception: "+e);
740
                } catch (ReadException e) {
741
                        e.printStackTrace();
742
                        fail("Exception: "+e);
743
                }
744

    
745
                try {
746
                        fc = (IFeatureCollection)fs.getDataCollection();
747
                } catch (ReadException e1) {
748
                        e1.printStackTrace();
749
                        fail();
750
                }
751
                assertEquals(9, fc.size());
752
                fc.dispose();
753

    
754

    
755

    
756
                try {
757
                        fs.close();
758
                } catch (CloseException e) {
759
                        e.printStackTrace();
760
                        fail("Exception:" + e);
761
                }
762

    
763

    
764

    
765
        }
766

    
767

    
768
        public void test3(){
769

    
770
                try {
771
                        preparar_prueba_table();
772
                } catch (Exception e) {
773
                        e.printStackTrace();
774
                        fail("Inicializando tabla prueba");
775
                }
776

    
777
                Register.selfRegister();
778
                DataManager manager = DataManager.getManager();
779

    
780

    
781
                PostgresqlStoreParameters dparam =
782
                        (PostgresqlStoreParameters)manager.createDataStoreParameters(DS_NAME);
783

    
784
                dparam.setHost(SERVER_IP);
785
                dparam.setPort(SERVER_PORT);
786
                dparam.setUser(SERVER_USER);
787
                dparam.setPassw(SERVER_PASWD);
788
                dparam.setSchema(SERVER_SCHEMA);
789
                dparam.setDb(SERVER_DBNAME);
790
                dparam.setTableName("prueba");
791
                dparam.setFieldId(new String[] {"gid"});
792
                dparam.setFields(new String[] {"*"});
793
                dparam.setDefaultGeometryField("the_geom");
794

    
795
                IFeatureStore fs=null;
796
                try {
797
                        fs = (IFeatureStore)manager.createDataStore(dparam);
798
                } catch (InitializeException e) {
799
                        e.printStackTrace();
800
                        fail("Exception:" + e);
801
                }
802
                assertNotNull("Can't create Feature Store", fs);
803

    
804
                try {
805
                        fs.open();
806
                } catch (OpenException e2) {
807
                        e2.printStackTrace();
808
                        fail();
809
                }
810
                int size;
811

    
812
                try {
813
                        fs.startEditing();
814
                } catch (ReadException e) {
815
                        e.printStackTrace();fail();
816
                }
817

    
818
                DBAttributeDescriptor attr =(DBAttributeDescriptor)fs.createAttributeDescriptor();
819
                attr.editing();
820
                try {
821
                        attr.setName("My_new_Field");
822
                        attr.setType(IFeatureAttributeDescriptor.TYPE_STRING);
823
                        attr.setSize(50);
824
                } catch (IsNotAttributeSettingException e1) {
825
                        // TODO Auto-generated catch block
826
                        e1.printStackTrace();
827
                }
828
                try {
829
                        fs.insert(attr);
830
                } catch (DataException e2) {
831
                        e2.printStackTrace();fail();
832
                }
833

    
834
                Iterator iter = null;
835

    
836

    
837

    
838
                IFeatureCollection fc=null;
839
                try {
840
                        fc = (IFeatureCollection)fs.getDataCollection();
841
                } catch (ReadException e1) {
842
                        e1.printStackTrace();fail();
843
                }
844

    
845
                size = fc.size();
846
                iter = fc.iterator();
847

    
848

    
849
                int i= 0;
850
                Feature feature = null;
851
                while (iter.hasNext()){
852
                        feature = (Feature)iter.next();
853
                        try {
854
                                feature.editing();
855
                                feature.set("My_new_Field","V"+i);
856
                        } catch (DataException e) {
857
                                e.printStackTrace();fail();
858
                        }
859

    
860
                        try {
861
                                fs.update(feature);
862
                        } catch (DataException e) {
863
                                e.printStackTrace();fail();
864
                        }
865

    
866
                }
867

    
868
                fc.dispose();
869

    
870

    
871
                try {
872
                        fc = (IFeatureCollection)fs.getDataCollection();
873
                } catch (ReadException e1) {
874
                        e1.printStackTrace();fail();
875
                }
876

    
877
                assertEquals(size, fc.size());
878

    
879
                iter = fc.iterator();
880

    
881
                while (iter.hasNext()){
882
                        feature = (Feature)iter.next();
883
                    assertTrue(feature.getString("My_new_Field") != null);
884
                    assertTrue(feature.getString("My_new_Field").startsWith("V"));
885
                }
886

    
887
                try {
888
                        fs.finishEditing();
889
                } catch (WriteException e) {
890
                        e.printStackTrace();fail();
891
                } catch (ReadException e) {
892
                        e.printStackTrace();fail();
893
                }
894

    
895
                try {
896
                        fc = (IFeatureCollection)fs.getDataCollection();
897
                } catch (ReadException e1) {
898
                        e1.printStackTrace();fail();
899
                }
900

    
901
                iter = fc.iterator();
902

    
903
                while (iter.hasNext()){
904
                        feature = (Feature)iter.next();
905
                    assertTrue(feature.getString("My_new_Field") != null);
906
                    assertTrue(feature.getString("My_new_Field").startsWith("V"));
907
                }
908

    
909
                fc.dispose();
910

    
911
                try {
912
                        fs.close();
913
                } catch (CloseException e) {
914
                        e.printStackTrace();fail();
915
                }
916

    
917
                try {
918
                        fs.dispose();
919
                } catch (CloseException e) {
920
                        e.printStackTrace();fail();
921
                }
922

    
923
        }
924
}