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