root / branches / v2_0_0_prep / libraries / libFMap_dataDB / src-test / org / gvsig / fmap / data / feature / db / DBTest.java @ 23080
History | View | Annotate | Download (13.6 KB)
1 |
package org.gvsig.fmap.data.feature.db; |
---|---|
2 |
|
3 |
import java.util.Iterator; |
4 |
|
5 |
import junit.framework.TestCase; |
6 |
|
7 |
import org.gvsig.exceptions.BaseException; |
8 |
import org.gvsig.fmap.data.CloseException; |
9 |
import org.gvsig.fmap.data.DataCollection; |
10 |
import org.gvsig.fmap.data.DataException; |
11 |
import org.gvsig.fmap.data.DataExplorer; |
12 |
import org.gvsig.fmap.data.DataExplorerParameters; |
13 |
import org.gvsig.fmap.data.DataManager; |
14 |
import org.gvsig.fmap.data.DataStoreParameters; |
15 |
import org.gvsig.fmap.data.InitializeException; |
16 |
import org.gvsig.fmap.data.ReadException; |
17 |
import org.gvsig.fmap.data.Resource; |
18 |
import org.gvsig.fmap.data.ResourceManager; |
19 |
import org.gvsig.fmap.data.WriteException; |
20 |
import org.gvsig.fmap.data.feature.AbstractFeatureStore; |
21 |
import org.gvsig.fmap.data.feature.Feature; |
22 |
import org.gvsig.fmap.data.feature.FeatureCollection; |
23 |
import org.gvsig.fmap.data.feature.FeatureStore; |
24 |
import org.gvsig.fmap.data.feature.FeatureType; |
25 |
import org.gvsig.fmap.data.feature.db.jdbc.JDBCExplorer; |
26 |
import org.gvsig.fmap.data.feature.db.jdbc.JDBCResource; |
27 |
import org.gvsig.fmap.data.feature.db.jdbc.JDBCStoreParameters; |
28 |
import org.gvsig.fmap.data.feature.db.jdbc.PrepareResourceObserver; |
29 |
import org.gvsig.fmap.data.feature.file.FileResource; |
30 |
import org.gvsig.fmap.data.feature.visitor.PrintlnFeaturesVisitor; |
31 |
import org.gvsig.fmap.geom.Geometry; |
32 |
import org.gvsig.fmap.geom.handler.Handler; |
33 |
import org.gvsig.tools.observer.Observer; |
34 |
|
35 |
import com.iver.utiles.XMLEntity; |
36 |
|
37 |
public abstract class DBTest extends TestCase { |
38 |
|
39 |
public void doFileResourceTest(JDBCStoreParameters params) { |
40 |
doFileResourceTest(params,true);
|
41 |
} |
42 |
|
43 |
public void doFileResourceTest(JDBCStoreParameters params, boolean testEdit) { |
44 |
DataManager manager = DataManager.getManager(); |
45 |
|
46 |
ResourceManager resMan = ResourceManager.getResourceManager(); |
47 |
|
48 |
AbstractFeatureStore store=null;
|
49 |
AbstractFeatureStore store2=null;
|
50 |
AbstractFeatureStore store3=null;
|
51 |
try {
|
52 |
store = (AbstractFeatureStore)manager.createDataStore(params); |
53 |
store2 = (AbstractFeatureStore)manager.createDataStore(params); |
54 |
store3 = (AbstractFeatureStore)manager.createDataStore(params); |
55 |
} catch (InitializeException e1) {
|
56 |
e1.printStackTrace();fail(); |
57 |
} |
58 |
|
59 |
int i=0; |
60 |
Resource res = null;
|
61 |
Resource tmpRes = null;
|
62 |
Object obj = null; |
63 |
|
64 |
Iterator iter = resMan.iterator();
|
65 |
while (iter.hasNext()){
|
66 |
obj = iter.next(); |
67 |
if (obj instanceof JDBCResource){ |
68 |
tmpRes = (Resource)obj; |
69 |
if (((JDBCResource)tmpRes).getUrl().equals(params.getUrl())){
|
70 |
i++; |
71 |
res=tmpRes; |
72 |
} |
73 |
} |
74 |
} |
75 |
|
76 |
assertEquals(1, i);
|
77 |
|
78 |
assertEquals(3, res.getRefencesCount());
|
79 |
|
80 |
try {
|
81 |
store.close(); |
82 |
} catch (CloseException e1) {
|
83 |
e1.printStackTrace();fail(); |
84 |
} |
85 |
|
86 |
assertEquals(false, res.isOpen());
|
87 |
|
88 |
DataCollection coll = null;
|
89 |
|
90 |
try {
|
91 |
coll =store.getDataCollection(); |
92 |
} catch (ReadException e1) {
|
93 |
e1.printStackTrace();fail(); |
94 |
} |
95 |
|
96 |
coll.iterator().next(); |
97 |
|
98 |
assertEquals(true, res.isOpen());
|
99 |
|
100 |
coll.dispose(); |
101 |
|
102 |
|
103 |
|
104 |
if (store.isEditable() && testEdit){
|
105 |
/*Test edition notification*/
|
106 |
|
107 |
int fCountOrg=0; |
108 |
int fCountFin=0; |
109 |
try {
|
110 |
fCountOrg = store.getDataCollection().size(); |
111 |
} catch (ReadException e2) {
|
112 |
// TODO Auto-generated catch block
|
113 |
e2.printStackTrace();fail(); |
114 |
} |
115 |
try {
|
116 |
store.startEditing(); |
117 |
} catch (ReadException e1) {
|
118 |
e1.printStackTrace();fail(); |
119 |
} |
120 |
|
121 |
try {
|
122 |
coll = store2.getDataCollection(); |
123 |
} catch (ReadException e1) {
|
124 |
e1.printStackTrace();fail(); |
125 |
} |
126 |
|
127 |
try {
|
128 |
store.finishEditing(); |
129 |
} catch (WriteException e1) {
|
130 |
e1.printStackTrace();fail(); |
131 |
} catch (ReadException e1) {
|
132 |
// TODO Auto-generated catch block
|
133 |
e1.printStackTrace();fail(); |
134 |
} |
135 |
|
136 |
try {
|
137 |
fCountFin = store.getDataCollection().size(); |
138 |
} catch (ReadException e2) {
|
139 |
// TODO Auto-generated catch block
|
140 |
e2.printStackTrace();fail(); |
141 |
} |
142 |
|
143 |
assertEquals(fCountOrg, fCountFin); |
144 |
|
145 |
|
146 |
boolean isOk = false; |
147 |
try{
|
148 |
coll.iterator().next(); |
149 |
} catch (Exception e){ |
150 |
isOk=true;
|
151 |
} |
152 |
assertTrue("Resource Changed Notification fails",isOk);
|
153 |
|
154 |
coll.dispose(); |
155 |
|
156 |
/*Test edition notification END*/
|
157 |
|
158 |
} |
159 |
|
160 |
|
161 |
JDBCExplorer explorer = null;
|
162 |
try {
|
163 |
explorer = (JDBCExplorer) store.getExplorer(); |
164 |
} catch (ReadException e) {
|
165 |
e.printStackTrace();fail(); |
166 |
} |
167 |
|
168 |
if (explorer != null){ |
169 |
assertEquals(4, res.getRefencesCount());
|
170 |
|
171 |
try {
|
172 |
explorer.list(); |
173 |
} catch (ReadException e) {
|
174 |
e.printStackTrace();fail(); |
175 |
} |
176 |
|
177 |
try {
|
178 |
explorer.dispose(); |
179 |
} catch (DataException e1) {
|
180 |
e1.printStackTrace();fail(); |
181 |
} |
182 |
|
183 |
|
184 |
assertEquals(3, res.getRefencesCount());
|
185 |
} |
186 |
try {
|
187 |
store3.dispose(); |
188 |
} catch (CloseException e1) {
|
189 |
e1.printStackTrace();fail(); |
190 |
} |
191 |
|
192 |
assertEquals(2, res.getRefencesCount());
|
193 |
|
194 |
try {
|
195 |
store2.dispose(); |
196 |
} catch (CloseException e1) {
|
197 |
e1.printStackTrace();fail(); |
198 |
} |
199 |
|
200 |
assertEquals(1, res.getRefencesCount());
|
201 |
|
202 |
try {
|
203 |
store.dispose(); |
204 |
} catch (CloseException e1) {
|
205 |
e1.printStackTrace();fail(); |
206 |
} |
207 |
|
208 |
assertEquals(0, res.getRefencesCount());
|
209 |
res = null;
|
210 |
|
211 |
i=0;
|
212 |
iter = resMan.iterator(); |
213 |
while (iter.hasNext()){
|
214 |
obj = iter.next(); |
215 |
if (obj instanceof FileResource){ |
216 |
tmpRes = (Resource)obj; |
217 |
if (((JDBCResource)tmpRes).getUrl().equals(params.getUrl())){
|
218 |
i++; |
219 |
res=tmpRes; |
220 |
} |
221 |
} |
222 |
} |
223 |
|
224 |
assertEquals(0, i);
|
225 |
|
226 |
doPrepareFileResourceTest(params); |
227 |
|
228 |
} |
229 |
|
230 |
public void doPrepareFileResourceTest(JDBCStoreParameters params) { |
231 |
DataManager manager = DataManager.getManager(); |
232 |
|
233 |
ResourceManager resMan = ResourceManager.getResourceManager(); |
234 |
|
235 |
String passw = params.getPassw();
|
236 |
params.setPassw("--------------");
|
237 |
|
238 |
Observer obs = new PrepareResourceObserver(params.getUrl(), passw); |
239 |
|
240 |
|
241 |
resMan.addObserver(obs); |
242 |
|
243 |
AbstractFeatureStore store=null;
|
244 |
try {
|
245 |
store = (AbstractFeatureStore)manager.createDataStore(params); |
246 |
} catch (InitializeException e1) {
|
247 |
e1.printStackTrace();fail(); |
248 |
} catch (Exception e2){ |
249 |
e2.printStackTrace();fail(); |
250 |
} |
251 |
|
252 |
try {
|
253 |
store.getDataCollection().iterator().next(); |
254 |
} catch (ReadException e) {
|
255 |
// TODO Auto-generated catch block
|
256 |
e.printStackTrace();fail(); |
257 |
} |
258 |
|
259 |
try {
|
260 |
store.close(); |
261 |
} catch (CloseException e) {
|
262 |
// TODO Auto-generated catch block
|
263 |
e.printStackTrace();fail(); |
264 |
} |
265 |
try {
|
266 |
store.dispose(); |
267 |
} catch (CloseException e) {
|
268 |
// TODO Auto-generated catch block
|
269 |
e.printStackTrace();fail(); |
270 |
} |
271 |
params.setPassw(passw); |
272 |
|
273 |
resMan.deleteObserver(obs); |
274 |
|
275 |
} |
276 |
|
277 |
public DBTest() {
|
278 |
super();
|
279 |
} |
280 |
|
281 |
protected void storeTest(DataStoreParameters dp, String filter, String order, boolean testEdit) { |
282 |
DataManager dsm=DataManager.getManager(); |
283 |
|
284 |
|
285 |
FeatureStore fs=null;
|
286 |
try {
|
287 |
fs = (FeatureStore)dsm.createDataStore(dp); |
288 |
} catch (InitializeException e) {
|
289 |
e.printStackTrace(); |
290 |
fail("Exception:" + e);
|
291 |
} |
292 |
//
|
293 |
// try {
|
294 |
// fs.open();
|
295 |
// } catch (OpenException e2) {
|
296 |
// e2.printStackTrace();
|
297 |
// fail();
|
298 |
// }
|
299 |
|
300 |
if (fs.isEditable() && testEdit) {
|
301 |
|
302 |
try {
|
303 |
fs.startEditing(); |
304 |
} catch (ReadException e) {
|
305 |
e.printStackTrace(); |
306 |
fail(); |
307 |
} |
308 |
try{
|
309 |
Feature feature1 = fs.createDefaultFeature(false);
|
310 |
feature1.editing(); |
311 |
feature1.set("ID",1); |
312 |
Feature feature2 = fs.createDefaultFeature(false);
|
313 |
feature2.editing(); |
314 |
feature2.set("ID",2); |
315 |
Feature feature3 = fs.createDefaultFeature(false);
|
316 |
feature3.editing(); |
317 |
feature3.set("ID",3); |
318 |
|
319 |
|
320 |
fs.insert(feature1); |
321 |
fs.insert(feature2); |
322 |
feature1.editing(); |
323 |
feature1.set(1,111111); |
324 |
fs.update(feature1); |
325 |
fs.delete(feature3); |
326 |
fs.delete(feature2); |
327 |
}catch (DataException e) {
|
328 |
e.printStackTrace(); |
329 |
fail(); |
330 |
} |
331 |
} |
332 |
|
333 |
//Mostrar por consola todos los registros.
|
334 |
FeatureType ft= fs.getDefaultFeatureType(); |
335 |
FeatureCollection featureCollection=null;
|
336 |
// featureCollection = (FeatureCollection)fs.getDataCollection();
|
337 |
// featureCollection = (FeatureCollection)fs.getDataCollection(ft,"NOMBRE = 'CALPE'",null);
|
338 |
// featureCollection = (FeatureCollection)fs.getDataCollection(ft,"AREA > 3.2213163729E7 and AREA < 3.2213163749E7",null);
|
339 |
try {
|
340 |
featureCollection = (FeatureCollection)fs.getDataCollection(ft,filter,order); |
341 |
} catch (ReadException e2) {
|
342 |
// TODO Auto-generated catch block
|
343 |
e2.printStackTrace(); |
344 |
} |
345 |
|
346 |
PrintlnFeaturesVisitor visitor=new PrintlnFeaturesVisitor(ft);
|
347 |
try {
|
348 |
featureCollection.accept(visitor); |
349 |
} catch (BaseException e1) {
|
350 |
e1.printStackTrace(); |
351 |
fail("Exception: "+e1);
|
352 |
} |
353 |
featureCollection.dispose(); |
354 |
|
355 |
if (fs.isEditable() && testEdit){
|
356 |
try {
|
357 |
fs.finishEditing(); |
358 |
// fs.cancelEditing();
|
359 |
} catch (WriteException e) {
|
360 |
e.printStackTrace(); |
361 |
fail("Exception: "+e);
|
362 |
} catch (ReadException e) {
|
363 |
e.printStackTrace(); |
364 |
fail("Exception: "+e);
|
365 |
} |
366 |
} |
367 |
// try {
|
368 |
// fs.close();
|
369 |
// } catch (CloseException e) {
|
370 |
// e.printStackTrace();
|
371 |
// fail("Exception: "+e);
|
372 |
// }
|
373 |
try {
|
374 |
fs.dispose(); |
375 |
} catch (CloseException e) {
|
376 |
// TODO Auto-generated catch block
|
377 |
e.printStackTrace(); |
378 |
} |
379 |
|
380 |
persistenceTest(dp); |
381 |
} |
382 |
|
383 |
protected void persistenceTest(DataStoreParameters params) { |
384 |
DataManager manager = DataManager.getManager(); |
385 |
|
386 |
DataStoreParameters params2 = null;
|
387 |
XMLEntity paramsXML = null;
|
388 |
XMLEntity params2XML = null;
|
389 |
|
390 |
paramsXML = params.getXMLEntity(); |
391 |
|
392 |
try {
|
393 |
params2 = manager.createDataStoreParameters(paramsXML); |
394 |
} catch (InitializeException e) {
|
395 |
e.printStackTrace(); |
396 |
fail(); |
397 |
} |
398 |
|
399 |
params2XML = params2.getXMLEntity(); |
400 |
|
401 |
assertEquals(paramsXML.toString(), params2XML.toString()); |
402 |
|
403 |
FeatureStore store = null;
|
404 |
XMLEntity storeXML = null;
|
405 |
FeatureStore store2 = null;
|
406 |
XMLEntity store2XML = null;
|
407 |
|
408 |
try {
|
409 |
store = (FeatureStore) manager.createDataStore(params); |
410 |
} catch (InitializeException e) {
|
411 |
e.printStackTrace(); |
412 |
fail(); |
413 |
} |
414 |
|
415 |
storeXML = store.getXMLEntity(); |
416 |
|
417 |
assertNotNull(storeXML); |
418 |
|
419 |
try {
|
420 |
store2 = (FeatureStore) manager.createDataStore(storeXML); |
421 |
} catch (InitializeException e) {
|
422 |
e.printStackTrace(); |
423 |
fail(); |
424 |
} |
425 |
|
426 |
store2XML = store2.getXMLEntity(); |
427 |
|
428 |
assertEquals(storeXML.toString(), store2XML.toString()); |
429 |
|
430 |
FeatureCollection coll = null;
|
431 |
FeatureCollection coll2 = null;
|
432 |
Iterator iter = null; |
433 |
Iterator iter2 = null; |
434 |
Feature feature = null;
|
435 |
Feature feature2 = null;
|
436 |
Comparable v1 = null; |
437 |
Comparable v2 = null; |
438 |
Handler h = null; |
439 |
Handler h2 = null; |
440 |
|
441 |
try {
|
442 |
coll = (FeatureCollection) store.getDataCollection(); |
443 |
coll2 = (FeatureCollection) store2.getDataCollection(); |
444 |
} catch (ReadException e) {
|
445 |
e.printStackTrace(); |
446 |
fail(); |
447 |
} |
448 |
|
449 |
assertEquals(coll.size(), coll2.size()); |
450 |
|
451 |
iter = coll.iterator(); |
452 |
iter2 = coll2.iterator(); |
453 |
int i;
|
454 |
int featureSize = store.getDefaultFeatureType().size();
|
455 |
|
456 |
try {
|
457 |
while (iter.hasNext()) {
|
458 |
feature = (Feature) iter.next(); |
459 |
feature2 = (Feature) iter2.next(); |
460 |
for (i = 0; i < featureSize; i++) { |
461 |
v1 = (Comparable) feature.get(i);
|
462 |
v2 = (Comparable) feature2.get(i);
|
463 |
if (v1 == null) { |
464 |
assertNull(v2); |
465 |
} else if (v1 instanceof Geometry) { |
466 |
assertTrue(v2 instanceof Geometry);
|
467 |
assertEquals(((Geometry) v1).getType(), ((Geometry) v2) |
468 |
.getType()); |
469 |
assertEquals( |
470 |
(((Geometry) v1) |
471 |
.getHandlers(Geometry.SELECTHANDLER)).length, |
472 |
(((Geometry) v2) |
473 |
.getHandlers(Geometry.SELECTHANDLER)).length); |
474 |
h = (((Geometry) v1) |
475 |
.getHandlers(Geometry.SELECTHANDLER))[0];
|
476 |
h2 = (((Geometry) v2) |
477 |
.getHandlers(Geometry.SELECTHANDLER))[0];
|
478 |
|
479 |
assertEquals(0.0, h.getPoint().distance(h2.getPoint()),
|
480 |
0.000001);
|
481 |
|
482 |
h = (((Geometry) v1) |
483 |
.getHandlers(Geometry.SELECTHANDLER))[(((Geometry) v1) |
484 |
.getHandlers(Geometry.SELECTHANDLER)).length - 1];
|
485 |
h2 = (((Geometry) v2) |
486 |
.getHandlers(Geometry.SELECTHANDLER))[(((Geometry) v2) |
487 |
.getHandlers(Geometry.SELECTHANDLER)).length - 1];
|
488 |
|
489 |
assertEquals(0.0, h.getPoint().distance(h2.getPoint()),
|
490 |
0.000001);
|
491 |
|
492 |
} else {
|
493 |
assertEquals(0, v1.compareTo(v2));
|
494 |
} |
495 |
} |
496 |
|
497 |
} |
498 |
} catch (Exception e) { |
499 |
e.printStackTrace(); |
500 |
fail(); |
501 |
} |
502 |
|
503 |
coll.dispose(); |
504 |
coll2.dispose(); |
505 |
|
506 |
try {
|
507 |
store.dispose(); |
508 |
store2.dispose(); |
509 |
} catch (CloseException e) {
|
510 |
e.printStackTrace(); |
511 |
fail(); |
512 |
} |
513 |
|
514 |
} |
515 |
|
516 |
protected void persistenceTest(DataExplorerParameters params1) { |
517 |
DataManager manager = DataManager.getManager(); |
518 |
|
519 |
XMLEntity params1XML; |
520 |
DataExplorerParameters params2 = null;
|
521 |
XMLEntity params2XML; |
522 |
|
523 |
DataExplorer explorer1 = null;
|
524 |
XMLEntity explorer1XML; |
525 |
DataExplorer explorer2 = null;
|
526 |
XMLEntity explorer2XML; |
527 |
|
528 |
params1XML = params1.getXMLEntity(); |
529 |
|
530 |
try {
|
531 |
params2 = manager.createDataExplorerParameters(params1XML); |
532 |
} catch (InitializeException e) {
|
533 |
e.printStackTrace(); |
534 |
fail(); |
535 |
} |
536 |
|
537 |
params2XML = params2.getXMLEntity(); |
538 |
|
539 |
assertEquals(params1XML.toString(), params2XML.toString()); |
540 |
|
541 |
try {
|
542 |
explorer1 = manager.createDataExplorer(params1); |
543 |
} catch (InitializeException e1) {
|
544 |
e1.printStackTrace(); |
545 |
fail(); |
546 |
} |
547 |
|
548 |
explorer1XML = explorer1.getXMLEntity(); |
549 |
|
550 |
try {
|
551 |
explorer2 = manager.createDataExplorer(explorer1XML); |
552 |
} catch (InitializeException e1) {
|
553 |
e1.printStackTrace(); |
554 |
fail(); |
555 |
} |
556 |
|
557 |
explorer2XML = explorer2.getXMLEntity(); |
558 |
|
559 |
assertEquals(explorer1XML.toString(), explorer2XML.toString()); |
560 |
|
561 |
DataStoreParameters list1[] = null; |
562 |
DataStoreParameters list2[] = null; |
563 |
|
564 |
try {
|
565 |
list1 = explorer1.list(); |
566 |
list2 = explorer2.list(); |
567 |
} catch (ReadException e) {
|
568 |
e.printStackTrace(); |
569 |
fail(); |
570 |
} |
571 |
|
572 |
assertEquals(list1.length, list2.length); |
573 |
|
574 |
for (int i = 0; i < list1.length; i++) { |
575 |
assertEquals(list1[i].getXMLEntity().toString(), list2[i] |
576 |
.getXMLEntity().toString()); |
577 |
} |
578 |
|
579 |
try {
|
580 |
explorer1.dispose(); |
581 |
explorer2.dispose(); |
582 |
} catch (DataException e) {
|
583 |
e.printStackTrace(); |
584 |
fail(); |
585 |
} |
586 |
|
587 |
|
588 |
} |
589 |
} |