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.csv / src / test / java / org / gvsig / fmap / dal / store / csv / TestCreate.java @ 46627
History | View | Annotate | Download (15.7 KB)
1 |
package org.gvsig.fmap.dal.store.csv; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.net.URL; |
5 |
import java.util.Date; |
6 |
import java.util.List; |
7 |
import junit.framework.TestCase; |
8 |
import static junit.framework.TestCase.assertEquals; |
9 |
import org.apache.commons.io.FileUtils; |
10 |
import org.apache.commons.io.FilenameUtils; |
11 |
import org.apache.commons.lang3.StringUtils; |
12 |
import org.gvsig.fmap.dal.DALLocator; |
13 |
import org.gvsig.fmap.dal.DataTypes; |
14 |
import org.gvsig.fmap.dal.DataManager; |
15 |
import org.gvsig.fmap.dal.DataServerExplorer; |
16 |
import org.gvsig.fmap.dal.DataStore; |
17 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
18 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
19 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
20 |
import org.gvsig.fmap.dal.feature.Feature; |
21 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
22 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
23 |
import org.gvsig.fmap.dal.feature.FeatureType; |
24 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
25 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
26 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
27 |
import org.slf4j.Logger; |
28 |
import org.slf4j.LoggerFactory; |
29 |
|
30 |
public class TestCreate extends TestCase { |
31 |
private static final Logger LOGGER = LoggerFactory.getLogger(TestCreate.class); |
32 |
|
33 |
public TestCreate(String testName) { |
34 |
super(testName);
|
35 |
} |
36 |
|
37 |
@Override
|
38 |
protected void setUp() throws Exception { |
39 |
super.setUp();
|
40 |
new DefaultLibrariesInitializer().fullInitialize();
|
41 |
} |
42 |
|
43 |
@Override
|
44 |
protected void tearDown() throws Exception { |
45 |
super.tearDown();
|
46 |
} |
47 |
|
48 |
// TODO add test methods here. The name must begin with 'test'. For example:
|
49 |
// public void testHello() {}
|
50 |
|
51 |
protected File getTargetFolder() throws Exception { |
52 |
URL url = this.getClass().getResource("/"); |
53 |
File x = new File(url.toURI()); |
54 |
File target = x.getParentFile();
|
55 |
return target;
|
56 |
} |
57 |
|
58 |
protected File getFile(String name) throws Exception { |
59 |
File x = new File(getTargetFolder(), name); |
60 |
return x;
|
61 |
} |
62 |
|
63 |
protected File getResource(String pathname) throws Exception { |
64 |
URL url = this.getClass().getResource(pathname); |
65 |
File x = new File(url.toURI()); |
66 |
return x;
|
67 |
} |
68 |
|
69 |
public FeatureStore openSourceStore1() throws Exception { |
70 |
DataManager dataManager = DALLocator.getDataManager(); |
71 |
File f = getResource("/org/gvsig/fmap/dal/store/csv/testCreateSource1.csv"); |
72 |
FeatureStore store = (FeatureStore) dataManager.openStore( |
73 |
DataStore.CSV_PROVIDER_NAME, |
74 |
"file",f,
|
75 |
"automaticTypesDetection", false, |
76 |
"locale","en" |
77 |
); |
78 |
return store;
|
79 |
} |
80 |
|
81 |
protected String getProviderName() { |
82 |
return DataStore.CSV_PROVIDER_NAME;
|
83 |
} |
84 |
|
85 |
protected String getTargetFilename() { |
86 |
return "testCreateTarget.csv"; |
87 |
} |
88 |
|
89 |
protected FeatureStore openTargetStore1() throws Exception { |
90 |
DataManager dataManager = DALLocator.getDataManager(); |
91 |
File f = getFile(getTargetFilename());
|
92 |
FeatureStore store; |
93 |
try {
|
94 |
store = (FeatureStore) dataManager.openStore( |
95 |
getProviderName(), |
96 |
"file",f,
|
97 |
"automaticTypesDetection", false, |
98 |
"locale","en" |
99 |
); |
100 |
} catch(Exception ex) { |
101 |
LOGGER.warn("Can't open TargetStore1("+f+")",ex); |
102 |
throw ex;
|
103 |
} |
104 |
return store;
|
105 |
} |
106 |
|
107 |
private void removeDALFile() throws Exception { |
108 |
File f = getFile(getTargetFilename());
|
109 |
f = new File(FilenameUtils.removeExtension(f.getAbsolutePath())+".dal"); |
110 |
FileUtils.deleteQuietly(f); |
111 |
} |
112 |
|
113 |
protected void createFrom(FeatureStore sourceStore) throws Exception { |
114 |
DataManager dataManager = DALLocator.getDataManager(); |
115 |
DataServerExplorer explorer = dataManager.openServerExplorer( |
116 |
DataServerExplorer.FILESYSTEM_SERVER_EXPLORER_NAME, |
117 |
"root", getTargetFolder()
|
118 |
); |
119 |
|
120 |
NewFeatureStoreParameters params = (NewFeatureStoreParameters) explorer.getAddParameters( |
121 |
getProviderName() |
122 |
); |
123 |
((FilesystemStoreParameters)params).setFile(getFile(getTargetFilename())); |
124 |
EditableFeatureType ft = params.getDefaultFeatureType(); |
125 |
ft.copyFrom(sourceStore.getDefaultFeatureType()); |
126 |
explorer.add(getProviderName(), params, true);
|
127 |
} |
128 |
|
129 |
protected void checkTypes(FeatureType sourceFeatureType) throws Exception { |
130 |
// DataType STRING_TYPE = ToolsLocator.getDataTypesManager().get(DataTypes.STRING);
|
131 |
// DataType INT_TYPE = ToolsLocator.getDataTypesManager().get(DataTypes.INT);
|
132 |
|
133 |
FeatureStore targetStore = openTargetStore1(); |
134 |
FeatureType targetFeatureType = targetStore.getDefaultFeatureType(); |
135 |
|
136 |
assertEquals("Feature type size",sourceFeatureType.size(), targetFeatureType.size());
|
137 |
for (int i = 0; i < sourceFeatureType.size(); i++) { |
138 |
FeatureAttributeDescriptor sourceAttr = sourceFeatureType.get(i); |
139 |
FeatureAttributeDescriptor targetAttr = targetFeatureType.get(i); |
140 |
switch(sourceAttr.getType()) {
|
141 |
case DataTypes.BYTE:
|
142 |
case DataTypes.INT:
|
143 |
case DataTypes.LONG:
|
144 |
case DataTypes.BOOLEAN:
|
145 |
case DataTypes.DATE:
|
146 |
case DataTypes.STRING:
|
147 |
case DataTypes.TIME:
|
148 |
case DataTypes.TIMESTAMP:
|
149 |
case DataTypes.DOUBLE:
|
150 |
case DataTypes.FLOAT:
|
151 |
assertEquals( |
152 |
String.format("Field %s name mismatch", sourceAttr.getName()), |
153 |
sourceAttr.getName(), |
154 |
targetAttr.getName() |
155 |
); |
156 |
assertEquals( |
157 |
String.format("Field %s type mismatch", sourceAttr.getName()), |
158 |
sourceAttr.getDataTypeName(), |
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 precision mismatch", sourceAttr.getName()), |
173 |
sourceAttr.getScale(), |
174 |
targetAttr.getScale() |
175 |
); |
176 |
break;
|
177 |
case DataTypes.GEOMETRY:
|
178 |
assertEquals( |
179 |
String.format("Field %s name mismatch", sourceAttr.getName()), |
180 |
sourceAttr.getName(), |
181 |
targetAttr.getName() |
182 |
); |
183 |
assertEquals( |
184 |
String.format("Field %s type mismatch", sourceAttr.getName()), |
185 |
sourceAttr.getDataTypeName(), |
186 |
targetAttr.getDataTypeName() |
187 |
); |
188 |
assertEquals( |
189 |
String.format("Field %s geometry type mismatch", sourceAttr.getName()), |
190 |
sourceAttr.getGeomType().getName(), |
191 |
targetAttr.getGeomType().getName() |
192 |
); |
193 |
assertEquals( |
194 |
String.format("Field %s geometry SRS mismatch", sourceAttr.getName()), |
195 |
sourceAttr.getSRS().toString(), |
196 |
targetAttr.getSRS().toString() |
197 |
); |
198 |
assertEquals( |
199 |
String.format("Field %s size mismatch", sourceAttr.getName()), |
200 |
sourceAttr.getSize(), |
201 |
targetAttr.getSize() |
202 |
); |
203 |
assertEquals( |
204 |
String.format("Field %s precision mismatch", sourceAttr.getName()), |
205 |
sourceAttr.getPrecision(), |
206 |
targetAttr.getPrecision() |
207 |
); |
208 |
break;
|
209 |
default:
|
210 |
fail( |
211 |
String.format("Field %s type %d (%s) not supported.", |
212 |
targetAttr.getName(), |
213 |
targetAttr.getType(), |
214 |
targetAttr.getDataTypeName() |
215 |
) |
216 |
); |
217 |
} |
218 |
} |
219 |
} |
220 |
|
221 |
protected void copyFrom(FeatureStore sourceStore, int mode) throws Exception { |
222 |
FeatureStore targetStore = openTargetStore1(); |
223 |
targetStore.edit(mode); // For debug
|
224 |
int counter = 0; |
225 |
try {
|
226 |
for (Feature sourceFeature : sourceStore.getFeatureSet()) {
|
227 |
EditableFeature targetFeature = targetStore.createNewFeature(sourceFeature); |
228 |
targetStore.insert(targetFeature); |
229 |
counter++; |
230 |
} |
231 |
} finally {
|
232 |
targetStore.finishEditing(); |
233 |
} |
234 |
} |
235 |
|
236 |
protected void checkData(FeatureStore sourceStore) throws Exception { |
237 |
FeatureStore targetStore = openTargetStore1(); |
238 |
|
239 |
List<Feature> sourceFeatures = sourceStore.getFeatures();
|
240 |
List<Feature> targetFeatures = targetStore.getFeatures();
|
241 |
assertEquals("Count features", sourceFeatures.size(), targetFeatures.size());
|
242 |
for (int i = 0; i < targetFeatures.size(); i++) { |
243 |
Feature sourceFeature = sourceFeatures.get(i); |
244 |
Feature targetFeature = targetFeatures.get(i); |
245 |
for (FeatureAttributeDescriptor sourceAttr : sourceStore.getDefaultFeatureType()) {
|
246 |
switch(sourceAttr.getType()) {
|
247 |
case DataTypes.BYTE:
|
248 |
assertEquals( |
249 |
String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
250 |
sourceFeature.getInt(sourceAttr.getName()), |
251 |
targetFeature.getInt(sourceAttr.getName()) |
252 |
); |
253 |
break;
|
254 |
case DataTypes.TIMESTAMP:
|
255 |
Date sourceTimestamp = sourceFeature.getDate(sourceAttr.getName());
|
256 |
Date targetTimestamp = targetFeature.getDate(sourceAttr.getName());
|
257 |
assertEquals( |
258 |
String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
259 |
sourceTimestamp, |
260 |
targetTimestamp |
261 |
); |
262 |
break;
|
263 |
case DataTypes.TIME:
|
264 |
assertEquals( |
265 |
String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
266 |
sourceFeature.getDate(sourceAttr.getName()), |
267 |
targetFeature.getDate(sourceAttr.getName()) |
268 |
); |
269 |
break;
|
270 |
// case DataTypes.FLOAT:
|
271 |
// case DataTypes.DOUBLE:
|
272 |
// assertEquals(
|
273 |
// String.format("Feature %03d attribute %s", i, sourceAttr.getName()),
|
274 |
// sourceFeature.getDouble(sourceAttr.getName()),
|
275 |
// targetFeature.getDouble(sourceAttr.getName())
|
276 |
// );
|
277 |
// break;
|
278 |
case DataTypes.GEOMETRY:
|
279 |
assertEquals( |
280 |
String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
281 |
sourceFeature.get(sourceAttr.getName()), |
282 |
targetFeature.get(sourceAttr.getName()) |
283 |
); |
284 |
break;
|
285 |
case DataTypes.STRING:
|
286 |
assertEquals( |
287 |
String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
288 |
StringUtils.defaultIfBlank(sourceFeature.getString(sourceAttr.getName()), null),
|
289 |
StringUtils.defaultIfBlank(targetFeature.getString(sourceAttr.getName()), null)
|
290 |
); |
291 |
break;
|
292 |
case DataTypes.INT:
|
293 |
case DataTypes.LONG:
|
294 |
case DataTypes.FLOAT:
|
295 |
case DataTypes.DOUBLE:
|
296 |
default:
|
297 |
Object sourceValue = sourceFeature.get(sourceAttr.getName());
|
298 |
Object targetValue = targetFeature.get(sourceAttr.getName());
|
299 |
if( sourceValue == null ) { |
300 |
LOGGER.info("sourceValue is null");
|
301 |
} |
302 |
assertEquals( |
303 |
String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
304 |
sourceValue, |
305 |
targetValue |
306 |
); |
307 |
} |
308 |
} |
309 |
} |
310 |
} |
311 |
|
312 |
|
313 |
public void testCreateWithDALFile() throws Exception { |
314 |
try {
|
315 |
FeatureStore sourceStore = openSourceStore1(); |
316 |
|
317 |
createFrom(sourceStore); |
318 |
|
319 |
checkTypes(sourceStore.getDefaultFeatureType()); |
320 |
copyFrom(sourceStore, FeatureStore.MODE_APPEND); |
321 |
checkData(sourceStore); |
322 |
|
323 |
createFrom(sourceStore); |
324 |
copyFrom(sourceStore, FeatureStore.MODE_FULLEDIT); |
325 |
checkData(sourceStore); |
326 |
} catch (Exception e) { |
327 |
e.printStackTrace(); |
328 |
throw e;
|
329 |
} |
330 |
|
331 |
} |
332 |
|
333 |
public void testCreateWithoutDALFile() throws Exception { |
334 |
FeatureStore sourceStore = openSourceStore1(); |
335 |
|
336 |
createFrom(sourceStore); |
337 |
removeDALFile(); |
338 |
|
339 |
checkTypes(sourceStore.getDefaultFeatureType()); |
340 |
copyFrom(sourceStore, FeatureStore.MODE_APPEND); |
341 |
removeDALFile(); |
342 |
checkData(sourceStore); |
343 |
|
344 |
createFrom(sourceStore); |
345 |
removeDALFile(); |
346 |
copyFrom(sourceStore, FeatureStore.MODE_FULLEDIT); |
347 |
removeDALFile(); |
348 |
checkData(sourceStore); |
349 |
|
350 |
} |
351 |
|
352 |
public void testCRLFAndQuotes() throws Exception { |
353 |
try {
|
354 |
FeatureStore sourceStore = openSourceStore1(); |
355 |
|
356 |
List<Feature> features = sourceStore.getFeatures();
|
357 |
Feature f20 = features.get(19);
|
358 |
Feature f21 = features.get(20);
|
359 |
Feature f22 = features.get(21);
|
360 |
Feature f23 = features.get(22);
|
361 |
Feature f24 = features.get(23);
|
362 |
Feature f25 = features.get(24);
|
363 |
|
364 |
assertEquals("f20", "Yo \nyo20", f20.getString("String")); |
365 |
assertEquals("f21", "Yo \nyo21", f21.getString("String")); |
366 |
assertEquals("f22", "\nYo yo22", f22.getString("String")); |
367 |
|
368 |
// Ojo, es lo que hace ahora el lector de CSV, en este caso
|
369 |
// elimina las comillas dobles, aunque pensamos que deberia mantenerlas.
|
370 |
// Si esto se corrige el test fallara y tendremos que evaluar el posible
|
371 |
// impacto en otras partes del codigo.
|
372 |
assertEquals("f23", "Yo yo23", f23.getString("String")); |
373 |
|
374 |
assertEquals("f24", "Yo \"yo24\"", f24.getString("String")); |
375 |
assertEquals("f25", "Yo\\n \"yo25\"", f25.getString("String")); |
376 |
} catch(Exception ex) { |
377 |
ex.printStackTrace(); |
378 |
throw ex;
|
379 |
} |
380 |
} |
381 |
} |