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.dbf / src / test / java / org / gvsig / fmap / dal / store / dbf / TestCreate.java @ 44844

History | View | Annotate | Download (14 KB)

1 44669 jjdelcerro
package org.gvsig.fmap.dal.store.dbf;
2 43512 jjdelcerro
3 44669 jjdelcerro
import java.io.File;
4
import java.util.List;
5 43512 jjdelcerro
import junit.framework.TestCase;
6 44669 jjdelcerro
import org.apache.commons.lang3.StringUtils;
7
import org.gvsig.fmap.dal.DALLocator;
8
import org.gvsig.fmap.dal.DataManager;
9
import org.gvsig.fmap.dal.DataServerExplorer;
10
import org.gvsig.fmap.dal.DataStore;
11 44844 jjdelcerro
import org.gvsig.fmap.dal.DataTypeUtils;
12 44669 jjdelcerro
import org.gvsig.fmap.dal.DataTypes;
13
import org.gvsig.fmap.dal.feature.EditableFeature;
14
import org.gvsig.fmap.dal.feature.EditableFeatureType;
15
import org.gvsig.fmap.dal.feature.Feature;
16
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
17
import org.gvsig.fmap.dal.feature.FeatureStore;
18
import org.gvsig.fmap.dal.feature.FeatureType;
19
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
20
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
21
import org.gvsig.fmap.dal.store.dbf.utils.FieldFormatter;
22 44098 jjdelcerro
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
23 43512 jjdelcerro
24 44669 jjdelcerro
public class TestCreate extends TestCase {
25 43512 jjdelcerro
26 44669 jjdelcerro
    public TestCreate(String testName) {
27 43512 jjdelcerro
        super(testName);
28
    }
29
30
    @Override
31
    protected void setUp() throws Exception {
32
        super.setUp();
33 44098 jjdelcerro
        new DefaultLibrariesInitializer().fullInitialize();
34 43512 jjdelcerro
    }
35
36
    @Override
37
    protected void tearDown() throws Exception {
38
        super.tearDown();
39
    }
40
41
    // TODO add test methods here. The name must begin with 'test'. For example:
42
    // public void testHello() {}
43
44 44669 jjdelcerro
    protected String getProviderName() {
45
        return DataStore.DBASE_PROVIDER_NAME;
46 43512 jjdelcerro
    }
47
48 44669 jjdelcerro
    protected String getTargetFilename() {
49
        return "testCreateTarget1.dbf";
50 43512 jjdelcerro
    }
51 44139 jjdelcerro
52 44669 jjdelcerro
    protected FeatureStore openTargetStore1() throws Exception {
53
        DataManager dataManager = DALLocator.getDataManager();
54
        File f = TestUtils.getResource(getTargetFilename());
55
        FeatureStore store = (FeatureStore) dataManager.openStore(
56
                getProviderName(),
57
                "DbfFile",f
58
        );
59
        return store;
60 43512 jjdelcerro
    }
61 44139 jjdelcerro
62 44669 jjdelcerro
    protected void createFrom(FeatureStore sourceStore) throws Exception {
63
        DataManager dataManager = DALLocator.getDataManager();
64
        DataServerExplorer explorer = dataManager.openServerExplorer(
65
                DataServerExplorer.FILESYSTEM_SERVER_EXPLORER_NAME,
66
                "root", TestUtils.getTargetFolder()
67
        );
68 44421 jjdelcerro
69 44669 jjdelcerro
        NewFeatureStoreParameters params = (NewFeatureStoreParameters) explorer.getAddParameters(
70
                getProviderName()
71
        );
72
        ((FilesystemStoreParameters)params).setFile(TestUtils.getResource(getTargetFilename()));
73
        EditableFeatureType ft = params.getDefaultFeatureType();
74
        ft.addAll(sourceStore.getDefaultFeatureType());
75
        explorer.add(getProviderName(), params, true);
76 44421 jjdelcerro
    }
77
78 44844 jjdelcerro
    protected void checkTypes(FeatureType sourceFeatureType, boolean useDalFile) throws Exception {
79 44669 jjdelcerro
        FeatureStore targetStore = openTargetStore1();
80
        FeatureType targetFeatureType = targetStore.getDefaultFeatureType();
81 44139 jjdelcerro
82 44669 jjdelcerro
        assertEquals("Feature type size",sourceFeatureType.size(), targetFeatureType.size());
83
        for (int i = 0; i < sourceFeatureType.size(); i++) {
84
            FeatureAttributeDescriptor sourceAttr = sourceFeatureType.get(i);
85
            FeatureAttributeDescriptor targetAttr = targetFeatureType.get(i);
86
            assertEquals(sourceAttr.getName(), sourceAttr.getName(), targetAttr.getName());
87
            switch(sourceAttr.getType()) {
88
                case DataTypes.TIME:
89
                    assertEquals(
90 44844 jjdelcerro
                            String.format("Field %s type mismatch", sourceAttr.getName()),
91
                            useDalFile?sourceAttr.getDataTypeName():DataTypes.STRING_NAME,
92
                            targetAttr.getDataTypeName()
93
                    );
94
                    assertEquals(
95
                            String.format("Field %s size mismatch", sourceAttr.getName()),
96
                            targetAttr.getSize(),
97
                            useDalFile? targetAttr.getSize():FieldFormatter.TIME_SIZE
98
                    );
99
                    assertEquals(
100 44669 jjdelcerro
                            String.format("Field %s precision mismatch", sourceAttr.getName()),
101
                            sourceAttr.getPrecision(),
102
                            targetAttr.getPrecision()
103
                    );
104
                    assertEquals(
105
                            String.format("Field %s scale mismatch", sourceAttr.getName()),
106
                            sourceAttr.getScale(),
107
                            targetAttr.getScale()
108
                    );
109
                    break;
110
                case DataTypes.TIMESTAMP:
111
                    assertEquals(
112 44844 jjdelcerro
                            String.format("Field %s type mismatch", sourceAttr.getName()),
113
                            useDalFile?sourceAttr.getDataTypeName():DataTypes.STRING_NAME,
114
                            targetAttr.getDataTypeName()
115
                    );
116
                    assertEquals(
117
                            String.format("Field %s size mismatch", sourceAttr.getName()),
118
                            targetAttr.getSize(),
119
                            useDalFile? targetAttr.getSize():FieldFormatter.TIMESTAMP_SIZE
120
                    );
121
                    assertEquals(
122 44669 jjdelcerro
                            String.format("Field %s precision mismatch", sourceAttr.getName()),
123
                            sourceAttr.getPrecision(),
124
                            targetAttr.getPrecision()
125
                    );
126
                    assertEquals(
127
                            String.format("Field %s scale mismatch", sourceAttr.getName()),
128
                            sourceAttr.getScale(),
129
                            targetAttr.getScale()
130
                    );
131
                    break;
132 44844 jjdelcerro
                case DataTypes.DECIMAL:
133
                    assertEquals(
134
                            String.format("Field %s type mismatch", sourceAttr.getName()),
135
                            sourceAttr.getDataTypeName(),
136
                            targetAttr.getDataTypeName()
137
                    );
138
                    assertEquals(
139
                            String.format("Field %s size mismatch", sourceAttr.getName()),
140
                            sourceAttr.getSize(),
141
                            targetAttr.getSize()
142
                    );
143
                    assertEquals(
144
                            String.format("Field %s precision mismatch", sourceAttr.getName()),
145
                            useDalFile?sourceAttr.getPrecision():sourceAttr.getPrecision()+1,
146
                            targetAttr.getPrecision()
147
                    );
148
                    assertEquals(
149
                            String.format("Field %s scale mismatch", sourceAttr.getName()),
150
                            sourceAttr.getScale(),
151
                            targetAttr.getScale()
152
                    );
153
                    break;
154
155 44669 jjdelcerro
                case DataTypes.BYTE:
156 44844 jjdelcerro
                    assertEquals(
157
                            String.format("Field %s type mismatch", sourceAttr.getName()),
158
                            useDalFile?sourceAttr.getDataTypeName():DataTypes.DECIMAL_NAME,
159
                            targetAttr.getDataTypeName()
160
                    );
161
                    assertEquals(
162
                            String.format("Field %s size mismatch", sourceAttr.getName()),
163
                            sourceAttr.getSize(),
164
                            targetAttr.getSize()
165
                    );
166
                    assertEquals(
167
                            String.format("Field %s precision mismatch", sourceAttr.getName()),
168
                            sourceAttr.getPrecision(),
169
                            targetAttr.getPrecision()
170
                    );
171
                    assertEquals(
172
                            String.format("Field %s scale mismatch", sourceAttr.getName()),
173
                            sourceAttr.getScale(),
174
                            targetAttr.getScale()
175
                    );
176
                    break;
177
178 44669 jjdelcerro
                case DataTypes.INT:
179
                case DataTypes.LONG:
180
                case DataTypes.BOOLEAN:
181
                case DataTypes.DATE:
182
                case DataTypes.STRING:
183
                case DataTypes.DOUBLE:
184
                case DataTypes.FLOAT:
185
                    assertEquals(
186
                            String.format("Field %s type mismatch", sourceAttr.getName()),
187
                            sourceAttr.getDataTypeName(),
188
                            targetAttr.getDataTypeName()
189
                    );
190
                    assertEquals(
191
                            String.format("Field %s size mismatch", sourceAttr.getName()),
192
                            sourceAttr.getSize(),
193
                            targetAttr.getSize()
194
                    );
195
                    assertEquals(
196
                            String.format("Field %s precision mismatch", sourceAttr.getName()),
197
                            sourceAttr.getPrecision(),
198
                            targetAttr.getPrecision()
199
                    );
200
                    assertEquals(
201
                            String.format("Field %s scale mismatch", sourceAttr.getName()),
202
                            sourceAttr.getScale(),
203
                            targetAttr.getScale()
204
                    );
205
                    break;
206
                default:
207
                    fail("Type '"+targetAttr.getType()+"' not supported for field '"+targetAttr.getName()+"'.");
208
            }
209
        }
210 43512 jjdelcerro
    }
211
212 44669 jjdelcerro
    protected void copyFrom(FeatureStore sourceStore, int mode) throws Exception {
213
        FeatureStore targetStore = openTargetStore1();
214
        targetStore.edit(mode);
215
        try {
216
            for (Feature sourceFeature : sourceStore.getFeatureSet()) {
217
                EditableFeature targetFeature = targetStore.createNewFeature(sourceFeature);
218
                targetStore.insert(targetFeature);
219
            }
220
        } finally {
221
            targetStore.finishEditing();
222
        }
223 43512 jjdelcerro
    }
224
225 44669 jjdelcerro
    protected void checkData(FeatureStore sourceStore) throws Exception {
226
        FeatureStore targetStore = openTargetStore1();
227 44139 jjdelcerro
228 44844 jjdelcerro
        FeatureType sourceFeatureType = sourceStore.getDefaultFeatureType();
229
        FeatureType targetFeatureType = targetStore.getDefaultFeatureType();
230
231 44669 jjdelcerro
        List<Feature> sourceFeatures = sourceStore.getFeatures();
232
        List<Feature> targetFeatures = targetStore.getFeatures();
233 44844 jjdelcerro
234 44669 jjdelcerro
        assertEquals("Count features", sourceFeatures.size(), targetFeatures.size());
235
        for (int i = 0; i < targetFeatures.size(); i++) {
236
            Feature sourceFeature = sourceFeatures.get(i);
237
            Feature targetFeature = targetFeatures.get(i);
238 44844 jjdelcerro
            for (FeatureAttributeDescriptor sourceAttr : sourceFeatureType) {
239
                FeatureAttributeDescriptor targetAttr = targetFeatureType.getAttributeDescriptor(sourceAttr.getName());
240 44669 jjdelcerro
                switch(sourceAttr.getType()) {
241
                    case DataTypes.TIMESTAMP:
242 44767 jjdelcerro
                        assertEquals(
243
                                String.format("Feature %03d attribute %s", i, sourceAttr.getName()),
244
                                sourceFeature.get(sourceAttr.getName()),
245
                                targetFeature.getTimestamp(sourceAttr.getName())
246
                        );
247
                        break;
248 44669 jjdelcerro
                    case DataTypes.TIME:
249
                        assertEquals(
250
                                String.format("Feature %03d attribute %s", i, sourceAttr.getName()),
251 44767 jjdelcerro
                                sourceFeature.get(sourceAttr.getName()),
252
                                targetFeature.getTime(sourceAttr.getName())
253 44669 jjdelcerro
                        );
254
                        break;
255
                    case DataTypes.STRING:
256
                        assertEquals(
257
                                String.format("Feature %03d attribute %s", i, sourceAttr.getName()),
258
                                StringUtils.defaultIfBlank(sourceFeature.getString(sourceAttr.getName()), null),
259
                                targetFeature.get(sourceAttr.getName())
260
                        );
261
                        break;
262 44844 jjdelcerro
                    case DataTypes.BYTE:
263 44669 jjdelcerro
                        assertEquals(
264
                                String.format("Feature %03d attribute %s", i, sourceAttr.getName()),
265
                                sourceFeature.get(sourceAttr.getName()),
266 44844 jjdelcerro
                                DataTypeUtils.coerce(DataTypes.BYTE, targetFeature.get(sourceAttr.getName()),null)
267
                        );
268
                        break;
269
                    default:
270
                        assertEquals(
271
                                String.format("Feature %03d attribute %s (%s/%s)", i, sourceAttr.getName(), sourceAttr.getDataType().getName(), targetAttr.getDataType().getName() ),
272
                                sourceFeature.get(sourceAttr.getName()),
273 44669 jjdelcerro
                                targetFeature.get(sourceAttr.getName())
274
                        );
275
                }
276
            }
277
        }
278 43512 jjdelcerro
    }
279
280 44384 jjdelcerro
281 44669 jjdelcerro
    public void testCreateWithDALFile() throws Exception {
282
        FeatureStore sourceStore = TestUtils.openSourceStore1();
283 44384 jjdelcerro
284 44669 jjdelcerro
        createFrom(sourceStore);
285 43512 jjdelcerro
286 44844 jjdelcerro
        checkTypes(sourceStore.getDefaultFeatureType(),true);
287 44669 jjdelcerro
        copyFrom(sourceStore, FeatureStore.MODE_APPEND);
288
        checkData(sourceStore);
289 44098 jjdelcerro
290 44669 jjdelcerro
        createFrom(sourceStore);
291
        copyFrom(sourceStore, FeatureStore.MODE_FULLEDIT);
292
        checkData(sourceStore);
293 44139 jjdelcerro
294 44098 jjdelcerro
    }
295
296 44669 jjdelcerro
    public void testCreateWithoutDALFile() throws Exception {
297
        FeatureStore sourceStore = TestUtils.openSourceStore1();
298 44098 jjdelcerro
299 44669 jjdelcerro
        createFrom(sourceStore);
300
        TestUtils.removeDALFile(this.getTargetFilename());
301 44098 jjdelcerro
302 44844 jjdelcerro
        checkTypes(sourceStore.getDefaultFeatureType(),false);
303 44669 jjdelcerro
        copyFrom(sourceStore, FeatureStore.MODE_APPEND);
304
        TestUtils.removeDALFile(this.getTargetFilename());
305
        checkData(sourceStore);
306 44098 jjdelcerro
307 44669 jjdelcerro
        createFrom(sourceStore);
308
        TestUtils.removeDALFile(this.getTargetFilename());
309
        copyFrom(sourceStore, FeatureStore.MODE_FULLEDIT);
310
        TestUtils.removeDALFile(this.getTargetFilename());
311
        checkData(sourceStore);
312 44139 jjdelcerro
313 44098 jjdelcerro
    }
314 43939 jjdelcerro
}