svn-gvsig-desktop / trunk / libraries / libDataSourceDBBaseDrivers / src-test / org / gvsig / data / datastores / vectorial / driver / jdbc / DataStoreTest.java @ 19833
History | View | Annotate | Download (22.2 KB)
1 |
package org.gvsig.data.datastores.vectorial.driver.jdbc; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.sql.Connection; |
5 |
import java.sql.DriverManager; |
6 |
import java.sql.SQLException; |
7 |
import java.sql.Statement; |
8 |
import java.util.Iterator; |
9 |
|
10 |
import junit.framework.TestCase; |
11 |
|
12 |
import org.gvsig.data.DataSourceManager; |
13 |
import org.gvsig.data.IDataStoreParameters; |
14 |
import org.gvsig.data.datastores.vectorial.driver.IDriverParameters; |
15 |
import org.gvsig.data.datastores.vectorial.driver.jdbc.h2.H2Store; |
16 |
import org.gvsig.data.datastores.vectorial.driver.jdbc.h2.H2StoreParameters; |
17 |
import org.gvsig.data.datastores.vectorial.driver.jdbc.h2.Register; |
18 |
import org.gvsig.data.datastores.vectorial.driver.shp.SHPStore; |
19 |
import org.gvsig.data.datastores.vectorial.driver.shp.SHPStoreParameters; |
20 |
import org.gvsig.data.datastores.vectorial.driver.shp.fileshp.SHP; |
21 |
import org.gvsig.data.exception.CloseException; |
22 |
import org.gvsig.data.exception.InitializeException; |
23 |
import org.gvsig.data.exception.OpenException; |
24 |
import org.gvsig.data.exception.ReadException; |
25 |
import org.gvsig.data.exception.WriteException; |
26 |
import org.gvsig.data.vectorial.Feature; |
27 |
import org.gvsig.data.vectorial.IFeature; |
28 |
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor; |
29 |
import org.gvsig.data.vectorial.IFeatureCollection; |
30 |
import org.gvsig.data.vectorial.IFeatureStore; |
31 |
import org.gvsig.data.vectorial.IFeatureType; |
32 |
import org.gvsig.data.vectorial.visitor.PrintlnFeaturesVisitor; |
33 |
import org.gvsig.exceptions.BaseException; |
34 |
import org.h2.tools.Server; |
35 |
|
36 |
|
37 |
public class DataStoreTest extends TestCase { |
38 |
|
39 |
private Server h2Server = null; |
40 |
private String h2Server_argsTCP[]= {"-baseDir", "."}; |
41 |
|
42 |
|
43 |
|
44 |
private File shpfile = new File(DataStoreTest.class.getResource("data/prueba.shp").getFile()); |
45 |
|
46 |
public static void main(String[] args) { |
47 |
junit.textui.TestRunner.run(DataStoreTest.class); |
48 |
} |
49 |
|
50 |
protected void setUp() throws Exception { |
51 |
super.setUp();
|
52 |
this.startH2Server();
|
53 |
} |
54 |
|
55 |
public void startH2Server(){ |
56 |
if (h2Server != null) |
57 |
throw new RuntimeException("H2 server ya arrancado"); |
58 |
Server myh2Server = null;
|
59 |
try{
|
60 |
myh2Server = Server.createTcpServer(h2Server_argsTCP); |
61 |
myh2Server.start(); |
62 |
|
63 |
} catch (Exception e){ |
64 |
throw new RuntimeException(e); |
65 |
} |
66 |
h2Server = myh2Server; |
67 |
System.out.println("\n************************"); |
68 |
System.out.println("* H2 SERVER STARTED!!!! *"); |
69 |
System.out.println("************************\n"); |
70 |
|
71 |
} |
72 |
|
73 |
public void stopH2Server(){ |
74 |
if (h2Server == null) |
75 |
return;
|
76 |
h2Server.stop(); |
77 |
h2Server.shutdown(); |
78 |
h2Server = null;
|
79 |
System.out.println("\n************************"); |
80 |
System.out.println("* H2 SERVER STOPED!!!! *"); |
81 |
System.out.println("************************\n"); |
82 |
} |
83 |
|
84 |
|
85 |
private void deleteTable(H2StoreParameters h2Params) { |
86 |
try {
|
87 |
Class.forName("org.h2.Driver"); |
88 |
Connection con=DriverManager.getConnection(h2Params.getUrl(), h2Params.getUser(), h2Params.getPassw()); |
89 |
Statement st = con.createStatement();
|
90 |
st.execute("Delete "+ h2Params.tableID());
|
91 |
st.close(); |
92 |
con.close(); |
93 |
System.out.println("* Table " + h2Params.tableID() + " DELETED!!"); |
94 |
} catch (ClassNotFoundException e) { |
95 |
// TODO Auto-generated catch block
|
96 |
e.printStackTrace(); |
97 |
} catch (SQLException e) { |
98 |
// TODO Auto-generated catch block
|
99 |
e.printStackTrace(); |
100 |
} |
101 |
|
102 |
|
103 |
} |
104 |
|
105 |
public void shp2H2(File shpfile,H2StoreParameters h2Param) { |
106 |
System.out.println("======= SHP2H2 =============="); |
107 |
org.gvsig.data.datastores.vectorial.driver.shp.Register.selfRegister(); |
108 |
org.gvsig.data.datastores.vectorial.driver.dbf.Register.selfRegister(); |
109 |
|
110 |
DataSourceManager dsm=DataSourceManager.getManager(); |
111 |
// File shpfile= new File(DataStoreTest.class.getResource("data/prueba.dbf").getFile());
|
112 |
|
113 |
// IDriverParameters dp=dsm.createDriverParameters("shp");
|
114 |
|
115 |
|
116 |
IDataStoreParameters dsp=dsm.createDataStoreParameters(SHPStore.DATASTORE_NAME); |
117 |
((SHPStoreParameters)dsp).setSHPFile(shpfile); |
118 |
((SHPStoreParameters)dsp).setSHXFile(SHP.getShxFile(shpfile)); |
119 |
((SHPStoreParameters)dsp).setDBFFile(SHP.getDbfFile(shpfile)); |
120 |
IFeatureStore fs=null;
|
121 |
try {
|
122 |
fs = (IFeatureStore)dsm.createDataStore(dsp); |
123 |
} catch (InitializeException e) {
|
124 |
e.printStackTrace(); |
125 |
fail("Exception:" + e);
|
126 |
} |
127 |
|
128 |
try {
|
129 |
fs.open(); |
130 |
} catch (OpenException e2) {
|
131 |
e2.printStackTrace(); |
132 |
fail(); |
133 |
} |
134 |
|
135 |
IFeatureType ft= fs.getDefaultFeatureType(); |
136 |
IFeatureCollection featureCollection=null;
|
137 |
try {
|
138 |
featureCollection = (IFeatureCollection)fs.getDataCollection(); |
139 |
} catch (ReadException e2) {
|
140 |
// TODO Auto-generated catch block
|
141 |
e2.printStackTrace(); |
142 |
} |
143 |
|
144 |
SHP2H2FeaturesVisitor visitor=new SHP2H2FeaturesVisitor(ft,h2Param);
|
145 |
visitor.createStructure(); |
146 |
|
147 |
try {
|
148 |
featureCollection.accept(visitor); |
149 |
} catch (BaseException e1) {
|
150 |
e1.printStackTrace(); |
151 |
System.out.println(e1.getMessageStack());
|
152 |
fail(); |
153 |
} |
154 |
|
155 |
try {
|
156 |
visitor.close(); |
157 |
fs.close(); |
158 |
} catch (CloseException e) {
|
159 |
e.printStackTrace(); |
160 |
fail("Exception: "+e);
|
161 |
} |
162 |
try {
|
163 |
fs.dispose(); |
164 |
} catch (CloseException e) {
|
165 |
// TODO Auto-generated catch block
|
166 |
e.printStackTrace(); |
167 |
} |
168 |
|
169 |
} |
170 |
|
171 |
|
172 |
public void testH2_sqlMode() { |
173 |
System.out.println("======= H2 (sql Mode)=============="); |
174 |
Register.selfRegister(); |
175 |
|
176 |
DataSourceManager dsm=DataSourceManager.getManager(); |
177 |
|
178 |
|
179 |
H2StoreParameters dp=(H2StoreParameters)dsm.createDataStoreParameters(H2Store.DATASTORE_NAME); |
180 |
|
181 |
|
182 |
dp.setHost("localhost");
|
183 |
dp.setUser("SA");
|
184 |
dp.setPassw("");
|
185 |
dp.setFields(new String[] {"*"}); |
186 |
dp.setGeometryField("GEOMETRY");
|
187 |
String s=String.valueOf(System.currentTimeMillis()); |
188 |
String tableName="TablaPrueba"+s.substring(s.length()-8); |
189 |
dp.setTableName(tableName.toUpperCase()); |
190 |
dp.setFieldId(new String[] {"ID"}); |
191 |
|
192 |
try{
|
193 |
this.shp2H2(shpfile, dp);
|
194 |
} catch (Exception e) { |
195 |
if (e.getCause() instanceof BaseException){ |
196 |
System.out.println(((BaseException)e).getLocalizedMessageStack());
|
197 |
} else{
|
198 |
e.printStackTrace(); |
199 |
} |
200 |
fail(); |
201 |
} |
202 |
|
203 |
H2StoreParameters dp2=(H2StoreParameters)dsm.createDataStoreParameters(H2Store.DATASTORE_NAME); |
204 |
|
205 |
dp2.setHost("localhost");
|
206 |
dp2.setUser("SA");
|
207 |
dp2.setPassw("");
|
208 |
dp2.setGeometryField("GEOMETRY");
|
209 |
dp2.setFieldId(new String[] {"ID"}); |
210 |
dp2.setSqlSoure("Select * from " + dp.tableID() );
|
211 |
|
212 |
driverTest((IDataStoreParameters)dp,null,null,false); |
213 |
|
214 |
Exception exc = null; |
215 |
|
216 |
IFeatureStore fs=null;
|
217 |
try {
|
218 |
fs = (IFeatureStore)dsm.createDataStore(dp2); |
219 |
} catch (InitializeException e) {
|
220 |
e.printStackTrace(); |
221 |
fail("Exception:" + e);
|
222 |
} |
223 |
assertNotNull("Can't create Feature Store", fs);
|
224 |
|
225 |
try {
|
226 |
fs.open(); |
227 |
} catch (OpenException e2) {
|
228 |
e2.printStackTrace(); |
229 |
fail(); |
230 |
} |
231 |
|
232 |
|
233 |
Iterator it;
|
234 |
IFeatureCollection fc =null;
|
235 |
|
236 |
try {
|
237 |
fc = (IFeatureCollection)fs.getDataCollection(); |
238 |
} catch (ReadException e1) {
|
239 |
e1.printStackTrace(); |
240 |
fail(); |
241 |
} |
242 |
|
243 |
assertEquals(9, fc.size());
|
244 |
|
245 |
assertFalse("Edition allowed in sqlSource mode", fs.isEditable());
|
246 |
|
247 |
try {
|
248 |
fs.startEditing(); |
249 |
} catch (ReadException e1) {
|
250 |
exc=e1; |
251 |
} |
252 |
assertNotNull("Edition allowed in sqlSource mode",exc);
|
253 |
|
254 |
exc=null;
|
255 |
try {
|
256 |
fc = (IFeatureCollection)fs.getDataCollection(null,"NOMBRE like 'B%'",null); |
257 |
} catch (ReadException e1) {
|
258 |
exc=e1; |
259 |
} |
260 |
assertNotNull("Filter allowed in sqlSource mode",exc);
|
261 |
|
262 |
exc=null;
|
263 |
try {
|
264 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"NOMBRE"); |
265 |
} catch (ReadException e1) {
|
266 |
exc=e1; |
267 |
} |
268 |
assertNotNull("Order allowed in sqlSource mode",exc);
|
269 |
|
270 |
|
271 |
try {
|
272 |
fs.close(); |
273 |
} catch (CloseException e) {
|
274 |
e.printStackTrace(); |
275 |
fail("Exception:" + e);
|
276 |
} |
277 |
|
278 |
|
279 |
deleteTable(dp); |
280 |
|
281 |
System.out.println("======= /H2 (sql Mode) =============="); |
282 |
} |
283 |
|
284 |
public void testH2() { |
285 |
System.out.println("======= H2 =============="); |
286 |
Register.selfRegister(); |
287 |
|
288 |
DataSourceManager dsm=DataSourceManager.getManager(); |
289 |
|
290 |
|
291 |
H2StoreParameters dp=(H2StoreParameters)dsm.createDataStoreParameters(H2Store.DATASTORE_NAME); |
292 |
|
293 |
|
294 |
dp.setHost("localhost");
|
295 |
dp.setUser("SA");
|
296 |
dp.setPassw("");
|
297 |
dp.setFields(new String[] {"*"}); |
298 |
dp.setGeometryField("GEOMETRY");
|
299 |
String s=String.valueOf(System.currentTimeMillis()); |
300 |
String tableName="TablaPrueba"+s.substring(s.length()-8); |
301 |
dp.setTableName(tableName.toUpperCase()); |
302 |
dp.setFieldId(new String[] {"ID"}); |
303 |
|
304 |
try{
|
305 |
this.shp2H2(shpfile, dp);
|
306 |
} catch (Exception e) { |
307 |
if (e.getCause() instanceof BaseException){ |
308 |
System.out.println(((BaseException)e).getLocalizedMessageStack());
|
309 |
} else{
|
310 |
e.printStackTrace(); |
311 |
} |
312 |
fail(); |
313 |
} |
314 |
|
315 |
|
316 |
|
317 |
|
318 |
driverTest((IDataStoreParameters)dp,null,null,true); |
319 |
|
320 |
IFeatureStore fs=null;
|
321 |
try {
|
322 |
fs = (IFeatureStore)dsm.createDataStore(dp); |
323 |
} catch (InitializeException e) {
|
324 |
e.printStackTrace(); |
325 |
fail("Exception:" + e);
|
326 |
} |
327 |
assertNotNull("Can't create Feature Store", fs);
|
328 |
|
329 |
try {
|
330 |
fs.open(); |
331 |
} catch (OpenException e2) {
|
332 |
e2.printStackTrace(); |
333 |
fail(); |
334 |
} |
335 |
|
336 |
Iterator it;
|
337 |
IFeatureCollection fc =null;
|
338 |
Comparable v1,v2;
|
339 |
IFeature feature,pfeature; |
340 |
long count;
|
341 |
|
342 |
|
343 |
try {
|
344 |
fc = (IFeatureCollection)fs.getDataCollection(); |
345 |
} catch (ReadException e1) {
|
346 |
e1.printStackTrace(); |
347 |
fail(); |
348 |
} |
349 |
|
350 |
assertEquals(9, fc.size());
|
351 |
|
352 |
try {
|
353 |
fc = (IFeatureCollection)fs.getDataCollection(null,"lower(NOMBRE) like 'b%'",null); |
354 |
} catch (ReadException e1) {
|
355 |
e1.printStackTrace(); |
356 |
fail(); |
357 |
} |
358 |
|
359 |
assertEquals(2, fc.size());
|
360 |
|
361 |
it = fc.iterator(); |
362 |
count=0;
|
363 |
while (it.hasNext()){
|
364 |
feature = (IFeature)it.next(); |
365 |
assertTrue("Filter error",feature.getString("NOMBRE").toLowerCase().startsWith("b")); |
366 |
count++; |
367 |
} |
368 |
assertEquals("Iteration error",2,count); |
369 |
|
370 |
|
371 |
try {
|
372 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"NOMBRE ASC"); |
373 |
} catch (ReadException e1) {
|
374 |
e1.printStackTrace(); |
375 |
fail(); |
376 |
} |
377 |
assertEquals(9, fc.size());
|
378 |
it = fc.iterator(); |
379 |
count=0;
|
380 |
pfeature = (IFeature)it.next(); |
381 |
count++; |
382 |
while (it.hasNext()){
|
383 |
feature = (IFeature)it.next(); |
384 |
v1 = (Comparable)pfeature.get("NOMBRE"); |
385 |
v2 = (Comparable)feature.get("NOMBRE"); |
386 |
pfeature=feature; |
387 |
assertTrue("Short error", (v1.compareTo(v1) <= 0)); |
388 |
count++; |
389 |
} |
390 |
assertEquals("Iteration error",9,count); |
391 |
|
392 |
|
393 |
try {
|
394 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"NOMBRE DESC"); |
395 |
} catch (ReadException e1) {
|
396 |
e1.printStackTrace(); |
397 |
fail(); |
398 |
} |
399 |
assertEquals(9, fc.size());
|
400 |
it = fc.iterator(); |
401 |
|
402 |
count=0;
|
403 |
pfeature = (IFeature)it.next(); |
404 |
count++; |
405 |
while (it.hasNext()){
|
406 |
feature = (IFeature)it.next(); |
407 |
v1 = (Comparable)pfeature.get("NOMBRE"); |
408 |
v2 = (Comparable)feature.get("NOMBRE"); |
409 |
pfeature=feature; |
410 |
assertTrue("Short error", (v1.compareTo(v1) >= 0)); |
411 |
count++; |
412 |
} |
413 |
assertEquals("Iteration error",9,count); |
414 |
|
415 |
|
416 |
try {
|
417 |
fc = (IFeatureCollection)fs.getDataCollection(null,"lower(NOMBRE) like 'b%'","NOMBRE"); |
418 |
} catch (ReadException e1) {
|
419 |
e1.printStackTrace(); |
420 |
fail(); |
421 |
} |
422 |
|
423 |
assertEquals(2, fc.size());
|
424 |
|
425 |
it = fc.iterator(); |
426 |
|
427 |
count=0;
|
428 |
pfeature = (IFeature)it.next(); |
429 |
assertTrue(pfeature.getString("NOMBRE").toLowerCase().startsWith("b")); |
430 |
count++; |
431 |
while (it.hasNext()){
|
432 |
feature = (IFeature)it.next(); |
433 |
assertTrue("Filter error",feature.getString("NOMBRE").toLowerCase().startsWith("b")); |
434 |
v1 = (Comparable)pfeature.get("NOMBRE"); |
435 |
v2 = (Comparable)feature.get("NOMBRE"); |
436 |
pfeature=feature; |
437 |
assertTrue("Short error", (v1.compareTo(v1) <= 0)); |
438 |
count++; |
439 |
} |
440 |
assertEquals("Iteration error",2,count); |
441 |
|
442 |
|
443 |
|
444 |
try {
|
445 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"Tipo,lower(NOMBRE) Desc"); |
446 |
} catch (ReadException e1) {
|
447 |
e1.printStackTrace(); |
448 |
fail(); |
449 |
} |
450 |
assertEquals(9, fc.size());
|
451 |
it = fc.iterator(); |
452 |
count=0;
|
453 |
pfeature = (IFeature)it.next(); |
454 |
System.out.println(pfeature.getString("NOMBRE")); |
455 |
count++; |
456 |
while (it.hasNext()){
|
457 |
feature = (IFeature)it.next(); |
458 |
v1 = (Comparable)((String)pfeature.get("NOMBRE")).toLowerCase(); |
459 |
v2 = (Comparable)((String)feature.get("NOMBRE")).toLowerCase(); |
460 |
pfeature=feature; |
461 |
assertTrue("Short error", (v1.compareTo(v2) >= 0)); |
462 |
System.out.println(pfeature.getString("NOMBRE")); |
463 |
count++; |
464 |
} |
465 |
assertEquals("Iteration error",9,count); |
466 |
|
467 |
|
468 |
|
469 |
/// CON EDICION
|
470 |
try {
|
471 |
fs.startEditing(); |
472 |
} catch (ReadException e1) {
|
473 |
e1.printStackTrace(); |
474 |
fail(); |
475 |
} |
476 |
|
477 |
|
478 |
IFeature newFeature = fs.createFeature(fs.getDefaultFeatureType()); |
479 |
newFeature.set("NOMBRE","BuRjaSOT"); |
480 |
newFeature.set("TIPO","MUNICIPIO"); |
481 |
fs.insert(newFeature); |
482 |
|
483 |
|
484 |
try {
|
485 |
fc = (IFeatureCollection)fs.getDataCollection(); |
486 |
} catch (ReadException e1) {
|
487 |
e1.printStackTrace(); |
488 |
fail(); |
489 |
} |
490 |
|
491 |
assertEquals(10, fc.size());
|
492 |
|
493 |
try {
|
494 |
fc = (IFeatureCollection)fs.getDataCollection(null,"lower(NOMBRE) like 'b%'",null); |
495 |
} catch (ReadException e1) {
|
496 |
e1.printStackTrace(); |
497 |
fail(); |
498 |
} |
499 |
|
500 |
assertEquals(3, fc.size());
|
501 |
|
502 |
it = fc.iterator(); |
503 |
count=0;
|
504 |
while (it.hasNext()){
|
505 |
feature = (IFeature)it.next(); |
506 |
assertTrue("Filter error",feature.getString("NOMBRE").toLowerCase().startsWith("b")); |
507 |
count++; |
508 |
} |
509 |
assertEquals("Iteration error",3,count); |
510 |
|
511 |
|
512 |
try {
|
513 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"NOMBRE ASC"); |
514 |
} catch (ReadException e1) {
|
515 |
e1.printStackTrace(); |
516 |
fail(); |
517 |
} |
518 |
assertEquals(10, fc.size());
|
519 |
it = fc.iterator(); |
520 |
count=0;
|
521 |
pfeature = (IFeature)it.next(); |
522 |
count++; |
523 |
while (it.hasNext()){
|
524 |
feature = (IFeature)it.next(); |
525 |
v1 = (Comparable)pfeature.get("NOMBRE"); |
526 |
v2 = (Comparable)feature.get("NOMBRE"); |
527 |
pfeature=feature; |
528 |
assertTrue("Short error", (v1.compareTo(v1) <= 0)); |
529 |
count++; |
530 |
} |
531 |
assertEquals("Iteration error",10,count); |
532 |
|
533 |
|
534 |
try {
|
535 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"NOMBRE DESC"); |
536 |
} catch (ReadException e1) {
|
537 |
e1.printStackTrace(); |
538 |
fail(); |
539 |
} |
540 |
assertEquals(10, fc.size());
|
541 |
it = fc.iterator(); |
542 |
|
543 |
count=0;
|
544 |
pfeature = (IFeature)it.next(); |
545 |
count++; |
546 |
while (it.hasNext()){
|
547 |
feature = (IFeature)it.next(); |
548 |
v1 = (Comparable)pfeature.get("NOMBRE"); |
549 |
v2 = (Comparable)feature.get("NOMBRE"); |
550 |
pfeature=feature; |
551 |
assertTrue("Short error", (v1.compareTo(v1) >= 0)); |
552 |
count++; |
553 |
} |
554 |
assertEquals("Iteration error",10,count); |
555 |
|
556 |
|
557 |
try {
|
558 |
fc = (IFeatureCollection)fs.getDataCollection(null,"lower(NOMBRE) like 'b%'","NOMBRE"); |
559 |
} catch (ReadException e1) {
|
560 |
e1.printStackTrace(); |
561 |
fail(); |
562 |
} |
563 |
|
564 |
assertEquals(3, fc.size());
|
565 |
|
566 |
it = fc.iterator(); |
567 |
|
568 |
count=0;
|
569 |
pfeature = (IFeature)it.next(); |
570 |
assertTrue(pfeature.getString("NOMBRE").toLowerCase().startsWith("b")); |
571 |
count++; |
572 |
while (it.hasNext()){
|
573 |
feature = (IFeature)it.next(); |
574 |
assertTrue("Filter error",feature.getString("NOMBRE").toLowerCase().startsWith("b")); |
575 |
v1 = (Comparable)pfeature.get("NOMBRE"); |
576 |
v2 = (Comparable)feature.get("NOMBRE"); |
577 |
pfeature=feature; |
578 |
assertTrue("Short error", (v1.compareTo(v1) <= 0)); |
579 |
count++; |
580 |
} |
581 |
assertEquals("Iteration error",3,count); |
582 |
|
583 |
|
584 |
|
585 |
try {
|
586 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"Tipo,lower(NOMBRE) Desc"); |
587 |
} catch (ReadException e1) {
|
588 |
e1.printStackTrace(); |
589 |
fail(); |
590 |
} |
591 |
assertEquals(10, fc.size());
|
592 |
it = fc.iterator(); |
593 |
count=0;
|
594 |
pfeature = (IFeature)it.next(); |
595 |
System.out.println(pfeature.getString("NOMBRE")); |
596 |
count++; |
597 |
while (it.hasNext()){
|
598 |
feature = (IFeature)it.next(); |
599 |
v1 = (Comparable)((String)pfeature.get("NOMBRE")).toLowerCase(); |
600 |
v2 = (Comparable)((String)feature.get("NOMBRE")).toLowerCase(); |
601 |
pfeature=feature; |
602 |
assertTrue("Short error", (v1.compareTo(v2) >= 0)); |
603 |
System.out.println(pfeature.getString("NOMBRE")); |
604 |
count++; |
605 |
} |
606 |
assertEquals("Iteration error",10,count); |
607 |
|
608 |
|
609 |
fs.cancelEditing(); |
610 |
|
611 |
|
612 |
//Insertar un elemento
|
613 |
try{
|
614 |
fs.startEditing(); |
615 |
} catch (ReadException e1) {
|
616 |
e1.printStackTrace(); |
617 |
fail(); |
618 |
} |
619 |
|
620 |
feature = fs.createDefaultFeature(true);
|
621 |
feature.set("ID", 90000); |
622 |
feature.set("NOMBRE","BurJASOT"); |
623 |
feature.set("TIPO", "OTRO"); |
624 |
fs.insert(feature); |
625 |
|
626 |
try {
|
627 |
fs.finishEditing(); |
628 |
} catch (WriteException e) {
|
629 |
e.printStackTrace(); |
630 |
fail("Exception: "+e);
|
631 |
} catch (ReadException e) {
|
632 |
e.printStackTrace(); |
633 |
fail("Exception: "+e);
|
634 |
} |
635 |
|
636 |
|
637 |
try {
|
638 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"lower(NOMBRE) Desc"); |
639 |
} catch (ReadException e1) {
|
640 |
e1.printStackTrace(); |
641 |
fail(); |
642 |
} |
643 |
assertEquals(10, fc.size());
|
644 |
it = fc.iterator(); |
645 |
count=0;
|
646 |
pfeature = (IFeature)it.next(); |
647 |
System.out.println(pfeature.getString("NOMBRE")); |
648 |
count++; |
649 |
while (it.hasNext()){
|
650 |
feature = (IFeature)it.next(); |
651 |
v1 = (Comparable)((String)pfeature.get("NOMBRE")).toLowerCase(); |
652 |
v2 = (Comparable)((String)feature.get("NOMBRE")).toLowerCase(); |
653 |
pfeature=feature; |
654 |
assertTrue("Short error: "+ v1.toString() + " >= " +v2.toString(), (v1.compareTo(v2) >= 0)); |
655 |
System.out.println(pfeature.getString("NOMBRE")); |
656 |
count++; |
657 |
} |
658 |
assertEquals("Iteration error",10,count); |
659 |
|
660 |
|
661 |
|
662 |
//Actualizacion
|
663 |
try{
|
664 |
fs.startEditing(); |
665 |
} catch (ReadException e1) {
|
666 |
e1.printStackTrace(); |
667 |
fail(); |
668 |
} |
669 |
|
670 |
|
671 |
try {
|
672 |
fc = (IFeatureCollection)fs.getDataCollection(null,"lower(NOMBRE) = 'burjasot'",null); |
673 |
} catch (ReadException e1) {
|
674 |
e1.printStackTrace(); |
675 |
fail(); |
676 |
} |
677 |
assertEquals(1, fc.size());
|
678 |
Feature old =(Feature)fc.iterator().next(); |
679 |
feature = fs.createDefaultFeature(false);
|
680 |
it = fs.getDefaultFeatureType().iterator(); |
681 |
IFeatureAttributeDescriptor attr; |
682 |
while(it.hasNext()){
|
683 |
attr = (IFeatureAttributeDescriptor)it.next(); |
684 |
// if (attr.getDataType() == IFeatureAttributeDescriptor.TYPE_GEOMETRY){
|
685 |
// feature.set(attr.getName(), old.get(attr.getName()));
|
686 |
// } else {
|
687 |
feature.set(attr.getName(), old.get(attr.getName())); |
688 |
// }
|
689 |
} |
690 |
feature.set("NOMBRE", feature.getString("NOMBRE")+"__KK__"); |
691 |
|
692 |
fs.update(feature, old); |
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 |
|
711 |
|
712 |
//Eliminacion
|
713 |
try{
|
714 |
fs.startEditing(); |
715 |
} catch (ReadException e1) {
|
716 |
e1.printStackTrace(); |
717 |
fail(); |
718 |
} |
719 |
|
720 |
|
721 |
try {
|
722 |
fc = (IFeatureCollection)fs.getDataCollection(null,"NOMBRE like '%__KK__'",null); |
723 |
} catch (ReadException e1) {
|
724 |
e1.printStackTrace(); |
725 |
fail(); |
726 |
} |
727 |
assertEquals(1, fc.size());
|
728 |
fs.delete((IFeature)fc.iterator().next()); |
729 |
try {
|
730 |
fs.finishEditing(); |
731 |
} catch (WriteException e) {
|
732 |
e.printStackTrace(); |
733 |
fail("Exception: "+e);
|
734 |
} catch (ReadException e) {
|
735 |
e.printStackTrace(); |
736 |
fail("Exception: "+e);
|
737 |
} |
738 |
|
739 |
try {
|
740 |
fc = (IFeatureCollection)fs.getDataCollection(); |
741 |
} catch (ReadException e1) {
|
742 |
e1.printStackTrace(); |
743 |
fail(); |
744 |
} |
745 |
assertEquals(9, fc.size());
|
746 |
|
747 |
|
748 |
|
749 |
|
750 |
try {
|
751 |
fs.close(); |
752 |
} catch (CloseException e) {
|
753 |
e.printStackTrace(); |
754 |
fail("Exception:" + e);
|
755 |
} |
756 |
|
757 |
|
758 |
deleteTable(dp); |
759 |
|
760 |
System.out.println("======= /H2 =============="); |
761 |
|
762 |
} |
763 |
|
764 |
|
765 |
|
766 |
// private IFeatureStore createFeatureStore(IDriverParameters dp){
|
767 |
// DataSourceManager dsm=DataSourceManager.getManager();
|
768 |
//
|
769 |
// IDataStoreParameters dsp=dsm.createDataStoreParameters(DriverStore.DATASTORE_NAME);
|
770 |
//
|
771 |
// ((IDriverStoreParameters)dsp).setDriverParameters(dp);
|
772 |
// IFeatureStore fs=null;
|
773 |
// try {
|
774 |
// fs = (IFeatureStore)dsm.createDataStore(dsp);
|
775 |
// } catch (InitializeException e) {
|
776 |
// e.printStackTrace();
|
777 |
// fail("Exception:" + e);
|
778 |
// }
|
779 |
// return fs;
|
780 |
//
|
781 |
// }
|
782 |
|
783 |
|
784 |
private void driverTest(IDataStoreParameters dp,String filter, String order,boolean testEdit){ |
785 |
DataSourceManager dsm=DataSourceManager.getManager(); |
786 |
|
787 |
|
788 |
IFeatureStore fs=null;
|
789 |
try {
|
790 |
fs = (IFeatureStore)dsm.createDataStore(dp); |
791 |
} catch (InitializeException e) {
|
792 |
e.printStackTrace(); |
793 |
fail("Exception:" + e);
|
794 |
} |
795 |
|
796 |
try {
|
797 |
fs.open(); |
798 |
} catch (OpenException e2) {
|
799 |
e2.printStackTrace(); |
800 |
fail(); |
801 |
} |
802 |
|
803 |
if (fs.isEditable() && testEdit) {
|
804 |
|
805 |
try {
|
806 |
fs.startEditing(); |
807 |
} catch (ReadException e) {
|
808 |
e.printStackTrace(); |
809 |
fail(); |
810 |
} |
811 |
|
812 |
IFeature feature1 = fs.createDefaultFeature(false);
|
813 |
feature1.set("ID",1); |
814 |
IFeature feature2 = fs.createDefaultFeature(false);
|
815 |
feature2.set("ID",2); |
816 |
IFeature feature3 = fs.createDefaultFeature(false);
|
817 |
feature3.set("ID",3); |
818 |
|
819 |
fs.insert(feature1); |
820 |
fs.insert(feature2); |
821 |
|
822 |
fs.update(feature3,feature1); |
823 |
fs.delete(feature3); |
824 |
fs.delete(feature2); |
825 |
} |
826 |
|
827 |
//Mostrar por consola todos los registros.
|
828 |
IFeatureType ft= fs.getDefaultFeatureType(); |
829 |
IFeatureCollection featureCollection=null;
|
830 |
// featureCollection = (IFeatureCollection)fs.getDataCollection();
|
831 |
// featureCollection = (IFeatureCollection)fs.getDataCollection(ft,"NOMBRE = 'CALPE'",null);
|
832 |
// featureCollection = (IFeatureCollection)fs.getDataCollection(ft,"AREA > 3.2213163729E7 and AREA < 3.2213163749E7",null);
|
833 |
try {
|
834 |
featureCollection = (IFeatureCollection)fs.getDataCollection(ft,filter,order); |
835 |
} catch (ReadException e2) {
|
836 |
// TODO Auto-generated catch block
|
837 |
e2.printStackTrace(); |
838 |
} |
839 |
|
840 |
PrintlnFeaturesVisitor visitor=new PrintlnFeaturesVisitor(ft);
|
841 |
try {
|
842 |
featureCollection.accept(visitor); |
843 |
} catch (BaseException e1) {
|
844 |
e1.printStackTrace(); |
845 |
fail("Exception: "+e1);
|
846 |
} |
847 |
|
848 |
if (fs.isEditable() && testEdit){
|
849 |
try {
|
850 |
fs.finishEditing(); |
851 |
// fs.cancelEditing();
|
852 |
} catch (WriteException e) {
|
853 |
e.printStackTrace(); |
854 |
fail("Exception: "+e);
|
855 |
} catch (ReadException e) {
|
856 |
e.printStackTrace(); |
857 |
fail("Exception: "+e);
|
858 |
} |
859 |
} |
860 |
try {
|
861 |
fs.close(); |
862 |
} catch (CloseException e) {
|
863 |
e.printStackTrace(); |
864 |
fail("Exception: "+e);
|
865 |
} |
866 |
try {
|
867 |
fs.dispose(); |
868 |
} catch (CloseException e) {
|
869 |
// TODO Auto-generated catch block
|
870 |
e.printStackTrace(); |
871 |
} |
872 |
} |
873 |
|
874 |
protected void tearDown() throws Exception { |
875 |
// TODO Auto-generated method stub
|
876 |
super.tearDown();
|
877 |
this.stopH2Server();
|
878 |
} |
879 |
} |