svn-gvsig-desktop / trunk / libraries / libDataSourceBaseDrivers / src-test / org / gvsig / data / datastores / vectorial / driver / DataStoreTest.java @ 19483
History | View | Annotate | Download (13.4 KB)
1 |
package org.gvsig.data.datastores.vectorial.driver; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.util.Iterator; |
5 |
|
6 |
import junit.framework.TestCase; |
7 |
|
8 |
import org.gvsig.data.DataSourceManager; |
9 |
import org.gvsig.data.IDataStoreParameters; |
10 |
import org.gvsig.data.datastores.vectorial.driver.dbf.DBFDriverParameters; |
11 |
import org.gvsig.data.datastores.vectorial.driver.shp.ShpDriverParameters; |
12 |
import org.gvsig.data.datastores.vectorial.driver.shp.fileshp.SHP; |
13 |
import org.gvsig.data.exception.CloseException; |
14 |
import org.gvsig.data.exception.InitializeException; |
15 |
import org.gvsig.data.exception.ReadException; |
16 |
import org.gvsig.data.exception.WriteException; |
17 |
import org.gvsig.data.vectorial.IFeature; |
18 |
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor; |
19 |
import org.gvsig.data.vectorial.IFeatureCollection; |
20 |
import org.gvsig.data.vectorial.IFeatureStore; |
21 |
import org.gvsig.data.vectorial.IFeatureType; |
22 |
import org.gvsig.data.visitor.IVisitor; |
23 |
|
24 |
public class DataStoreTest extends TestCase { |
25 |
|
26 |
private File dbffile = new File(DataStoreTest.class.getResource("data/prueba.dbf").getFile()); |
27 |
private File shpfile = new File(DataStoreTest.class.getResource("data/prueba.shp").getFile()); |
28 |
|
29 |
public static void main(String[] args) { |
30 |
junit.textui.TestRunner.run(DataStoreTest.class); |
31 |
} |
32 |
|
33 |
protected void setUp() throws Exception { |
34 |
super.setUp();
|
35 |
|
36 |
} |
37 |
|
38 |
public void testDBF() { |
39 |
System.out.println("======= DBF =============="); |
40 |
org.gvsig.data.datastores.vectorial.driver.dbf.Register.selfRegister(); |
41 |
org.gvsig.data.datastores.vectorial.driver.Register.selfRegister(); |
42 |
|
43 |
DataSourceManager dsm=DataSourceManager.getManager(); |
44 |
|
45 |
|
46 |
IDriverParameters dp=dsm.createDriverParameters("dbf");
|
47 |
((DBFDriverParameters)dp).setDBFFile(dbffile); |
48 |
|
49 |
driverTest(dp,null,null,true); |
50 |
|
51 |
IFeatureStore fs = createFeatureStore(dp); |
52 |
assertNotNull("Can't create Feature Store", fs);
|
53 |
|
54 |
fs.open(); |
55 |
|
56 |
Iterator it;
|
57 |
IFeatureCollection fc; |
58 |
Comparable v1,v2;
|
59 |
IFeature feature,pfeature; |
60 |
long count;
|
61 |
|
62 |
|
63 |
fc = (IFeatureCollection)fs.getDataCollection(); |
64 |
|
65 |
assertEquals(9, fc.size());
|
66 |
|
67 |
fc = (IFeatureCollection)fs.getDataCollection(null,"lower(NOMBRE) like 'b%'",null); |
68 |
|
69 |
assertEquals(2, fc.size());
|
70 |
|
71 |
it = fc.iterator(); |
72 |
count=0;
|
73 |
while (it.hasNext()){
|
74 |
feature = (IFeature)it.next(); |
75 |
assertTrue("Filter error",feature.getString("NOMBRE").toLowerCase().startsWith("b")); |
76 |
count++; |
77 |
} |
78 |
assertEquals("Iteration error",2,count); |
79 |
|
80 |
|
81 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"ASC NOMBRE"); |
82 |
assertEquals(9, fc.size());
|
83 |
it = fc.iterator(); |
84 |
count=0;
|
85 |
pfeature = (IFeature)it.next(); |
86 |
count++; |
87 |
while (it.hasNext()){
|
88 |
feature = (IFeature)it.next(); |
89 |
v1 = (Comparable)pfeature.get("NOMBRE"); |
90 |
v2 = (Comparable)feature.get("NOMBRE"); |
91 |
pfeature=feature; |
92 |
assertTrue("Short error", (v1.compareTo(v1) <= 0)); |
93 |
count++; |
94 |
} |
95 |
assertEquals("Iteration error",9,count); |
96 |
|
97 |
|
98 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"DESC NOMBRE"); |
99 |
assertEquals(9, fc.size());
|
100 |
it = fc.iterator(); |
101 |
|
102 |
count=0;
|
103 |
pfeature = (IFeature)it.next(); |
104 |
count++; |
105 |
while (it.hasNext()){
|
106 |
feature = (IFeature)it.next(); |
107 |
v1 = (Comparable)pfeature.get("NOMBRE"); |
108 |
v2 = (Comparable)feature.get("NOMBRE"); |
109 |
pfeature=feature; |
110 |
assertTrue("Short error", (v1.compareTo(v1) >= 0)); |
111 |
count++; |
112 |
} |
113 |
assertEquals("Iteration error",9,count); |
114 |
|
115 |
|
116 |
fc = (IFeatureCollection)fs.getDataCollection(null,"lower(NOMBRE) like 'b%'","ASC NOMBRE"); |
117 |
|
118 |
assertEquals(2, fc.size());
|
119 |
|
120 |
it = fc.iterator(); |
121 |
|
122 |
count=0;
|
123 |
pfeature = (IFeature)it.next(); |
124 |
assertTrue(pfeature.getString("NOMBRE").toLowerCase().startsWith("b")); |
125 |
count++; |
126 |
while (it.hasNext()){
|
127 |
feature = (IFeature)it.next(); |
128 |
assertTrue("Filter error",feature.getString("NOMBRE").toLowerCase().startsWith("b")); |
129 |
v1 = (Comparable)pfeature.get("NOMBRE"); |
130 |
v2 = (Comparable)feature.get("NOMBRE"); |
131 |
pfeature=feature; |
132 |
assertTrue("Short error", (v1.compareTo(v1) <= 0)); |
133 |
count++; |
134 |
} |
135 |
assertEquals("Iteration error",2,count); |
136 |
|
137 |
|
138 |
|
139 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"Tipo,Desc lower(NOMBRE)"); |
140 |
assertEquals(9, fc.size());
|
141 |
it = fc.iterator(); |
142 |
count=0;
|
143 |
pfeature = (IFeature)it.next(); |
144 |
System.out.println(pfeature.getString("NOMBRE")); |
145 |
count++; |
146 |
while (it.hasNext()){
|
147 |
feature = (IFeature)it.next(); |
148 |
v1 = (Comparable)((String)pfeature.get("NOMBRE")).toLowerCase(); |
149 |
v2 = (Comparable)((String)feature.get("NOMBRE")).toLowerCase(); |
150 |
pfeature=feature; |
151 |
assertTrue("Short error", (v1.compareTo(v2) >= 0)); |
152 |
System.out.println(pfeature.getString("NOMBRE")); |
153 |
count++; |
154 |
} |
155 |
assertEquals("Iteration error",9,count); |
156 |
|
157 |
|
158 |
try {
|
159 |
fs.close(); |
160 |
} catch (CloseException e) {
|
161 |
e.printStackTrace(); |
162 |
fail("Exception:" + e);
|
163 |
} |
164 |
|
165 |
|
166 |
System.out.println("======= /DBF =============="); |
167 |
|
168 |
} |
169 |
|
170 |
public void testSHP() { |
171 |
System.out.println("======= SHP =============="); |
172 |
org.gvsig.data.datastores.vectorial.driver.shp.Register.selfRegister(); |
173 |
org.gvsig.data.datastores.vectorial.driver.dbf.Register.selfRegister(); |
174 |
org.gvsig.data.datastores.vectorial.driver.Register.selfRegister(); |
175 |
|
176 |
DataSourceManager dsm=DataSourceManager.getManager(); |
177 |
|
178 |
|
179 |
IDriverParameters dp=dsm.createDriverParameters("shp");
|
180 |
((ShpDriverParameters)dp).setSHPFile(shpfile); |
181 |
((ShpDriverParameters)dp).setSHXFile(SHP.getShxFile(shpfile)); |
182 |
((ShpDriverParameters)dp).setDBFFile(SHP.getDbfFile(shpfile)); |
183 |
|
184 |
|
185 |
driverTest(dp,null,null,true); |
186 |
|
187 |
|
188 |
|
189 |
|
190 |
IFeatureStore fs = createFeatureStore(dp); |
191 |
assertNotNull("Can't create Feature Store", fs);
|
192 |
|
193 |
fs.open(); |
194 |
|
195 |
Iterator it;
|
196 |
IFeatureCollection fc; |
197 |
Comparable v1,v2;
|
198 |
IFeature feature,pfeature; |
199 |
long count;
|
200 |
|
201 |
|
202 |
fc = (IFeatureCollection)fs.getDataCollection(); |
203 |
|
204 |
assertEquals(9, fc.size());
|
205 |
|
206 |
fc = (IFeatureCollection)fs.getDataCollection(null,"lower(NOMBRE) like 'b%'",null); |
207 |
|
208 |
assertEquals(2, fc.size());
|
209 |
|
210 |
it = fc.iterator(); |
211 |
count=0;
|
212 |
while (it.hasNext()){
|
213 |
feature = (IFeature)it.next(); |
214 |
assertTrue("Filter error",feature.getString("NOMBRE").toLowerCase().startsWith("b")); |
215 |
count++; |
216 |
} |
217 |
assertEquals("Iteration error",2,count); |
218 |
|
219 |
|
220 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"ASC NOMBRE"); |
221 |
assertEquals(9, fc.size());
|
222 |
it = fc.iterator(); |
223 |
count=0;
|
224 |
pfeature = (IFeature)it.next(); |
225 |
count++; |
226 |
while (it.hasNext()){
|
227 |
feature = (IFeature)it.next(); |
228 |
v1 = (Comparable)pfeature.get("NOMBRE"); |
229 |
v2 = (Comparable)feature.get("NOMBRE"); |
230 |
pfeature=feature; |
231 |
assertTrue("Short error", (v1.compareTo(v2) <= 0)); |
232 |
count++; |
233 |
} |
234 |
assertEquals("Iteration error",9,count); |
235 |
|
236 |
|
237 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"DESC NOMBRE"); |
238 |
assertEquals(9, fc.size());
|
239 |
it = fc.iterator(); |
240 |
count=0;
|
241 |
pfeature = (IFeature)it.next(); |
242 |
System.out.println(pfeature.getString("NOMBRE")); |
243 |
count++; |
244 |
while (it.hasNext()){
|
245 |
feature = (IFeature)it.next(); |
246 |
v1 = (Comparable)pfeature.get("NOMBRE"); |
247 |
v2 = (Comparable)feature.get("NOMBRE"); |
248 |
pfeature=feature; |
249 |
assertTrue("Short error", (v1.compareTo(v2) >= 0)); |
250 |
System.out.println(pfeature.getString("NOMBRE")); |
251 |
count++; |
252 |
} |
253 |
assertEquals("Iteration error",9,count); |
254 |
|
255 |
|
256 |
fc = (IFeatureCollection)fs.getDataCollection(null,"lower(NOMBRE) like 'b%'","ASC NOMBRE"); |
257 |
|
258 |
assertEquals(2, fc.size());
|
259 |
|
260 |
it = fc.iterator(); |
261 |
|
262 |
count=0;
|
263 |
pfeature = (IFeature)it.next(); |
264 |
assertTrue(pfeature.getString("NOMBRE").toLowerCase().startsWith("b")); |
265 |
count++; |
266 |
while (it.hasNext()){
|
267 |
feature = (IFeature)it.next(); |
268 |
assertTrue("Filter error",feature.getString("NOMBRE").toLowerCase().startsWith("b")); |
269 |
v1 = (Comparable)pfeature.get("NOMBRE"); |
270 |
v2 = (Comparable)feature.get("NOMBRE"); |
271 |
pfeature=feature; |
272 |
assertTrue("Short error", (v1.compareTo(v2) <= 0)); |
273 |
count++; |
274 |
} |
275 |
assertEquals("Iteration error",2,count); |
276 |
|
277 |
|
278 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"ASC lower(NOMBRE)"); |
279 |
assertEquals(9, fc.size());
|
280 |
it = fc.iterator(); |
281 |
count=0;
|
282 |
pfeature = (IFeature)it.next(); |
283 |
count++; |
284 |
while (it.hasNext()){
|
285 |
feature = (IFeature)it.next(); |
286 |
v1 = (Comparable)((String)pfeature.get("NOMBRE")).toLowerCase(); |
287 |
v2 = (Comparable)((String)feature.get("NOMBRE")).toLowerCase(); |
288 |
pfeature=feature; |
289 |
assertTrue("Short error", (v1.compareTo(v2) <= 0)); |
290 |
count++; |
291 |
} |
292 |
assertEquals("Iteration error",9,count); |
293 |
|
294 |
|
295 |
|
296 |
fc = (IFeatureCollection)fs.getDataCollection(null,null,"Tipo,Desc lower(NOMBRE)"); |
297 |
assertEquals(9, fc.size());
|
298 |
it = fc.iterator(); |
299 |
count=0;
|
300 |
pfeature = (IFeature)it.next(); |
301 |
System.out.println(pfeature.getString("NOMBRE")); |
302 |
count++; |
303 |
while (it.hasNext()){
|
304 |
feature = (IFeature)it.next(); |
305 |
v1 = (Comparable)((String)pfeature.get("NOMBRE")).toLowerCase(); |
306 |
v2 = (Comparable)((String)feature.get("NOMBRE")).toLowerCase(); |
307 |
pfeature=feature; |
308 |
assertTrue("Short error", (v1.compareTo(v2) >= 0)); |
309 |
System.out.println(pfeature.getString("NOMBRE")); |
310 |
count++; |
311 |
} |
312 |
assertEquals("Iteration error",9,count); |
313 |
|
314 |
try {
|
315 |
fs.close(); |
316 |
} catch (CloseException e) {
|
317 |
e.printStackTrace(); |
318 |
fail("Exception:" + e);
|
319 |
} |
320 |
|
321 |
|
322 |
|
323 |
System.out.println("======= /SHP =============="); |
324 |
|
325 |
} |
326 |
|
327 |
private IFeatureStore createFeatureStore(IDriverParameters dp){
|
328 |
DataSourceManager dsm=DataSourceManager.getManager(); |
329 |
|
330 |
IDataStoreParameters dsp=dsm.createDataStoreParameters(DriverStore.DATASTORE_NAME); |
331 |
|
332 |
((IDriverStoreParameters)dsp).setDriverParameters(dp); |
333 |
IFeatureStore fs=null;
|
334 |
try {
|
335 |
fs = (IFeatureStore)dsm.createDataStore(dsp); |
336 |
} catch (InitializeException e) {
|
337 |
e.printStackTrace(); |
338 |
fail("Exception:" + e);
|
339 |
} |
340 |
return fs;
|
341 |
|
342 |
} |
343 |
|
344 |
|
345 |
private void driverTest(IDriverParameters dp,String filter, String order,boolean testEdit){ |
346 |
DataSourceManager dsm=DataSourceManager.getManager(); |
347 |
|
348 |
IDataStoreParameters dsp=dsm.createDataStoreParameters(DriverStore.DATASTORE_NAME); |
349 |
|
350 |
((IDriverStoreParameters)dsp).setDriverParameters(dp); |
351 |
IFeatureStore fs=createFeatureStore(dp); |
352 |
|
353 |
fs.open(); |
354 |
|
355 |
if (fs.isEditable() && testEdit) {
|
356 |
fs.startEditing(); |
357 |
|
358 |
IFeature feature1 = fs.createDefaultFeature(false);
|
359 |
IFeature feature2 = fs.createDefaultFeature(false);
|
360 |
IFeature feature3 = fs.createDefaultFeature(false);
|
361 |
|
362 |
fs.insert(feature1); |
363 |
fs.insert(feature2); |
364 |
|
365 |
fs.update(feature3,feature1); |
366 |
fs.delete(feature3); |
367 |
fs.delete(feature2); |
368 |
} |
369 |
|
370 |
//Mostrar por consola todos los registros.
|
371 |
IFeatureType ft= fs.getDefaultFeatureType(); |
372 |
IFeatureCollection featureCollection; |
373 |
// featureCollection = (IFeatureCollection)fs.getDataCollection();
|
374 |
// featureCollection = (IFeatureCollection)fs.getDataCollection(ft,"NOMBRE = 'CALPE'",null);
|
375 |
// featureCollection = (IFeatureCollection)fs.getDataCollection(ft,"AREA > 3.2213163729E7 and AREA < 3.2213163749E7",null);
|
376 |
featureCollection = (IFeatureCollection)fs.getDataCollection(ft,filter,order); |
377 |
|
378 |
VisitorExample visitor=new VisitorExample(ft);
|
379 |
featureCollection.accept(visitor); |
380 |
|
381 |
if (fs.isEditable() && testEdit){
|
382 |
try {
|
383 |
fs.finishEditing(); |
384 |
} catch (WriteException e) {
|
385 |
e.printStackTrace(); |
386 |
fail("Exception: "+e);
|
387 |
} catch (ReadException e) {
|
388 |
e.printStackTrace(); |
389 |
fail("Exception: "+e);
|
390 |
} |
391 |
} |
392 |
try {
|
393 |
fs.close(); |
394 |
} catch (CloseException e) {
|
395 |
e.printStackTrace(); |
396 |
fail("Exception: "+e);
|
397 |
} |
398 |
fs.dispose(); |
399 |
} |
400 |
|
401 |
private class VisitorExample implements IVisitor{ |
402 |
private IFeatureType featureType;
|
403 |
public VisitorExample(IFeatureType ft){
|
404 |
this.featureType=ft;
|
405 |
} |
406 |
|
407 |
public void visit(Object obj) { |
408 |
IFeature feature=(IFeature)obj; |
409 |
System.out.println("Feature Class ------------------- "+feature.getClass().toString()); |
410 |
for (int i=0;i<featureType.size();i++){ |
411 |
|
412 |
IFeatureAttributeDescriptor descriptor=(IFeatureAttributeDescriptor)featureType.get(i); |
413 |
String type=descriptor.getDataType();
|
414 |
if (type.equals(IFeatureAttributeDescriptor.TYPE_BOOLEAN)){
|
415 |
System.out.println(((IFeatureAttributeDescriptor)featureType.get(i)).getName()+" ----- "+ feature.getBoolean(i)+ "---- Boolean"); |
416 |
}else if (type.equals(IFeatureAttributeDescriptor.TYPE_BYTE)){ |
417 |
System.out.println(((IFeatureAttributeDescriptor)featureType.get(i)).getName()+" ----- "+ feature.getByte(i)+ "---- Byte"); |
418 |
}else if (type.equals(IFeatureAttributeDescriptor.TYPE_DATE)){ |
419 |
System.out.println(((IFeatureAttributeDescriptor)featureType.get(i)).getName()+" ----- "+ feature.getDate(i)+ "---- Date"); |
420 |
}else if (type.equals(IFeatureAttributeDescriptor.TYPE_DOUBLE)){ |
421 |
System.out.println(((IFeatureAttributeDescriptor)featureType.get(i)).getName()+" ----- "+ feature.getDouble(i)+ "---- Double"); |
422 |
}else if (type.equals(IFeatureAttributeDescriptor.TYPE_FLOAT)){ |
423 |
System.out.println(((IFeatureAttributeDescriptor)featureType.get(i)).getName()+" ----- "+ feature.getFloat(i)+ "---- Float"); |
424 |
}else if (type.equals(IFeatureAttributeDescriptor.TYPE_INT)){ |
425 |
System.out.println(((IFeatureAttributeDescriptor)featureType.get(i)).getName()+" ----- "+ feature.getInt(i)+ "---- Integer"); |
426 |
}else if (type.equals(IFeatureAttributeDescriptor.TYPE_LONG)){ |
427 |
System.out.println(((IFeatureAttributeDescriptor)featureType.get(i)).getName()+" ----- "+ feature.getLong(i)+ "---- Long"); |
428 |
}else if (type.equals(IFeatureAttributeDescriptor.TYPE_STRING)){ |
429 |
System.out.println(((IFeatureAttributeDescriptor)featureType.get(i)).getName()+" ----- "+ feature.getString(i)+ "---- String"); |
430 |
} |
431 |
} |
432 |
} |
433 |
|
434 |
} |
435 |
} |