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