Statistics
| Revision:

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 @ 44767

History | View | Annotate | Download (16 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
                case DataTypes.DECIMAL:
111
                    assertEquals(
112
                            String.format("Field %s name mismatch", sourceAttr.getName()), 
113
                            sourceAttr.getName(), 
114
                            targetAttr.getName()
115
                    );
116
                    assertEquals(
117
                            String.format("Field %s type mismatch", sourceAttr.getName()), 
118
                            sourceAttr.getDataTypeName(), 
119
                            targetAttr.getDataTypeName()
120
                    );
121
                    assertEquals(
122
                            String.format("Field %s size mismatch", sourceAttr.getName()), 
123
                            sourceAttr.getSize(),
124
                            targetAttr.getSize()
125
                    );
126
                    assertEquals(
127
                            String.format("Field %s precision mismatch", sourceAttr.getName()), 
128
                            sourceAttr.getPrecision(),
129
                            targetAttr.getPrecision()
130
                    );
131
                    assertEquals(
132
                            String.format("Field %s scale mismatch", sourceAttr.getName()), 
133
                            sourceAttr.getScale(),
134
                            targetAttr.getScale()
135
                    );
136
                    break;
137
                case DataTypes.TIME:
138
                    assertEquals(
139
                            String.format("Field %s name mismatch", sourceAttr.getName()), 
140
                            sourceAttr.getName(), 
141
                            targetAttr.getName()
142
                    );
143
                    if( targetAttr.getType()==DataTypes.STRING ) {
144
                        // Cuando no hay fichero DAL puede leer un TIME como 
145
                        // un STRING de tama?o TIME_SIZE.
146
                        assertEquals(
147
                                String.format("Field %s size mismatch", sourceAttr.getName()), 
148
                                FieldFormatter.TIME_SIZE,
149
                                targetAttr.getSize()
150
                        );
151
                    } else if( targetAttr.getType()==DataTypes.TIME ) {
152
                        // Si hay fichero DAL el tipo pude ser correcto (TIME).
153
                        assertEquals(
154
                                String.format("Field %s size mismatch", sourceAttr.getName()), 
155
                                sourceAttr.getSize(),
156
                                targetAttr.getSize()
157
                        );
158
                    }
159
                    assertEquals(
160
                            String.format("Field %s precision mismatch", sourceAttr.getName()), 
161
                            sourceAttr.getPrecision(),
162
                            targetAttr.getPrecision()
163
                    );
164
                    assertEquals(
165
                            String.format("Field %s scale mismatch", sourceAttr.getName()), 
166
                            sourceAttr.getScale(),
167
                            targetAttr.getScale()
168
                    );
169
                    break;
170
                case DataTypes.TIMESTAMP:
171
                    assertEquals(
172
                            String.format("Field %s name mismatch", sourceAttr.getName()), 
173
                            sourceAttr.getName(), 
174
                            targetAttr.getName()
175
                    );
176
                    if( targetAttr.getType()==DataTypes.STRING ) {
177
                        // Cuando no hay fichero DAL puede leer un TIMESTAMP como 
178
                        // un STRING de tama?o TIMESTAMP_SIZE.
179
                        assertEquals(
180
                                String.format("Field %s size mismatch", sourceAttr.getName()), 
181
                                FieldFormatter.TIMESTAMP_SIZE,
182
                                targetAttr.getSize()
183
                        );
184
                    } else if( targetAttr.getType()==DataTypes.TIMESTAMP ) {
185
                        // Si hay fichero DAL el tipo pude ser correcto (TIMESTAMP).
186
                        assertEquals(
187
                                String.format("Field %s size mismatch", sourceAttr.getName()), 
188
                                sourceAttr.getSize(),
189
                                targetAttr.getSize()
190
                        );
191
                    }
192
                    assertEquals(
193
                            String.format("Field %s precision mismatch", sourceAttr.getName()), 
194
                            sourceAttr.getPrecision(),
195
                            targetAttr.getPrecision()
196
                    );
197
                    assertEquals(
198
                            String.format("Field %s scale mismatch", sourceAttr.getName()), 
199
                            sourceAttr.getScale(),
200
                            targetAttr.getScale()
201
                    );
202
                    break;
203
                case DataTypes.GEOMETRY:
204
                    if( targetAttr.getName().equalsIgnoreCase(GEOMETRY_ATTIBUTE_NAME) ) {
205
                        // Si no hay fichero DAL se le habra asignado el nombre por defecto.
206
                    } else {
207
                        assertEquals(
208
                                String.format("Field %s name mismatch", sourceAttr.getName()), 
209
                                sourceAttr.getName(), 
210
                                targetAttr.getName()
211
                        );
212
                    }
213
                    assertEquals(
214
                            String.format("Field %s type mismatch", sourceAttr.getName()), 
215
                            sourceAttr.getDataTypeName(), 
216
                            targetAttr.getDataTypeName()
217
                    );
218
                    assertEquals(
219
                            String.format("Field %s geometry type mismatch", sourceAttr.getName()), 
220
                            sourceAttr.getGeomType().getName(), 
221
                            targetAttr.getGeomType().getName()
222
                    );
223
                    assertEquals(
224
                            String.format("Field %s geometry SRS mismatch", sourceAttr.getName()), 
225
                            sourceAttr.getSRS().toString(), 
226
                            targetAttr.getSRS().toString()
227
                    );
228
                    assertEquals(
229
                            String.format("Field %s size mismatch", sourceAttr.getName()), 
230
                            sourceAttr.getSize(),
231
                            targetAttr.getSize()
232
                    );
233
                    assertEquals(
234
                            String.format("Field %s precision mismatch", sourceAttr.getName()), 
235
                            sourceAttr.getPrecision(),
236
                            targetAttr.getPrecision()
237
                    );
238
                    assertEquals(
239
                            String.format("Field %s scale mismatch", sourceAttr.getName()), 
240
                            sourceAttr.getScale(),
241
                            targetAttr.getScale()
242
                    );
243
                    break;
244
                default:
245
                    fail(
246
                        String.format("Field %s type %d (%s) not supported.", 
247
                                targetAttr.getName(),
248
                                targetAttr.getType(),
249
                                targetAttr.getDataTypeName()
250
                        )
251
                    );
252
            }
253
        }
254
    }
255
    
256
    protected void copyFrom(FeatureStore sourceStore, int mode) throws Exception {
257
        FeatureStore targetStore = openTargetStore1();
258
        targetStore.edit(mode);
259
        try {
260
            for (Feature sourceFeature : sourceStore.getFeatureSet()) {
261
                EditableFeature targetFeature = targetStore.createNewFeature(sourceFeature);
262
                targetStore.insert(targetFeature);
263
            }
264
        } finally {
265
            targetStore.finishEditing();
266
        }
267
    }
268
    
269
    protected void checkData(FeatureStore sourceStore) throws Exception {
270
        FeatureStore targetStore = openTargetStore1();
271

    
272
        List<Feature> sourceFeatures = sourceStore.getFeatures();
273
        List<Feature> targetFeatures = targetStore.getFeatures();
274
        assertEquals("Count features", sourceFeatures.size(), targetFeatures.size());
275
        for (int i = 0; i < targetFeatures.size(); i++) {
276
            Feature sourceFeature = sourceFeatures.get(i);
277
            Feature targetFeature = targetFeatures.get(i);
278
            for (FeatureAttributeDescriptor sourceAttr : sourceStore.getDefaultFeatureType()) {
279
                switch(sourceAttr.getType()) {
280
                    case DataTypes.TIMESTAMP:
281
                        assertEquals(
282
                                String.format("Feature %03d attribute %s", i, sourceAttr.getName()),
283
                                sourceFeature.get(sourceAttr.getName()),
284
                                targetFeature.getTimestamp(sourceAttr.getName())
285
                        );
286
                        break;
287
                    case DataTypes.TIME:
288
                        assertEquals(
289
                                String.format("Feature %03d attribute %s", i, sourceAttr.getName()),
290
                                sourceFeature.get(sourceAttr.getName()),
291
                                targetFeature.getTime(sourceAttr.getName())
292
                        );
293
                        break;
294
                    case DataTypes.GEOMETRY:
295
                        assertEquals(
296
                                String.format("Feature %03d attribute %s", i, sourceAttr.getName()),
297
                                sourceFeature.get(sourceAttr.getName()),
298
                                targetFeature.get(sourceAttr.getName())
299
                        );
300
                        break;
301
                    case DataTypes.STRING:
302
                        assertEquals(
303
                                String.format("Feature %03d attribute %s", i, sourceAttr.getName()),
304
                                StringUtils.defaultIfBlank(sourceFeature.getString(sourceAttr.getName()), null),
305
                                targetFeature.get(sourceAttr.getName())
306
                        );
307
                        break;
308
                    default:
309
                        Object sourceValue = sourceFeature.get(sourceAttr.getName());
310
                        Object targetValue = targetFeature.get(sourceAttr.getName());
311
                        if( sourceValue == null ) {
312
                            LOGGER.info("sourceValue is null");
313
                        }
314
                        assertEquals(
315
                                String.format("Feature %03d attribute %s", i, sourceAttr.getName()),
316
                                sourceValue,
317
                                targetValue
318
                        );
319
                }
320
            }
321
        }
322
    }
323
    
324
    
325
    public void testCreateWithDALFile() throws Exception {
326
        FeatureStore sourceStore = TestUtils.openSourceStore1();
327
        
328
        createFrom(sourceStore);        
329
        
330
        checkTypes(sourceStore.getDefaultFeatureType());
331
        copyFrom(sourceStore, FeatureStore.MODE_APPEND);
332
        checkData(sourceStore);
333
        
334
        createFrom(sourceStore);
335
        copyFrom(sourceStore, FeatureStore.MODE_FULLEDIT);
336
        checkData(sourceStore);
337

    
338
    }
339
    
340
    public void testCreateWithoutDALFile() throws Exception {
341
        FeatureStore sourceStore = TestUtils.openSourceStore1();
342
        
343
        createFrom(sourceStore);        
344
        TestUtils.removeDALFile(getTargetFilename());
345
        
346
        checkTypes(sourceStore.getDefaultFeatureType());
347
        copyFrom(sourceStore, FeatureStore.MODE_APPEND);
348
        TestUtils.removeDALFile(getTargetFilename());
349
        checkData(sourceStore);
350
        
351
        createFrom(sourceStore);
352
        TestUtils.removeDALFile(getTargetFilename());
353
        copyFrom(sourceStore, FeatureStore.MODE_FULLEDIT);
354
        TestUtils.removeDALFile(getTargetFilename());
355
        checkData(sourceStore);
356

    
357
    }
358
}