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 @ 44767
History | View | Annotate | Download (11.9 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.Date; |
||
5 | import java.util.List; |
||
6 | 43512 | jjdelcerro | import junit.framework.TestCase; |
7 | 44669 | jjdelcerro | 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.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 | 44669 | jjdelcerro | protected void checkTypes(FeatureType sourceFeatureType) throws Exception { |
79 | // DataType STRING_TYPE = ToolsLocator.getDataTypesManager().get(DataTypes.STRING);
|
||
80 | // DataType INT_TYPE = ToolsLocator.getDataTypesManager().get(DataTypes.INT);
|
||
81 | 43512 | jjdelcerro | |
82 | 44669 | jjdelcerro | FeatureStore targetStore = openTargetStore1(); |
83 | FeatureType targetFeatureType = targetStore.getDefaultFeatureType(); |
||
84 | 44139 | jjdelcerro | |
85 | 44669 | jjdelcerro | assertEquals("Feature type size",sourceFeatureType.size(), targetFeatureType.size());
|
86 | for (int i = 0; i < sourceFeatureType.size(); i++) { |
||
87 | FeatureAttributeDescriptor sourceAttr = sourceFeatureType.get(i); |
||
88 | FeatureAttributeDescriptor targetAttr = targetFeatureType.get(i); |
||
89 | assertEquals(sourceAttr.getName(), sourceAttr.getName(), targetAttr.getName()); |
||
90 | switch(sourceAttr.getType()) {
|
||
91 | case DataTypes.TIME:
|
||
92 | if( targetAttr.getType()==DataTypes.STRING ) {
|
||
93 | // Cuando no hay fichero DAL puede leer un TIME como
|
||
94 | // un STRING de tama?o TIME_SIZE.
|
||
95 | assertEquals( |
||
96 | String.format("Field %s size mismatch", sourceAttr.getName()), |
||
97 | FieldFormatter.TIME_SIZE, |
||
98 | targetAttr.getSize() |
||
99 | ); |
||
100 | } else if( targetAttr.getType()==DataTypes.TIME ) { |
||
101 | // Si hay fichero DAL el tipo pude ser correcto (TIME).
|
||
102 | assertEquals( |
||
103 | String.format("Field %s size mismatch", sourceAttr.getName()), |
||
104 | sourceAttr.getSize(), |
||
105 | targetAttr.getSize() |
||
106 | ); |
||
107 | } |
||
108 | assertEquals( |
||
109 | String.format("Field %s precision mismatch", sourceAttr.getName()), |
||
110 | sourceAttr.getPrecision(), |
||
111 | targetAttr.getPrecision() |
||
112 | ); |
||
113 | assertEquals( |
||
114 | String.format("Field %s scale mismatch", sourceAttr.getName()), |
||
115 | sourceAttr.getScale(), |
||
116 | targetAttr.getScale() |
||
117 | ); |
||
118 | break;
|
||
119 | case DataTypes.TIMESTAMP:
|
||
120 | if( targetAttr.getType()==DataTypes.STRING ) {
|
||
121 | // Cuando no hay fichero DAL puede leer un TIMESTAMP como
|
||
122 | // un STRING de tama?o TIMESTAMP_SIZE.
|
||
123 | assertEquals( |
||
124 | String.format("Field %s size mismatch", sourceAttr.getName()), |
||
125 | FieldFormatter.TIMESTAMP_SIZE, |
||
126 | targetAttr.getSize() |
||
127 | ); |
||
128 | } else if( targetAttr.getType()==DataTypes.TIMESTAMP ) { |
||
129 | // Si hay fichero DAL el tipo pude ser correcto (TIMESTAMP).
|
||
130 | assertEquals( |
||
131 | String.format("Field %s size mismatch", sourceAttr.getName()), |
||
132 | sourceAttr.getSize(), |
||
133 | targetAttr.getSize() |
||
134 | ); |
||
135 | } |
||
136 | assertEquals( |
||
137 | String.format("Field %s precision mismatch", sourceAttr.getName()), |
||
138 | sourceAttr.getPrecision(), |
||
139 | targetAttr.getPrecision() |
||
140 | ); |
||
141 | assertEquals( |
||
142 | String.format("Field %s scale mismatch", sourceAttr.getName()), |
||
143 | sourceAttr.getScale(), |
||
144 | targetAttr.getScale() |
||
145 | ); |
||
146 | break;
|
||
147 | case DataTypes.BYTE:
|
||
148 | case DataTypes.INT:
|
||
149 | case DataTypes.LONG:
|
||
150 | case DataTypes.BOOLEAN:
|
||
151 | case DataTypes.DATE:
|
||
152 | case DataTypes.STRING:
|
||
153 | case DataTypes.DECIMAL:
|
||
154 | case DataTypes.DOUBLE:
|
||
155 | case DataTypes.FLOAT:
|
||
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 scale mismatch", sourceAttr.getName()), |
||
173 | sourceAttr.getScale(), |
||
174 | targetAttr.getScale() |
||
175 | ); |
||
176 | break;
|
||
177 | default:
|
||
178 | fail("Type '"+targetAttr.getType()+"' not supported for field '"+targetAttr.getName()+"'."); |
||
179 | } |
||
180 | } |
||
181 | 43512 | jjdelcerro | } |
182 | |||
183 | 44669 | jjdelcerro | protected void copyFrom(FeatureStore sourceStore, int mode) throws Exception { |
184 | FeatureStore targetStore = openTargetStore1(); |
||
185 | targetStore.edit(mode); |
||
186 | try {
|
||
187 | for (Feature sourceFeature : sourceStore.getFeatureSet()) {
|
||
188 | EditableFeature targetFeature = targetStore.createNewFeature(sourceFeature); |
||
189 | targetStore.insert(targetFeature); |
||
190 | } |
||
191 | } finally {
|
||
192 | targetStore.finishEditing(); |
||
193 | } |
||
194 | 43512 | jjdelcerro | } |
195 | |||
196 | 44669 | jjdelcerro | protected void checkData(FeatureStore sourceStore) throws Exception { |
197 | FeatureStore targetStore = openTargetStore1(); |
||
198 | 44139 | jjdelcerro | |
199 | 44669 | jjdelcerro | List<Feature> sourceFeatures = sourceStore.getFeatures();
|
200 | List<Feature> targetFeatures = targetStore.getFeatures();
|
||
201 | assertEquals("Count features", sourceFeatures.size(), targetFeatures.size());
|
||
202 | for (int i = 0; i < targetFeatures.size(); i++) { |
||
203 | Feature sourceFeature = sourceFeatures.get(i); |
||
204 | Feature targetFeature = targetFeatures.get(i); |
||
205 | for (FeatureAttributeDescriptor sourceAttr : sourceStore.getDefaultFeatureType()) {
|
||
206 | switch(sourceAttr.getType()) {
|
||
207 | case DataTypes.TIMESTAMP:
|
||
208 | 44767 | jjdelcerro | assertEquals( |
209 | String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
||
210 | sourceFeature.get(sourceAttr.getName()), |
||
211 | targetFeature.getTimestamp(sourceAttr.getName()) |
||
212 | ); |
||
213 | break;
|
||
214 | 44669 | jjdelcerro | case DataTypes.TIME:
|
215 | assertEquals( |
||
216 | String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
||
217 | 44767 | jjdelcerro | sourceFeature.get(sourceAttr.getName()), |
218 | targetFeature.getTime(sourceAttr.getName()) |
||
219 | 44669 | jjdelcerro | ); |
220 | break;
|
||
221 | case DataTypes.STRING:
|
||
222 | assertEquals( |
||
223 | String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
||
224 | StringUtils.defaultIfBlank(sourceFeature.getString(sourceAttr.getName()), null),
|
||
225 | targetFeature.get(sourceAttr.getName()) |
||
226 | ); |
||
227 | break;
|
||
228 | default:
|
||
229 | assertEquals( |
||
230 | String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
||
231 | sourceFeature.get(sourceAttr.getName()), |
||
232 | targetFeature.get(sourceAttr.getName()) |
||
233 | ); |
||
234 | } |
||
235 | } |
||
236 | } |
||
237 | 43512 | jjdelcerro | } |
238 | |||
239 | 44384 | jjdelcerro | |
240 | 44669 | jjdelcerro | public void testCreateWithDALFile() throws Exception { |
241 | FeatureStore sourceStore = TestUtils.openSourceStore1(); |
||
242 | 44384 | jjdelcerro | |
243 | 44669 | jjdelcerro | createFrom(sourceStore); |
244 | 43512 | jjdelcerro | |
245 | 44669 | jjdelcerro | checkTypes(sourceStore.getDefaultFeatureType()); |
246 | copyFrom(sourceStore, FeatureStore.MODE_APPEND); |
||
247 | checkData(sourceStore); |
||
248 | 44098 | jjdelcerro | |
249 | 44669 | jjdelcerro | createFrom(sourceStore); |
250 | copyFrom(sourceStore, FeatureStore.MODE_FULLEDIT); |
||
251 | checkData(sourceStore); |
||
252 | 44139 | jjdelcerro | |
253 | 44098 | jjdelcerro | } |
254 | |||
255 | 44669 | jjdelcerro | public void testCreateWithoutDALFile() throws Exception { |
256 | FeatureStore sourceStore = TestUtils.openSourceStore1(); |
||
257 | 44098 | jjdelcerro | |
258 | 44669 | jjdelcerro | createFrom(sourceStore); |
259 | TestUtils.removeDALFile(this.getTargetFilename());
|
||
260 | 44098 | jjdelcerro | |
261 | 44669 | jjdelcerro | checkTypes(sourceStore.getDefaultFeatureType()); |
262 | copyFrom(sourceStore, FeatureStore.MODE_APPEND); |
||
263 | TestUtils.removeDALFile(this.getTargetFilename());
|
||
264 | checkData(sourceStore); |
||
265 | 44098 | jjdelcerro | |
266 | 44669 | jjdelcerro | createFrom(sourceStore); |
267 | TestUtils.removeDALFile(this.getTargetFilename());
|
||
268 | copyFrom(sourceStore, FeatureStore.MODE_FULLEDIT); |
||
269 | TestUtils.removeDALFile(this.getTargetFilename());
|
||
270 | checkData(sourceStore); |
||
271 | 44139 | jjdelcerro | |
272 | 44098 | jjdelcerro | } |
273 | 43939 | jjdelcerro | } |