Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libDataSourceBaseDrivers / src-test / org / gvsig / data / datastores / vectorial / driver / DataStoreTest.java @ 19472

History | View | Annotate | Download (9.48 KB)

1 19401 vcaballero
package org.gvsig.data.datastores.vectorial.driver;
2
3
import java.io.File;
4 19468 jmvivo
import java.util.Iterator;
5 19401 vcaballero
6 19402 vcaballero
import junit.framework.TestCase;
7
8 19401 vcaballero
import org.gvsig.data.DataSourceManager;
9
import org.gvsig.data.IDataStoreParameters;
10
import org.gvsig.data.datastores.vectorial.driver.dbf.DBFDriverParameters;
11 19449 jmvivo
import org.gvsig.data.datastores.vectorial.driver.shp.ShpDriverParameters;
12
import org.gvsig.data.datastores.vectorial.driver.shp.fileshp.SHP;
13 19443 vcaballero
import org.gvsig.data.exception.CloseException;
14
import org.gvsig.data.exception.InitializeException;
15 19449 jmvivo
import org.gvsig.data.exception.ReadException;
16
import org.gvsig.data.exception.WriteException;
17 19401 vcaballero
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 19467 vcaballero
import org.gvsig.data.visitor.IVisitor;
23 19401 vcaballero
24
public class DataStoreTest extends TestCase {
25
26 19402 vcaballero
        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 19401 vcaballero
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 19468 jmvivo
                System.out.println("======= DBF ==============");
40 19401 vcaballero
                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 19472 jmvivo
                driverTest(dp,null,null,true);
50 19449 jmvivo
51 19468 jmvivo
                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 19470 jmvivo
                long count;
61 19468 jmvivo
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 19472 jmvivo
//                assertEquals(2, fc.size());
70 19468 jmvivo
71
                it = fc.iterator();
72 19470 jmvivo
                count=0;
73 19468 jmvivo
                while (it.hasNext()){
74
                        feature = (IFeature)it.next();
75 19472 jmvivo
                        System.out.println(feature.getString("NOMBRE"));
76 19470 jmvivo
                        assertTrue("Filter error",feature.getString("NOMBRE").toLowerCase().startsWith("b"));
77
                        count++;
78 19468 jmvivo
                }
79 19470 jmvivo
                assertEquals("Iteration error",2,count);
80 19468 jmvivo
81 19470 jmvivo
82 19468 jmvivo
                fc = (IFeatureCollection)fs.getDataCollection(null,null,"ASC NOMBRE");
83
                assertEquals(9, fc.size());
84
//                FIXME: falta test
85
                it = fc.iterator();
86 19470 jmvivo
                count=0;
87 19468 jmvivo
                pfeature = (IFeature)it.next();
88 19470 jmvivo
                count++;
89 19468 jmvivo
                while (it.hasNext()){
90
                        feature = (IFeature)it.next();
91
                        v1 = (Comparable)pfeature.get("NOMBRE");
92
                        v2 = (Comparable)feature.get("NOMBRE");
93
                        pfeature=feature;
94
                        System.out.println("OJO!!!! NO SE ESTAN HACINEDO COMPROBACIONES DE ORDEN");
95
//                        assertTrue("Short error", (v1.compareTo(v1) <= 0));
96 19470 jmvivo
                        count++;
97 19468 jmvivo
                }
98 19470 jmvivo
                assertEquals("Iteration error",9,count);
99 19468 jmvivo
100
101
                fc = (IFeatureCollection)fs.getDataCollection(null,null,"DESC NOMBRE");
102
                assertEquals(9, fc.size());
103
//                FIXME: falta test
104
                it = fc.iterator();
105
106 19470 jmvivo
                count=0;
107 19468 jmvivo
                pfeature = (IFeature)it.next();
108 19470 jmvivo
                count++;
109 19468 jmvivo
                while (it.hasNext()){
110
                        feature = (IFeature)it.next();
111
                        v1 = (Comparable)pfeature.get("NOMBRE");
112
                        v2 = (Comparable)feature.get("NOMBRE");
113
                        pfeature=feature;
114
                        System.out.println("OJO!!!! NO SE ESTAN HACINEDO COMPROBACIONES DE ORDEN");
115
//                        assertTrue("Short error", (v1.compareTo(v1) >= 0));
116 19470 jmvivo
                        count++;
117 19468 jmvivo
                }
118 19470 jmvivo
                assertEquals("Iteration error",9,count);
119 19468 jmvivo
120
121
                fc = (IFeatureCollection)fs.getDataCollection(null,"lower(NOMBRE) like 'b%'","ASC NOMBRE");
122
123
                assertEquals(2, fc.size());
124
125
                it = fc.iterator();
126
127 19470 jmvivo
                count=0;
128 19468 jmvivo
                pfeature = (IFeature)it.next();
129
                assertTrue(pfeature.getString("NOMBRE").toLowerCase().startsWith("b"));
130 19470 jmvivo
                count++;
131 19468 jmvivo
                while (it.hasNext()){
132
                        feature = (IFeature)it.next();
133 19470 jmvivo
                        assertTrue("Filter error",feature.getString("NOMBRE").toLowerCase().startsWith("b"));
134 19468 jmvivo
                        v1 = (Comparable)pfeature.get("NOMBRE");
135
                        v2 = (Comparable)feature.get("NOMBRE");
136
                        pfeature=feature;
137
                        System.out.println("OJO!!!! NO SE ESTAN HACINEDO COMPROBACIONES DE ORDEN");
138
//                        assertTrue("Short error", (v1.compareTo(v1) <= 0));
139 19470 jmvivo
                        count++;
140 19468 jmvivo
                }
141 19470 jmvivo
                assertEquals("Iteration error",2,count);
142 19468 jmvivo
143
                try {
144
                        fs.close();
145
                } catch (CloseException e) {
146 19470 jmvivo
                        e.printStackTrace();
147 19468 jmvivo
                        fail("Exception:" + e);
148
                }
149
150
151
                System.out.println("======= /DBF ==============");
152
153 19449 jmvivo
        }
154
155
        public void testSHP() {
156 19468 jmvivo
                System.out.println("======= SHP ==============");
157 19449 jmvivo
                org.gvsig.data.datastores.vectorial.driver.shp.Register.selfRegister();
158
                org.gvsig.data.datastores.vectorial.driver.dbf.Register.selfRegister();
159
                org.gvsig.data.datastores.vectorial.driver.Register.selfRegister();
160
161
                DataSourceManager dsm=DataSourceManager.getManager();
162
163
164
                IDriverParameters dp=dsm.createDriverParameters("shp");
165
                ((ShpDriverParameters)dp).setSHPFile(shpfile);
166 19459 vcaballero
                ((ShpDriverParameters)dp).setSHXFile(SHP.getShxFile(shpfile));
167
                ((ShpDriverParameters)dp).setDBFFile(SHP.getDbfFile(shpfile));
168 19449 jmvivo
169
170 19472 jmvivo
                driverTest(dp,null,null,true);
171 19449 jmvivo
172 19468 jmvivo
173
174
175
                System.out.println("======= /SHP ==============");
176
177 19449 jmvivo
        }
178
179 19468 jmvivo
        private IFeatureStore createFeatureStore(IDriverParameters dp){
180 19449 jmvivo
                DataSourceManager dsm=DataSourceManager.getManager();
181
182
                IDataStoreParameters dsp=dsm.createDataStoreParameters(DriverStore.DATASTORE_NAME);
183
184 19401 vcaballero
                ((IDriverStoreParameters)dsp).setDriverParameters(dp);
185 19443 vcaballero
                IFeatureStore fs=null;
186
                try {
187
                        fs = (IFeatureStore)dsm.createDataStore(dsp);
188
                } catch (InitializeException e) {
189 19470 jmvivo
                        e.printStackTrace();
190 19468 jmvivo
                        fail("Exception:" + e);
191 19443 vcaballero
                }
192 19468 jmvivo
                return fs;
193 19401 vcaballero
194 19468 jmvivo
        }
195
196
197 19470 jmvivo
        private void driverTest(IDriverParameters dp,String filter, String order,boolean testEdit){
198 19468 jmvivo
                DataSourceManager dsm=DataSourceManager.getManager();
199
200
                IDataStoreParameters dsp=dsm.createDataStoreParameters(DriverStore.DATASTORE_NAME);
201
202
                ((IDriverStoreParameters)dsp).setDriverParameters(dp);
203
                IFeatureStore fs=createFeatureStore(dp);
204
205 19449 jmvivo
                fs.open();
206 19401 vcaballero
207 19472 jmvivo
                if (fs.isEditable() && testEdit) {
208 19449 jmvivo
                        fs.startEditing();
209
210
                        IFeature feature1 = fs.createDefaultFeature(false);
211
                        IFeature feature2 = fs.createDefaultFeature(false);
212
                        IFeature feature3 = fs.createDefaultFeature(false);
213
214
                        fs.insert(feature1);
215
                        fs.insert(feature2);
216
217
                        fs.update(feature3,feature1);
218
                        fs.delete(feature3);
219
                        fs.delete(feature2);
220
                }
221
222 19401 vcaballero
                //Mostrar por consola todos los registros.
223
                IFeatureType ft= fs.getDefaultFeatureType();
224 19449 jmvivo
                IFeatureCollection featureCollection;
225
//                featureCollection = (IFeatureCollection)fs.getDataCollection();
226
//                featureCollection = (IFeatureCollection)fs.getDataCollection(ft,"NOMBRE = 'CALPE'",null);
227
//                featureCollection = (IFeatureCollection)fs.getDataCollection(ft,"AREA > 3.2213163729E7 and AREA < 3.2213163749E7",null);
228 19463 jmvivo
                featureCollection = (IFeatureCollection)fs.getDataCollection(ft,filter,order);
229 19449 jmvivo
230 19467 vcaballero
                VisitorExample visitor=new VisitorExample(ft);
231
                featureCollection.accept(visitor);
232 19449 jmvivo
233 19472 jmvivo
                if (fs.isEditable() && testEdit){
234 19449 jmvivo
                        try {
235
                                fs.finishEditing();
236
                        } catch (WriteException e) {
237
                                e.printStackTrace();
238 19470 jmvivo
                                fail("Exception: "+e);
239 19449 jmvivo
                        } catch (ReadException e) {
240
                                e.printStackTrace();
241 19470 jmvivo
                                fail("Exception: "+e);
242 19449 jmvivo
                        }
243
                }
244 19443 vcaballero
                try {
245
                        fs.close();
246
                } catch (CloseException e) {
247
                        e.printStackTrace();
248 19470 jmvivo
                        fail("Exception: "+e);
249 19443 vcaballero
                }
250 19401 vcaballero
                fs.dispose();
251
        }
252 19467 vcaballero
253
        private class VisitorExample implements IVisitor{
254
                private IFeatureType featureType;
255
                public VisitorExample(IFeatureType ft){
256
                        this.featureType=ft;
257
                }
258
259
                public void visit(Object obj) {
260
                        IFeature feature=(IFeature)obj;
261
                        System.out.println("Feature Class ------------------- "+feature.getClass().toString());
262
                        for (int i=0;i<featureType.size();i++){
263
264
                                IFeatureAttributeDescriptor descriptor=(IFeatureAttributeDescriptor)featureType.get(i);
265
                                String type=descriptor.getDataType();
266
                                if (type.equals(IFeatureAttributeDescriptor.TYPE_BOOLEAN)){
267
                                        System.out.println(((IFeatureAttributeDescriptor)featureType.get(i)).getName()+" ----- "+ feature.getBoolean(i)+ "---- Boolean");
268
                                }else if (type.equals(IFeatureAttributeDescriptor.TYPE_BYTE)){
269
                                        System.out.println(((IFeatureAttributeDescriptor)featureType.get(i)).getName()+" ----- "+ feature.getByte(i)+ "---- Byte");
270
                                }else if (type.equals(IFeatureAttributeDescriptor.TYPE_DATE)){
271
                                        System.out.println(((IFeatureAttributeDescriptor)featureType.get(i)).getName()+" ----- "+ feature.getDate(i)+ "---- Date");
272
                                }else if (type.equals(IFeatureAttributeDescriptor.TYPE_DOUBLE)){
273
                                        System.out.println(((IFeatureAttributeDescriptor)featureType.get(i)).getName()+" ----- "+ feature.getDouble(i)+ "---- Double");
274
                                }else if (type.equals(IFeatureAttributeDescriptor.TYPE_FLOAT)){
275
                                        System.out.println(((IFeatureAttributeDescriptor)featureType.get(i)).getName()+" ----- "+ feature.getFloat(i)+ "---- Float");
276
                                }else if (type.equals(IFeatureAttributeDescriptor.TYPE_INT)){
277
                                        System.out.println(((IFeatureAttributeDescriptor)featureType.get(i)).getName()+" ----- "+ feature.getInt(i)+ "---- Integer");
278
                                }else if (type.equals(IFeatureAttributeDescriptor.TYPE_LONG)){
279
                                        System.out.println(((IFeatureAttributeDescriptor)featureType.get(i)).getName()+" ----- "+ feature.getLong(i)+ "---- Long");
280
                                }else if (type.equals(IFeatureAttributeDescriptor.TYPE_STRING)){
281
                                        System.out.println(((IFeatureAttributeDescriptor)featureType.get(i)).getName()+" ----- "+ feature.getString(i)+ "---- String");
282
                                }
283
                        }
284 19468 jmvivo
                }
285 19467 vcaballero
286 19470 jmvivo
        }
287
}