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