svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataDB / src-test / org / gvsig / data / datastores / vectorial / db / jdbc / h2 / H2Test.java @ 20973
History | View | Annotate | Download (27 KB)
1 |
package org.gvsig.data.datastores.vectorial.db.jdbc.h2; |
---|---|
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 org.gvsig.data.CloseException; |
11 |
import org.gvsig.data.DataException; |
12 |
import org.gvsig.data.DataManager; |
13 |
import org.gvsig.data.IDataStoreParameters; |
14 |
import org.gvsig.data.InitializeException; |
15 |
import org.gvsig.data.OpenException; |
16 |
import org.gvsig.data.ReadException; |
17 |
import org.gvsig.data.WriteException; |
18 |
import org.gvsig.data.datastores.vectorial.db.DBAttributeDescriptor; |
19 |
import org.gvsig.data.datastores.vectorial.db.DBFeatureType; |
20 |
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCTest; |
21 |
import org.gvsig.data.datastores.vectorial.file.shp.SHPStore; |
22 |
import org.gvsig.data.datastores.vectorial.file.shp.SHPStoreParameters; |
23 |
import org.gvsig.data.vectorial.Feature; |
24 |
import org.gvsig.data.vectorial.IFeature; |
25 |
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor; |
26 |
import org.gvsig.data.vectorial.IFeatureCollection; |
27 |
import org.gvsig.data.vectorial.IFeatureStore; |
28 |
import org.gvsig.data.vectorial.IFeatureStoreExplorer; |
29 |
import org.gvsig.data.vectorial.IFeatureType; |
30 |
import org.gvsig.data.vectorial.IsNotAttributeSettingException; |
31 |
import org.gvsig.exceptions.BaseException; |
32 |
import org.h2.tools.Server; |
33 |
|
34 |
|
35 |
public class H2Test extends JDBCTest { |
36 |
|
37 |
private Server h2Server = null; |
38 |
private Server h2WebServer = null; |
39 |
private String h2Server_argsTCP[]= {"-baseDir", "."}; |
40 |
private String h2Server_argsWeb[]= {"-webport", "8089"}; |
41 |
// private boolean runH2WebServer = false;
|
42 |
private boolean runH2WebServer = false; |
43 |
private boolean startsH2Server=true; |
44 |
|
45 |
|
46 |
private File shpfile = new File(H2Test.class.getResource("data/prueba.shp").getFile()); |
47 |
|
48 |
public static void main(String[] args) { |
49 |
junit.textui.TestRunner.run(H2Test.class); |
50 |
} |
51 |
|
52 |
protected void setUp() throws Exception { |
53 |
super.setUp();
|
54 |
if (startsH2Server)
|
55 |
this.startH2Server();
|
56 |
} |
57 |
|
58 |
public void startH2Server(){ |
59 |
if (h2Server != null) |
60 |
throw new RuntimeException("H2 server ya arrancado"); |
61 |
Server myh2Server = null;
|
62 |
Server myh2WebServer = null;
|
63 |
try{
|
64 |
myh2Server = Server.createTcpServer(h2Server_argsTCP); |
65 |
myh2Server.start(); |
66 |
if (runH2WebServer){
|
67 |
myh2WebServer = Server.createWebServer(h2Server_argsWeb); |
68 |
myh2WebServer.start(); |
69 |
} |
70 |
|
71 |
} catch (Exception e){ |
72 |
throw new RuntimeException(e); |
73 |
} |
74 |
h2Server = myh2Server; |
75 |
System.out.println("\n************************"); |
76 |
if (runH2WebServer){
|
77 |
System.out.println("* H2 SERVER STARTED web: "+ h2Server_argsWeb[1] + "!!!! *"); |
78 |
}else{
|
79 |
System.out.println("* H2 SERVER STARTED!!!! *"); |
80 |
} |
81 |
System.out.println("************************\n"); |
82 |
|
83 |
} |
84 |
|
85 |
public void stopH2Server(){ |
86 |
if (h2Server == null) |
87 |
return;
|
88 |
h2Server.stop(); |
89 |
h2Server.shutdown(); |
90 |
h2Server = null;
|
91 |
if (h2WebServer != null){ |
92 |
h2WebServer.stop(); |
93 |
h2WebServer.shutdown(); |
94 |
h2WebServer = null;
|
95 |
} |
96 |
System.out.println("\n************************"); |
97 |
System.out.println("* H2 SERVER STOPED!!!! *"); |
98 |
System.out.println("************************\n"); |
99 |
} |
100 |
|
101 |
|
102 |
private void deleteTable(H2StoreParameters h2Params) { |
103 |
try {
|
104 |
Class.forName("org.h2.Driver"); |
105 |
Connection con=DriverManager.getConnection(h2Params.getUrl(), h2Params.getUser(), h2Params.getPassw()); |
106 |
Statement st = con.createStatement();
|
107 |
st.execute("Delete "+ h2Params.tableID());
|
108 |
st.close(); |
109 |
con.close(); |
110 |
System.out.println("* Table " + h2Params.tableID() + " DELETED!!"); |
111 |
} catch (ClassNotFoundException e) { |
112 |
// TODO Auto-generated catch block
|
113 |
e.printStackTrace(); |
114 |
} catch (SQLException e) { |
115 |
// TODO Auto-generated catch block
|
116 |
e.printStackTrace(); |
117 |
} |
118 |
|
119 |
|
120 |
} |
121 |
|
122 |
public void shp2H2(File shpfile,H2StoreParameters h2Param) { |
123 |
System.out.println("======= SHP2H2 =============="); |
124 |
org.gvsig.data.datastores.vectorial.file.shp.Register.selfRegister(); |
125 |
org.gvsig.data.datastores.vectorial.file.dbf.Register.selfRegister(); |
126 |
|
127 |
DataManager dsm=DataManager.getManager(); |
128 |
// File shpfile= new File(DataStoreTest.class.getResource("data/prueba.dbf").getFile());
|
129 |
|
130 |
// IDriverParameters dp=dsm.createDriverParameters("shp");
|
131 |
|
132 |
|
133 |
IDataStoreParameters dsp=dsm.createDataStoreParameters(SHPStore.DATASTORE_NAME); |
134 |
((SHPStoreParameters)dsp).setFile(shpfile); |
135 |
// ((SHPStoreParameters)dsp).setSHXFile(SHP.getShxFile(shpfile));
|
136 |
// ((SHPStoreParameters)dsp).setDBFFile(SHP.getDbfFile(shpfile));
|
137 |
IFeatureStore fs=null;
|
138 |
try {
|
139 |
fs = (IFeatureStore)dsm.createDataStore(dsp); |
140 |
} catch (InitializeException e) {
|
141 |
e.printStackTrace(); |
142 |
fail("Exception:" + e);
|
143 |
} |
144 |
|
145 |
try {
|
146 |
fs.open(); |
147 |
} catch (OpenException e2) {
|
148 |
e2.printStackTrace(); |
149 |
fail(); |
150 |
} |
151 |
|
152 |
IFeatureType ft= fs.getDefaultFeatureType(); |
153 |
IFeatureCollection featureCollection=null;
|
154 |
try {
|
155 |
featureCollection = (IFeatureCollection)fs.getDataCollection(); |
156 |
} catch (ReadException e2) {
|
157 |
// TODO Auto-generated catch block
|
158 |
e2.printStackTrace(); |
159 |
} |
160 |
|
161 |
SHP2H2FeaturesVisitor visitor=new SHP2H2FeaturesVisitor(ft,h2Param);
|
162 |
visitor.createStructure(); |
163 |
|
164 |
try {
|
165 |
featureCollection.accept(visitor); |
166 |
} catch (BaseException e1) {
|
167 |
e1.printStackTrace(); |
168 |
System.out.println(e1.getMessageStack());
|
169 |
fail(); |
170 |
} |
171 |
|
172 |
try {
|
173 |
visitor.close(); |
174 |
fs.close(); |
175 |
} catch (CloseException e) {
|
176 |
e.printStackTrace(); |
177 |
fail("Exception: "+e);
|
178 |
} |
179 |
try {
|
180 |
fs.dispose(); |
181 |
} catch (CloseException e) {
|
182 |
// TODO Auto-generated catch block
|
183 |
e.printStackTrace(); |
184 |
} |
185 |
|
186 |
} |
187 |
|
188 |
|
189 |
public void testH2_sqlMode() { |
190 |
System.out.println("======= H2 (sql Mode)=============="); |
191 |
Register.selfRegister(); |
192 |
|
193 |
DataManager dsm=DataManager.getManager(); |
194 |
|
195 |
|
196 |
H2StoreParameters dp=this.cretateTablePrueba();
|
197 |
|
198 |
H2StoreParameters dp2=(H2StoreParameters)dsm.createDataStoreParameters(H2Store.DATASTORE_NAME); |
199 |
|
200 |
dp2.setHost("localhost");
|
201 |
dp2.setUser("SA");
|
202 |
dp2.setPassw("");
|
203 |
dp2.setGeometryField("GEOMETRY");
|
204 |
dp2.setFieldId(new String[] {"ID"}); |
205 |
dp2.setSqlSoure("Select * from " + dp.tableID() );
|
206 |
|
207 |
storeTest((IDataStoreParameters)dp,null,null,false); |
208 |
|
209 |
Exception exc = null; |
210 |
|
211 |
IFeatureStore fs=null;
|
212 |
try {
|
213 |
fs = (IFeatureStore)dsm.createDataStore(dp2); |
214 |
} catch (InitializeException e) {
|
215 |
e.printStackTrace(); |
216 |
fail("Exception:" + e);
|
217 |
} |
218 |
assertNotNull("Can't create Feature Store", fs);
|
219 |
|
220 |
try {
|
221 |
fs.open(); |
222 |
} catch (OpenException e2) {
|
223 |
e2.printStackTrace(); |
224 |
fail(); |
225 |
} |
226 |
|
227 |
|
228 |
IFeatureCollection fc =null;
|
229 |
|
230 |
try {
|
231 |
fc = (IFeatureCollection)fs.getDataCollection(); |
232 |
} catch (ReadException e1) {
|
233 |
e1.printStackTrace(); |
234 |
fail(); |
235 |
} |
236 |
|
237 |
assertEquals(9, fc.size());
|
238 |
|
239 |
fc.dispose(); |
240 |
|
241 |
assertFalse("Edition allowed in sqlSource mode", fs.isEditable());
|
242 |
|
243 |
try {
|
244 |
fs.startEditing(); |
245 |
} catch (ReadException e1) {
|
246 |
exc=e1; |
247 |
} |
248 |
assertNotNull("Edition allowed in sqlSource mode",exc);
|
249 |
|
250 |
exc=null;
|
251 |
try {
|
252 |
fc = (IFeatureCollection)fs.getDataCollection(null,"NOMBRE like 'B%'",null); |
253 |
} catch (ReadException e1) {
|
254 |
exc=e1; |
255 |
} |
256 |
assertNotNull("Filter allowed in sqlSource mode",exc);
|
257 |
|
258 |
exc=null;
|
259 |
try {
|
260 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"NOMBRE"); |
261 |
} catch (ReadException e1) {
|
262 |
exc=e1; |
263 |
} |
264 |
assertNotNull("Order allowed in sqlSource mode",exc);
|
265 |
|
266 |
|
267 |
try {
|
268 |
fs.close(); |
269 |
} catch (CloseException e) {
|
270 |
e.printStackTrace(); |
271 |
fail("Exception:" + e);
|
272 |
} |
273 |
|
274 |
try {
|
275 |
fs.dispose(); |
276 |
} catch (CloseException e) {
|
277 |
e.printStackTrace();fail(); |
278 |
} |
279 |
|
280 |
|
281 |
deleteTable(dp); |
282 |
|
283 |
System.out.println("======= /H2 (sql Mode) =============="); |
284 |
} |
285 |
|
286 |
private String nuevoNombreDeTabla(String prefijo){ |
287 |
String s=String.valueOf(System.currentTimeMillis()); |
288 |
return prefijo+s.substring(s.length()-8); |
289 |
} |
290 |
|
291 |
private H2StoreParameters cretateTablePrueba(){
|
292 |
Register.selfRegister(); |
293 |
DataManager dsm=DataManager.getManager(); |
294 |
|
295 |
|
296 |
H2StoreParameters dp=(H2StoreParameters)dsm.createDataStoreParameters(H2Store.DATASTORE_NAME); |
297 |
|
298 |
|
299 |
dp.setHost("localhost");
|
300 |
dp.setUser("SA");
|
301 |
dp.setPassw("");
|
302 |
dp.setFields(new String[] {"*"}); |
303 |
dp.setGeometryField("GEOMETRY");
|
304 |
dp.setTableName(nuevoNombreDeTabla("testh2").toUpperCase());
|
305 |
dp.setFieldId(new String[] {"ID"}); |
306 |
|
307 |
try{
|
308 |
this.shp2H2(shpfile, dp);
|
309 |
} catch (Exception e) { |
310 |
if (e.getCause() instanceof BaseException){ |
311 |
System.out.println(((BaseException)e).getLocalizedMessageStack());
|
312 |
} else{
|
313 |
e.printStackTrace(); |
314 |
} |
315 |
fail(); |
316 |
} |
317 |
|
318 |
return dp;
|
319 |
|
320 |
} |
321 |
|
322 |
public void testH2() { |
323 |
System.out.println("======= H2 =============="); |
324 |
Register.selfRegister(); |
325 |
|
326 |
DataManager dsm=DataManager.getManager(); |
327 |
|
328 |
|
329 |
H2StoreParameters dp=cretateTablePrueba(); |
330 |
|
331 |
storeTest((IDataStoreParameters)dp,null,null,true); |
332 |
|
333 |
IFeatureStore fs=null;
|
334 |
try {
|
335 |
fs = (IFeatureStore)dsm.createDataStore(dp); |
336 |
} catch (InitializeException e) {
|
337 |
e.printStackTrace(); |
338 |
fail("Exception:" + e);
|
339 |
} |
340 |
assertNotNull("Can't create Feature Store", fs);
|
341 |
|
342 |
try {
|
343 |
fs.open(); |
344 |
} catch (OpenException e2) {
|
345 |
e2.printStackTrace(); |
346 |
fail(); |
347 |
} |
348 |
|
349 |
Iterator it;
|
350 |
IFeatureCollection fc =null;
|
351 |
Comparable v1,v2;
|
352 |
IFeature feature=null,pfeature=null; |
353 |
long count;
|
354 |
|
355 |
|
356 |
try {
|
357 |
fc = (IFeatureCollection)fs.getDataCollection(); |
358 |
} catch (ReadException e1) {
|
359 |
e1.printStackTrace(); |
360 |
fail(); |
361 |
} |
362 |
|
363 |
assertEquals(9, fc.size());
|
364 |
|
365 |
fc.dispose(); |
366 |
|
367 |
try {
|
368 |
fc = (IFeatureCollection)fs.getDataCollection(null,"lower(NOMBRE) like 'b%'",null); |
369 |
} catch (ReadException e1) {
|
370 |
e1.printStackTrace(); |
371 |
fail(); |
372 |
} |
373 |
|
374 |
assertEquals(2, fc.size());
|
375 |
|
376 |
it = fc.iterator(); |
377 |
count=0;
|
378 |
while (it.hasNext()){
|
379 |
feature = (IFeature)it.next(); |
380 |
assertTrue("Filter error",feature.getString("NOMBRE").toLowerCase().startsWith("b")); |
381 |
count++; |
382 |
} |
383 |
assertEquals("Iteration error",2,count); |
384 |
|
385 |
fc.dispose(); |
386 |
|
387 |
|
388 |
try {
|
389 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"NOMBRE ASC"); |
390 |
} catch (ReadException e1) {
|
391 |
e1.printStackTrace(); |
392 |
fail(); |
393 |
} |
394 |
assertEquals(9, fc.size());
|
395 |
it = fc.iterator(); |
396 |
count=0;
|
397 |
pfeature = (IFeature)it.next(); |
398 |
count++; |
399 |
while (it.hasNext()){
|
400 |
feature = (IFeature)it.next(); |
401 |
v1 = (Comparable)pfeature.get("NOMBRE"); |
402 |
v2 = (Comparable)feature.get("NOMBRE"); |
403 |
pfeature=feature; |
404 |
assertTrue("Short error", (v1.compareTo(v1) <= 0)); |
405 |
count++; |
406 |
} |
407 |
assertEquals("Iteration error",9,count); |
408 |
|
409 |
fc.dispose(); |
410 |
|
411 |
|
412 |
try {
|
413 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"NOMBRE DESC"); |
414 |
} catch (ReadException e1) {
|
415 |
e1.printStackTrace(); |
416 |
fail(); |
417 |
} |
418 |
assertEquals(9, fc.size());
|
419 |
it = fc.iterator(); |
420 |
|
421 |
count=0;
|
422 |
pfeature = (IFeature)it.next(); |
423 |
count++; |
424 |
while (it.hasNext()){
|
425 |
feature = (IFeature)it.next(); |
426 |
v1 = (Comparable)pfeature.get("NOMBRE"); |
427 |
v2 = (Comparable)feature.get("NOMBRE"); |
428 |
pfeature=feature; |
429 |
assertTrue("Short error", (v1.compareTo(v1) >= 0)); |
430 |
count++; |
431 |
} |
432 |
assertEquals("Iteration error",9,count); |
433 |
|
434 |
fc.dispose(); |
435 |
|
436 |
|
437 |
|
438 |
try {
|
439 |
fc = (IFeatureCollection)fs.getDataCollection(null,"lower(NOMBRE) like 'b%'","NOMBRE"); |
440 |
} catch (ReadException e1) {
|
441 |
e1.printStackTrace(); |
442 |
fail(); |
443 |
} |
444 |
|
445 |
assertEquals(2, fc.size());
|
446 |
|
447 |
it = fc.iterator(); |
448 |
|
449 |
count=0;
|
450 |
pfeature = (IFeature)it.next(); |
451 |
assertTrue(pfeature.getString("NOMBRE").toLowerCase().startsWith("b")); |
452 |
count++; |
453 |
while (it.hasNext()){
|
454 |
feature = (IFeature)it.next(); |
455 |
assertTrue("Filter error",feature.getString("NOMBRE").toLowerCase().startsWith("b")); |
456 |
v1 = (Comparable)pfeature.get("NOMBRE"); |
457 |
v2 = (Comparable)feature.get("NOMBRE"); |
458 |
pfeature=feature; |
459 |
assertTrue("Short error", (v1.compareTo(v1) <= 0)); |
460 |
count++; |
461 |
} |
462 |
assertEquals("Iteration error",2,count); |
463 |
fc.dispose(); |
464 |
|
465 |
|
466 |
|
467 |
try {
|
468 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"Tipo,lower(NOMBRE) Desc"); |
469 |
} catch (ReadException e1) {
|
470 |
e1.printStackTrace(); |
471 |
fail(); |
472 |
} |
473 |
assertEquals(9, fc.size());
|
474 |
it = fc.iterator(); |
475 |
count=0;
|
476 |
pfeature = (IFeature)it.next(); |
477 |
System.out.println(pfeature.getString("NOMBRE")); |
478 |
count++; |
479 |
while (it.hasNext()){
|
480 |
feature = (IFeature)it.next(); |
481 |
v1 = (Comparable)((String)pfeature.get("NOMBRE")).toLowerCase(); |
482 |
v2 = (Comparable)((String)feature.get("NOMBRE")).toLowerCase(); |
483 |
pfeature=feature; |
484 |
assertTrue("Short error", (v1.compareTo(v2) >= 0)); |
485 |
System.out.println(pfeature.getString("NOMBRE")); |
486 |
count++; |
487 |
} |
488 |
assertEquals("Iteration error",9,count); |
489 |
fc.dispose(); |
490 |
|
491 |
|
492 |
|
493 |
/// CON EDICION
|
494 |
try {
|
495 |
fs.startEditing(); |
496 |
} catch (ReadException e1) {
|
497 |
e1.printStackTrace(); |
498 |
fail(); |
499 |
} |
500 |
|
501 |
try{
|
502 |
IFeature newFeature = fs.createFeature(fs.getDefaultFeatureType()); |
503 |
newFeature.editing(); |
504 |
newFeature.set("NOMBRE","BuRjaSOT"); |
505 |
newFeature.set("TIPO","MUNICIPIO"); |
506 |
fs.insert(newFeature); |
507 |
} catch (DataException e2) {
|
508 |
// TODO Auto-generated catch block
|
509 |
e2.printStackTrace(); |
510 |
} |
511 |
|
512 |
try {
|
513 |
fc = (IFeatureCollection)fs.getDataCollection(); |
514 |
} catch (ReadException e1) {
|
515 |
e1.printStackTrace(); |
516 |
fail(); |
517 |
} |
518 |
|
519 |
assertEquals(10, fc.size());
|
520 |
fc.dispose(); |
521 |
|
522 |
try {
|
523 |
fc = (IFeatureCollection)fs.getDataCollection(null,"lower(NOMBRE) like 'b%'",null); |
524 |
} catch (ReadException e1) {
|
525 |
e1.printStackTrace(); |
526 |
fail(); |
527 |
} |
528 |
|
529 |
assertEquals(3, fc.size());
|
530 |
|
531 |
it = fc.iterator(); |
532 |
count=0;
|
533 |
while (it.hasNext()){
|
534 |
feature = (IFeature)it.next(); |
535 |
assertTrue("Filter error",feature.getString("NOMBRE").toLowerCase().startsWith("b")); |
536 |
count++; |
537 |
} |
538 |
assertEquals("Iteration error",3,count); |
539 |
fc.dispose(); |
540 |
|
541 |
|
542 |
try {
|
543 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"NOMBRE ASC"); |
544 |
} catch (ReadException e1) {
|
545 |
e1.printStackTrace(); |
546 |
fail(); |
547 |
} |
548 |
assertEquals(10, fc.size());
|
549 |
it = fc.iterator(); |
550 |
count=0;
|
551 |
pfeature = (IFeature)it.next(); |
552 |
count++; |
553 |
while (it.hasNext()){
|
554 |
feature = (IFeature)it.next(); |
555 |
v1 = (Comparable)pfeature.get("NOMBRE"); |
556 |
v2 = (Comparable)feature.get("NOMBRE"); |
557 |
pfeature=feature; |
558 |
assertTrue("Short error", (v1.compareTo(v1) <= 0)); |
559 |
count++; |
560 |
} |
561 |
assertEquals("Iteration error",10,count); |
562 |
fc.dispose(); |
563 |
|
564 |
|
565 |
try {
|
566 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"NOMBRE DESC"); |
567 |
} catch (ReadException e1) {
|
568 |
e1.printStackTrace(); |
569 |
fail(); |
570 |
} |
571 |
assertEquals(10, fc.size());
|
572 |
it = fc.iterator(); |
573 |
|
574 |
count=0;
|
575 |
pfeature = (IFeature)it.next(); |
576 |
count++; |
577 |
while (it.hasNext()){
|
578 |
feature = (IFeature)it.next(); |
579 |
v1 = (Comparable)pfeature.get("NOMBRE"); |
580 |
v2 = (Comparable)feature.get("NOMBRE"); |
581 |
pfeature=feature; |
582 |
assertTrue("Short error", (v1.compareTo(v1) >= 0)); |
583 |
count++; |
584 |
} |
585 |
assertEquals("Iteration error",10,count); |
586 |
fc.dispose(); |
587 |
|
588 |
|
589 |
try {
|
590 |
fc = (IFeatureCollection)fs.getDataCollection(null,"lower(NOMBRE) like 'b%'","NOMBRE"); |
591 |
} catch (ReadException e1) {
|
592 |
e1.printStackTrace(); |
593 |
fail(); |
594 |
} |
595 |
|
596 |
assertEquals(3, fc.size());
|
597 |
|
598 |
it = fc.iterator(); |
599 |
|
600 |
count=0;
|
601 |
pfeature = (IFeature)it.next(); |
602 |
assertTrue(pfeature.getString("NOMBRE").toLowerCase().startsWith("b")); |
603 |
count++; |
604 |
while (it.hasNext()){
|
605 |
feature = (IFeature)it.next(); |
606 |
assertTrue("Filter error",feature.getString("NOMBRE").toLowerCase().startsWith("b")); |
607 |
v1 = (Comparable)pfeature.get("NOMBRE"); |
608 |
v2 = (Comparable)feature.get("NOMBRE"); |
609 |
pfeature=feature; |
610 |
assertTrue("Short error", (v1.compareTo(v1) <= 0)); |
611 |
count++; |
612 |
} |
613 |
assertEquals("Iteration error",3,count); |
614 |
fc.dispose(); |
615 |
|
616 |
|
617 |
|
618 |
try {
|
619 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"Tipo,lower(NOMBRE) Desc"); |
620 |
} catch (ReadException e1) {
|
621 |
e1.printStackTrace(); |
622 |
fail(); |
623 |
} |
624 |
assertEquals(10, fc.size());
|
625 |
it = fc.iterator(); |
626 |
count=0;
|
627 |
pfeature = (IFeature)it.next(); |
628 |
System.out.println(pfeature.getString("NOMBRE")); |
629 |
count++; |
630 |
while (it.hasNext()){
|
631 |
feature = (IFeature)it.next(); |
632 |
v1 = (Comparable)((String)pfeature.get("NOMBRE")).toLowerCase(); |
633 |
v2 = (Comparable)((String)feature.get("NOMBRE")).toLowerCase(); |
634 |
pfeature=feature; |
635 |
assertTrue("Short error", (v1.compareTo(v2) >= 0)); |
636 |
System.out.println(pfeature.getString("NOMBRE")); |
637 |
count++; |
638 |
} |
639 |
assertEquals("Iteration error",10,count); |
640 |
fc.dispose(); |
641 |
|
642 |
|
643 |
fs.cancelEditing(); |
644 |
|
645 |
|
646 |
//Insertar un elemento
|
647 |
try{
|
648 |
fs.startEditing(); |
649 |
} catch (ReadException e1) {
|
650 |
e1.printStackTrace(); |
651 |
fail(); |
652 |
} |
653 |
|
654 |
try{
|
655 |
feature = fs.createDefaultFeature(true);
|
656 |
feature.editing(); |
657 |
feature.set("ID", 90000); |
658 |
feature.set("NOMBRE","BurJASOT"); |
659 |
feature.set("TIPO", "OTRO"); |
660 |
fs.insert(feature); |
661 |
fs.finishEditing(); |
662 |
} catch (DataException e) {
|
663 |
e.printStackTrace(); |
664 |
fail("Exception: "+e);
|
665 |
} |
666 |
|
667 |
|
668 |
try {
|
669 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"lower(NOMBRE) Desc"); |
670 |
} catch (ReadException e1) {
|
671 |
e1.printStackTrace(); |
672 |
fail(); |
673 |
} |
674 |
assertEquals(10, fc.size());
|
675 |
it = fc.iterator(); |
676 |
count=0;
|
677 |
pfeature = (IFeature)it.next(); |
678 |
System.out.println(pfeature.getString("NOMBRE")); |
679 |
count++; |
680 |
while (it.hasNext()){
|
681 |
feature = (IFeature)it.next(); |
682 |
v1 = (Comparable)((String)pfeature.get("NOMBRE")).toLowerCase(); |
683 |
v2 = (Comparable)((String)feature.get("NOMBRE")).toLowerCase(); |
684 |
pfeature=feature; |
685 |
assertTrue("Short error: "+ v1.toString() + " >= " +v2.toString(), (v1.compareTo(v2) >= 0)); |
686 |
System.out.println(pfeature.getString("NOMBRE")); |
687 |
count++; |
688 |
} |
689 |
assertEquals("Iteration error",10,count); |
690 |
fc.dispose(); |
691 |
|
692 |
//Actualizacion
|
693 |
try{
|
694 |
fs.startEditing(); |
695 |
} catch (ReadException e1) {
|
696 |
e1.printStackTrace(); |
697 |
fail(); |
698 |
} |
699 |
|
700 |
|
701 |
try {
|
702 |
fc = (IFeatureCollection)fs.getDataCollection(null,"lower(NOMBRE) = 'burjasot'",null); |
703 |
} catch (ReadException e1) {
|
704 |
e1.printStackTrace(); |
705 |
fail(); |
706 |
} |
707 |
assertEquals(1, fc.size());
|
708 |
feature =(Feature)fc.iterator().next(); |
709 |
boolean isOk=false; |
710 |
try {
|
711 |
// feature.editing();
|
712 |
feature.set("NOMBRE", feature.getString("NOMBRE")+"__KK__"); |
713 |
} catch (DataException e2) {
|
714 |
isOk=true;
|
715 |
} |
716 |
assertTrue("editing feature control", isOk);
|
717 |
|
718 |
try {
|
719 |
feature.editing(); |
720 |
feature.set("NOMBRE", feature.getString("NOMBRE")+"__KK__"); |
721 |
} catch (DataException e2) {
|
722 |
isOk=true;
|
723 |
e2.printStackTrace(); |
724 |
fail("Exception: "+e2);
|
725 |
} |
726 |
|
727 |
try {
|
728 |
fs.update(feature); |
729 |
} catch (DataException e2) {
|
730 |
e2.printStackTrace();fail(); |
731 |
} |
732 |
|
733 |
try {
|
734 |
fs.finishEditing(); |
735 |
} catch (WriteException e) {
|
736 |
e.printStackTrace(); |
737 |
fail("Exception: "+e);
|
738 |
} catch (ReadException e) {
|
739 |
e.printStackTrace(); |
740 |
fail("Exception: "+e);
|
741 |
} |
742 |
|
743 |
try {
|
744 |
fc = (IFeatureCollection)fs.getDataCollection(null,"NOMBRE like '%__KK__'",null); |
745 |
} catch (ReadException e1) {
|
746 |
e1.printStackTrace(); |
747 |
fail(); |
748 |
} |
749 |
assertEquals(1, fc.size());
|
750 |
fc.dispose(); |
751 |
|
752 |
|
753 |
//Eliminacion
|
754 |
try{
|
755 |
fs.startEditing(); |
756 |
} catch (ReadException e1) {
|
757 |
e1.printStackTrace(); |
758 |
fail(); |
759 |
} |
760 |
|
761 |
|
762 |
try {
|
763 |
fc = (IFeatureCollection)fs.getDataCollection(null,"NOMBRE like '%__KK__'",null); |
764 |
} catch (ReadException e1) {
|
765 |
e1.printStackTrace(); |
766 |
fail(); |
767 |
} |
768 |
assertEquals(1, fc.size());
|
769 |
try {
|
770 |
fs.delete((IFeature)fc.iterator().next()); |
771 |
} catch (DataException e2) {
|
772 |
e2.printStackTrace();fail(); |
773 |
} |
774 |
fc.dispose(); |
775 |
try {
|
776 |
fs.finishEditing(); |
777 |
} catch (WriteException e) {
|
778 |
e.printStackTrace(); |
779 |
fail("Exception: "+e);
|
780 |
} catch (ReadException e) {
|
781 |
e.printStackTrace(); |
782 |
fail("Exception: "+e);
|
783 |
} |
784 |
|
785 |
try {
|
786 |
fc = (IFeatureCollection)fs.getDataCollection(); |
787 |
} catch (ReadException e1) {
|
788 |
e1.printStackTrace(); |
789 |
fail(); |
790 |
} |
791 |
assertEquals(9, fc.size());
|
792 |
fc.dispose(); |
793 |
|
794 |
|
795 |
|
796 |
try {
|
797 |
fs.close(); |
798 |
} catch (CloseException e) {
|
799 |
e.printStackTrace(); |
800 |
fail("Exception:" + e);
|
801 |
} |
802 |
|
803 |
try {
|
804 |
fs.dispose(); |
805 |
} catch (CloseException e) {
|
806 |
e.printStackTrace();fail(); |
807 |
} |
808 |
|
809 |
// TODO FALTA Notificaci?n de edici?n
|
810 |
JDBCTest.doFileResourceTest(dp); |
811 |
// JDBCTest.doFileResourceTest(dp,false);
|
812 |
|
813 |
deleteTable(dp); |
814 |
|
815 |
System.out.println("======= /H2 =============="); |
816 |
|
817 |
} |
818 |
|
819 |
public void testH2_explorer_list(){ |
820 |
System.out.println("======= H2 explorer list =============="); |
821 |
Register.selfRegister(); |
822 |
|
823 |
DataManager dm=DataManager.getManager(); |
824 |
|
825 |
|
826 |
H2ExplorerParameters dp=(H2ExplorerParameters)dm.createDataExplorerParameters(H2Explorer.DATAEXPLORER_NAME); |
827 |
|
828 |
dp.setHost("localhost");
|
829 |
dp.setUser("SA");
|
830 |
dp.setPassw("");
|
831 |
dp.setShowInformationDBTables(true);
|
832 |
|
833 |
IFeatureStoreExplorer explorer=null;
|
834 |
try {
|
835 |
explorer = (IFeatureStoreExplorer)dm.createDataExplorer(dp); |
836 |
} catch (InitializeException e1) {
|
837 |
e1.printStackTrace(); |
838 |
fail(); return;
|
839 |
} |
840 |
|
841 |
IDataStoreParameters[] list= null; |
842 |
|
843 |
try {
|
844 |
list = explorer.list(); |
845 |
} catch (ReadException e) {
|
846 |
e.printStackTrace(); |
847 |
fail();return;
|
848 |
} |
849 |
|
850 |
H2StoreParameters h2param= null;
|
851 |
DBFeatureType ftype=null;
|
852 |
Iterator iter = null; |
853 |
DBAttributeDescriptor attr= null;
|
854 |
|
855 |
int i;
|
856 |
for (i=0;i<list.length;i++){ |
857 |
h2param = (H2StoreParameters)list[i]; |
858 |
System.out.println("Table: "+h2param.tableID()); |
859 |
try {
|
860 |
ftype =(DBFeatureType)(explorer.getFeatureTypes(h2param)[0]);
|
861 |
} catch (ReadException e) {
|
862 |
e.printStackTrace(); |
863 |
fail(); return;
|
864 |
} |
865 |
System.out.println("\tNum fields: "+ftype.size()); |
866 |
System.out.println("\tPk field: "+ftype.getFieldsId()[0]); |
867 |
System.out.println("\tFields: "); |
868 |
|
869 |
|
870 |
iter = ftype.iterator(); |
871 |
while(iter.hasNext()){
|
872 |
attr = (DBAttributeDescriptor) iter.next(); |
873 |
System.out.println("\t\t "+attr.getName()+" "+attr.getDataType()); |
874 |
} |
875 |
|
876 |
} |
877 |
|
878 |
try {
|
879 |
explorer.dispose(); |
880 |
} catch (DataException e) {
|
881 |
e.printStackTrace(); |
882 |
} |
883 |
|
884 |
|
885 |
System.out.println("======= H2 explorer list=============="); |
886 |
} |
887 |
|
888 |
public void testH2_explorer_remove(){ |
889 |
System.out.println("======= H2 explorer delete=============="); |
890 |
Register.selfRegister(); |
891 |
|
892 |
DataManager dm=DataManager.getManager(); |
893 |
|
894 |
H2StoreParameters ds=cretateTablePrueba(); |
895 |
|
896 |
H2ExplorerParameters dex=(H2ExplorerParameters)dm.createDataExplorerParameters(H2Explorer.DATAEXPLORER_NAME); |
897 |
|
898 |
dex.loadFromStoreParameters(ds); |
899 |
dex.setShowInformationDBTables(false);
|
900 |
|
901 |
IFeatureStoreExplorer explorer=null;
|
902 |
try {
|
903 |
explorer = (IFeatureStoreExplorer)dm.createDataExplorer(dex); |
904 |
} catch (InitializeException e1) {
|
905 |
e1.printStackTrace(); |
906 |
fail(); return;
|
907 |
} |
908 |
|
909 |
IDataStoreParameters[] list= null; |
910 |
|
911 |
try {
|
912 |
list = explorer.list(); |
913 |
} catch (ReadException e) {
|
914 |
e.printStackTrace(); |
915 |
fail();return;
|
916 |
} |
917 |
|
918 |
if (list.length < 1){ |
919 |
fail(); return;
|
920 |
} |
921 |
int i;
|
922 |
boolean found=false; |
923 |
H2StoreParameters dsaux=null;
|
924 |
for(i=0;i<list.length;i++){ |
925 |
dsaux = (H2StoreParameters)list[i]; |
926 |
if (dsaux.getTableName().equals(ds.getTableName())){
|
927 |
found=true;
|
928 |
break;
|
929 |
} |
930 |
} |
931 |
if (!found){
|
932 |
fail(); |
933 |
} |
934 |
|
935 |
try {
|
936 |
explorer.remove(dsaux); |
937 |
} catch (ReadException e) {
|
938 |
e.printStackTrace(); |
939 |
fail();return;
|
940 |
} |
941 |
|
942 |
try {
|
943 |
list = explorer.list(); |
944 |
} catch (ReadException e) {
|
945 |
e.printStackTrace(); |
946 |
fail();return;
|
947 |
} |
948 |
|
949 |
found = false;
|
950 |
for(i=0;i<list.length;i++){ |
951 |
dsaux = (H2StoreParameters)list[i]; |
952 |
if (dsaux.getTableName().equals(ds.getTableName())){
|
953 |
found=true;
|
954 |
break;
|
955 |
} |
956 |
} |
957 |
if (found){
|
958 |
fail(); |
959 |
} |
960 |
|
961 |
try {
|
962 |
explorer.dispose(); |
963 |
} catch (DataException e) {
|
964 |
e.printStackTrace();fail(); |
965 |
} |
966 |
|
967 |
System.out.println("======= /H2 explorer remove=============="); |
968 |
} |
969 |
|
970 |
public void testH2_explorer_add_remove(){ |
971 |
System.out.println("======= H2 explorer add/remove=============="); |
972 |
Register.selfRegister(); |
973 |
|
974 |
DataManager dm=DataManager.getManager(); |
975 |
|
976 |
|
977 |
H2ExplorerParameters dex=(H2ExplorerParameters)dm.createDataExplorerParameters(H2Explorer.DATAEXPLORER_NAME); |
978 |
|
979 |
|
980 |
dex.setHost("localhost");
|
981 |
dex.setUser("SA");
|
982 |
dex.setPassw("");
|
983 |
|
984 |
IFeatureStoreExplorer explorer=null;
|
985 |
try {
|
986 |
explorer = (IFeatureStoreExplorer)dm.createDataExplorer(dex); |
987 |
} catch (InitializeException e1) {
|
988 |
e1.printStackTrace(); |
989 |
fail(); return;
|
990 |
} |
991 |
|
992 |
|
993 |
|
994 |
|
995 |
H2NewStoreParameter newdp = (H2NewStoreParameter) explorer.createNewDataStoreParameter(); |
996 |
|
997 |
newdp.getH2Parameters().setTableName(nuevoNombreDeTabla("th2_create").toUpperCase());
|
998 |
newdp.getH2Parameters().setFieldId(new String[] {"ID"}); |
999 |
|
1000 |
|
1001 |
DBFeatureType ftype = new DBFeatureType();
|
1002 |
|
1003 |
newdp.setFeatureType(ftype); |
1004 |
DBAttributeDescriptor attr; |
1005 |
|
1006 |
attr = (DBAttributeDescriptor) ftype.createAttributeDescriptor(); |
1007 |
try {
|
1008 |
attr.loading(); |
1009 |
attr.setName("ID");
|
1010 |
attr.setAutoIncrement(true);
|
1011 |
attr.setType(IFeatureAttributeDescriptor.TYPE_INT); |
1012 |
attr.setPrimaryKey(true);
|
1013 |
attr.stopLoading(); |
1014 |
ftype.add(attr); |
1015 |
|
1016 |
attr = (DBAttributeDescriptor) ftype.createAttributeDescriptor(); |
1017 |
attr.loading(); |
1018 |
attr.setName("MYGEOMETRY");
|
1019 |
attr.setType(IFeatureAttributeDescriptor.TYPE_GEOMETRY); |
1020 |
attr.stopLoading(); |
1021 |
ftype.add(attr); |
1022 |
ftype.setDefaultGeometry(attr.getName()); |
1023 |
|
1024 |
attr = (DBAttributeDescriptor) ftype.createAttributeDescriptor(); |
1025 |
attr.loading(); |
1026 |
attr.setName("MYSTRING");
|
1027 |
attr.setSize(50);
|
1028 |
attr.setDefaultValue("def");
|
1029 |
attr.setType(IFeatureAttributeDescriptor.TYPE_STRING); |
1030 |
attr.stopLoading(); |
1031 |
ftype.add(attr); |
1032 |
|
1033 |
|
1034 |
attr = (DBAttributeDescriptor) ftype.createAttributeDescriptor(); |
1035 |
attr.loading(); |
1036 |
attr.setName("MYDOUBLE");
|
1037 |
|
1038 |
attr.setType(IFeatureAttributeDescriptor.TYPE_DOUBLE); |
1039 |
|
1040 |
attr.stopLoading(); |
1041 |
|
1042 |
ftype.add(attr); |
1043 |
|
1044 |
ftype.setFieldsId(newdp.getH2Parameters().getFieldsId()); |
1045 |
} catch (DataException e2) {
|
1046 |
e2.printStackTrace(); |
1047 |
fail();return;
|
1048 |
} |
1049 |
|
1050 |
try {
|
1051 |
explorer.add(newdp); |
1052 |
} catch (InitializeException e1) {
|
1053 |
e1.printStackTrace(); |
1054 |
fail();return;
|
1055 |
} catch (WriteException e1) {
|
1056 |
e1.printStackTrace(); |
1057 |
fail();return;
|
1058 |
} |
1059 |
|
1060 |
|
1061 |
IDataStoreParameters[] list= null; |
1062 |
|
1063 |
try {
|
1064 |
list = explorer.list(); |
1065 |
} catch (ReadException e) {
|
1066 |
e.printStackTrace(); |
1067 |
fail();return;
|
1068 |
} |
1069 |
|
1070 |
if (list.length < 1){ |
1071 |
fail(); return;
|
1072 |
} |
1073 |
int i;
|
1074 |
boolean found=false; |
1075 |
H2StoreParameters dsaux=null;
|
1076 |
for(i=0;i<list.length;i++){ |
1077 |
dsaux = (H2StoreParameters)list[i]; |
1078 |
if (dsaux.getTableName().equalsIgnoreCase(newdp.getH2Parameters().getTableName())){
|
1079 |
found=true;
|
1080 |
break;
|
1081 |
} |
1082 |
} |
1083 |
if (!found){
|
1084 |
fail(); |
1085 |
} |
1086 |
|
1087 |
try {
|
1088 |
explorer.remove(dsaux); |
1089 |
} catch (ReadException e) {
|
1090 |
e.printStackTrace(); |
1091 |
fail();return;
|
1092 |
} |
1093 |
|
1094 |
try {
|
1095 |
list = explorer.list(); |
1096 |
} catch (ReadException e) {
|
1097 |
e.printStackTrace(); |
1098 |
fail();return;
|
1099 |
} |
1100 |
|
1101 |
found = false;
|
1102 |
for(i=0;i<list.length;i++){ |
1103 |
dsaux = (H2StoreParameters)list[i]; |
1104 |
if (dsaux.getTableName().equals(newdp.getH2Parameters().getTableName())){
|
1105 |
found=true;
|
1106 |
break;
|
1107 |
} |
1108 |
} |
1109 |
if (found){
|
1110 |
fail(); |
1111 |
} |
1112 |
|
1113 |
try {
|
1114 |
explorer.dispose(); |
1115 |
} catch (DataException e) {
|
1116 |
e.printStackTrace();fail(); |
1117 |
} |
1118 |
|
1119 |
System.out.println("======= /H2 explorer add/remove=============="); |
1120 |
} |
1121 |
|
1122 |
|
1123 |
protected void tearDown() throws Exception { |
1124 |
// TODO Auto-generated method stub
|
1125 |
super.tearDown();
|
1126 |
if (startsH2Server)
|
1127 |
this.stopH2Server();
|
1128 |
|
1129 |
|
1130 |
} |
1131 |
} |