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 @ 44926
History | View | Annotate | Download (15.5 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.addAll(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 |
} else {
|
150 |
assertEquals( |
151 |
String.format("Field %s type mismatch", sourceAttr.getName()), |
152 |
sourceAttr.getScale()==0?"Integer":sourceAttr.getDataTypeName(), |
153 |
targetAttr.getDataTypeName() |
154 |
); |
155 |
// assertEquals(
|
156 |
// String.format("Field %s precision mismatch", sourceAttr.getName()),
|
157 |
// sourceAttr.getPrecision(),
|
158 |
// targetAttr.getPrecision()
|
159 |
// );
|
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 scale mismatch", sourceAttr.getName()), |
168 |
sourceAttr.getScale(), |
169 |
targetAttr.getScale() |
170 |
); |
171 |
break;
|
172 |
|
173 |
case DataTypes.BYTE:
|
174 |
if( useDalFile ) {
|
175 |
assertEquals( |
176 |
String.format("Field %s type mismatch", sourceAttr.getName()), |
177 |
sourceAttr.getDataTypeName(), |
178 |
targetAttr.getDataTypeName() |
179 |
); |
180 |
} else {
|
181 |
assertEquals( |
182 |
String.format("Field %s type mismatch", sourceAttr.getName()), |
183 |
DataTypes.DECIMAL_NAME, |
184 |
targetAttr.getDataTypeName() |
185 |
); |
186 |
} |
187 |
assertEquals( |
188 |
String.format("Field %s size mismatch", sourceAttr.getName()), |
189 |
sourceAttr.getSize(), |
190 |
targetAttr.getSize() |
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 |
|
204 |
case DataTypes.DOUBLE:
|
205 |
case DataTypes.FLOAT:
|
206 |
break;
|
207 |
case DataTypes.INT:
|
208 |
case DataTypes.LONG:
|
209 |
case DataTypes.BOOLEAN:
|
210 |
case DataTypes.DATE:
|
211 |
case DataTypes.STRING:
|
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 size mismatch", sourceAttr.getName()), |
219 |
sourceAttr.getSize(), |
220 |
targetAttr.getSize() |
221 |
); |
222 |
assertEquals( |
223 |
String.format("Field %s precision mismatch", sourceAttr.getName()), |
224 |
sourceAttr.getPrecision(), |
225 |
targetAttr.getPrecision() |
226 |
); |
227 |
assertEquals( |
228 |
String.format("Field %s scale mismatch", sourceAttr.getName()), |
229 |
sourceAttr.getScale(), |
230 |
targetAttr.getScale() |
231 |
); |
232 |
break;
|
233 |
default:
|
234 |
fail("Type '"+targetAttr.getType()+"' not supported for field '"+targetAttr.getName()+"'."); |
235 |
} |
236 |
} |
237 |
} |
238 |
|
239 |
protected void copyFrom(FeatureStore sourceStore, int mode) throws Exception { |
240 |
FeatureStore targetStore = openTargetStore1(); |
241 |
targetStore.edit(mode); |
242 |
try {
|
243 |
for (Feature sourceFeature : sourceStore.getFeatureSet()) {
|
244 |
EditableFeature targetFeature = targetStore.createNewFeature(sourceFeature); |
245 |
targetStore.insert(targetFeature); |
246 |
} |
247 |
} finally {
|
248 |
targetStore.finishEditing(); |
249 |
} |
250 |
} |
251 |
|
252 |
protected void checkData(FeatureStore sourceStore) throws Exception { |
253 |
FeatureStore targetStore = openTargetStore1(); |
254 |
|
255 |
FeatureType sourceFeatureType = sourceStore.getDefaultFeatureType(); |
256 |
FeatureType targetFeatureType = targetStore.getDefaultFeatureType(); |
257 |
|
258 |
List<Feature> sourceFeatures = sourceStore.getFeatures();
|
259 |
List<Feature> targetFeatures = targetStore.getFeatures();
|
260 |
|
261 |
assertEquals("Count features", sourceFeatures.size(), targetFeatures.size());
|
262 |
for (int i = 0; i < targetFeatures.size(); i++) { |
263 |
Feature sourceFeature = sourceFeatures.get(i); |
264 |
Feature targetFeature = targetFeatures.get(i); |
265 |
for (FeatureAttributeDescriptor sourceAttr : sourceFeatureType) {
|
266 |
FeatureAttributeDescriptor targetAttr = targetFeatureType.getAttributeDescriptor(sourceAttr.getName()); |
267 |
switch(sourceAttr.getType()) {
|
268 |
case DataTypes.TIMESTAMP:
|
269 |
assertEquals( |
270 |
String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
271 |
sourceFeature.get(sourceAttr.getName()), |
272 |
targetFeature.getTimestamp(sourceAttr.getName()) |
273 |
); |
274 |
break;
|
275 |
case DataTypes.TIME:
|
276 |
assertEquals( |
277 |
String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
278 |
sourceFeature.get(sourceAttr.getName()), |
279 |
targetFeature.getTime(sourceAttr.getName()) |
280 |
); |
281 |
break;
|
282 |
case DataTypes.STRING:
|
283 |
assertEquals( |
284 |
String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
285 |
StringUtils.defaultIfBlank(sourceFeature.getString(sourceAttr.getName()), null),
|
286 |
targetFeature.get(sourceAttr.getName()) |
287 |
); |
288 |
break;
|
289 |
case DataTypes.BYTE:
|
290 |
assertEquals( |
291 |
String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
292 |
sourceFeature.get(sourceAttr.getName()), |
293 |
DataTypeUtils.coerce(DataTypes.BYTE, targetFeature.get(sourceAttr.getName()),null)
|
294 |
); |
295 |
break;
|
296 |
case DataTypes.DECIMAL:
|
297 |
assertEquals( |
298 |
String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
299 |
sourceFeature.get(sourceAttr.getName()), |
300 |
targetFeature.getDecimal(sourceAttr.getName()) |
301 |
); |
302 |
break;
|
303 |
default:
|
304 |
assertEquals( |
305 |
String.format("Feature %03d attribute %s (%s/%s)", i, sourceAttr.getName(), sourceAttr.getDataType().getName(), targetAttr.getDataType().getName() ), |
306 |
sourceFeature.get(sourceAttr.getName()), |
307 |
targetFeature.get(sourceAttr.getName()) |
308 |
); |
309 |
} |
310 |
} |
311 |
} |
312 |
} |
313 |
|
314 |
|
315 |
public void testCreateWithDALFile() throws Exception { |
316 |
FeatureStore sourceStore = TestUtils.openSourceStore1(); |
317 |
|
318 |
createFrom(sourceStore); |
319 |
|
320 |
checkTypes(sourceStore.getDefaultFeatureType(),true);
|
321 |
copyFrom(sourceStore, FeatureStore.MODE_APPEND); |
322 |
checkData(sourceStore); |
323 |
|
324 |
createFrom(sourceStore); |
325 |
copyFrom(sourceStore, FeatureStore.MODE_FULLEDIT); |
326 |
checkData(sourceStore); |
327 |
|
328 |
} |
329 |
|
330 |
public void testCreateWithoutDALFile() throws Exception { |
331 |
FeatureStore sourceStore = TestUtils.openSourceStore1(); |
332 |
|
333 |
createFrom(sourceStore); |
334 |
TestUtils.removeDALFile(this.getTargetFilename());
|
335 |
|
336 |
checkTypes(sourceStore.getDefaultFeatureType(),false);
|
337 |
copyFrom(sourceStore, FeatureStore.MODE_APPEND); |
338 |
TestUtils.removeDALFile(this.getTargetFilename());
|
339 |
checkData(sourceStore); |
340 |
|
341 |
createFrom(sourceStore); |
342 |
TestUtils.removeDALFile(this.getTargetFilename());
|
343 |
copyFrom(sourceStore, FeatureStore.MODE_FULLEDIT); |
344 |
TestUtils.removeDALFile(this.getTargetFilename());
|
345 |
checkData(sourceStore); |
346 |
|
347 |
} |
348 |
} |