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 |
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.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 |
import org.gvsig.fmap.dal.DataTypeUtils; |
12 |
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 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
23 |
|
24 |
public class TestCreate extends TestCase { |
25 |
|
26 |
public TestCreate(String testName) { |
27 |
super(testName);
|
28 |
} |
29 |
|
30 |
@Override
|
31 |
protected void setUp() throws Exception { |
32 |
super.setUp();
|
33 |
new DefaultLibrariesInitializer().fullInitialize();
|
34 |
} |
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 |
protected String getProviderName() { |
45 |
return DataStore.DBASE_PROVIDER_NAME;
|
46 |
} |
47 |
|
48 |
protected String getTargetFilename() { |
49 |
return "testCreateTarget1.dbf"; |
50 |
} |
51 |
|
52 |
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 |
} |
61 |
|
62 |
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 |
|
69 |
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 |
} |
77 |
|
78 |
protected void checkTypes(FeatureType sourceFeatureType, boolean useDalFile) throws Exception { |
79 |
FeatureStore targetStore = openTargetStore1(); |
80 |
FeatureType targetFeatureType = targetStore.getDefaultFeatureType(); |
81 |
|
82 |
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 |
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 |
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 |
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 |
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 |
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 |
case DataTypes.BYTE:
|
156 |
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 |
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 |
} |
211 |
|
212 |
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 |
} |
224 |
|
225 |
protected void checkData(FeatureStore sourceStore) throws Exception { |
226 |
FeatureStore targetStore = openTargetStore1(); |
227 |
|
228 |
FeatureType sourceFeatureType = sourceStore.getDefaultFeatureType(); |
229 |
FeatureType targetFeatureType = targetStore.getDefaultFeatureType(); |
230 |
|
231 |
List<Feature> sourceFeatures = sourceStore.getFeatures();
|
232 |
List<Feature> targetFeatures = targetStore.getFeatures();
|
233 |
|
234 |
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 |
for (FeatureAttributeDescriptor sourceAttr : sourceFeatureType) {
|
239 |
FeatureAttributeDescriptor targetAttr = targetFeatureType.getAttributeDescriptor(sourceAttr.getName()); |
240 |
switch(sourceAttr.getType()) {
|
241 |
case DataTypes.TIMESTAMP:
|
242 |
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 |
case DataTypes.TIME:
|
249 |
assertEquals( |
250 |
String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
251 |
sourceFeature.get(sourceAttr.getName()), |
252 |
targetFeature.getTime(sourceAttr.getName()) |
253 |
); |
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 |
case DataTypes.BYTE:
|
263 |
assertEquals( |
264 |
String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
265 |
sourceFeature.get(sourceAttr.getName()), |
266 |
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 |
targetFeature.get(sourceAttr.getName()) |
274 |
); |
275 |
} |
276 |
} |
277 |
} |
278 |
} |
279 |
|
280 |
|
281 |
public void testCreateWithDALFile() throws Exception { |
282 |
FeatureStore sourceStore = TestUtils.openSourceStore1(); |
283 |
|
284 |
createFrom(sourceStore); |
285 |
|
286 |
checkTypes(sourceStore.getDefaultFeatureType(),true);
|
287 |
copyFrom(sourceStore, FeatureStore.MODE_APPEND); |
288 |
checkData(sourceStore); |
289 |
|
290 |
createFrom(sourceStore); |
291 |
copyFrom(sourceStore, FeatureStore.MODE_FULLEDIT); |
292 |
checkData(sourceStore); |
293 |
|
294 |
} |
295 |
|
296 |
public void testCreateWithoutDALFile() throws Exception { |
297 |
FeatureStore sourceStore = TestUtils.openSourceStore1(); |
298 |
|
299 |
createFrom(sourceStore); |
300 |
TestUtils.removeDALFile(this.getTargetFilename());
|
301 |
|
302 |
checkTypes(sourceStore.getDefaultFeatureType(),false);
|
303 |
copyFrom(sourceStore, FeatureStore.MODE_APPEND); |
304 |
TestUtils.removeDALFile(this.getTargetFilename());
|
305 |
checkData(sourceStore); |
306 |
|
307 |
createFrom(sourceStore); |
308 |
TestUtils.removeDALFile(this.getTargetFilename());
|
309 |
copyFrom(sourceStore, FeatureStore.MODE_FULLEDIT); |
310 |
TestUtils.removeDALFile(this.getTargetFilename());
|
311 |
checkData(sourceStore); |
312 |
|
313 |
} |
314 |
} |