svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.h2spatial / org.gvsig.h2spatial.h2gis132 / org.gvsig.h2spatial.h2gis132.provider / src / test / java / org / gvsig / fmap / dal / store / h2 / TestComputedAttributes.java @ 46050
History | View | Annotate | Download (18 KB)
1 | 45472 | jjdelcerro | package org.gvsig.fmap.dal.store.h2; |
---|---|---|---|
2 | |||
3 | 46050 | omartinez | import java.util.ArrayList; |
4 | 45472 | jjdelcerro | import java.util.Date; |
5 | import java.util.List; |
||
6 | import junit.framework.TestCase; |
||
7 | import static junit.framework.TestCase.assertEquals; |
||
8 | import org.gvsig.expressionevaluator.ExpressionBuilder; |
||
9 | import org.gvsig.expressionevaluator.ExpressionUtils; |
||
10 | import org.gvsig.fmap.dal.DALLocator; |
||
11 | import org.gvsig.fmap.dal.DataTypes; |
||
12 | import org.gvsig.fmap.dal.DataManager; |
||
13 | import org.gvsig.fmap.dal.DataStore; |
||
14 | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
||
15 | import org.gvsig.fmap.dal.feature.EditableFeature; |
||
16 | 46050 | omartinez | import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
17 | 45472 | jjdelcerro | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
18 | import org.gvsig.fmap.dal.feature.Feature; |
||
19 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
20 | import org.gvsig.fmap.dal.feature.FeatureQuery; |
||
21 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
22 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
23 | import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
||
24 | 46050 | omartinez | import org.gvsig.fmap.dal.impl.expressionevaluator.DefaultFeatureAttributeEmulatorExpression; |
25 | 45472 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
26 | import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer; |
||
27 | 46050 | omartinez | import org.gvsig.tools.dispose.DisposeUtils; |
28 | 45472 | jjdelcerro | import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
29 | import org.slf4j.Logger; |
||
30 | import org.slf4j.LoggerFactory; |
||
31 | |||
32 | 46050 | omartinez | public class TestComputedAttributes extends TestCase { |
33 | private static final Logger LOGGER = LoggerFactory.getLogger(TestComputedAttributes.class); |
||
34 | 45472 | jjdelcerro | |
35 | public static final String DBNAME = "testCreate"; |
||
36 | |||
37 | 46050 | omartinez | public TestComputedAttributes(String testName) { |
38 | 45472 | jjdelcerro | super(testName);
|
39 | } |
||
40 | |||
41 | @Override
|
||
42 | protected void setUp() throws Exception { |
||
43 | super.setUp();
|
||
44 | new DefaultLibrariesInitializer().fullInitialize();
|
||
45 | } |
||
46 | |||
47 | @Override
|
||
48 | protected void tearDown() throws Exception { |
||
49 | super.tearDown();
|
||
50 | } |
||
51 | |||
52 | // TODO add test methods here. The name must begin with 'test'. For example:
|
||
53 | // public void testHello() {}
|
||
54 | |||
55 | protected String getProviderName() { |
||
56 | return DataStore.H2SPATIAL_PROVIDER_NAME;
|
||
57 | } |
||
58 | |||
59 | protected String getTargetName() { |
||
60 | 46050 | omartinez | return "testCreateTarget2"; |
61 | 45472 | jjdelcerro | } |
62 | |||
63 | protected FeatureStore openTargetStore1(JDBCServerExplorer explorer) throws Exception { |
||
64 | JDBCStoreParameters params = explorer.get(getTargetName()); |
||
65 | |||
66 | DataManager dataManager = DALLocator.getDataManager(); |
||
67 | FeatureStore store; |
||
68 | try {
|
||
69 | store = (FeatureStore) dataManager.openStore( |
||
70 | getProviderName(), |
||
71 | params |
||
72 | ); |
||
73 | } catch(ValidateDataParametersException ex) {
|
||
74 | LOGGER.warn(ex.getLocalizedMessageStack()); |
||
75 | throw ex;
|
||
76 | } |
||
77 | return store;
|
||
78 | } |
||
79 | |||
80 | protected void createFrom(JDBCServerExplorer explorer, FeatureStore sourceStore) throws Exception { |
||
81 | NewFeatureStoreParameters params = (NewFeatureStoreParameters) explorer.getAddParameters( |
||
82 | getTargetName() |
||
83 | ); |
||
84 | EditableFeatureType ft = params.getDefaultFeatureType(); |
||
85 | ft.addAll(sourceStore.getDefaultFeatureType()); |
||
86 | explorer.add(getProviderName(), params, true);
|
||
87 | } |
||
88 | |||
89 | protected void checkTypes(JDBCServerExplorer explorer, FeatureType sourceFeatureType) throws Exception { |
||
90 | // DataType STRING_TYPE = ToolsLocator.getDataTypesManager().get(DataTypes.STRING);
|
||
91 | // DataType INT_TYPE = ToolsLocator.getDataTypesManager().get(DataTypes.INT);
|
||
92 | |||
93 | FeatureStore targetStore = openTargetStore1(explorer); |
||
94 | FeatureType targetFeatureType = targetStore.getDefaultFeatureType(); |
||
95 | |||
96 | assertEquals("Feature type size",sourceFeatureType.size(), targetFeatureType.size());
|
||
97 | for (int i = 0; i < sourceFeatureType.size(); i++) { |
||
98 | FeatureAttributeDescriptor sourceAttr = sourceFeatureType.get(i); |
||
99 | FeatureAttributeDescriptor targetAttr = targetFeatureType.get(i); |
||
100 | switch(sourceAttr.getType()) {
|
||
101 | case DataTypes.BYTE:
|
||
102 | case DataTypes.INT:
|
||
103 | case DataTypes.LONG:
|
||
104 | case DataTypes.BOOLEAN:
|
||
105 | case DataTypes.DATE:
|
||
106 | case DataTypes.STRING:
|
||
107 | case DataTypes.TIME:
|
||
108 | case DataTypes.TIMESTAMP:
|
||
109 | case DataTypes.DOUBLE:
|
||
110 | case DataTypes.FLOAT:
|
||
111 | case DataTypes.DECIMAL:
|
||
112 | assertEquals( |
||
113 | String.format("Field %s name mismatch", sourceAttr.getName()), |
||
114 | sourceAttr.getName(), |
||
115 | targetAttr.getName() |
||
116 | ); |
||
117 | assertEquals( |
||
118 | String.format("Field %s type mismatch", sourceAttr.getName()), |
||
119 | sourceAttr.getDataTypeName(), |
||
120 | targetAttr.getDataTypeName() |
||
121 | ); |
||
122 | assertEquals( |
||
123 | String.format("Field %s size mismatch", sourceAttr.getName()), |
||
124 | sourceAttr.getSize(), |
||
125 | targetAttr.getSize() |
||
126 | ); |
||
127 | assertEquals( |
||
128 | String.format("Field %s precision mismatch", sourceAttr.getName()), |
||
129 | sourceAttr.getPrecision(), |
||
130 | targetAttr.getPrecision() |
||
131 | ); |
||
132 | assertEquals( |
||
133 | String.format("Field %s scale mismatch", sourceAttr.getName()), |
||
134 | sourceAttr.getScale(), |
||
135 | targetAttr.getScale() |
||
136 | ); |
||
137 | break;
|
||
138 | case DataTypes.GEOMETRY:
|
||
139 | assertEquals( |
||
140 | String.format("Field %s name mismatch", sourceAttr.getName()), |
||
141 | sourceAttr.getName(), |
||
142 | targetAttr.getName() |
||
143 | ); |
||
144 | assertEquals( |
||
145 | String.format("Field %s type mismatch", sourceAttr.getName()), |
||
146 | sourceAttr.getDataTypeName(), |
||
147 | targetAttr.getDataTypeName() |
||
148 | ); |
||
149 | assertEquals( |
||
150 | String.format("Field %s geometry type mismatch", sourceAttr.getName()), |
||
151 | sourceAttr.getGeomType().getName(), |
||
152 | targetAttr.getGeomType().getName() |
||
153 | ); |
||
154 | assertEquals( |
||
155 | String.format("Field %s geometry SRS mismatch", sourceAttr.getName()), |
||
156 | sourceAttr.getSRS().toString(), |
||
157 | targetAttr.getSRS().toString() |
||
158 | ); |
||
159 | assertEquals( |
||
160 | String.format("Field %s size mismatch", sourceAttr.getName()), |
||
161 | sourceAttr.getSize(), |
||
162 | targetAttr.getSize() |
||
163 | ); |
||
164 | assertEquals( |
||
165 | String.format("Field %s precision mismatch", sourceAttr.getName()), |
||
166 | sourceAttr.getPrecision(), |
||
167 | targetAttr.getPrecision() |
||
168 | ); |
||
169 | break;
|
||
170 | default:
|
||
171 | fail( |
||
172 | String.format("Field %s type %d (%s) not supported.", |
||
173 | targetAttr.getName(), |
||
174 | targetAttr.getType(), |
||
175 | targetAttr.getDataTypeName() |
||
176 | ) |
||
177 | ); |
||
178 | } |
||
179 | } |
||
180 | } |
||
181 | |||
182 | protected void copyFrom(JDBCServerExplorer explorer, FeatureStore sourceStore, int mode) throws Exception { |
||
183 | FeatureStore targetStore = openTargetStore1(explorer); |
||
184 | targetStore.edit(mode); |
||
185 | try {
|
||
186 | for (Feature sourceFeature : sourceStore.getFeatureSet()) {
|
||
187 | EditableFeature targetFeature = targetStore.createNewFeature(sourceFeature); |
||
188 | targetStore.insert(targetFeature); |
||
189 | } |
||
190 | } finally {
|
||
191 | targetStore.finishEditing(); |
||
192 | } |
||
193 | } |
||
194 | |||
195 | protected void checkData(JDBCServerExplorer explorer, FeatureStore sourceStore) throws Exception { |
||
196 | FeatureStore targetStore = openTargetStore1(explorer); |
||
197 | |||
198 | List<Feature> sourceFeatures = sourceStore.getFeatures();
|
||
199 | List<Feature> targetFeatures = targetStore.getFeatures();
|
||
200 | assertEquals("Count features", sourceFeatures.size(), targetFeatures.size());
|
||
201 | for (int i = 0; i < targetFeatures.size(); i++) { |
||
202 | Feature sourceFeature = sourceFeatures.get(i); |
||
203 | Feature targetFeature = targetFeatures.get(i); |
||
204 | for (FeatureAttributeDescriptor sourceAttr : sourceStore.getDefaultFeatureType()) {
|
||
205 | switch(sourceAttr.getType()) {
|
||
206 | case DataTypes.BYTE:
|
||
207 | assertEquals( |
||
208 | String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
||
209 | sourceFeature.getInt(sourceAttr.getName()), |
||
210 | targetFeature.getInt(sourceAttr.getName()) |
||
211 | ); |
||
212 | break;
|
||
213 | case DataTypes.TIMESTAMP:
|
||
214 | Date sourceTimestamp = sourceFeature.getDate(sourceAttr.getName());
|
||
215 | Date targetTimestamp = targetFeature.getDate(sourceAttr.getName());
|
||
216 | assertEquals( |
||
217 | String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
||
218 | sourceTimestamp, |
||
219 | targetTimestamp |
||
220 | ); |
||
221 | break;
|
||
222 | case DataTypes.TIME:
|
||
223 | assertEquals( |
||
224 | String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
||
225 | sourceFeature.getDate(sourceAttr.getName()), |
||
226 | targetFeature.getDate(sourceAttr.getName()) |
||
227 | ); |
||
228 | break;
|
||
229 | case DataTypes.GEOMETRY:
|
||
230 | assertEquals( |
||
231 | String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
||
232 | sourceFeature.get(sourceAttr.getName()), |
||
233 | targetFeature.get(sourceAttr.getName()) |
||
234 | ); |
||
235 | break;
|
||
236 | case DataTypes.STRING:
|
||
237 | case DataTypes.INT:
|
||
238 | case DataTypes.LONG:
|
||
239 | case DataTypes.FLOAT:
|
||
240 | case DataTypes.DOUBLE:
|
||
241 | default:
|
||
242 | Object sourceValue = sourceFeature.get(sourceAttr.getName());
|
||
243 | Object targetValue = targetFeature.get(sourceAttr.getName());
|
||
244 | if( sourceValue == null ) { |
||
245 | LOGGER.info(String.format("Feature %03d attribute %s is null", i, sourceAttr.getName())); |
||
246 | } |
||
247 | assertEquals( |
||
248 | String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
||
249 | sourceValue, |
||
250 | targetValue |
||
251 | ); |
||
252 | } |
||
253 | } |
||
254 | } |
||
255 | } |
||
256 | |||
257 | |||
258 | 46050 | omartinez | public void testComputed1() throws Exception { |
259 | FeatureStore sourceStore = TestUtils.openSourceStore2(); |
||
260 | 45472 | jjdelcerro | JDBCServerExplorer explorer = TestUtils.openServerExplorer(DBNAME); |
261 | |||
262 | createFrom(explorer, sourceStore); |
||
263 | copyFrom(explorer, sourceStore, FeatureStore.MODE_APPEND); |
||
264 | 46050 | omartinez | |
265 | FeatureStore h2Store = openTargetStore1(explorer); |
||
266 | h2Store.edit(); |
||
267 | FeatureType featureType = h2Store.getDefaultFeatureType(); |
||
268 | EditableFeatureType eFeatureType = featureType.getEditable(); |
||
269 | eFeatureType.add("Compu1",
|
||
270 | DataTypes.INTEGER, |
||
271 | new DefaultFeatureAttributeEmulatorExpression(
|
||
272 | eFeatureType, |
||
273 | ExpressionUtils.createExpression("ID*2")
|
||
274 | )); |
||
275 | eFeatureType.add("Compu2",
|
||
276 | DataTypes.INTEGER, |
||
277 | new DefaultFeatureAttributeEmulatorExpression(
|
||
278 | eFeatureType, |
||
279 | ExpressionUtils.createExpression("Poblacion+10000+Compu1")
|
||
280 | )); |
||
281 | h2Store.update(eFeatureType); |
||
282 | h2Store.finishEditing(); |
||
283 | List<Feature> features = h2Store.getFeatures();
|
||
284 | for (int i = 0; i < features.size(); i++) { |
||
285 | Feature feature = features.get(i); |
||
286 | assertEquals("Compu1 "+i, feature.getInt("ID") * 2, feature.getInt("Compu1")); |
||
287 | if(feature.get("Poblacion")==null) { |
||
288 | assertEquals("Compu2 "+i, null, feature.get("Compu2")); |
||
289 | } else {
|
||
290 | assertEquals("Compu2 "+i, feature.getInt("Poblacion") + 10000 + feature.getInt("Compu1"), feature.getInt("Compu2")); |
||
291 | } |
||
292 | } |
||
293 | DisposeUtils.dispose(h2Store); |
||
294 | 45472 | jjdelcerro | } |
295 | 46050 | omartinez | |
296 | public void testComputed2() throws Exception { |
||
297 | FeatureStore sourceStore = TestUtils.openSourceStore2(); |
||
298 | 45472 | jjdelcerro | JDBCServerExplorer explorer = TestUtils.openServerExplorer(DBNAME); |
299 | |||
300 | createFrom(explorer, sourceStore); |
||
301 | copyFrom(explorer, sourceStore, FeatureStore.MODE_APPEND); |
||
302 | |||
303 | FeatureStore h2Store = openTargetStore1(explorer); |
||
304 | 46050 | omartinez | h2Store.edit(); |
305 | FeatureType featureType = h2Store.getDefaultFeatureType(); |
||
306 | EditableFeatureType eFeatureType = featureType.getEditable(); |
||
307 | FeatureQuery query = sourceStore.createFeatureQuery(); |
||
308 | eFeatureType.add("CompuID",
|
||
309 | DataTypes.INTEGER, |
||
310 | new DefaultFeatureAttributeEmulatorExpression(
|
||
311 | eFeatureType, |
||
312 | ExpressionUtils.createExpression("MOD(ID,10)")
|
||
313 | )); |
||
314 | eFeatureType.add("CompuPob",
|
||
315 | DataTypes.INTEGER, |
||
316 | new DefaultFeatureAttributeEmulatorExpression(
|
||
317 | eFeatureType, |
||
318 | ExpressionUtils.createExpression("Poblacion+1")
|
||
319 | )); |
||
320 | eFeatureType.add("CompuProv",
|
||
321 | DataTypes.STRING, |
||
322 | new DefaultFeatureAttributeEmulatorExpression(
|
||
323 | eFeatureType, |
||
324 | ExpressionUtils.createExpression("UPPER(Provincia)")
|
||
325 | )); |
||
326 | EditableFeatureAttributeDescriptor extraColumn1 = |
||
327 | query.getExtraColumn().add("ExtraID900", DataTypes.INTEGER);
|
||
328 | EditableFeatureAttributeDescriptor extraColumn2 = |
||
329 | query.getExtraColumn().add("ExtraPobDen", DataTypes.INTEGER);
|
||
330 | EditableFeatureAttributeDescriptor extraColumn3 = |
||
331 | query.getExtraColumn().add("ExtraAno", DataTypes.INTEGER);
|
||
332 | 45472 | jjdelcerro | |
333 | 46050 | omartinez | extraColumn1.setFeatureAttributeEmulator( |
334 | new DefaultFeatureAttributeEmulatorExpression(
|
||
335 | eFeatureType, |
||
336 | ExpressionUtils.createExpression("CompuID+900")));
|
||
337 | extraColumn2.setFeatureAttributeEmulator( |
||
338 | new DefaultFeatureAttributeEmulatorExpression(
|
||
339 | eFeatureType, |
||
340 | ExpressionUtils.createExpression("CompuPob+Densidad")));
|
||
341 | extraColumn3.setFeatureAttributeEmulator( |
||
342 | new DefaultFeatureAttributeEmulatorExpression(
|
||
343 | eFeatureType, |
||
344 | ExpressionUtils.createExpression("EXTRACT(YEAR FROM Fecha)")));
|
||
345 | |||
346 | query.getGroupByColumns().add("Comunidad");
|
||
347 | query.getGroupByColumns().add("CompuProv");
|
||
348 | query.getGroupByColumns().add("ExtraAno");
|
||
349 | query.getAggregateFunctions().put("ID", "MIN"); |
||
350 | query.getAggregateFunctions().put("Poblacion", "MIN"); |
||
351 | query.getAggregateFunctions().put("CompuID", "MIN"); |
||
352 | query.getAggregateFunctions().put("CompuPob", "SUM"); |
||
353 | query.getAggregateFunctions().put("ExtraID900", "SUM"); |
||
354 | query.getAggregateFunctions().put("ExtraPobDen", "SUM"); |
||
355 | h2Store.update(eFeatureType); |
||
356 | h2Store.finishEditing(); |
||
357 | |||
358 | |||
359 | List<Feature> features0 = h2Store.getFeatures(query);
|
||
360 | ArrayList<Feature> features = new ArrayList<>(); |
||
361 | |||
362 | System.out.println("ID,Comunidad,Provincia,Ciudad,Poblacion,Densidad,Fecha,CompuID,CompuPob,CompuProv,ExtraID900,ExtraPobDen,ExtraAno"); |
||
363 | for (int i = 0; i < features0.size(); i++) { |
||
364 | Feature feature = features0.get(i); |
||
365 | features.add(feature.getCopy()); |
||
366 | System.out.print(feature.toString());
|
||
367 | System.out.print(","+feature.get("ExtraID900")); |
||
368 | System.out.print(","+feature.get("ExtraPobDen")); |
||
369 | System.out.println(","+feature.get("ExtraAno")); |
||
370 | } |
||
371 | System.out.println("ID,Comunidad,Provincia,Ciudad,Poblacion,Densidad,Fecha,CompuID,CompuPob,CompuProv,ExtraID900,ExtraPobDen,ExtraAno"); |
||
372 | |||
373 | features0 = null;
|
||
374 | String[] header = new String[]{"ID","Comunidad","Provincia","Ciudad","Poblacion","Densidad","Fecha","CompuID","CompuPob","CompuProv","ExtraID900","ExtraPobDen","ExtraAno"}; |
||
375 | ArrayList<Object[]> values = new ArrayList<Object[]>(); |
||
376 | values.add(new Object[]{0, null, null, null, null, null, null, 0, null,"",900,null,null}); |
||
377 | values.add(new Object[]{1, "GVA", null, null, 500, null, null, 1, 1502, "VALENCIA",1803,1505,2019}); |
||
378 | values.add(new Object[]{3, "GVA", null, null, 50, null, null, 3, 352, "VALENCIA",1807,359,2020}); |
||
379 | values.add(new Object[]{5, "GVA", null, null, 200, null, null, 5, 201, "ALICANTE",905,206,2019}); |
||
380 | values.add(new Object[]{6, "GVA", null, null, 20, null, null, 6, 422, "ALICANTE",1813,1135,2020}); |
||
381 | values.add(new Object[]{8, "GVA", null, null, 100, null, null, 8, 702, "CASTELLON",1817,719,2019}); |
||
382 | for (int i = 0; i < features.size(); i++) { |
||
383 | for (int j = 0; j < header.length; j++) { |
||
384 | assertEquals("feature["+i+"]["+header[j]+"]:", values.get(i)[j],features.get(i).get(header[j])); |
||
385 | } |
||
386 | } |
||
387 | |||
388 | DisposeUtils.dispose(h2Store); |
||
389 | 45472 | jjdelcerro | } |
390 | 46050 | omartinez | |
391 | |||
392 | 45472 | jjdelcerro | |
393 | } |