svn-gvsig-desktop / trunk / libraries / libDataSourceBaseDrivers / src-test / org / gvsig / data / datastores / vectorial / file / DataExplorerTest.java @ 20033
History | View | Annotate | Download (6.85 KB)
1 | 19844 | vcaballero | package org.gvsig.data.datastores.vectorial.file; |
---|---|---|---|
2 | 19533 | jmvivo | |
3 | import java.io.File; |
||
4 | |||
5 | import junit.framework.TestCase; |
||
6 | |||
7 | 19910 | vcaballero | import org.gvsig.data.DataManager; |
8 | import org.gvsig.data.IDataExplorer; |
||
9 | import org.gvsig.data.IDataExplorerParameters; |
||
10 | import org.gvsig.data.IDataStore; |
||
11 | 19533 | jmvivo | import org.gvsig.data.IDataStoreParameters; |
12 | 19910 | vcaballero | 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 | 19533 | jmvivo | import org.gvsig.data.exception.ReadException; |
21 | 19910 | vcaballero | import org.gvsig.data.vectorial.DefaultAttributeDescriptor; |
22 | import org.gvsig.data.vectorial.DefaultFeatureType; |
||
23 | import org.gvsig.data.vectorial.IDataFeatureExplorer; |
||
24 | import org.gvsig.data.vectorial.IFeatureAttributeDescriptor; |
||
25 | 19533 | jmvivo | import org.gvsig.data.vectorial.IFeatureCollection; |
26 | import org.gvsig.data.vectorial.IFeatureStore; |
||
27 | 19910 | vcaballero | import org.gvsig.data.vectorial.IFeatureType; |
28 | import org.gvsig.data.vectorial.INewDataStoreParameters; |
||
29 | 19533 | jmvivo | |
30 | 19910 | vcaballero | public class DataExplorerTest extends TestCase { |
31 | 19533 | jmvivo | private File dbffile = new File(DataStoreTest.class.getResource("data/prueba.dbf").getFile()); |
32 | private File shpfile = new File(DataStoreTest.class.getResource("data/prueba.shp").getFile()); |
||
33 | |||
34 | 19910 | vcaballero | public void testDBFExplorer(){ |
35 | 19844 | vcaballero | org.gvsig.data.datastores.vectorial.file.dbf.Register.selfRegister(); |
36 | 19910 | vcaballero | IDataStoreParameters[] params = null; |
37 | params=list(DBFDataExplorer.DATASOURCE_NAME,dbffile.getParent()); |
||
38 | 19533 | jmvivo | |
39 | 19940 | vcaballero | assertEquals(2, params.length);
|
40 | 19533 | jmvivo | |
41 | 19976 | jmvivo | String file=dbffile.getParent()+File.separator+"pruebaCrear.dbf"; |
42 | 19910 | vcaballero | DBFStoreParameters parameters=new DBFStoreParameters();
|
43 | parameters.setDBFFile(new File(file)); |
||
44 | 19533 | jmvivo | |
45 | 19910 | vcaballero | DefaultFeatureType dft=new DefaultFeatureType();
|
46 | DefaultAttributeDescriptor fad=new DefaultAttributeDescriptor();
|
||
47 | fad.setName("CAMPO1");
|
||
48 | fad.setType(IFeatureAttributeDescriptor.TYPE_STRING); |
||
49 | fad.setSize(10);
|
||
50 | dft.add(fad); |
||
51 | 19533 | jmvivo | |
52 | |||
53 | 19910 | vcaballero | add(DBFDataExplorer.DATASOURCE_NAME,file,parameters,dft); |
54 | |||
55 | params=list(DBFDataExplorer.DATASOURCE_NAME,dbffile.getParent()); |
||
56 | 19940 | vcaballero | assertEquals(3, params.length);
|
57 | 19910 | vcaballero | IFeatureStore fs=(IFeatureStore)createDataStore(DBFDataExplorer.DATASOURCE_NAME,file,parameters); |
58 | 19533 | jmvivo | try {
|
59 | fs.open(); |
||
60 | |||
61 | IFeatureCollection fc = (IFeatureCollection) fs.getDataCollection(); |
||
62 | |||
63 | 19910 | vcaballero | assertEquals(0, fc.size());
|
64 | 19533 | jmvivo | |
65 | 19976 | jmvivo | fc.dispose(); |
66 | |||
67 | 19533 | jmvivo | fs.close(); |
68 | 19940 | vcaballero | fs.dispose(); |
69 | 19533 | jmvivo | } catch (Exception e) { |
70 | e.printStackTrace(); |
||
71 | fail(); |
||
72 | } |
||
73 | 19910 | vcaballero | try {
|
74 | remove(DBFDataExplorer.DATASOURCE_NAME,file,parameters); |
||
75 | } catch (ReadException e) {
|
||
76 | // TODO Auto-generated catch block
|
||
77 | e.printStackTrace(); |
||
78 | } |
||
79 | params=list(DBFDataExplorer.DATASOURCE_NAME,dbffile.getParent()); |
||
80 | 19533 | jmvivo | |
81 | 19940 | vcaballero | assertEquals(2, params.length);
|
82 | 19533 | jmvivo | } |
83 | |||
84 | |||
85 | |||
86 | 19910 | vcaballero | public void testSHPExplorer(){ |
87 | org.gvsig.data.datastores.vectorial.file.shp.Register.selfRegister(); |
||
88 | 19533 | jmvivo | |
89 | IDataStoreParameters[] params = null; |
||
90 | 19910 | vcaballero | params=list(SHPDataExplorer.DATASOURCE_NAME,shpfile.getParent()); |
91 | |||
92 | 19940 | vcaballero | assertEquals(2, params.length);
|
93 | 19533 | jmvivo | |
94 | 19976 | jmvivo | String file=shpfile.getParent()+File.separator+"pruebaCrear.shp"; |
95 | 19910 | vcaballero | SHPStoreParameters parameters=new SHPStoreParameters();
|
96 | parameters.setSHPFile(new File(file)); |
||
97 | parameters.setDBFFile(SHP.getDbfFile(new File(file))); |
||
98 | parameters.setSHXFile(SHP.getShxFile(new File(file))); |
||
99 | 19533 | jmvivo | |
100 | 19910 | vcaballero | DefaultFeatureType dft=new DefaultFeatureType();
|
101 | DefaultAttributeDescriptor fad1=new DefaultAttributeDescriptor();
|
||
102 | fad1.setName("CAMPO1");
|
||
103 | fad1.setType(IFeatureAttributeDescriptor.TYPE_STRING); |
||
104 | fad1.setSize(10);
|
||
105 | DefaultAttributeDescriptor fad2=new DefaultAttributeDescriptor();
|
||
106 | fad2.setName("Geometry");
|
||
107 | fad2.setType(IFeatureAttributeDescriptor.TYPE_GEOMETRY); |
||
108 | dft.add(fad1); |
||
109 | dft.add(fad2); |
||
110 | |||
111 | |||
112 | add(SHPDataExplorer.DATASOURCE_NAME,file,parameters,dft); |
||
113 | |||
114 | params=list(SHPDataExplorer.DATASOURCE_NAME,shpfile.getParent()); |
||
115 | 19940 | vcaballero | assertEquals(3, params.length);
|
116 | 19910 | vcaballero | IFeatureStore fs=(IFeatureStore)createDataStore(SHPDataExplorer.DATASOURCE_NAME,file,parameters); |
117 | 19533 | jmvivo | try {
|
118 | fs.open(); |
||
119 | |||
120 | IFeatureCollection fc = (IFeatureCollection) fs.getDataCollection(); |
||
121 | |||
122 | 19910 | vcaballero | assertEquals(0, fc.size());
|
123 | 19533 | jmvivo | |
124 | fs.close(); |
||
125 | 19940 | vcaballero | fs.dispose(); |
126 | 19533 | jmvivo | } catch (Exception e) { |
127 | e.printStackTrace(); |
||
128 | fail(); |
||
129 | } |
||
130 | 19910 | vcaballero | try {
|
131 | remove(SHPDataExplorer.DATASOURCE_NAME,file,parameters); |
||
132 | } catch (ReadException e) {
|
||
133 | // TODO Auto-generated catch block
|
||
134 | e.printStackTrace(); |
||
135 | } |
||
136 | params=list(SHPDataExplorer.DATASOURCE_NAME,shpfile.getParent()); |
||
137 | 19533 | jmvivo | |
138 | 19940 | vcaballero | assertEquals(2, params.length);
|
139 | 19910 | vcaballero | } |
140 | 19533 | jmvivo | |
141 | 19910 | vcaballero | private void remove(String name, String file,IDataStoreParameters parameters) throws ReadException { |
142 | DataManager dsm=DataManager.getManager(); |
||
143 | IDataExplorerParameters dsp = dsm.createDataExplorerParameters( |
||
144 | name); |
||
145 | ((DataExplorerFileParameters)dsp).setSource(new File(file).getParentFile()); |
||
146 | IDataFeatureExplorer src = (IDataFeatureExplorer)dsm.createDataExplorer(dsp); |
||
147 | src.remove(parameters); |
||
148 | |||
149 | 19533 | jmvivo | } |
150 | 19910 | vcaballero | |
151 | private void add(String name, String file,IDataStoreParameters parameters,IFeatureType featureType) { |
||
152 | DataManager dsm=DataManager.getManager(); |
||
153 | IDataExplorerParameters dsp = dsm.createDataExplorerParameters( |
||
154 | name); |
||
155 | ((DataExplorerFileParameters)dsp).setSource(new File(file).getParentFile()); |
||
156 | IDataFeatureExplorer src = (IDataFeatureExplorer)dsm.createDataExplorer(dsp); |
||
157 | |||
158 | INewDataStoreParameters ndsp=src.createNewDataStoreParameter(parameters); |
||
159 | |||
160 | ndsp.setFeatureType(featureType); |
||
161 | try {
|
||
162 | src.add(ndsp); |
||
163 | } catch (OpenException e) {
|
||
164 | // TODO Auto-generated catch block
|
||
165 | e.printStackTrace(); |
||
166 | } catch (InitializeWriterException e) {
|
||
167 | // TODO Auto-generated catch block
|
||
168 | e.printStackTrace(); |
||
169 | } |
||
170 | |||
171 | |||
172 | } |
||
173 | private IDataStore createDataStore(String name, String file,IDataStoreParameters parameters){ |
||
174 | DataManager dsm=DataManager.getManager(); |
||
175 | IDataExplorerParameters dsp = dsm.createDataExplorerParameters( |
||
176 | name); |
||
177 | ((DataExplorerFileParameters)dsp).setSource(new File(file).getParentFile()); |
||
178 | IDataFeatureExplorer src = (IDataFeatureExplorer)dsm.createDataExplorer(dsp); |
||
179 | try {
|
||
180 | return src.createDataStore(parameters);
|
||
181 | } catch (OpenException e) {
|
||
182 | // TODO Auto-generated catch block
|
||
183 | e.printStackTrace(); |
||
184 | } catch (InitializeWriterException e) {
|
||
185 | // TODO Auto-generated catch block
|
||
186 | e.printStackTrace(); |
||
187 | } catch (InitializeException e) {
|
||
188 | // TODO Auto-generated catch block
|
||
189 | e.printStackTrace(); |
||
190 | } |
||
191 | return null; |
||
192 | } |
||
193 | private IDataStoreParameters[] list(String name,String parent){ |
||
194 | DataManager dsm=DataManager.getManager(); |
||
195 | |||
196 | IDataExplorerParameters dsp = dsm.createDataExplorerParameters( |
||
197 | name); |
||
198 | ((DataExplorerFileParameters)dsp).setSource(new File(parent)); |
||
199 | |||
200 | IDataExplorer src = dsm.createDataExplorer(dsp); |
||
201 | |||
202 | IDataStoreParameters[] params = null; |
||
203 | try {
|
||
204 | params = src.list(); |
||
205 | } catch (ReadException e) {
|
||
206 | e.printStackTrace(); |
||
207 | fail(); |
||
208 | } |
||
209 | return params;
|
||
210 | } |
||
211 | 19533 | jmvivo | } |