Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_daldb / src-test / org / gvsig / fmap / data / feature / db / jdbc / postgresql / postgresqlTest.java @ 24491

History | View | Annotate | Download (22.4 KB)

1
package org.gvsig.fmap.data.feature.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.fmap.dal.DataManager;
10
import org.gvsig.fmap.dal.exceptions.CloseException;
11
import org.gvsig.fmap.dal.exceptions.DataException;
12
import org.gvsig.fmap.dal.exceptions.InitializeException;
13
import org.gvsig.fmap.dal.exceptions.ReadException;
14
import org.gvsig.fmap.dal.exceptions.WriteException;
15
import org.gvsig.fmap.dal.feature.AbstractFeature;
16
import org.gvsig.fmap.dal.feature.Feature;
17
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
18
import org.gvsig.fmap.dal.feature.FeatureSet;
19
import org.gvsig.fmap.dal.feature.FeatureStore;
20
import org.gvsig.fmap.dal.feature.exceptions.IsNotAttributeSettingException;
21
import org.gvsig.fmap.dal.impl.DefaultDataManager;
22
import org.gvsig.fmap.data.feature.db.DBAttributeDescriptor;
23
import org.gvsig.fmap.data.feature.db.jdbc.JDBCExplorerParameter;
24
import org.gvsig.fmap.data.feature.db.jdbc.JDBCTest;
25

    
26
public class postgresqlTest extends JDBCTest {
27

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

    
35

    
36
        private static String DS_NAME = PostgresqlStore.DATASTORE_NAME;
37
        private static String DE_NAME = PostgresqlExplorer.DATAEXPLORER_NAME;
38

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

    
43
                Connection conn = null;
44

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

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

    
77

    
78

    
79

    
80
        }
81

    
82
        public void test1(){
83
                Register.selfRegister();
84

    
85
                try {
86
                        preparar_prueba_table();
87
                } catch (Exception e) {
88
                        e.printStackTrace();
89
                        fail("Inicializando tabla prueba");
90
                }
91
                DataManager manager = DefaultDataManager.getManager();
92

    
93

    
94
                PostgresqlStoreParameters dparam=null;
95
                try {
96
                        dparam = (PostgresqlStoreParameters)manager.createStoreParameters(DS_NAME);
97
                } catch (InitializeException e) {
98
                        e.printStackTrace();
99
                        fail();
100
                }
101

    
102
                dparam.setHost(SERVER_IP);
103
                dparam.setPort(SERVER_PORT);
104
                dparam.setUser(SERVER_USER);
105
                dparam.setPassw(SERVER_PASWD);
106
                dparam.setSchema(SERVER_SCHEMA);
107
                dparam.setDb(SERVER_DBNAME);
108
                dparam.setTableName("prueba");
109
                dparam.setFieldsId(new String[] {"gid"});
110
                dparam.setFields(new String[] {"*"});
111
                dparam.setDefaultGeometryField("the_geom");
112

    
113
                storeTest(dparam, null, null, false);
114

    
115
                doFileResourceTest(dparam);
116

    
117
        }
118

    
119
        public void testSimpleIteration38K_regs(){
120
                Register.selfRegister();
121
                DataManager manager = DefaultDataManager.getManager();
122

    
123

    
124
                PostgresqlStoreParameters dparam=null;
125
                try {
126
                        dparam = (PostgresqlStoreParameters)manager.createStoreParameters(DS_NAME);
127
                } catch (InitializeException e1) {
128
                        e1.printStackTrace();
129
                        fail();
130
                }
131

    
132
                dparam.setHost(SERVER_IP);
133
                dparam.setPort(SERVER_PORT);
134
                dparam.setUser(SERVER_USER);
135
                dparam.setPassw(SERVER_PASWD);
136
                dparam.setSchema(null);
137
                dparam.setDb(SERVER_DBNAME);
138
                dparam.setTableName("hidropol");
139
//                dparam.setTableName("ejes");
140
                dparam.setFieldsId(new String[] {"gid"});
141
                dparam.setFields(new String[] {"*"});
142
                dparam.setDefaultGeometryField("the_geom");
143

    
144
//                storeTest(dparam, null, null, false);
145

    
146
                FeatureStore fs=null;
147
                try {
148
                        fs = (FeatureStore) manager.createStore(dparam);
149
                } catch (InitializeException e) {
150
                        e.printStackTrace();
151
                        fail();
152
                }
153
                FeatureSet fc=null;
154
                try {
155
                        fc = (FeatureSet) fs.getDataSet();
156
                } catch (ReadException e) {
157
                        e.printStackTrace();
158
                        fail();
159
                }
160

    
161
                int i=0;
162
                int count = fc.size();
163
                Iterator iter = fc.iterator();
164
                while (true){
165
                        try{
166
                                iter.next();
167
                                i++;
168
                        } catch (NoSuchElementException e) {
169
                                break;
170
                        }
171
                }
172
                assertEquals(count, i);
173

    
174
                fc.dispose();
175

    
176
//                try {
177
//                        fs.close();
178
//                } catch (CloseException e) {
179
//                        e.printStackTrace();
180
//                        fail("Exception: "+e);
181
//                }
182
                try {
183
                        fs.dispose();
184
                } catch (CloseException e) {
185
                        // TODO Auto-generated catch block
186
                        e.printStackTrace();
187
                }
188

    
189

    
190
        }
191

    
192
        public void test__sqlMode(){
193
                Register.selfRegister();
194
                DataManager manager = DefaultDataManager.getManager();
195

    
196

    
197
                PostgresqlStoreParameters dparam=null;
198
                try {
199
                        dparam = (PostgresqlStoreParameters)manager.createStoreParameters(DS_NAME);
200
                } catch (InitializeException e2) {
201
                        e2.printStackTrace();
202
                        fail();
203
                }
204

    
205
                dparam.setHost(SERVER_IP);
206
                dparam.setPort(SERVER_PORT);
207
                dparam.setUser(SERVER_USER);
208
                dparam.setPassw(SERVER_PASWD);
209
                dparam.setSchema(SERVER_SCHEMA);
210
                dparam.setDb(SERVER_DBNAME);
211
//                dparam.setTableName("prueba");
212
                dparam.setSqlSoure("Select * from "+ SERVER_SCHEMA+".prueba");
213
                dparam.setFieldsId(new String[] {"gid"});
214
                dparam.setFields(new String[] {"*"});
215
                dparam.setDefaultGeometryField("the_geom");
216

    
217
                storeTest(dparam, null, null, false);
218

    
219
                Exception exc = null;
220

    
221
                FeatureStore fs=null;
222
                try {
223
                        fs = (FeatureStore)manager.createStore(dparam);
224
                } catch (InitializeException e) {
225
                        e.printStackTrace();
226
                        fail("Exception:" + e);
227
                }
228
                assertNotNull("Can't create Feature Store", fs);
229

    
230
//                try {
231
//                        fs.open();
232
//                } catch (OpenException e2) {
233
//                        e2.printStackTrace();
234
//                        fail();
235
//                }
236

    
237

    
238
                FeatureSet fc =null;
239

    
240
                try {
241
                        fc = (FeatureSet)fs.getDataSet();
242
                } catch (ReadException e1) {
243
                        e1.printStackTrace();
244
                        fail();
245
                }
246

    
247
                assertEquals(9, fc.size());
248

    
249
                fc.dispose();
250

    
251
                assertFalse("Edition allowed in sqlSource mode", fs.allowWrite());
252

    
253
                try {
254
                        fs.edit();
255
                } catch (ReadException e1) {
256
                        exc=e1;
257
                }
258
                assertNotNull("Edition allowed in sqlSource mode",exc);
259

    
260
                exc=null;
261
                try {
262
                        fc = (FeatureSet)fs.getDataCollection(fs.getDefaultFeatureType(),"nombre like 'B%'",null);
263
                } catch (ReadException e1) {
264
                        exc=e1;
265
                }
266
                assertNotNull("Filter allowed in sqlSource mode",exc);
267

    
268
                exc=null;
269
                try {
270
                        fc = (FeatureSet)fs.getDataCollection(fs.getDefaultFeatureType(),null,"nombre");
271
                } catch (ReadException e1) {
272
                        exc=e1;
273
                }
274
                assertNotNull("Order allowed in sqlSource mode",exc);
275

    
276

    
277
//                try {
278
//                        fs.close();
279
//                } catch (CloseException e) {
280
//                        e.printStackTrace();
281
//                        fail("Exception:" + e);
282
//                }
283

    
284
        }
285

    
286
        public void test2(){
287

    
288
                try {
289
                        preparar_prueba_table();
290
                } catch (Exception e) {
291
                        e.printStackTrace();
292
                        fail("Inicializando tabla prueba");
293
                }
294

    
295
                Register.selfRegister();
296
                DataManager manager = DefaultDataManager.getManager();
297

    
298

    
299
                PostgresqlStoreParameters dparam=null;
300
                try {
301
                        dparam = (PostgresqlStoreParameters)manager.createStoreParameters(DS_NAME);
302
                } catch (InitializeException e3) {
303
                        e3.printStackTrace();
304
                        fail();
305
                }
306

    
307
                dparam.setHost(SERVER_IP);
308
                dparam.setPort(SERVER_PORT);
309
                dparam.setUser(SERVER_USER);
310
                dparam.setPassw(SERVER_PASWD);
311
                dparam.setSchema(SERVER_SCHEMA);
312
                dparam.setDb(SERVER_DBNAME);
313
                dparam.setTableName("prueba");
314
                dparam.setFieldsId(new String[] {"gid"});
315
                dparam.setFields(new String[] {"*"});
316
                dparam.setDefaultGeometryField("the_geom");
317

    
318
//                storeTest(dparam, null, null, true);
319
                storeTest(dparam, null, null, false);
320

    
321
                FeatureStore fs=null;
322
                try {
323
                        fs = (FeatureStore)manager.createStore(dparam);
324
                } catch (InitializeException e) {
325
                        e.printStackTrace();
326
                        fail("Exception:" + e);
327
                }
328
                assertNotNull("Can't create Feature Store", fs);
329

    
330
//                try {
331
//                        fs.open();
332
//                } catch (OpenException e2) {
333
//                        e2.printStackTrace();
334
//                        fail();
335
//                }
336

    
337
                Iterator it;
338
                FeatureSet fc =null;
339
                Comparable v1,v2;
340
                Feature feature=null,pfeature=null;
341
                long count;
342

    
343

    
344
                try {
345
                        fc = (FeatureSet)fs.getDataSet();
346
                } catch (ReadException e1) {
347
                        e1.printStackTrace();
348
                        fail();
349
                }
350

    
351
                assertEquals(9, fc.size());
352

    
353
                fc.dispose();
354

    
355
                try {
356
                        fc = (FeatureSet)fs.getDataCollection(fs.getDefaultFeatureType(),"lower(nombre) like 'b%'",null);
357
                } catch (ReadException e1) {
358
                        e1.printStackTrace();
359
                        fail();
360
                }
361

    
362
                assertEquals(2, fc.size());
363

    
364
                it = fc.iterator();
365
                count=0;
366
                while (it.hasNext()){
367
                        feature = (Feature)it.next();
368
                        assertTrue("Filter error",feature.getString("nombre").toLowerCase().startsWith("b"));
369
                        count++;
370
                }
371
                assertEquals("Iteration error",2,count);
372

    
373
                fc.dispose();
374

    
375

    
376
                try {
377
                        fc = (FeatureSet)fs.getDataCollection(fs.getDefaultFeatureType(),null,"nombre ASC");
378
                } catch (ReadException e1) {
379
                        e1.printStackTrace();
380
                        fail();
381
                }
382
                assertEquals(9, fc.size());
383
                it = fc.iterator();
384
                count=0;
385
                pfeature = (Feature)it.next();
386
                count++;
387
                while (it.hasNext()){
388
                        feature = (Feature)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
                try {
401
                        fc = (FeatureSet)fs.getDataCollection(fs.getDefaultFeatureType(),null,"nombre DESC");
402
                } catch (ReadException e1) {
403
                        e1.printStackTrace();
404
                        fail();
405
                }
406
                assertEquals(9, fc.size());
407
                it = fc.iterator();
408

    
409
                count=0;
410
                pfeature = (Feature)it.next();
411
                count++;
412
                while (it.hasNext()){
413
                        feature = (Feature)it.next();
414
                        v1 = (Comparable)pfeature.get("nombre");
415
                        v2 = (Comparable)feature.get("nombre");
416
                        pfeature=feature;
417
                        assertTrue("Short error", (v1.compareTo(v1) >= 0));
418
                        count++;
419
                }
420
                assertEquals("Iteration error",9,count);
421

    
422
                fc.dispose();
423

    
424

    
425

    
426
                try {
427
                        fc = (FeatureSet)fs.getDataCollection(fs.getDefaultFeatureType(),"lower(nombre) like 'b%'","nombre");
428
                } catch (ReadException e1) {
429
                        e1.printStackTrace();
430
                        fail();
431
                }
432

    
433
                assertEquals(2, fc.size());
434

    
435
                it = fc.iterator();
436

    
437
                count=0;
438
                pfeature = (Feature)it.next();
439
                assertTrue(pfeature.getString("nombre").toLowerCase().startsWith("b"));
440
                count++;
441
                while (it.hasNext()){
442
                        feature = (Feature)it.next();
443
                        assertTrue("Filter error",feature.getString("nombre").toLowerCase().startsWith("b"));
444
                        v1 = (Comparable)pfeature.get("nombre");
445
                        v2 = (Comparable)feature.get("nombre");
446
                        pfeature=feature;
447
                        assertTrue("Short error", (v1.compareTo(v1) <= 0));
448
                        count++;
449
                }
450
                assertEquals("Iteration error",2,count);
451
                fc.dispose();
452

    
453

    
454

    
455
                try {
456
                        fc = (FeatureSet)fs.getDataCollection(fs.getDefaultFeatureType(),null,"Tipo,lower(nombre) Desc");
457
                } catch (ReadException e1) {
458
                        e1.printStackTrace();
459
                        fail();
460
                }
461
                assertEquals(9, fc.size());
462
                it = fc.iterator();
463
                count=0;
464
                pfeature = (Feature)it.next();
465
                System.out.println(pfeature.getString("nombre"));
466
                count++;
467
                while (it.hasNext()){
468
                        feature = (Feature)it.next();
469
                        v1 = ((String)pfeature.get("nombre")).toLowerCase();
470
                        v2 = ((String)feature.get("nombre")).toLowerCase();
471
                        pfeature=feature;
472
                        assertTrue("Short error", (v1.compareTo(v2) >= 0));
473
                        System.out.println(pfeature.getString("nombre"));
474
                        count++;
475
                }
476
                assertEquals("Iteration error",9,count);
477
                fc.dispose();
478

    
479

    
480

    
481
                /// CON  EDICION
482
                try {
483
                        fs.edit();
484
                } catch (ReadException e1) {
485
                        e1.printStackTrace();
486
                        fail();
487
                }
488

    
489
                try{
490
                        Feature newFeature = fs.createNewFeature(true);
491
                        newFeature.editing();
492
                        newFeature.setAttribute("nombre","BuRjaSOT");
493
                        newFeature.setAttribute("tipo","MUNICIPIO");
494
                        fs.insert(newFeature);
495
                }catch (DataException e) {
496
                        e.printStackTrace();
497
                        fail();
498
                }
499

    
500
                try {
501
                        fc = (FeatureSet)fs.getDataSet();
502
                } catch (ReadException e1) {
503
                        e1.printStackTrace();
504
                        fail();
505
                }
506

    
507
                assertEquals(10, fc.size());
508
                fc.dispose();
509

    
510
                try {
511
                        fc = (FeatureSet)fs.getDataCollection(fs.getDefaultFeatureType(),"lower(nombre) like 'b%'",null);
512
                } catch (ReadException e1) {
513
                        e1.printStackTrace();
514
                        fail();
515
                }
516

    
517
                assertEquals(3, fc.size());
518

    
519
                it = fc.iterator();
520
                count=0;
521
                while (it.hasNext()){
522
                        feature = (Feature)it.next();
523
                        assertTrue("Filter error",feature.getString("nombre").toLowerCase().startsWith("b"));
524
                        count++;
525
                }
526
                assertEquals("Iteration error",3,count);
527
                fc.dispose();
528

    
529

    
530
                try {
531
                        fc = (FeatureSet)fs.getDataCollection(fs.getDefaultFeatureType(),null,"nombre ASC");
532
                } catch (ReadException e1) {
533
                        e1.printStackTrace();
534
                        fail();
535
                }
536
                assertEquals(10, fc.size());
537
                it = fc.iterator();
538
                count=0;
539
                pfeature = (Feature)it.next();
540
                count++;
541
                while (it.hasNext()){
542
                        feature = (Feature)it.next();
543
                        v1 = (Comparable)pfeature.get("nombre");
544
                        v2 = (Comparable)feature.get("nombre");
545
                        pfeature=feature;
546
                        assertTrue("Short error", (v1.compareTo(v1) <= 0));
547
                        count++;
548
                }
549
                assertEquals("Iteration error",10,count);
550
                fc.dispose();
551

    
552

    
553
                try {
554
                        fc = (FeatureSet)fs.getDataCollection(fs.getDefaultFeatureType(),null,"nombre DESC");
555
                } catch (ReadException e1) {
556
                        e1.printStackTrace();
557
                        fail();
558
                }
559
                assertEquals(10, fc.size());
560
                it = fc.iterator();
561

    
562
                count=0;
563
                pfeature = (Feature)it.next();
564
                count++;
565
                while (it.hasNext()){
566
                        feature = (Feature)it.next();
567
                        v1 = (Comparable)pfeature.get("nombre");
568
                        v2 = (Comparable)feature.get("nombre");
569
                        pfeature=feature;
570
                        assertTrue("Short error", (v1.compareTo(v1) >= 0));
571
                        count++;
572
                }
573
                assertEquals("Iteration error",10,count);
574
                fc.dispose();
575

    
576

    
577
                try {
578
                        fc = (FeatureSet)fs.getDataCollection(fs.getDefaultFeatureType(),"lower(nombre) like 'b%'","nombre");
579
                } catch (ReadException e1) {
580
                        e1.printStackTrace();
581
                        fail();
582
                }
583

    
584
                assertEquals(3, fc.size());
585

    
586
                it = fc.iterator();
587

    
588
                count=0;
589
                pfeature = (Feature)it.next();
590
                assertTrue(pfeature.getString("nombre").toLowerCase().startsWith("b"));
591
                count++;
592
                while (it.hasNext()){
593
                        feature = (Feature)it.next();
594
                        assertTrue("Filter error",feature.getString("nombre").toLowerCase().startsWith("b"));
595
                        v1 = (Comparable)pfeature.get("nombre");
596
                        v2 = (Comparable)feature.get("nombre");
597
                        pfeature=feature;
598
                        assertTrue("Short error", (v1.compareTo(v1) <= 0));
599
                        count++;
600
                }
601
                assertEquals("Iteration error",3,count);
602
                fc.dispose();
603

    
604

    
605

    
606
                try {
607
                        fc = (FeatureSet)fs.getDataCollection(fs.getDefaultFeatureType(),null,"Tipo,lower(nombre) Desc");
608
                } catch (ReadException e1) {
609
                        e1.printStackTrace();
610
                        fail();
611
                }
612
                assertEquals(10, fc.size());
613
                it = fc.iterator();
614
                count=0;
615
                pfeature = (Feature)it.next();
616
                System.out.println(pfeature.getString("nombre"));
617
                count++;
618
                while (it.hasNext()){
619
                        feature = (Feature)it.next();
620
                        v1 = ((String)pfeature.get("nombre")).toLowerCase();
621
                        v2 = ((String)feature.get("nombre")).toLowerCase();
622
                        pfeature=feature;
623
                        assertTrue("Short error", (v1.compareTo(v2) >= 0));
624
                        System.out.println(pfeature.getString("nombre"));
625
                        count++;
626
                }
627
                assertEquals("Iteration error",10,count);
628
                fc.dispose();
629

    
630

    
631
                fs.cancelEditing();
632

    
633

    
634
                //Insertar un elemento
635
                try{
636
                        fs.edit();
637
                } catch (ReadException e1) {
638
                        e1.printStackTrace();
639
                        fail();
640
                }
641
                try{
642
                feature = fs.createNewFeature(true);
643
                feature.editing();
644
                //                feature.set("id", 90000);
645
                feature.setAttribute("nombre","BurJASOT");
646
                feature.setAttribute("tipo", "OTRO");
647
                fs.insert(feature);
648
                }catch (DataException e) {
649
                        e.printStackTrace();
650
                        fail();
651
                }
652
                try {
653
                        fs.finishEditing();
654
                } catch (WriteException e) {
655
                        e.printStackTrace();
656
                        fail("Exception: "+e);
657
                } catch (ReadException e) {
658
                        e.printStackTrace();
659
                        fail("Exception: "+e);
660
                }
661

    
662

    
663
                try {
664
                        fc = (FeatureSet)fs.getDataCollection(fs.getDefaultFeatureType(),null,"lower(nombre) Desc");
665
                } catch (ReadException e1) {
666
                        e1.printStackTrace();
667
                        fail();
668
                }
669
                assertEquals(10, fc.size());
670
                it = fc.iterator();
671
                count=0;
672
                pfeature = (Feature)it.next();
673
                System.out.println(pfeature.getString("nombre"));
674
                count++;
675
                while (it.hasNext()){
676
                        feature = (Feature)it.next();
677
                        v1 = ((String)pfeature.get("nombre")).toLowerCase();
678
                        v2 = ((String)feature.get("nombre")).toLowerCase();
679
                        pfeature=feature;
680
                        assertTrue("Short error: "+ v1.toString() + " >= " +v2.toString(), (v1.compareTo(v2) >= 0));
681
                        System.out.println(pfeature.getString("nombre"));
682
                        count++;
683
                }
684
                assertEquals("Iteration error",10,count);
685
                fc.dispose();
686

    
687

    
688

    
689
                //Actualizacion
690
                try{
691
                        fs.edit();
692
                } catch (ReadException e1) {
693
                        e1.printStackTrace();
694
                        fail();
695
                }
696

    
697

    
698
                try {
699
                        fc = (FeatureSet)fs.getDataCollection(fs.getDefaultFeatureType(),"lower(nombre) = 'burjasot'",null);
700
                } catch (ReadException e1) {
701
                        e1.printStackTrace();
702
                        fail();
703
                }
704
                assertEquals(1, fc.size());
705
                feature =(AbstractFeature)fc.iterator().next();
706
                try {
707
                        feature.editing();
708
                        feature.setAttribute("nombre", feature.getString("nombre")+"__KK__");
709
                } catch (DataException e2) {
710
                        // TODO Auto-generated catch block
711
                        e2.printStackTrace();
712
                }
713
                try {
714
                        fs.update(feature);
715
                } catch (DataException e2) {
716
                        e2.printStackTrace();fail();
717
                }
718
                try {
719
                        fs.finishEditing();
720
                } catch (WriteException e) {
721
                        e.printStackTrace();
722
                        fail("Exception: "+e);
723
                } catch (ReadException e) {
724
                        e.printStackTrace();
725
                        fail("Exception: "+e);
726
                }
727

    
728
                try {
729
                        fc = (FeatureSet)fs.getDataCollection(fs.getDefaultFeatureType(),"nombre like '%__KK__'",null);
730
                } catch (ReadException e1) {
731
                        e1.printStackTrace();
732
                        fail();
733
                }
734
                assertEquals(1, fc.size());
735
                fc.dispose();
736

    
737

    
738
                //Eliminacion
739
                try{
740
                        fs.edit();
741
                } catch (ReadException e1) {
742
                        e1.printStackTrace();
743
                        fail();
744
                }
745

    
746

    
747
                try {
748
                        fc = (FeatureSet)fs.getDataCollection(fs.getDefaultFeatureType(),"nombre like '%__KK__'",null);
749
                } catch (ReadException e1) {
750
                        e1.printStackTrace();
751
                        fail();
752
                }
753
                assertEquals(1, fc.size());
754
                try {
755
                        fs.delete((Feature)fc.iterator().next());
756
                } catch (DataException e2) {
757
                        e2.printStackTrace();fail();
758
                }
759
                fc.dispose();
760
                try {
761
                        fs.finishEditing();
762
                } catch (WriteException e) {
763
                        e.printStackTrace();
764
                        fail("Exception: "+e);
765
                } catch (ReadException e) {
766
                        e.printStackTrace();
767
                        fail("Exception: "+e);
768
                }
769

    
770
                try {
771
                        fc = (FeatureSet)fs.getDataSet();
772
                } catch (ReadException e1) {
773
                        e1.printStackTrace();
774
                        fail();
775
                }
776
                assertEquals(9, fc.size());
777
                fc.dispose();
778

    
779

    
780

    
781
//                try {
782
//                        fs.close();
783
//                } catch (CloseException e) {
784
//                        e.printStackTrace();
785
//                        fail("Exception:" + e);
786
//                }
787

    
788

    
789

    
790
        }
791

    
792

    
793
        public void test3(){
794

    
795
                try {
796
                        preparar_prueba_table();
797
                } catch (Exception e) {
798
                        e.printStackTrace();
799
                        fail("Inicializando tabla prueba");
800
                }
801

    
802
                Register.selfRegister();
803
                DataManager manager = DefaultDataManager.getManager();
804

    
805

    
806
                PostgresqlStoreParameters dparam=null;
807
                try {
808
                        dparam = (PostgresqlStoreParameters)manager.createStoreParameters(DS_NAME);
809
                } catch (InitializeException e3) {
810
                        e3.printStackTrace();
811
                        fail();
812
                }
813

    
814
                dparam.setHost(SERVER_IP);
815
                dparam.setPort(SERVER_PORT);
816
                dparam.setUser(SERVER_USER);
817
                dparam.setPassw(SERVER_PASWD);
818
                dparam.setSchema(SERVER_SCHEMA);
819
                dparam.setDb(SERVER_DBNAME);
820
                dparam.setTableName("prueba");
821
                dparam.setFieldsId(new String[] {"gid"});
822
                dparam.setFields(new String[] {"*"});
823
                dparam.setDefaultGeometryField("the_geom");
824

    
825
                FeatureStore fs=null;
826
                try {
827
                        fs = (FeatureStore)manager.createStore(dparam);
828
                } catch (InitializeException e) {
829
                        e.printStackTrace();
830
                        fail("Exception:" + e);
831
                }
832
                assertNotNull("Can't create Feature Store", fs);
833

    
834
//                try {
835
//                        fs.open();
836
//                } catch (OpenException e2) {
837
//                        e2.printStackTrace();
838
//                        fail();
839
//                }
840
                int size;
841

    
842
                try {
843
                        fs.edit();
844
                } catch (ReadException e) {
845
                        e.printStackTrace();fail();
846
                }
847

    
848
                DBAttributeDescriptor attr =(DBAttributeDescriptor)fs.getDefaultFeatureType().createNewAttributeDescriptor();
849
                attr.editing();
850
                try {
851
                        attr.setName("My_new_Field");
852
                        attr.setType(FeatureAttributeDescriptor.STRING);
853
                        attr.setSize(50);
854
                } catch (IsNotAttributeSettingException e1) {
855
                        // TODO Auto-generated catch block
856
                        e1.printStackTrace();
857
                }
858
                try {
859
                        fs.insert(attr);
860
                } catch (DataException e2) {
861
                        e2.printStackTrace();fail();
862
                }
863

    
864
                Iterator iter = null;
865

    
866

    
867

    
868
                FeatureSet fc=null;
869
                try {
870
                        fc = (FeatureSet)fs.getDataSet();
871
                } catch (ReadException e1) {
872
                        e1.printStackTrace();fail();
873
                }
874

    
875
                size = fc.size();
876
                iter = fc.iterator();
877

    
878

    
879
                int i= 0;
880
                AbstractFeature feature = null;
881
                while (iter.hasNext()){
882
                        feature = (AbstractFeature)iter.next();
883
                        try {
884
                                feature.editing();
885
                                feature.setAttribute("My_new_Field","V"+i);
886
                        } catch (DataException e) {
887
                                e.printStackTrace();fail();
888
                        }
889

    
890
                        try {
891
                                fs.update(feature);
892
                        } catch (DataException e) {
893
                                e.printStackTrace();fail();
894
                        }
895

    
896
                }
897

    
898
                fc.dispose();
899

    
900

    
901
                try {
902
                        fc = (FeatureSet)fs.getDataSet();
903
                } catch (ReadException e1) {
904
                        e1.printStackTrace();fail();
905
                }
906

    
907
                assertEquals(size, fc.size());
908

    
909
                iter = fc.iterator();
910

    
911
                while (iter.hasNext()){
912
                        feature = (AbstractFeature)iter.next();
913
                    assertTrue(feature.getString("My_new_Field") != null);
914
                    assertTrue(feature.getString("My_new_Field").startsWith("V"));
915
                }
916

    
917
                try {
918
                        fs.finishEditing();
919
                } catch (WriteException e) {
920
                        e.printStackTrace();fail();
921
                } catch (ReadException e) {
922
                        e.printStackTrace();fail();
923
                }
924

    
925
                try {
926
                        fc = (FeatureSet)fs.getDataSet();
927
                } catch (ReadException e1) {
928
                        e1.printStackTrace();fail();
929
                }
930

    
931
                iter = fc.iterator();
932

    
933
                while (iter.hasNext()){
934
                        feature = (AbstractFeature)iter.next();
935
                    assertTrue(feature.getString("my_new_field") != null);
936
                    assertTrue(feature.getString("my_new_field").startsWith("V"));
937
                }
938

    
939
                fc.dispose();
940

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

    
947
                try {
948
                        fs.dispose();
949
                } catch (CloseException e) {
950
                        e.printStackTrace();fail();
951
                }
952

    
953
        }
954

    
955
        public void test_explorer_persistence() {
956
                Register.selfRegister();
957
                DataManager manager = DefaultDataManager.getManager();
958

    
959
                JDBCExplorerParameter params = null;
960
                try {
961
                        params = (JDBCExplorerParameter) manager
962
                                        .createExplorerParameters(DE_NAME);
963
                } catch (InitializeException e3) {
964
                        e3.printStackTrace();
965
                        fail();
966
                }
967

    
968
                params.setHost(SERVER_IP);
969
                params.setPort(SERVER_PORT);
970
                params.setUser(SERVER_USER);
971
                params.setPassw(SERVER_PASWD);
972
                params.setSchema(SERVER_SCHEMA);
973
                params.setDb(SERVER_DBNAME);
974

    
975

    
976
                this.persistenceTest(params);
977

    
978
        }
979
}