svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.jdbc / src / test / java / org / gvsig / fmap / dal / store / jdbc2 / AbstractTestComputedAttributes.java @ 46101
History | View | Annotate | Download (8.92 KB)
1 |
package org.gvsig.fmap.dal.store.jdbc2; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.List; |
5 |
import junit.framework.TestCase; |
6 |
import static junit.framework.TestCase.assertEquals; |
7 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
8 |
import org.gvsig.fmap.dal.DataTypes; |
9 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
10 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
11 |
import org.gvsig.fmap.dal.feature.Feature; |
12 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
13 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
14 |
import org.gvsig.fmap.dal.feature.FeatureType; |
15 |
import org.gvsig.fmap.dal.impl.expressionevaluator.DefaultFeatureAttributeEmulatorExpression; |
16 |
import org.gvsig.tools.dispose.DisposeUtils; |
17 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
18 |
import org.slf4j.Logger; |
19 |
import org.slf4j.LoggerFactory; |
20 |
|
21 |
public abstract class AbstractTestComputedAttributes extends TestCase { |
22 |
|
23 |
protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractTestComputedAttributes.class); |
24 |
|
25 |
protected AbstractTestUtils utils;
|
26 |
|
27 |
public AbstractTestComputedAttributes(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 |
public AbstractTestUtils utils() {
|
43 |
if (this.utils == null) { |
44 |
this.utils = this.createUtils(); |
45 |
} |
46 |
return this.utils; |
47 |
} |
48 |
|
49 |
protected abstract AbstractTestUtils createUtils(); |
50 |
|
51 |
// TODO add test methods here. The name must begin with 'test'. For example:
|
52 |
// public void testHello() {}
|
53 |
|
54 |
public void testComputed1() throws Exception { |
55 |
if( !utils().isTheDatabaseAvailable() ) {
|
56 |
return;
|
57 |
} |
58 |
FeatureStore sourceStore = utils().openSourceStore2(); |
59 |
JDBCServerExplorer explorer = utils().openServerExplorer("testCreate");
|
60 |
|
61 |
utils().info_jdbc(explorer); |
62 |
|
63 |
utils().drop_tables(explorer, "testCreateTarget2");
|
64 |
utils().create_table_from(explorer, "testCreateTarget2", sourceStore);
|
65 |
utils().insert_into_from(explorer, "testCreateTarget2", sourceStore, FeatureStore.MODE_APPEND);
|
66 |
|
67 |
FeatureStore dbstore = utils().openStore(explorer,"testCreateTarget2");
|
68 |
dbstore.edit(); |
69 |
FeatureType featureType = dbstore.getDefaultFeatureType(); |
70 |
EditableFeatureType eFeatureType = featureType.getEditable(); |
71 |
eFeatureType.add("Compu1",
|
72 |
DataTypes.INTEGER, |
73 |
new DefaultFeatureAttributeEmulatorExpression(
|
74 |
eFeatureType, |
75 |
ExpressionUtils.createExpression("ID*2")
|
76 |
)); |
77 |
eFeatureType.add("Compu2",
|
78 |
DataTypes.INTEGER, |
79 |
new DefaultFeatureAttributeEmulatorExpression(
|
80 |
eFeatureType, |
81 |
ExpressionUtils.createExpression("Poblacion+10000+Compu1")
|
82 |
)); |
83 |
dbstore.update(eFeatureType); |
84 |
dbstore.finishEditing(); |
85 |
List<Feature> features = dbstore.getFeatures();
|
86 |
for (int i = 0; i < features.size(); i++) { |
87 |
Feature feature = features.get(i); |
88 |
assertEquals("Compu1 "+i, feature.getInt("ID") * 2, feature.getInt("Compu1")); |
89 |
if(feature.get("Poblacion")==null) { |
90 |
assertEquals("Compu2 "+i, null, feature.get("Compu2")); |
91 |
} else {
|
92 |
assertEquals("Compu2 "+i, feature.getInt("Poblacion") + 10000 + feature.getInt("Compu1"), feature.getInt("Compu2")); |
93 |
} |
94 |
} |
95 |
DisposeUtils.dispose(dbstore); |
96 |
} |
97 |
|
98 |
@SuppressWarnings("UnusedAssignment") |
99 |
public void testComputed2() throws Exception { |
100 |
if( !utils().isTheDatabaseAvailable() ) {
|
101 |
return;
|
102 |
} |
103 |
String testTableName = "testComputedAttributes2"; |
104 |
JDBCServerExplorer explorer = utils().openServerExplorer("computedAttr");
|
105 |
|
106 |
utils().info_jdbc(explorer); |
107 |
utils().drop_tables(explorer, testTableName); |
108 |
|
109 |
FeatureStore sourceStore = utils().openSourceStore2(); |
110 |
|
111 |
utils().create_table_from(explorer, testTableName, sourceStore); |
112 |
utils().insert_into_from(explorer, testTableName, sourceStore, FeatureStore.MODE_APPEND); |
113 |
|
114 |
FeatureStore dbstore = utils().openStore(explorer,testTableName); |
115 |
|
116 |
dbstore.edit(); |
117 |
FeatureType featureType = dbstore.getDefaultFeatureType(); |
118 |
EditableFeatureType eFeatureType = featureType.getEditable(); |
119 |
FeatureQuery query = sourceStore.createFeatureQuery(); |
120 |
eFeatureType.add("CompuID",
|
121 |
DataTypes.INTEGER, |
122 |
new DefaultFeatureAttributeEmulatorExpression(
|
123 |
eFeatureType, |
124 |
ExpressionUtils.createExpression("MOD(ID,10)")
|
125 |
)); |
126 |
eFeatureType.add("CompuPob",
|
127 |
DataTypes.INTEGER, |
128 |
new DefaultFeatureAttributeEmulatorExpression(
|
129 |
eFeatureType, |
130 |
ExpressionUtils.createExpression("Poblacion+1")
|
131 |
)); |
132 |
eFeatureType.add("CompuProv",
|
133 |
DataTypes.STRING, |
134 |
new DefaultFeatureAttributeEmulatorExpression(
|
135 |
eFeatureType, |
136 |
ExpressionUtils.createExpression("UPPER(Provincia)")
|
137 |
)); |
138 |
EditableFeatureAttributeDescriptor extraColumn1 = |
139 |
query.getExtraColumn().add("ExtraID900", DataTypes.INTEGER);
|
140 |
EditableFeatureAttributeDescriptor extraColumn2 = |
141 |
query.getExtraColumn().add("ExtraPobDen", DataTypes.INTEGER);
|
142 |
EditableFeatureAttributeDescriptor extraColumn3 = |
143 |
query.getExtraColumn().add("ExtraAno", DataTypes.INTEGER);
|
144 |
|
145 |
extraColumn1.setFeatureAttributeEmulator( |
146 |
new DefaultFeatureAttributeEmulatorExpression(
|
147 |
eFeatureType, |
148 |
ExpressionUtils.createExpression("CompuID+900")));
|
149 |
extraColumn2.setFeatureAttributeEmulator( |
150 |
new DefaultFeatureAttributeEmulatorExpression(
|
151 |
eFeatureType, |
152 |
ExpressionUtils.createExpression("CompuPob+Densidad")));
|
153 |
extraColumn3.setFeatureAttributeEmulator( |
154 |
new DefaultFeatureAttributeEmulatorExpression(
|
155 |
eFeatureType, |
156 |
ExpressionUtils.createExpression("EXTRACT(YEAR FROM Fecha)")));
|
157 |
|
158 |
query.getGroupByColumns().add("Comunidad");
|
159 |
query.getGroupByColumns().add("CompuProv");
|
160 |
query.getGroupByColumns().add("ExtraAno");
|
161 |
query.getAggregateFunctions().put("ID", "MIN"); |
162 |
query.getAggregateFunctions().put("Poblacion", "MIN"); |
163 |
query.getAggregateFunctions().put("CompuID", "MIN"); |
164 |
query.getAggregateFunctions().put("CompuPob", "SUM"); |
165 |
query.getAggregateFunctions().put("ExtraID900", "SUM"); |
166 |
query.getAggregateFunctions().put("ExtraPobDen", "SUM"); |
167 |
dbstore.update(eFeatureType); |
168 |
dbstore.finishEditing(); |
169 |
|
170 |
|
171 |
List<Feature> features0 = dbstore.getFeatures(query);
|
172 |
ArrayList<Feature> features = new ArrayList<>(); |
173 |
|
174 |
System.out.println("ID,Comunidad,Provincia,Ciudad,Poblacion,Densidad,Fecha,CompuID,CompuPob,CompuProv,ExtraID900,ExtraPobDen,ExtraAno"); |
175 |
for (int i = 0; i < features0.size(); i++) { |
176 |
Feature feature = features0.get(i); |
177 |
features.add(feature.getCopy()); |
178 |
System.out.print(feature.toString());
|
179 |
System.out.print(","+feature.get("ExtraID900")); |
180 |
System.out.print(","+feature.get("ExtraPobDen")); |
181 |
System.out.println(","+feature.get("ExtraAno")); |
182 |
} |
183 |
System.out.println("ID,Comunidad,Provincia,Ciudad,Poblacion,Densidad,Fecha,CompuID,CompuPob,CompuProv,ExtraID900,ExtraPobDen,ExtraAno"); |
184 |
|
185 |
features0 = null;
|
186 |
String[] header = new String[]{"ID","Comunidad","Provincia","Ciudad","Poblacion","Densidad","Fecha","CompuID","CompuPob","CompuProv","ExtraID900","ExtraPobDen","ExtraAno"}; |
187 |
ArrayList<Object[]> values = new ArrayList<>(); |
188 |
values.add(new Object[]{0, null, null, null, null, null, null, 0, null,"",900,null,null}); |
189 |
values.add(new Object[]{1, "GVA", null, null, 500, null, null, 1, 1502, "VALENCIA",1803,1505,2019}); |
190 |
values.add(new Object[]{3, "GVA", null, null, 50, null, null, 3, 352, "VALENCIA",1807,359,2020}); |
191 |
values.add(new Object[]{5, "GVA", null, null, 200, null, null, 5, 201, "ALICANTE",905,206,2019}); |
192 |
values.add(new Object[]{6, "GVA", null, null, 20, null, null, 6, 422, "ALICANTE",1813,1135,2020}); |
193 |
values.add(new Object[]{8, "GVA", null, null, 100, null, null, 8, 702, "CASTELLON",1817,719,2019}); |
194 |
for (int i = 0; i < features.size(); i++) { |
195 |
for (int j = 0; j < header.length; j++) { |
196 |
assertEquals("feature["+i+"]["+header[j]+"]:", values.get(i)[j],features.get(i).get(header[j])); |
197 |
} |
198 |
} |
199 |
|
200 |
DisposeUtils.dispose(dbstore); |
201 |
} |
202 |
|
203 |
} |