root / branches / v2_0_0_prep / libraries / libFMap_dataFile / src-test / org / gvsig / fmap / data / feature / file / DataExplorerTest.java @ 23754
History | View | Annotate | Download (9.89 KB)
1 |
package org.gvsig.fmap.data.feature.file; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
|
5 |
import junit.framework.TestCase; |
6 |
|
7 |
import org.gvsig.fmap.data.DataExplorer; |
8 |
import org.gvsig.fmap.data.DataExplorerParameters; |
9 |
import org.gvsig.fmap.data.DataManager; |
10 |
import org.gvsig.fmap.data.DataStore; |
11 |
import org.gvsig.fmap.data.DataStoreParameters; |
12 |
import org.gvsig.fmap.data.exceptions.DataException; |
13 |
import org.gvsig.fmap.data.exceptions.InitializeException; |
14 |
import org.gvsig.fmap.data.exceptions.OpenException; |
15 |
import org.gvsig.fmap.data.exceptions.ReadException; |
16 |
import org.gvsig.fmap.data.exceptions.WriteException; |
17 |
import org.gvsig.fmap.data.feature.AttributeDescriptor; |
18 |
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor; |
19 |
import org.gvsig.fmap.data.feature.FeatureCollection; |
20 |
import org.gvsig.fmap.data.feature.FeatureStore; |
21 |
import org.gvsig.fmap.data.feature.FeatureExplorer; |
22 |
import org.gvsig.fmap.data.feature.FeatureType; |
23 |
import org.gvsig.fmap.data.feature.NewFeatureStoreParameters; |
24 |
import org.gvsig.fmap.data.feature.exceptions.IsNotAttributeSettingException; |
25 |
import org.gvsig.fmap.data.feature.file.dbf.DBFDataExplorer; |
26 |
import org.gvsig.fmap.data.feature.file.dbf.DBFStoreParameters; |
27 |
import org.gvsig.fmap.data.feature.file.shp.SHPDataExplorer; |
28 |
import org.gvsig.fmap.data.feature.file.shp.SHPStoreParameters; |
29 |
import org.gvsig.fmap.data.feature.impl.DefaultFeatureType; |
30 |
import org.gvsig.fmap.data.impl.DefaultDataManager; |
31 |
|
32 |
import com.iver.utiles.XMLEntity; |
33 |
|
34 |
public class DataExplorerTest extends TestCase { |
35 |
private File dbffile = new File(DataStoreTest.class.getResource("data/prueba.dbf").getFile()); |
36 |
private File shpfile = new File(DataStoreTest.class.getResource("data/prueba.shp").getFile()); |
37 |
|
38 |
public void testDBFExplorer(){ |
39 |
org.gvsig.fmap.data.feature.file.dbf.Register.selfRegister(); |
40 |
DataStoreParameters[] params = null; |
41 |
params=list(DBFDataExplorer.DATASOURCE_NAME,dbffile.getParent()); |
42 |
|
43 |
assertEquals(3, params.length);
|
44 |
|
45 |
String file=dbffile.getParent()+File.separator+"pruebaCrear.dbf"; |
46 |
DBFStoreParameters parameters=new DBFStoreParameters();
|
47 |
parameters.setFile(new File(file)); |
48 |
|
49 |
DefaultFeatureType dft=new DefaultFeatureType();
|
50 |
AttributeDescriptor fad=(AttributeDescriptor) dft.createAttributeDescriptor(); |
51 |
try {
|
52 |
fad.loading(); |
53 |
fad.setName("CAMPO1");
|
54 |
fad.setType(FeatureAttributeDescriptor.STRING); |
55 |
fad.setSize(10);
|
56 |
fad.stopLoading(); |
57 |
} catch (IsNotAttributeSettingException e1) {
|
58 |
e1.printStackTrace(); |
59 |
} |
60 |
dft.add(fad); |
61 |
|
62 |
|
63 |
add(DBFDataExplorer.DATASOURCE_NAME,file,dft); |
64 |
|
65 |
params=list(DBFDataExplorer.DATASOURCE_NAME,dbffile.getParent()); |
66 |
assertEquals(4, params.length);
|
67 |
FeatureStore fs=(FeatureStore)createDataStore(DBFDataExplorer.DATASOURCE_NAME,file,parameters); |
68 |
try {
|
69 |
// fs.open();
|
70 |
|
71 |
FeatureCollection fc = (FeatureCollection) fs.getDataCollection(); |
72 |
|
73 |
assertEquals(0, fc.size());
|
74 |
|
75 |
fc.dispose(); |
76 |
|
77 |
// fs.close();
|
78 |
fs.dispose(); |
79 |
} catch (Exception e) { |
80 |
e.printStackTrace(); |
81 |
fail(); |
82 |
} |
83 |
try {
|
84 |
remove(DBFDataExplorer.DATASOURCE_NAME,file,parameters); |
85 |
} catch (ReadException e) {
|
86 |
// TODO Auto-generated catch block
|
87 |
e.printStackTrace(); |
88 |
} |
89 |
params=list(DBFDataExplorer.DATASOURCE_NAME,dbffile.getParent()); |
90 |
|
91 |
assertEquals(3, params.length);
|
92 |
|
93 |
persistenceTest(this.getParams(DBFDataExplorer.DATASOURCE_NAME, dbffile
|
94 |
.getParent())); |
95 |
} |
96 |
|
97 |
|
98 |
|
99 |
public void testSHPExplorer(){ |
100 |
org.gvsig.fmap.data.feature.file.shp.Register.selfRegister(); |
101 |
|
102 |
DataStoreParameters[] params = null; |
103 |
params=list(SHPDataExplorer.DATASOURCE_NAME,shpfile.getParent()); |
104 |
|
105 |
assertEquals(3, params.length);
|
106 |
|
107 |
String file=shpfile.getParent()+File.separator+"pruebaCrear.shp"; |
108 |
SHPStoreParameters parameters=new SHPStoreParameters();
|
109 |
parameters.setFile(new File(file)); |
110 |
// parameters.setDBFFile(SHP.getDbfFile(new File(file)));
|
111 |
// parameters.setSHXFile(SHP.getShxFile(new File(file)));
|
112 |
|
113 |
DefaultFeatureType dft=new DefaultFeatureType();
|
114 |
AttributeDescriptor fad1=(AttributeDescriptor) dft.createAttributeDescriptor(); |
115 |
try{
|
116 |
fad1.loading(); |
117 |
fad1.setName("CAMPO1");
|
118 |
fad1.setType(FeatureAttributeDescriptor.STRING); |
119 |
fad1.setSize(10);
|
120 |
fad1.stopLoading(); |
121 |
}catch (IsNotAttributeSettingException e) {
|
122 |
e.printStackTrace(); |
123 |
} |
124 |
AttributeDescriptor fad2=(AttributeDescriptor) dft.createAttributeDescriptor(); |
125 |
try{
|
126 |
fad2.loading(); |
127 |
fad2.setName("Geometry");
|
128 |
fad2.setType(FeatureAttributeDescriptor.GEOMETRY); |
129 |
fad2.stopLoading(); |
130 |
}catch (IsNotAttributeSettingException e) {
|
131 |
e.printStackTrace(); |
132 |
} |
133 |
dft.add(fad1); |
134 |
dft.add(fad2); |
135 |
|
136 |
|
137 |
add(SHPDataExplorer.DATASOURCE_NAME,file,dft); |
138 |
|
139 |
params=list(SHPDataExplorer.DATASOURCE_NAME,shpfile.getParent()); |
140 |
assertEquals(4, params.length);
|
141 |
FeatureStore fs=(FeatureStore)createDataStore(SHPDataExplorer.DATASOURCE_NAME,file,parameters); |
142 |
try {
|
143 |
// fs.open();
|
144 |
|
145 |
FeatureCollection fc = (FeatureCollection) fs.getDataCollection(); |
146 |
|
147 |
assertEquals(0, fc.size());
|
148 |
|
149 |
// fs.close();
|
150 |
fs.dispose(); |
151 |
} catch (Exception e) { |
152 |
e.printStackTrace(); |
153 |
fail(); |
154 |
} |
155 |
try {
|
156 |
remove(SHPDataExplorer.DATASOURCE_NAME,file,parameters); |
157 |
} catch (ReadException e) {
|
158 |
// TODO Auto-generated catch block
|
159 |
e.printStackTrace(); |
160 |
} |
161 |
params=list(SHPDataExplorer.DATASOURCE_NAME,shpfile.getParent()); |
162 |
|
163 |
assertEquals(3, params.length);
|
164 |
persistenceTest(this.getParams(SHPDataExplorer.DATASOURCE_NAME, shpfile
|
165 |
.getParent())); |
166 |
} |
167 |
|
168 |
private void remove(String name, String file,DataStoreParameters parameters) throws ReadException { |
169 |
DataManager dsm=DefaultDataManager.getManager(); |
170 |
DataExplorerParameters dsp = dsm.createDataExplorerParameters( |
171 |
name); |
172 |
((FileExplorerParameters)dsp).setSource(new File(file).getParentFile()); |
173 |
FeatureExplorer src = (FeatureExplorer)dsm.createDataExplorer(dsp); |
174 |
src.remove(parameters); |
175 |
|
176 |
} |
177 |
|
178 |
private void add(String name, String file,FeatureType featureType) { |
179 |
DataManager dsm=DefaultDataManager.getManager(); |
180 |
DataExplorerParameters dsp=null;
|
181 |
try {
|
182 |
dsp = dsm.createDataExplorerParameters( |
183 |
name); |
184 |
} catch (InitializeException e2) {
|
185 |
e2.printStackTrace(); |
186 |
fail(); |
187 |
} |
188 |
|
189 |
((FileExplorerParameters)dsp).setSource(new File(file).getParentFile()); |
190 |
FeatureExplorer src; |
191 |
try {
|
192 |
src = (FeatureExplorer)dsm.createDataExplorer(dsp); |
193 |
} catch (InitializeException e1) {
|
194 |
e1.printStackTrace(); |
195 |
fail();return;
|
196 |
} |
197 |
|
198 |
NewFeatureStoreParameters ndsp; |
199 |
try {
|
200 |
ndsp = (NewFeatureStoreParameters) src |
201 |
.createNewDataStoreParameter(); |
202 |
} catch (InitializeException e1) {
|
203 |
e1.printStackTrace(); |
204 |
fail(); |
205 |
return;
|
206 |
} |
207 |
|
208 |
((FileStoreParameters)ndsp.getDataStoreParameters()).setFile(new File(file)); |
209 |
ndsp.setFeatureType(featureType); |
210 |
try {
|
211 |
src.add(ndsp); |
212 |
} catch (InitializeException e) {
|
213 |
// TODO Auto-generated catch block
|
214 |
e.printStackTrace(); |
215 |
fail();return;
|
216 |
} catch (WriteException e) {
|
217 |
// TODO Auto-generated catch block
|
218 |
e.printStackTrace(); |
219 |
fail();return;
|
220 |
} |
221 |
|
222 |
|
223 |
} |
224 |
private DataStore createDataStore(String name, String file,DataStoreParameters parameters){ |
225 |
DataManager dsm=DefaultDataManager.getManager(); |
226 |
DataExplorerParameters dsp = getParams(name, new File(file) |
227 |
.getParentFile().getAbsolutePath()); |
228 |
FeatureExplorer src; |
229 |
try {
|
230 |
src = (FeatureExplorer)dsm.createDataExplorer(dsp); |
231 |
} catch (InitializeException e1) {
|
232 |
fail();return null; |
233 |
} |
234 |
try {
|
235 |
return src.createDataStore(parameters);
|
236 |
} catch (OpenException e) {
|
237 |
// TODO Auto-generated catch block
|
238 |
e.printStackTrace(); |
239 |
} catch (WriteException e) {
|
240 |
// TODO Auto-generated catch block
|
241 |
e.printStackTrace(); |
242 |
} catch (InitializeException e) {
|
243 |
// TODO Auto-generated catch block
|
244 |
e.printStackTrace(); |
245 |
} |
246 |
return null; |
247 |
} |
248 |
private DataStoreParameters[] list(String name,String parent){ |
249 |
DataManager dsm = DefaultDataManager.getManager(); |
250 |
|
251 |
DataExplorerParameters dsp = getParams(name, parent); |
252 |
DataExplorer src; |
253 |
try {
|
254 |
src = dsm.createDataExplorer(dsp); |
255 |
} catch (InitializeException e1) {
|
256 |
e1.printStackTrace(); |
257 |
fail();return null; |
258 |
} |
259 |
|
260 |
DataStoreParameters[] params = null; |
261 |
try {
|
262 |
params = src.list(); |
263 |
} catch (ReadException e) {
|
264 |
e.printStackTrace(); |
265 |
fail(); |
266 |
} |
267 |
return params;
|
268 |
} |
269 |
|
270 |
protected void persistenceTest(DataExplorerParameters params1) { |
271 |
DataManager manager = DefaultDataManager.getManager(); |
272 |
|
273 |
XMLEntity params1XML; |
274 |
DataExplorerParameters params2 = null;
|
275 |
XMLEntity params2XML; |
276 |
|
277 |
DataExplorer explorer1 = null;
|
278 |
XMLEntity explorer1XML; |
279 |
DataExplorer explorer2 = null;
|
280 |
XMLEntity explorer2XML; |
281 |
|
282 |
params1XML = params1.getXMLEntity(); |
283 |
|
284 |
try {
|
285 |
params2 = manager.createDataExplorerParameters(params1XML); |
286 |
} catch (InitializeException e) {
|
287 |
e.printStackTrace(); |
288 |
fail(); |
289 |
} |
290 |
|
291 |
params2XML = params2.getXMLEntity(); |
292 |
|
293 |
assertEquals(params1XML.toString(), params2XML.toString()); |
294 |
|
295 |
try {
|
296 |
explorer1 = manager.createDataExplorer(params1); |
297 |
} catch (InitializeException e1) {
|
298 |
e1.printStackTrace(); |
299 |
fail(); |
300 |
} |
301 |
|
302 |
explorer1XML = explorer1.getXMLEntity(); |
303 |
|
304 |
try {
|
305 |
explorer2 = manager.createDataExplorer(explorer1XML); |
306 |
} catch (InitializeException e1) {
|
307 |
e1.printStackTrace(); |
308 |
fail(); |
309 |
} |
310 |
|
311 |
explorer2XML = explorer2.getXMLEntity(); |
312 |
|
313 |
assertEquals(explorer1XML.toString(), explorer2XML.toString()); |
314 |
|
315 |
|
316 |
DataStoreParameters list1[] = null; |
317 |
DataStoreParameters list2[] = null; |
318 |
|
319 |
try {
|
320 |
list1 = explorer1.list(); |
321 |
list2 = explorer2.list(); |
322 |
} catch (ReadException e) {
|
323 |
e.printStackTrace(); |
324 |
fail(); |
325 |
} |
326 |
|
327 |
assertEquals(list1.length, list2.length); |
328 |
|
329 |
for (int i=0;i< list1.length;i++){ |
330 |
assertEquals(list1[i].getXMLEntity().toString(), list2[i] |
331 |
.getXMLEntity().toString()); |
332 |
} |
333 |
|
334 |
try {
|
335 |
explorer1.dispose(); |
336 |
explorer2.dispose(); |
337 |
} catch (DataException e) {
|
338 |
e.printStackTrace(); |
339 |
fail(); |
340 |
} |
341 |
|
342 |
} |
343 |
|
344 |
protected DataExplorerParameters getParams(String name, String parent) { |
345 |
DataManager dsm = DefaultDataManager.getManager(); |
346 |
|
347 |
FileExplorerParameters dsp = null;
|
348 |
try {
|
349 |
dsp = (FileExplorerParameters) dsm |
350 |
.createDataExplorerParameters(name); |
351 |
} catch (InitializeException e2) {
|
352 |
e2.printStackTrace(); |
353 |
fail(); |
354 |
} |
355 |
dsp.setSource(new File(parent)); |
356 |
return dsp;
|
357 |
} |
358 |
} |