root / trunk / libraries / libFMap_dataDB / src-test / org / gvsig / data / datastores / vectorial / db / jdbc / postgresql / postgresqlTest.java @ 20908
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 |
// ","+
|
63 |
// " CONSTRAINT enforce_geotype_the_geom CHECK (geometrytype(the_geom) = 'MULTIPOLYGON'::text OR the_geom IS NULL),"+
|
64 |
// " CONSTRAINT enforce_srid_the_geom CHECK (srid(the_geom) = 23030)"+
|
65 |
") "+
|
66 |
"WITHOUT OIDS");
|
67 |
|
68 |
st.execute("ALTER TABLE ds_test.prueba OWNER TO \"testCase\"");
|
69 |
st.execute("Insert into "+SERVER_SCHEMA+".prueba select * from "+SERVER_SCHEMA+".prueba_backup"); |
70 |
st.execute("SELECT setval('"+SERVER_SCHEMA+".prueba_gid_seq', (select max(gid) from "+SERVER_SCHEMA+".prueba))"); |
71 |
st.close(); |
72 |
conn.commit(); |
73 |
conn.close(); |
74 |
|
75 |
|
76 |
|
77 |
|
78 |
} |
79 |
|
80 |
public void test1(){ |
81 |
Register.selfRegister(); |
82 |
|
83 |
try {
|
84 |
preparar_prueba_table(); |
85 |
} catch (Exception e) { |
86 |
e.printStackTrace(); |
87 |
fail("Inicializando tabla prueba");
|
88 |
} |
89 |
DataManager manager = DataManager.getManager(); |
90 |
|
91 |
|
92 |
PostgresqlStoreParameters dparam = |
93 |
(PostgresqlStoreParameters)manager.createDataStoreParameters(DS_NAME); |
94 |
|
95 |
dparam.setHost(SERVER_IP); |
96 |
dparam.setPort(SERVER_PORT); |
97 |
dparam.setUser(SERVER_USER); |
98 |
dparam.setPassw(SERVER_PASWD); |
99 |
dparam.setSchema(SERVER_SCHEMA); |
100 |
dparam.setDb(SERVER_DBNAME); |
101 |
dparam.setTableName("prueba");
|
102 |
dparam.setFieldId(new String[] {"gid"}); |
103 |
dparam.setFields(new String[] {"*"}); |
104 |
dparam.setDefaultGeometryField("the_geom");
|
105 |
|
106 |
storeTest(dparam, null, null, false); |
107 |
|
108 |
} |
109 |
|
110 |
public void testSimpleIteration38K_regs(){ |
111 |
Register.selfRegister(); |
112 |
DataManager manager = DataManager.getManager(); |
113 |
|
114 |
|
115 |
PostgresqlStoreParameters dparam = |
116 |
(PostgresqlStoreParameters)manager.createDataStoreParameters(DS_NAME); |
117 |
|
118 |
dparam.setHost(SERVER_IP); |
119 |
dparam.setPort(SERVER_PORT); |
120 |
dparam.setUser(SERVER_USER); |
121 |
dparam.setPassw(SERVER_PASWD); |
122 |
dparam.setSchema(null);
|
123 |
dparam.setDb(SERVER_DBNAME); |
124 |
dparam.setTableName("hidropol");
|
125 |
// dparam.setTableName("ejes");
|
126 |
dparam.setFieldId(new String[] {"gid"}); |
127 |
dparam.setFields(new String[] {"*"}); |
128 |
dparam.setDefaultGeometryField("the_geom");
|
129 |
|
130 |
// storeTest(dparam, null, null, false);
|
131 |
|
132 |
IFeatureStore fs=null;
|
133 |
try {
|
134 |
fs = (IFeatureStore) manager.createDataStore(dparam); |
135 |
} catch (InitializeException e) {
|
136 |
e.printStackTrace(); |
137 |
fail(); |
138 |
} |
139 |
IFeatureCollection fc=null;
|
140 |
try {
|
141 |
fc = (IFeatureCollection) fs.getDataCollection(); |
142 |
} catch (ReadException e) {
|
143 |
e.printStackTrace(); |
144 |
fail(); |
145 |
} |
146 |
|
147 |
int i=0; |
148 |
int count = fc.size();
|
149 |
Iterator iter = fc.iterator();
|
150 |
while (true){ |
151 |
try{
|
152 |
iter.next(); |
153 |
i++; |
154 |
} catch (NoSuchElementException e) { |
155 |
break;
|
156 |
} |
157 |
} |
158 |
assertEquals(count, i); |
159 |
|
160 |
fc.dispose(); |
161 |
|
162 |
try {
|
163 |
fs.close(); |
164 |
} catch (CloseException e) {
|
165 |
e.printStackTrace(); |
166 |
fail("Exception: "+e);
|
167 |
} |
168 |
try {
|
169 |
fs.dispose(); |
170 |
} catch (CloseException e) {
|
171 |
// TODO Auto-generated catch block
|
172 |
e.printStackTrace(); |
173 |
} |
174 |
|
175 |
|
176 |
} |
177 |
|
178 |
public void test__sqlMode(){ |
179 |
Register.selfRegister(); |
180 |
DataManager manager = DataManager.getManager(); |
181 |
|
182 |
|
183 |
PostgresqlStoreParameters dparam = |
184 |
(PostgresqlStoreParameters)manager.createDataStoreParameters(DS_NAME); |
185 |
|
186 |
dparam.setHost(SERVER_IP); |
187 |
dparam.setPort(SERVER_PORT); |
188 |
dparam.setUser(SERVER_USER); |
189 |
dparam.setPassw(SERVER_PASWD); |
190 |
dparam.setSchema(SERVER_SCHEMA); |
191 |
dparam.setDb(SERVER_DBNAME); |
192 |
// dparam.setTableName("prueba");
|
193 |
dparam.setSqlSoure("Select * from "+ SERVER_SCHEMA+".prueba"); |
194 |
dparam.setFieldId(new String[] {"gid"}); |
195 |
dparam.setFields(new String[] {"*"}); |
196 |
dparam.setDefaultGeometryField("the_geom");
|
197 |
|
198 |
storeTest(dparam, null, null, false); |
199 |
|
200 |
Exception exc = null; |
201 |
|
202 |
IFeatureStore fs=null;
|
203 |
try {
|
204 |
fs = (IFeatureStore)manager.createDataStore(dparam); |
205 |
} catch (InitializeException e) {
|
206 |
e.printStackTrace(); |
207 |
fail("Exception:" + e);
|
208 |
} |
209 |
assertNotNull("Can't create Feature Store", fs);
|
210 |
|
211 |
try {
|
212 |
fs.open(); |
213 |
} catch (OpenException e2) {
|
214 |
e2.printStackTrace(); |
215 |
fail(); |
216 |
} |
217 |
|
218 |
|
219 |
IFeatureCollection fc =null;
|
220 |
|
221 |
try {
|
222 |
fc = (IFeatureCollection)fs.getDataCollection(); |
223 |
} catch (ReadException e1) {
|
224 |
e1.printStackTrace(); |
225 |
fail(); |
226 |
} |
227 |
|
228 |
assertEquals(9, fc.size());
|
229 |
|
230 |
fc.dispose(); |
231 |
|
232 |
assertFalse("Edition allowed in sqlSource mode", fs.isEditable());
|
233 |
|
234 |
try {
|
235 |
fs.startEditing(); |
236 |
} catch (ReadException e1) {
|
237 |
exc=e1; |
238 |
} |
239 |
assertNotNull("Edition allowed in sqlSource mode",exc);
|
240 |
|
241 |
exc=null;
|
242 |
try {
|
243 |
fc = (IFeatureCollection)fs.getDataCollection(null,"nombre like 'B%'",null); |
244 |
} catch (ReadException e1) {
|
245 |
exc=e1; |
246 |
} |
247 |
assertNotNull("Filter allowed in sqlSource mode",exc);
|
248 |
|
249 |
exc=null;
|
250 |
try {
|
251 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"nombre"); |
252 |
} catch (ReadException e1) {
|
253 |
exc=e1; |
254 |
} |
255 |
assertNotNull("Order allowed in sqlSource mode",exc);
|
256 |
|
257 |
|
258 |
try {
|
259 |
fs.close(); |
260 |
} catch (CloseException e) {
|
261 |
e.printStackTrace(); |
262 |
fail("Exception:" + e);
|
263 |
} |
264 |
|
265 |
} |
266 |
|
267 |
public void test2(){ |
268 |
|
269 |
try {
|
270 |
preparar_prueba_table(); |
271 |
} catch (Exception e) { |
272 |
e.printStackTrace(); |
273 |
fail("Inicializando tabla prueba");
|
274 |
} |
275 |
|
276 |
Register.selfRegister(); |
277 |
DataManager manager = DataManager.getManager(); |
278 |
|
279 |
|
280 |
PostgresqlStoreParameters dparam = |
281 |
(PostgresqlStoreParameters)manager.createDataStoreParameters(DS_NAME); |
282 |
|
283 |
dparam.setHost(SERVER_IP); |
284 |
dparam.setPort(SERVER_PORT); |
285 |
dparam.setUser(SERVER_USER); |
286 |
dparam.setPassw(SERVER_PASWD); |
287 |
dparam.setSchema(SERVER_SCHEMA); |
288 |
dparam.setDb(SERVER_DBNAME); |
289 |
dparam.setTableName("prueba");
|
290 |
dparam.setFieldId(new String[] {"gid"}); |
291 |
dparam.setFields(new String[] {"*"}); |
292 |
dparam.setDefaultGeometryField("the_geom");
|
293 |
|
294 |
// storeTest(dparam, null, null, true);
|
295 |
storeTest(dparam, null, null, false); |
296 |
|
297 |
IFeatureStore fs=null;
|
298 |
try {
|
299 |
fs = (IFeatureStore)manager.createDataStore(dparam); |
300 |
} catch (InitializeException e) {
|
301 |
e.printStackTrace(); |
302 |
fail("Exception:" + e);
|
303 |
} |
304 |
assertNotNull("Can't create Feature Store", fs);
|
305 |
|
306 |
try {
|
307 |
fs.open(); |
308 |
} catch (OpenException e2) {
|
309 |
e2.printStackTrace(); |
310 |
fail(); |
311 |
} |
312 |
|
313 |
Iterator it;
|
314 |
IFeatureCollection fc =null;
|
315 |
Comparable v1,v2;
|
316 |
IFeature feature=null,pfeature=null; |
317 |
long count;
|
318 |
|
319 |
|
320 |
try {
|
321 |
fc = (IFeatureCollection)fs.getDataCollection(); |
322 |
} catch (ReadException e1) {
|
323 |
e1.printStackTrace(); |
324 |
fail(); |
325 |
} |
326 |
|
327 |
assertEquals(9, fc.size());
|
328 |
|
329 |
fc.dispose(); |
330 |
|
331 |
try {
|
332 |
fc = (IFeatureCollection)fs.getDataCollection(null,"lower(nombre) like 'b%'",null); |
333 |
} catch (ReadException e1) {
|
334 |
e1.printStackTrace(); |
335 |
fail(); |
336 |
} |
337 |
|
338 |
assertEquals(2, fc.size());
|
339 |
|
340 |
it = fc.iterator(); |
341 |
count=0;
|
342 |
while (it.hasNext()){
|
343 |
feature = (IFeature)it.next(); |
344 |
assertTrue("Filter error",feature.getString("nombre").toLowerCase().startsWith("b")); |
345 |
count++; |
346 |
} |
347 |
assertEquals("Iteration error",2,count); |
348 |
|
349 |
fc.dispose(); |
350 |
|
351 |
|
352 |
try {
|
353 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"nombre ASC"); |
354 |
} catch (ReadException e1) {
|
355 |
e1.printStackTrace(); |
356 |
fail(); |
357 |
} |
358 |
assertEquals(9, fc.size());
|
359 |
it = fc.iterator(); |
360 |
count=0;
|
361 |
pfeature = (IFeature)it.next(); |
362 |
count++; |
363 |
while (it.hasNext()){
|
364 |
feature = (IFeature)it.next(); |
365 |
v1 = (Comparable)pfeature.get("nombre"); |
366 |
v2 = (Comparable)feature.get("nombre"); |
367 |
pfeature=feature; |
368 |
assertTrue("Short error", (v1.compareTo(v1) <= 0)); |
369 |
count++; |
370 |
} |
371 |
assertEquals("Iteration error",9,count); |
372 |
|
373 |
fc.dispose(); |
374 |
|
375 |
|
376 |
try {
|
377 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"nombre DESC"); |
378 |
} catch (ReadException e1) {
|
379 |
e1.printStackTrace(); |
380 |
fail(); |
381 |
} |
382 |
assertEquals(9, fc.size());
|
383 |
it = fc.iterator(); |
384 |
|
385 |
count=0;
|
386 |
pfeature = (IFeature)it.next(); |
387 |
count++; |
388 |
while (it.hasNext()){
|
389 |
feature = (IFeature)it.next(); |
390 |
v1 = (Comparable)pfeature.get("nombre"); |
391 |
v2 = (Comparable)feature.get("nombre"); |
392 |
pfeature=feature; |
393 |
assertTrue("Short error", (v1.compareTo(v1) >= 0)); |
394 |
count++; |
395 |
} |
396 |
assertEquals("Iteration error",9,count); |
397 |
|
398 |
fc.dispose(); |
399 |
|
400 |
|
401 |
|
402 |
try {
|
403 |
fc = (IFeatureCollection)fs.getDataCollection(null,"lower(nombre) like 'b%'","nombre"); |
404 |
} catch (ReadException e1) {
|
405 |
e1.printStackTrace(); |
406 |
fail(); |
407 |
} |
408 |
|
409 |
assertEquals(2, fc.size());
|
410 |
|
411 |
it = fc.iterator(); |
412 |
|
413 |
count=0;
|
414 |
pfeature = (IFeature)it.next(); |
415 |
assertTrue(pfeature.getString("nombre").toLowerCase().startsWith("b")); |
416 |
count++; |
417 |
while (it.hasNext()){
|
418 |
feature = (IFeature)it.next(); |
419 |
assertTrue("Filter error",feature.getString("nombre").toLowerCase().startsWith("b")); |
420 |
v1 = (Comparable)pfeature.get("nombre"); |
421 |
v2 = (Comparable)feature.get("nombre"); |
422 |
pfeature=feature; |
423 |
assertTrue("Short error", (v1.compareTo(v1) <= 0)); |
424 |
count++; |
425 |
} |
426 |
assertEquals("Iteration error",2,count); |
427 |
fc.dispose(); |
428 |
|
429 |
|
430 |
|
431 |
try {
|
432 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"Tipo,lower(nombre) Desc"); |
433 |
} catch (ReadException e1) {
|
434 |
e1.printStackTrace(); |
435 |
fail(); |
436 |
} |
437 |
assertEquals(9, fc.size());
|
438 |
it = fc.iterator(); |
439 |
count=0;
|
440 |
pfeature = (IFeature)it.next(); |
441 |
System.out.println(pfeature.getString("nombre")); |
442 |
count++; |
443 |
while (it.hasNext()){
|
444 |
feature = (IFeature)it.next(); |
445 |
v1 = (Comparable)((String)pfeature.get("nombre")).toLowerCase(); |
446 |
v2 = (Comparable)((String)feature.get("nombre")).toLowerCase(); |
447 |
pfeature=feature; |
448 |
assertTrue("Short error", (v1.compareTo(v2) >= 0)); |
449 |
System.out.println(pfeature.getString("nombre")); |
450 |
count++; |
451 |
} |
452 |
assertEquals("Iteration error",9,count); |
453 |
fc.dispose(); |
454 |
|
455 |
|
456 |
|
457 |
/// CON EDICION
|
458 |
try {
|
459 |
fs.startEditing(); |
460 |
} catch (ReadException e1) {
|
461 |
e1.printStackTrace(); |
462 |
fail(); |
463 |
} |
464 |
|
465 |
try{
|
466 |
IFeature newFeature = fs.createFeature(fs.getDefaultFeatureType()); |
467 |
newFeature.editing(); |
468 |
newFeature.set("nombre","BuRjaSOT"); |
469 |
newFeature.set("tipo","MUNICIPIO"); |
470 |
fs.insert(newFeature); |
471 |
}catch (DataException e) {
|
472 |
e.printStackTrace(); |
473 |
fail(); |
474 |
} |
475 |
|
476 |
try {
|
477 |
fc = (IFeatureCollection)fs.getDataCollection(); |
478 |
} catch (ReadException e1) {
|
479 |
e1.printStackTrace(); |
480 |
fail(); |
481 |
} |
482 |
|
483 |
assertEquals(10, fc.size());
|
484 |
fc.dispose(); |
485 |
|
486 |
try {
|
487 |
fc = (IFeatureCollection)fs.getDataCollection(null,"lower(nombre) like 'b%'",null); |
488 |
} catch (ReadException e1) {
|
489 |
e1.printStackTrace(); |
490 |
fail(); |
491 |
} |
492 |
|
493 |
assertEquals(3, fc.size());
|
494 |
|
495 |
it = fc.iterator(); |
496 |
count=0;
|
497 |
while (it.hasNext()){
|
498 |
feature = (IFeature)it.next(); |
499 |
assertTrue("Filter error",feature.getString("nombre").toLowerCase().startsWith("b")); |
500 |
count++; |
501 |
} |
502 |
assertEquals("Iteration error",3,count); |
503 |
fc.dispose(); |
504 |
|
505 |
|
506 |
try {
|
507 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"nombre ASC"); |
508 |
} catch (ReadException e1) {
|
509 |
e1.printStackTrace(); |
510 |
fail(); |
511 |
} |
512 |
assertEquals(10, fc.size());
|
513 |
it = fc.iterator(); |
514 |
count=0;
|
515 |
pfeature = (IFeature)it.next(); |
516 |
count++; |
517 |
while (it.hasNext()){
|
518 |
feature = (IFeature)it.next(); |
519 |
v1 = (Comparable)pfeature.get("nombre"); |
520 |
v2 = (Comparable)feature.get("nombre"); |
521 |
pfeature=feature; |
522 |
assertTrue("Short error", (v1.compareTo(v1) <= 0)); |
523 |
count++; |
524 |
} |
525 |
assertEquals("Iteration error",10,count); |
526 |
fc.dispose(); |
527 |
|
528 |
|
529 |
try {
|
530 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"nombre DESC"); |
531 |
} catch (ReadException e1) {
|
532 |
e1.printStackTrace(); |
533 |
fail(); |
534 |
} |
535 |
assertEquals(10, fc.size());
|
536 |
it = fc.iterator(); |
537 |
|
538 |
count=0;
|
539 |
pfeature = (IFeature)it.next(); |
540 |
count++; |
541 |
while (it.hasNext()){
|
542 |
feature = (IFeature)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 = (IFeatureCollection)fs.getDataCollection(null,"lower(nombre) like 'b%'","nombre"); |
555 |
} catch (ReadException e1) {
|
556 |
e1.printStackTrace(); |
557 |
fail(); |
558 |
} |
559 |
|
560 |
assertEquals(3, fc.size());
|
561 |
|
562 |
it = fc.iterator(); |
563 |
|
564 |
count=0;
|
565 |
pfeature = (IFeature)it.next(); |
566 |
assertTrue(pfeature.getString("nombre").toLowerCase().startsWith("b")); |
567 |
count++; |
568 |
while (it.hasNext()){
|
569 |
feature = (IFeature)it.next(); |
570 |
assertTrue("Filter error",feature.getString("nombre").toLowerCase().startsWith("b")); |
571 |
v1 = (Comparable)pfeature.get("nombre"); |
572 |
v2 = (Comparable)feature.get("nombre"); |
573 |
pfeature=feature; |
574 |
assertTrue("Short error", (v1.compareTo(v1) <= 0)); |
575 |
count++; |
576 |
} |
577 |
assertEquals("Iteration error",3,count); |
578 |
fc.dispose(); |
579 |
|
580 |
|
581 |
|
582 |
try {
|
583 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"Tipo,lower(nombre) Desc"); |
584 |
} catch (ReadException e1) {
|
585 |
e1.printStackTrace(); |
586 |
fail(); |
587 |
} |
588 |
assertEquals(10, fc.size());
|
589 |
it = fc.iterator(); |
590 |
count=0;
|
591 |
pfeature = (IFeature)it.next(); |
592 |
System.out.println(pfeature.getString("nombre")); |
593 |
count++; |
594 |
while (it.hasNext()){
|
595 |
feature = (IFeature)it.next(); |
596 |
v1 = (Comparable)((String)pfeature.get("nombre")).toLowerCase(); |
597 |
v2 = (Comparable)((String)feature.get("nombre")).toLowerCase(); |
598 |
pfeature=feature; |
599 |
assertTrue("Short error", (v1.compareTo(v2) >= 0)); |
600 |
System.out.println(pfeature.getString("nombre")); |
601 |
count++; |
602 |
} |
603 |
assertEquals("Iteration error",10,count); |
604 |
fc.dispose(); |
605 |
|
606 |
|
607 |
fs.cancelEditing(); |
608 |
|
609 |
|
610 |
//Insertar un elemento
|
611 |
try{
|
612 |
fs.startEditing(); |
613 |
} catch (ReadException e1) {
|
614 |
e1.printStackTrace(); |
615 |
fail(); |
616 |
} |
617 |
try{
|
618 |
feature = fs.createDefaultFeature(true);
|
619 |
feature.editing(); |
620 |
// feature.set("id", 90000);
|
621 |
feature.set("nombre","BurJASOT"); |
622 |
feature.set("tipo", "OTRO"); |
623 |
fs.insert(feature); |
624 |
}catch (DataException e) {
|
625 |
e.printStackTrace(); |
626 |
fail(); |
627 |
} |
628 |
try {
|
629 |
fs.finishEditing(); |
630 |
} catch (WriteException e) {
|
631 |
e.printStackTrace(); |
632 |
fail("Exception: "+e);
|
633 |
} catch (ReadException e) {
|
634 |
e.printStackTrace(); |
635 |
fail("Exception: "+e);
|
636 |
} |
637 |
|
638 |
|
639 |
try {
|
640 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"lower(nombre) Desc"); |
641 |
} catch (ReadException e1) {
|
642 |
e1.printStackTrace(); |
643 |
fail(); |
644 |
} |
645 |
assertEquals(10, fc.size());
|
646 |
it = fc.iterator(); |
647 |
count=0;
|
648 |
pfeature = (IFeature)it.next(); |
649 |
System.out.println(pfeature.getString("nombre")); |
650 |
count++; |
651 |
while (it.hasNext()){
|
652 |
feature = (IFeature)it.next(); |
653 |
v1 = (Comparable)((String)pfeature.get("nombre")).toLowerCase(); |
654 |
v2 = (Comparable)((String)feature.get("nombre")).toLowerCase(); |
655 |
pfeature=feature; |
656 |
assertTrue("Short error: "+ v1.toString() + " >= " +v2.toString(), (v1.compareTo(v2) >= 0)); |
657 |
System.out.println(pfeature.getString("nombre")); |
658 |
count++; |
659 |
} |
660 |
assertEquals("Iteration error",10,count); |
661 |
fc.dispose(); |
662 |
|
663 |
|
664 |
|
665 |
//Actualizacion
|
666 |
try{
|
667 |
fs.startEditing(); |
668 |
} catch (ReadException e1) {
|
669 |
e1.printStackTrace(); |
670 |
fail(); |
671 |
} |
672 |
|
673 |
|
674 |
try {
|
675 |
fc = (IFeatureCollection)fs.getDataCollection(null,"lower(nombre) = 'burjasot'",null); |
676 |
} catch (ReadException e1) {
|
677 |
e1.printStackTrace(); |
678 |
fail(); |
679 |
} |
680 |
assertEquals(1, fc.size());
|
681 |
feature =(Feature)fc.iterator().next(); |
682 |
try {
|
683 |
feature.editing(); |
684 |
feature.set("nombre", feature.getString("nombre")+"__KK__"); |
685 |
} catch (DataException e2) {
|
686 |
// TODO Auto-generated catch block
|
687 |
e2.printStackTrace(); |
688 |
} |
689 |
try {
|
690 |
fs.update(feature); |
691 |
} catch (DataException e2) {
|
692 |
e2.printStackTrace();fail(); |
693 |
} |
694 |
try {
|
695 |
fs.finishEditing(); |
696 |
} catch (WriteException e) {
|
697 |
e.printStackTrace(); |
698 |
fail("Exception: "+e);
|
699 |
} catch (ReadException e) {
|
700 |
e.printStackTrace(); |
701 |
fail("Exception: "+e);
|
702 |
} |
703 |
|
704 |
try {
|
705 |
fc = (IFeatureCollection)fs.getDataCollection(null,"nombre like '%__KK__'",null); |
706 |
} catch (ReadException e1) {
|
707 |
e1.printStackTrace(); |
708 |
fail(); |
709 |
} |
710 |
assertEquals(1, fc.size());
|
711 |
fc.dispose(); |
712 |
|
713 |
|
714 |
//Eliminacion
|
715 |
try{
|
716 |
fs.startEditing(); |
717 |
} catch (ReadException e1) {
|
718 |
e1.printStackTrace(); |
719 |
fail(); |
720 |
} |
721 |
|
722 |
|
723 |
try {
|
724 |
fc = (IFeatureCollection)fs.getDataCollection(null,"nombre like '%__KK__'",null); |
725 |
} catch (ReadException e1) {
|
726 |
e1.printStackTrace(); |
727 |
fail(); |
728 |
} |
729 |
assertEquals(1, fc.size());
|
730 |
try {
|
731 |
fs.delete((IFeature)fc.iterator().next()); |
732 |
} catch (DataException e2) {
|
733 |
e2.printStackTrace();fail(); |
734 |
} |
735 |
fc.dispose(); |
736 |
try {
|
737 |
fs.finishEditing(); |
738 |
} catch (WriteException e) {
|
739 |
e.printStackTrace(); |
740 |
fail("Exception: "+e);
|
741 |
} catch (ReadException e) {
|
742 |
e.printStackTrace(); |
743 |
fail("Exception: "+e);
|
744 |
} |
745 |
|
746 |
try {
|
747 |
fc = (IFeatureCollection)fs.getDataCollection(); |
748 |
} catch (ReadException e1) {
|
749 |
e1.printStackTrace(); |
750 |
fail(); |
751 |
} |
752 |
assertEquals(9, fc.size());
|
753 |
fc.dispose(); |
754 |
|
755 |
|
756 |
|
757 |
try {
|
758 |
fs.close(); |
759 |
} catch (CloseException e) {
|
760 |
e.printStackTrace(); |
761 |
fail("Exception:" + e);
|
762 |
} |
763 |
|
764 |
|
765 |
|
766 |
} |
767 |
|
768 |
|
769 |
public void test3(){ |
770 |
|
771 |
try {
|
772 |
preparar_prueba_table(); |
773 |
} catch (Exception e) { |
774 |
e.printStackTrace(); |
775 |
fail("Inicializando tabla prueba");
|
776 |
} |
777 |
|
778 |
Register.selfRegister(); |
779 |
DataManager manager = DataManager.getManager(); |
780 |
|
781 |
|
782 |
PostgresqlStoreParameters dparam = |
783 |
(PostgresqlStoreParameters)manager.createDataStoreParameters(DS_NAME); |
784 |
|
785 |
dparam.setHost(SERVER_IP); |
786 |
dparam.setPort(SERVER_PORT); |
787 |
dparam.setUser(SERVER_USER); |
788 |
dparam.setPassw(SERVER_PASWD); |
789 |
dparam.setSchema(SERVER_SCHEMA); |
790 |
dparam.setDb(SERVER_DBNAME); |
791 |
dparam.setTableName("prueba");
|
792 |
dparam.setFieldId(new String[] {"gid"}); |
793 |
dparam.setFields(new String[] {"*"}); |
794 |
dparam.setDefaultGeometryField("the_geom");
|
795 |
|
796 |
IFeatureStore fs=null;
|
797 |
try {
|
798 |
fs = (IFeatureStore)manager.createDataStore(dparam); |
799 |
} catch (InitializeException e) {
|
800 |
e.printStackTrace(); |
801 |
fail("Exception:" + e);
|
802 |
} |
803 |
assertNotNull("Can't create Feature Store", fs);
|
804 |
|
805 |
try {
|
806 |
fs.open(); |
807 |
} catch (OpenException e2) {
|
808 |
e2.printStackTrace(); |
809 |
fail(); |
810 |
} |
811 |
int size;
|
812 |
|
813 |
try {
|
814 |
fs.startEditing(); |
815 |
} catch (ReadException e) {
|
816 |
e.printStackTrace();fail(); |
817 |
} |
818 |
|
819 |
DBAttributeDescriptor attr =(DBAttributeDescriptor)fs.getDefaultFeatureType().createNewAttributeDescriptor(); |
820 |
attr.editing(); |
821 |
try {
|
822 |
attr.setName("My_new_Field");
|
823 |
attr.setType(IFeatureAttributeDescriptor.TYPE_STRING); |
824 |
attr.setSize(50);
|
825 |
} catch (IsNotAttributeSettingException e1) {
|
826 |
// TODO Auto-generated catch block
|
827 |
e1.printStackTrace(); |
828 |
} |
829 |
try {
|
830 |
fs.insert(attr); |
831 |
} catch (DataException e2) {
|
832 |
e2.printStackTrace();fail(); |
833 |
} |
834 |
|
835 |
Iterator iter = null; |
836 |
|
837 |
|
838 |
|
839 |
IFeatureCollection fc=null;
|
840 |
try {
|
841 |
fc = (IFeatureCollection)fs.getDataCollection(); |
842 |
} catch (ReadException e1) {
|
843 |
e1.printStackTrace();fail(); |
844 |
} |
845 |
|
846 |
size = fc.size(); |
847 |
iter = fc.iterator(); |
848 |
|
849 |
|
850 |
int i= 0; |
851 |
Feature feature = null;
|
852 |
while (iter.hasNext()){
|
853 |
feature = (Feature)iter.next(); |
854 |
try {
|
855 |
feature.editing(); |
856 |
feature.set("My_new_Field","V"+i); |
857 |
} catch (DataException e) {
|
858 |
e.printStackTrace();fail(); |
859 |
} |
860 |
|
861 |
try {
|
862 |
fs.update(feature); |
863 |
} catch (DataException e) {
|
864 |
e.printStackTrace();fail(); |
865 |
} |
866 |
|
867 |
} |
868 |
|
869 |
fc.dispose(); |
870 |
|
871 |
|
872 |
try {
|
873 |
fc = (IFeatureCollection)fs.getDataCollection(); |
874 |
} catch (ReadException e1) {
|
875 |
e1.printStackTrace();fail(); |
876 |
} |
877 |
|
878 |
assertEquals(size, fc.size()); |
879 |
|
880 |
iter = fc.iterator(); |
881 |
|
882 |
while (iter.hasNext()){
|
883 |
feature = (Feature)iter.next(); |
884 |
assertTrue(feature.getString("My_new_Field") != null); |
885 |
assertTrue(feature.getString("My_new_Field").startsWith("V")); |
886 |
} |
887 |
|
888 |
try {
|
889 |
fs.finishEditing(); |
890 |
} catch (WriteException e) {
|
891 |
e.printStackTrace();fail(); |
892 |
} catch (ReadException e) {
|
893 |
e.printStackTrace();fail(); |
894 |
} |
895 |
|
896 |
try {
|
897 |
fc = (IFeatureCollection)fs.getDataCollection(); |
898 |
} catch (ReadException e1) {
|
899 |
e1.printStackTrace();fail(); |
900 |
} |
901 |
|
902 |
iter = fc.iterator(); |
903 |
|
904 |
while (iter.hasNext()){
|
905 |
feature = (Feature)iter.next(); |
906 |
assertTrue(feature.getString("my_new_field") != null); |
907 |
assertTrue(feature.getString("my_new_field").startsWith("V")); |
908 |
} |
909 |
|
910 |
fc.dispose(); |
911 |
|
912 |
try {
|
913 |
fs.close(); |
914 |
} catch (CloseException e) {
|
915 |
e.printStackTrace();fail(); |
916 |
} |
917 |
|
918 |
try {
|
919 |
fs.dispose(); |
920 |
} catch (CloseException e) {
|
921 |
e.printStackTrace();fail(); |
922 |
} |
923 |
|
924 |
} |
925 |
} |