svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.shp / src / test / java / org / gvsig / fmap / dal / store / shp / TestCreate.java @ 44669
History | View | Annotate | Download (16.1 KB)
1 |
package org.gvsig.fmap.dal.store.shp; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.util.Date; |
5 |
import java.util.List; |
6 |
import junit.framework.TestCase; |
7 |
import static junit.framework.TestCase.assertEquals; |
8 |
import org.apache.commons.lang3.StringUtils; |
9 |
import org.gvsig.fmap.dal.DALLocator; |
10 |
import org.gvsig.fmap.dal.DataTypes; |
11 |
import org.gvsig.fmap.dal.DataManager; |
12 |
import org.gvsig.fmap.dal.DataParameters; |
13 |
import org.gvsig.fmap.dal.DataServerExplorer; |
14 |
import org.gvsig.fmap.dal.DataStore; |
15 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
16 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
17 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
18 |
import org.gvsig.fmap.dal.feature.Feature; |
19 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
20 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
21 |
import org.gvsig.fmap.dal.feature.FeatureType; |
22 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
23 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
24 |
import org.gvsig.fmap.dal.store.dbf.utils.FieldFormatter; |
25 |
import static org.gvsig.fmap.dal.store.shp.SHPStoreProvider.GEOMETRY_ATTIBUTE_NAME; |
26 |
import org.gvsig.fmap.geom.Geometry; |
27 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
28 |
import org.slf4j.Logger; |
29 |
import org.slf4j.LoggerFactory; |
30 |
|
31 |
public class TestCreate extends TestCase { |
32 |
private static final Logger LOGGER = LoggerFactory.getLogger(TestCreate.class); |
33 |
|
34 |
public TestCreate(String testName) { |
35 |
super(testName);
|
36 |
} |
37 |
|
38 |
@Override
|
39 |
protected void setUp() throws Exception { |
40 |
super.setUp();
|
41 |
new DefaultLibrariesInitializer().fullInitialize();
|
42 |
} |
43 |
|
44 |
@Override
|
45 |
protected void tearDown() throws Exception { |
46 |
super.tearDown();
|
47 |
} |
48 |
|
49 |
// TODO add test methods here. The name must begin with 'test'. For example:
|
50 |
// public void testHello() {}
|
51 |
|
52 |
protected String getProviderName() { |
53 |
return DataStore.SHAPE_PROVIDER_NAME;
|
54 |
} |
55 |
|
56 |
protected String getTargetFilename() { |
57 |
return "testCreateTarget1.shp"; |
58 |
} |
59 |
|
60 |
protected FeatureStore openTargetStore1() throws Exception { |
61 |
DataManager dataManager = DALLocator.getDataManager(); |
62 |
File f = TestUtils.getResource(getTargetFilename());
|
63 |
FeatureStore store; |
64 |
try {
|
65 |
store = (FeatureStore) dataManager.openStore( |
66 |
getProviderName(), |
67 |
"shpFile=",f,
|
68 |
DataParameters.CRS_PARAMTER_NAME, "EPSG:4326"
|
69 |
); |
70 |
} catch(ValidateDataParametersException ex) {
|
71 |
LOGGER.warn(ex.getLocalizedMessageStack()); |
72 |
throw ex;
|
73 |
} |
74 |
return store;
|
75 |
} |
76 |
|
77 |
protected void createFrom(FeatureStore sourceStore) throws Exception { |
78 |
DataServerExplorer explorer = TestUtils.openServerExplorer(); |
79 |
NewFeatureStoreParameters params = (NewFeatureStoreParameters) explorer.getAddParameters( |
80 |
getProviderName() |
81 |
); |
82 |
((FilesystemStoreParameters)params).setFile(TestUtils.getResource(getTargetFilename())); |
83 |
params.setDynValue(DataParameters.CRS_PARAMTER_NAME, "EPSG:4326");
|
84 |
params.setDynValue("GeometryType", Geometry.TYPES.POINT);
|
85 |
EditableFeatureType ft = params.getDefaultFeatureType(); |
86 |
ft.addAll(sourceStore.getDefaultFeatureType()); |
87 |
explorer.add(getProviderName(), params, true);
|
88 |
} |
89 |
|
90 |
protected void checkTypes(FeatureType sourceFeatureType) throws Exception { |
91 |
// DataType STRING_TYPE = ToolsLocator.getDataTypesManager().get(DataTypes.STRING);
|
92 |
// DataType INT_TYPE = ToolsLocator.getDataTypesManager().get(DataTypes.INT);
|
93 |
|
94 |
FeatureStore targetStore = openTargetStore1(); |
95 |
FeatureType targetFeatureType = targetStore.getDefaultFeatureType(); |
96 |
|
97 |
assertEquals("Feature type size",sourceFeatureType.size(), targetFeatureType.size());
|
98 |
for (int i = 0; i < sourceFeatureType.size(); i++) { |
99 |
FeatureAttributeDescriptor sourceAttr = sourceFeatureType.get(i); |
100 |
FeatureAttributeDescriptor targetAttr = targetFeatureType.get(i); |
101 |
switch(sourceAttr.getType()) {
|
102 |
case DataTypes.BYTE:
|
103 |
case DataTypes.INT:
|
104 |
case DataTypes.LONG:
|
105 |
case DataTypes.BOOLEAN:
|
106 |
case DataTypes.DATE:
|
107 |
case DataTypes.STRING:
|
108 |
case DataTypes.DOUBLE:
|
109 |
case DataTypes.FLOAT:
|
110 |
assertEquals( |
111 |
String.format("Field %s name mismatch", sourceAttr.getName()), |
112 |
sourceAttr.getName(), |
113 |
targetAttr.getName() |
114 |
); |
115 |
assertEquals( |
116 |
String.format("Field %s type mismatch", sourceAttr.getName()), |
117 |
sourceAttr.getDataTypeName(), |
118 |
targetAttr.getDataTypeName() |
119 |
); |
120 |
assertEquals( |
121 |
String.format("Field %s size mismatch", sourceAttr.getName()), |
122 |
sourceAttr.getSize(), |
123 |
targetAttr.getSize() |
124 |
); |
125 |
assertEquals( |
126 |
String.format("Field %s precision mismatch", sourceAttr.getName()), |
127 |
sourceAttr.getPrecision(), |
128 |
targetAttr.getPrecision() |
129 |
); |
130 |
assertEquals( |
131 |
String.format("Field %s scale mismatch", sourceAttr.getName()), |
132 |
sourceAttr.getScale(), |
133 |
targetAttr.getScale() |
134 |
); |
135 |
break;
|
136 |
case DataTypes.TIME:
|
137 |
assertEquals( |
138 |
String.format("Field %s name mismatch", sourceAttr.getName()), |
139 |
sourceAttr.getName(), |
140 |
targetAttr.getName() |
141 |
); |
142 |
if( targetAttr.getType()==DataTypes.STRING ) {
|
143 |
// Cuando no hay fichero DAL puede leer un TIME como
|
144 |
// un STRING de tama?o TIME_SIZE.
|
145 |
assertEquals( |
146 |
String.format("Field %s size mismatch", sourceAttr.getName()), |
147 |
FieldFormatter.TIME_SIZE, |
148 |
targetAttr.getSize() |
149 |
); |
150 |
} else if( targetAttr.getType()==DataTypes.TIME ) { |
151 |
// Si hay fichero DAL el tipo pude ser correcto (TIME).
|
152 |
assertEquals( |
153 |
String.format("Field %s size mismatch", sourceAttr.getName()), |
154 |
sourceAttr.getSize(), |
155 |
targetAttr.getSize() |
156 |
); |
157 |
} |
158 |
assertEquals( |
159 |
String.format("Field %s precision mismatch", sourceAttr.getName()), |
160 |
sourceAttr.getPrecision(), |
161 |
targetAttr.getPrecision() |
162 |
); |
163 |
assertEquals( |
164 |
String.format("Field %s scale mismatch", sourceAttr.getName()), |
165 |
sourceAttr.getScale(), |
166 |
targetAttr.getScale() |
167 |
); |
168 |
break;
|
169 |
case DataTypes.TIMESTAMP:
|
170 |
assertEquals( |
171 |
String.format("Field %s name mismatch", sourceAttr.getName()), |
172 |
sourceAttr.getName(), |
173 |
targetAttr.getName() |
174 |
); |
175 |
if( targetAttr.getType()==DataTypes.STRING ) {
|
176 |
// Cuando no hay fichero DAL puede leer un TIMESTAMP como
|
177 |
// un STRING de tama?o TIMESTAMP_SIZE.
|
178 |
assertEquals( |
179 |
String.format("Field %s size mismatch", sourceAttr.getName()), |
180 |
FieldFormatter.TIMESTAMP_SIZE, |
181 |
targetAttr.getSize() |
182 |
); |
183 |
} else if( targetAttr.getType()==DataTypes.TIMESTAMP ) { |
184 |
// Si hay fichero DAL el tipo pude ser correcto (TIMESTAMP).
|
185 |
assertEquals( |
186 |
String.format("Field %s size mismatch", sourceAttr.getName()), |
187 |
sourceAttr.getSize(), |
188 |
targetAttr.getSize() |
189 |
); |
190 |
} |
191 |
assertEquals( |
192 |
String.format("Field %s precision mismatch", sourceAttr.getName()), |
193 |
sourceAttr.getPrecision(), |
194 |
targetAttr.getPrecision() |
195 |
); |
196 |
assertEquals( |
197 |
String.format("Field %s scale mismatch", sourceAttr.getName()), |
198 |
sourceAttr.getScale(), |
199 |
targetAttr.getScale() |
200 |
); |
201 |
break;
|
202 |
case DataTypes.GEOMETRY:
|
203 |
if( targetAttr.getName().equalsIgnoreCase(GEOMETRY_ATTIBUTE_NAME) ) {
|
204 |
// Si no hay fichero DAL se le habra asignado el nombre por defecto.
|
205 |
} else {
|
206 |
assertEquals( |
207 |
String.format("Field %s name mismatch", sourceAttr.getName()), |
208 |
sourceAttr.getName(), |
209 |
targetAttr.getName() |
210 |
); |
211 |
} |
212 |
assertEquals( |
213 |
String.format("Field %s type mismatch", sourceAttr.getName()), |
214 |
sourceAttr.getDataTypeName(), |
215 |
targetAttr.getDataTypeName() |
216 |
); |
217 |
assertEquals( |
218 |
String.format("Field %s geometry type mismatch", sourceAttr.getName()), |
219 |
sourceAttr.getGeomType().getName(), |
220 |
targetAttr.getGeomType().getName() |
221 |
); |
222 |
assertEquals( |
223 |
String.format("Field %s geometry SRS mismatch", sourceAttr.getName()), |
224 |
sourceAttr.getSRS().toString(), |
225 |
targetAttr.getSRS().toString() |
226 |
); |
227 |
assertEquals( |
228 |
String.format("Field %s size mismatch", sourceAttr.getName()), |
229 |
sourceAttr.getSize(), |
230 |
targetAttr.getSize() |
231 |
); |
232 |
assertEquals( |
233 |
String.format("Field %s precision mismatch", sourceAttr.getName()), |
234 |
sourceAttr.getPrecision(), |
235 |
targetAttr.getPrecision() |
236 |
); |
237 |
assertEquals( |
238 |
String.format("Field %s scale mismatch", sourceAttr.getName()), |
239 |
sourceAttr.getScale(), |
240 |
targetAttr.getScale() |
241 |
); |
242 |
break;
|
243 |
default:
|
244 |
fail( |
245 |
String.format("Field %s type %d (%s) not supported.", |
246 |
targetAttr.getName(), |
247 |
targetAttr.getType(), |
248 |
targetAttr.getDataTypeName() |
249 |
) |
250 |
); |
251 |
} |
252 |
} |
253 |
} |
254 |
|
255 |
protected void copyFrom(FeatureStore sourceStore, int mode) throws Exception { |
256 |
FeatureStore targetStore = openTargetStore1(); |
257 |
targetStore.edit(mode); |
258 |
try {
|
259 |
for (Feature sourceFeature : sourceStore.getFeatureSet()) {
|
260 |
EditableFeature targetFeature = targetStore.createNewFeature(sourceFeature); |
261 |
targetStore.insert(targetFeature); |
262 |
} |
263 |
} finally {
|
264 |
targetStore.finishEditing(); |
265 |
} |
266 |
} |
267 |
|
268 |
protected void checkData(FeatureStore sourceStore) throws Exception { |
269 |
FeatureStore targetStore = openTargetStore1(); |
270 |
|
271 |
List<Feature> sourceFeatures = sourceStore.getFeatures();
|
272 |
List<Feature> targetFeatures = targetStore.getFeatures();
|
273 |
assertEquals("Count features", sourceFeatures.size(), targetFeatures.size());
|
274 |
for (int i = 0; i < targetFeatures.size(); i++) { |
275 |
Feature sourceFeature = sourceFeatures.get(i); |
276 |
Feature targetFeature = targetFeatures.get(i); |
277 |
for (FeatureAttributeDescriptor sourceAttr : sourceStore.getDefaultFeatureType()) {
|
278 |
switch(sourceAttr.getType()) {
|
279 |
case DataTypes.TIMESTAMP:
|
280 |
Date sourceTimestamp = sourceFeature.getDate(sourceAttr.getName());
|
281 |
Date targetTimestamp = targetFeature.getDate(sourceAttr.getName());
|
282 |
assertEquals( |
283 |
String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
284 |
sourceTimestamp, |
285 |
targetTimestamp |
286 |
); |
287 |
break;
|
288 |
case DataTypes.TIME:
|
289 |
assertEquals( |
290 |
String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
291 |
sourceFeature.getDate(sourceAttr.getName()), |
292 |
targetFeature.getDate(sourceAttr.getName()) |
293 |
); |
294 |
break;
|
295 |
case DataTypes.GEOMETRY:
|
296 |
assertEquals( |
297 |
String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
298 |
sourceFeature.get(sourceAttr.getName()), |
299 |
targetFeature.get(sourceAttr.getName()) |
300 |
); |
301 |
break;
|
302 |
case DataTypes.STRING:
|
303 |
assertEquals( |
304 |
String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
305 |
StringUtils.defaultIfBlank(sourceFeature.getString(sourceAttr.getName()), null),
|
306 |
targetFeature.get(sourceAttr.getName()) |
307 |
); |
308 |
break;
|
309 |
default:
|
310 |
Object sourceValue = sourceFeature.get(sourceAttr.getName());
|
311 |
Object targetValue = targetFeature.get(sourceAttr.getName());
|
312 |
if( sourceValue == null ) { |
313 |
LOGGER.info("sourceValue is null");
|
314 |
} |
315 |
assertEquals( |
316 |
String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
317 |
sourceValue, |
318 |
targetValue |
319 |
); |
320 |
} |
321 |
} |
322 |
} |
323 |
} |
324 |
|
325 |
|
326 |
public void testCreateWithDALFile() throws Exception { |
327 |
FeatureStore sourceStore = TestUtils.openSourceStore1(); |
328 |
|
329 |
createFrom(sourceStore); |
330 |
|
331 |
checkTypes(sourceStore.getDefaultFeatureType()); |
332 |
copyFrom(sourceStore, FeatureStore.MODE_APPEND); |
333 |
checkData(sourceStore); |
334 |
|
335 |
createFrom(sourceStore); |
336 |
copyFrom(sourceStore, FeatureStore.MODE_FULLEDIT); |
337 |
checkData(sourceStore); |
338 |
|
339 |
} |
340 |
|
341 |
public void testCreateWithoutDALFile() throws Exception { |
342 |
FeatureStore sourceStore = TestUtils.openSourceStore1(); |
343 |
|
344 |
createFrom(sourceStore); |
345 |
TestUtils.removeDALFile(getTargetFilename()); |
346 |
|
347 |
checkTypes(sourceStore.getDefaultFeatureType()); |
348 |
copyFrom(sourceStore, FeatureStore.MODE_APPEND); |
349 |
TestUtils.removeDALFile(getTargetFilename()); |
350 |
checkData(sourceStore); |
351 |
|
352 |
createFrom(sourceStore); |
353 |
TestUtils.removeDALFile(getTargetFilename()); |
354 |
copyFrom(sourceStore, FeatureStore.MODE_FULLEDIT); |
355 |
TestUtils.removeDALFile(getTargetFilename()); |
356 |
checkData(sourceStore); |
357 |
|
358 |
} |
359 |
} |