svn-gvsig-desktop / trunk / libraries / libFMap_dataFile / src-test / org / gvsig / data / datastores / vectorial / file / DataExplorerTest.java @ 20744
History | View | Annotate | Download (7.58 KB)
1 |
package org.gvsig.data.datastores.vectorial.file; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
|
5 |
import junit.framework.TestCase; |
6 |
|
7 |
import org.gvsig.data.DataManager; |
8 |
import org.gvsig.data.IDataExplorer; |
9 |
import org.gvsig.data.IDataExplorerParameters; |
10 |
import org.gvsig.data.IDataStore; |
11 |
import org.gvsig.data.IDataStoreParameters; |
12 |
import org.gvsig.data.datastores.vectorial.file.dbf.DBFDataExplorer; |
13 |
import org.gvsig.data.datastores.vectorial.file.dbf.DBFStoreParameters; |
14 |
import org.gvsig.data.datastores.vectorial.file.shp.SHPDataExplorer; |
15 |
import org.gvsig.data.datastores.vectorial.file.shp.SHPStoreParameters; |
16 |
import org.gvsig.data.datastores.vectorial.file.shp.utils.SHP; |
17 |
import org.gvsig.data.exception.InitializeException; |
18 |
import org.gvsig.data.exception.InitializeWriterException; |
19 |
import org.gvsig.data.exception.OpenException; |
20 |
import org.gvsig.data.exception.ReadException; |
21 |
import org.gvsig.data.exception.WriteException; |
22 |
import org.gvsig.data.vectorial.AttributeDescriptor; |
23 |
import org.gvsig.data.vectorial.FeatureType; |
24 |
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor; |
25 |
import org.gvsig.data.vectorial.IFeatureCollection; |
26 |
import org.gvsig.data.vectorial.IFeatureStore; |
27 |
import org.gvsig.data.vectorial.IFeatureStoreExplorer; |
28 |
import org.gvsig.data.vectorial.IFeatureType; |
29 |
import org.gvsig.data.vectorial.INewFeatureStoreParameters; |
30 |
import org.gvsig.data.vectorial.IsNotAttributeSettingException; |
31 |
|
32 |
public class DataExplorerTest extends TestCase { |
33 |
private File dbffile = new File(DataStoreTest.class.getResource("data/prueba.dbf").getFile()); |
34 |
private File shpfile = new File(DataStoreTest.class.getResource("data/prueba.shp").getFile()); |
35 |
|
36 |
public void testDBFExplorer(){ |
37 |
org.gvsig.data.datastores.vectorial.file.dbf.Register.selfRegister(); |
38 |
IDataStoreParameters[] params = null; |
39 |
params=list(DBFDataExplorer.DATASOURCE_NAME,dbffile.getParent()); |
40 |
|
41 |
assertEquals(2, params.length);
|
42 |
|
43 |
String file=dbffile.getParent()+File.separator+"pruebaCrear.dbf"; |
44 |
DBFStoreParameters parameters=new DBFStoreParameters();
|
45 |
parameters.setFile(new File(file)); |
46 |
|
47 |
FeatureType dft=new FeatureType();
|
48 |
AttributeDescriptor fad=new AttributeDescriptor();
|
49 |
try {
|
50 |
fad.loading(); |
51 |
fad.setName("CAMPO1");
|
52 |
fad.setType(IFeatureAttributeDescriptor.TYPE_STRING); |
53 |
fad.setSize(10);
|
54 |
fad.stopLoading(); |
55 |
} catch (IsNotAttributeSettingException e1) {
|
56 |
e1.printStackTrace(); |
57 |
} |
58 |
dft.add(fad); |
59 |
|
60 |
|
61 |
add(DBFDataExplorer.DATASOURCE_NAME,file,dft); |
62 |
|
63 |
params=list(DBFDataExplorer.DATASOURCE_NAME,dbffile.getParent()); |
64 |
assertEquals(3, params.length);
|
65 |
IFeatureStore fs=(IFeatureStore)createDataStore(DBFDataExplorer.DATASOURCE_NAME,file,parameters); |
66 |
try {
|
67 |
fs.open(); |
68 |
|
69 |
IFeatureCollection fc = (IFeatureCollection) fs.getDataCollection(); |
70 |
|
71 |
assertEquals(0, fc.size());
|
72 |
|
73 |
fc.dispose(); |
74 |
|
75 |
fs.close(); |
76 |
fs.dispose(); |
77 |
} catch (Exception e) { |
78 |
e.printStackTrace(); |
79 |
fail(); |
80 |
} |
81 |
try {
|
82 |
remove(DBFDataExplorer.DATASOURCE_NAME,file,parameters); |
83 |
} catch (ReadException e) {
|
84 |
// TODO Auto-generated catch block
|
85 |
e.printStackTrace(); |
86 |
} |
87 |
params=list(DBFDataExplorer.DATASOURCE_NAME,dbffile.getParent()); |
88 |
|
89 |
assertEquals(2, params.length);
|
90 |
} |
91 |
|
92 |
|
93 |
|
94 |
public void testSHPExplorer(){ |
95 |
org.gvsig.data.datastores.vectorial.file.shp.Register.selfRegister(); |
96 |
|
97 |
IDataStoreParameters[] params = null; |
98 |
params=list(SHPDataExplorer.DATASOURCE_NAME,shpfile.getParent()); |
99 |
|
100 |
assertEquals(2, params.length);
|
101 |
|
102 |
String file=shpfile.getParent()+File.separator+"pruebaCrear.shp"; |
103 |
SHPStoreParameters parameters=new SHPStoreParameters();
|
104 |
parameters.setFile(new File(file)); |
105 |
// parameters.setDBFFile(SHP.getDbfFile(new File(file)));
|
106 |
// parameters.setSHXFile(SHP.getShxFile(new File(file)));
|
107 |
|
108 |
FeatureType dft=new FeatureType();
|
109 |
AttributeDescriptor fad1=new AttributeDescriptor();
|
110 |
try{
|
111 |
fad1.loading(); |
112 |
fad1.setName("CAMPO1");
|
113 |
fad1.setType(IFeatureAttributeDescriptor.TYPE_STRING); |
114 |
fad1.setSize(10);
|
115 |
fad1.stopLoading(); |
116 |
}catch (IsNotAttributeSettingException e) {
|
117 |
e.printStackTrace(); |
118 |
} |
119 |
AttributeDescriptor fad2=new AttributeDescriptor();
|
120 |
try{
|
121 |
fad2.loading(); |
122 |
fad2.setName("Geometry");
|
123 |
fad2.setType(IFeatureAttributeDescriptor.TYPE_GEOMETRY); |
124 |
fad2.stopLoading(); |
125 |
}catch (IsNotAttributeSettingException e) {
|
126 |
e.printStackTrace(); |
127 |
} |
128 |
dft.add(fad1); |
129 |
dft.add(fad2); |
130 |
|
131 |
|
132 |
add(SHPDataExplorer.DATASOURCE_NAME,file,dft); |
133 |
|
134 |
params=list(SHPDataExplorer.DATASOURCE_NAME,shpfile.getParent()); |
135 |
assertEquals(3, params.length);
|
136 |
IFeatureStore fs=(IFeatureStore)createDataStore(SHPDataExplorer.DATASOURCE_NAME,file,parameters); |
137 |
try {
|
138 |
fs.open(); |
139 |
|
140 |
IFeatureCollection fc = (IFeatureCollection) fs.getDataCollection(); |
141 |
|
142 |
assertEquals(0, fc.size());
|
143 |
|
144 |
fs.close(); |
145 |
fs.dispose(); |
146 |
} catch (Exception e) { |
147 |
e.printStackTrace(); |
148 |
fail(); |
149 |
} |
150 |
try {
|
151 |
remove(SHPDataExplorer.DATASOURCE_NAME,file,parameters); |
152 |
} catch (ReadException e) {
|
153 |
// TODO Auto-generated catch block
|
154 |
e.printStackTrace(); |
155 |
} |
156 |
params=list(SHPDataExplorer.DATASOURCE_NAME,shpfile.getParent()); |
157 |
|
158 |
assertEquals(2, params.length);
|
159 |
} |
160 |
|
161 |
private void remove(String name, String file,IDataStoreParameters parameters) throws ReadException { |
162 |
DataManager dsm=DataManager.getManager(); |
163 |
IDataExplorerParameters dsp = dsm.createDataExplorerParameters( |
164 |
name); |
165 |
((FileExplorerParameters)dsp).setSource(new File(file).getParentFile()); |
166 |
IFeatureStoreExplorer src = (IFeatureStoreExplorer)dsm.createDataExplorer(dsp); |
167 |
src.remove(parameters); |
168 |
|
169 |
} |
170 |
|
171 |
private void add(String name, String file,IFeatureType featureType) { |
172 |
DataManager dsm=DataManager.getManager(); |
173 |
IDataExplorerParameters dsp = dsm.createDataExplorerParameters( |
174 |
name); |
175 |
|
176 |
((FileExplorerParameters)dsp).setSource(new File(file).getParentFile()); |
177 |
IFeatureStoreExplorer src; |
178 |
try {
|
179 |
src = (IFeatureStoreExplorer)dsm.createDataExplorer(dsp); |
180 |
} catch (InitializeException e1) {
|
181 |
e1.printStackTrace(); |
182 |
fail();return;
|
183 |
} |
184 |
|
185 |
INewFeatureStoreParameters ndsp=(INewFeatureStoreParameters)src.createNewDataStoreParameter(); |
186 |
|
187 |
((IFileStoreParameters)ndsp.getDataStoreParameters()).setFile(new File(file)); |
188 |
ndsp.setFeatureType(featureType); |
189 |
try {
|
190 |
src.add(ndsp); |
191 |
} catch (InitializeException e) {
|
192 |
// TODO Auto-generated catch block
|
193 |
e.printStackTrace(); |
194 |
fail();return;
|
195 |
} catch (WriteException e) {
|
196 |
// TODO Auto-generated catch block
|
197 |
e.printStackTrace(); |
198 |
fail();return;
|
199 |
} |
200 |
|
201 |
|
202 |
} |
203 |
private IDataStore createDataStore(String name, String file,IDataStoreParameters parameters){ |
204 |
DataManager dsm=DataManager.getManager(); |
205 |
IDataExplorerParameters dsp = dsm.createDataExplorerParameters( |
206 |
name); |
207 |
((FileExplorerParameters)dsp).setSource(new File(file).getParentFile()); |
208 |
IFeatureStoreExplorer src; |
209 |
try {
|
210 |
src = (IFeatureStoreExplorer)dsm.createDataExplorer(dsp); |
211 |
} catch (InitializeException e1) {
|
212 |
fail();return null; |
213 |
} |
214 |
try {
|
215 |
return src.createDataStore(parameters);
|
216 |
} catch (OpenException e) {
|
217 |
// TODO Auto-generated catch block
|
218 |
e.printStackTrace(); |
219 |
} catch (WriteException e) {
|
220 |
// TODO Auto-generated catch block
|
221 |
e.printStackTrace(); |
222 |
} catch (InitializeException e) {
|
223 |
// TODO Auto-generated catch block
|
224 |
e.printStackTrace(); |
225 |
} |
226 |
return null; |
227 |
} |
228 |
private IDataStoreParameters[] list(String name,String parent){ |
229 |
DataManager dsm=DataManager.getManager(); |
230 |
|
231 |
IDataExplorerParameters dsp = dsm.createDataExplorerParameters( |
232 |
name); |
233 |
((FileExplorerParameters)dsp).setSource(new File(parent)); |
234 |
|
235 |
IDataExplorer src; |
236 |
try {
|
237 |
src = dsm.createDataExplorer(dsp); |
238 |
} catch (InitializeException e1) {
|
239 |
e1.printStackTrace(); |
240 |
fail();return null; |
241 |
} |
242 |
|
243 |
IDataStoreParameters[] params = null; |
244 |
try {
|
245 |
params = src.list(); |
246 |
} catch (ReadException e) {
|
247 |
e.printStackTrace(); |
248 |
fail(); |
249 |
} |
250 |
return params;
|
251 |
} |
252 |
} |