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 / AbstractTestFeatureReferenceIteratorToFeatureIterator.java @ 46517
History | View | Annotate | Download (13.7 KB)
1 | 46101 | jjdelcerro | package org.gvsig.fmap.dal.store.jdbc2; |
---|---|---|---|
2 | 45472 | jjdelcerro | |
3 | 46309 | jjdelcerro | import java.util.ArrayList; |
4 | import java.util.HashMap; |
||
5 | import java.util.HashSet; |
||
6 | 46317 | fdiaz | import java.util.Iterator; |
7 | 46309 | jjdelcerro | import java.util.List; |
8 | import java.util.Map; |
||
9 | import java.util.Objects; |
||
10 | import java.util.Set; |
||
11 | 45472 | jjdelcerro | import junit.framework.TestCase; |
12 | import static junit.framework.TestCase.assertEquals; |
||
13 | 46317 | fdiaz | import org.apache.commons.lang3.StringUtils; |
14 | 46309 | jjdelcerro | import org.apache.commons.lang3.tuple.Pair; |
15 | import org.apache.log4j.Level; |
||
16 | import org.apache.log4j.LogManager; |
||
17 | 46317 | fdiaz | import org.gvsig.expressionevaluator.ExpressionBuilder; |
18 | import org.gvsig.expressionevaluator.ExpressionUtils; |
||
19 | 46101 | jjdelcerro | import org.gvsig.fmap.dal.DatabaseWorkspaceManager; |
20 | 46317 | fdiaz | import org.gvsig.fmap.dal.feature.EditableFeature; |
21 | 46309 | jjdelcerro | import org.gvsig.fmap.dal.feature.Feature; |
22 | 46317 | fdiaz | import org.gvsig.fmap.dal.feature.FeatureReference; |
23 | 46309 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureSelection; |
24 | 45472 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureStore; |
25 | 46317 | fdiaz | import org.gvsig.fmap.dal.feature.FeatureStoreNotification; |
26 | 46309 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.impl.ResulSetControlerBase; |
27 | 46050 | omartinez | import org.gvsig.tools.dispose.DisposeUtils; |
28 | 45472 | jjdelcerro | import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
29 | 46317 | fdiaz | import org.gvsig.tools.observer.ComplexObserver; |
30 | import org.gvsig.tools.observer.Observable; |
||
31 | import org.gvsig.tools.observer.Observer; |
||
32 | 45472 | jjdelcerro | import org.slf4j.Logger; |
33 | import org.slf4j.LoggerFactory; |
||
34 | |||
35 | 46309 | jjdelcerro | public abstract class AbstractTestFeatureReferenceIteratorToFeatureIterator extends TestCase { |
36 | 46101 | jjdelcerro | |
37 | 46309 | jjdelcerro | protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractTestFeatureReferenceIteratorToFeatureIterator.class); |
38 | 46101 | jjdelcerro | |
39 | protected AbstractTestUtils utils;
|
||
40 | |||
41 | 46309 | jjdelcerro | public AbstractTestFeatureReferenceIteratorToFeatureIterator(String testName) { |
42 | 45472 | jjdelcerro | super(testName);
|
43 | } |
||
44 | 46101 | jjdelcerro | |
45 | 45472 | jjdelcerro | @Override
|
46 | protected void setUp() throws Exception { |
||
47 | super.setUp();
|
||
48 | new DefaultLibrariesInitializer().fullInitialize();
|
||
49 | } |
||
50 | 46101 | jjdelcerro | |
51 | 45472 | jjdelcerro | @Override
|
52 | protected void tearDown() throws Exception { |
||
53 | super.tearDown();
|
||
54 | } |
||
55 | |||
56 | 46101 | jjdelcerro | public AbstractTestUtils utils() {
|
57 | if (this.utils == null) { |
||
58 | this.utils = this.createUtils(); |
||
59 | 45472 | jjdelcerro | } |
60 | 46101 | jjdelcerro | return this.utils; |
61 | 45472 | jjdelcerro | } |
62 | |||
63 | 46101 | jjdelcerro | protected abstract AbstractTestUtils createUtils(); |
64 | 45472 | jjdelcerro | |
65 | 46309 | jjdelcerro | private List<Pair<org.apache.log4j.Logger,Level>> get_loggers(Object... classes) { |
66 | List<Pair<org.apache.log4j.Logger,Level>> loggers = new ArrayList<>(); |
||
67 | for (Object theClass : classes) { |
||
68 | org.apache.log4j.Logger logger; |
||
69 | if( theClass instanceof Class ) { |
||
70 | logger = LogManager.getLogger((Class)theClass); |
||
71 | } else {
|
||
72 | logger = LogManager.getLogger(Objects.toString(theClass, null)); |
||
73 | } |
||
74 | if( logger!=null ) { |
||
75 | loggers.add(Pair.of(logger,logger.getLevel())); |
||
76 | } |
||
77 | } |
||
78 | return loggers;
|
||
79 | } |
||
80 | |||
81 | private void restore_level(List<Pair<org.apache.log4j.Logger,Level>> loggers) { |
||
82 | for (Pair<org.apache.log4j.Logger, Level> logger : loggers) { |
||
83 | logger.getLeft().setLevel(logger.getRight()); |
||
84 | } |
||
85 | } |
||
86 | |||
87 | private void set_level(List<Pair<org.apache.log4j.Logger,Level>> loggers, Level level) { |
||
88 | for (Pair<org.apache.log4j.Logger, Level> logger : loggers) { |
||
89 | logger.getLeft().setLevel(level); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | protected void testFRI2FI1() throws Exception { |
||
94 | 45472 | jjdelcerro | try {
|
95 | 46101 | jjdelcerro | if (!utils().isTheDatabaseAvailable()) {
|
96 | return;
|
||
97 | 45472 | jjdelcerro | } |
98 | 46309 | jjdelcerro | |
99 | List<Pair<org.apache.log4j.Logger, Level>> loggers = get_loggers(JDBCUtils.class, ResulSetControlerBase.class, "org.gvsig.fmap.dal.store.h2.H2SpatialHelper"); |
||
100 | set_level(loggers, Level.INFO);
|
||
101 | |||
102 | FeatureStore sourceStore = utils().openCSVStore("/org/gvsig/fmap/dal/store/jdbc2/esp_poblaciones-nogeom.csv");
|
||
103 | JDBCServerExplorer explorer = utils().openServerExplorer("testFRI2FI");
|
||
104 | 45472 | jjdelcerro | |
105 | 46101 | jjdelcerro | utils().info_jdbc(explorer); |
106 | 45472 | jjdelcerro | |
107 | 46101 | jjdelcerro | // Importamos el CSV en la bbdd
|
108 | 46309 | jjdelcerro | LOGGER.info("Creating table 'poblaciones' in h2 temporary database.");
|
109 | utils().drop_tables(explorer, "poblaciones", DatabaseWorkspaceManager.TABLE_RESOURCES_NAME);
|
||
110 | utils().create_table_from(explorer, "poblaciones", sourceStore);
|
||
111 | utils().insert_into_from(explorer, "poblaciones", sourceStore, FeatureStore.MODE_APPEND);
|
||
112 | 46050 | omartinez | |
113 | 46309 | jjdelcerro | LOGGER.info("Loading table 'poblaciones' in in memory and populate selection.");
|
114 | FeatureStore dbstore = utils().openStore(explorer, "poblaciones");
|
||
115 | FeatureSelection selection = dbstore.getFeatureSelection(); |
||
116 | Map<Integer,Feature> features = new HashMap<>(); |
||
117 | for (Feature feature : dbstore.getFeatureSet()) {
|
||
118 | Feature f = feature; |
||
119 | int pk = f.getInt("COD_POBLACION"); |
||
120 | features.put(pk,feature.getCopy()); |
||
121 | selection.select(feature); |
||
122 | } |
||
123 | // selection.selectAll();
|
||
124 | |||
125 | // LOGGER.info("Getting features from selection (1)");
|
||
126 | // long t1 = System.currentTimeMillis();
|
||
127 | // int count1 = 0;
|
||
128 | // Set<Integer> found1 = new HashSet<>();
|
||
129 | // for (FeatureReference featureReference : selection.referenceIterable()) {
|
||
130 | // Feature f = featureReference.getFeature();
|
||
131 | // int pk = f.getInt("COD_POBLACION");
|
||
132 | // assertNotNull("row "+count1, features.get(pk));
|
||
133 | // found1.add(pk);
|
||
134 | // count1++;
|
||
135 | // }
|
||
136 | // assertEquals("features recuperadas (1)", features.size(), found1.size());
|
||
137 | // long time1 = System.currentTimeMillis()-t1;
|
||
138 | // LOGGER.info("time (1) "+time1+"ms, count "+count1);
|
||
139 | LOGGER.info("Getting features from selection (2)");
|
||
140 | long t2 = System.currentTimeMillis(); |
||
141 | int count2 = 0; |
||
142 | Set<Integer> found2 = new HashSet<>(); |
||
143 | for (Feature feature : dbstore.getFeaturesIterable(selection.referenceIterator()) ) {
|
||
144 | Feature f = feature; |
||
145 | int pk = f.getInt("COD_POBLACION"); |
||
146 | assertNotNull("row "+count2, features.get(pk));
|
||
147 | found2.add(pk); |
||
148 | count2++; |
||
149 | } |
||
150 | assertEquals("features recuperadas (2)", features.size(), found2.size());
|
||
151 | long time2 = System.currentTimeMillis()-t2; |
||
152 | // LOGGER.info("time (1) "+time1+"ms, count "+count1);
|
||
153 | LOGGER.info("time (2) "+time2+"ms, count "+count2); |
||
154 | |||
155 | restore_level(loggers); |
||
156 | |||
157 | 46101 | jjdelcerro | DisposeUtils.dispose(dbstore); |
158 | |||
159 | } catch (Throwable th) { |
||
160 | LOGGER.warn("", th);
|
||
161 | throw th;
|
||
162 | } |
||
163 | 45472 | jjdelcerro | } |
164 | 46317 | fdiaz | protected void testFRI2FI2() throws Exception { |
165 | try {
|
||
166 | if (!utils().isTheDatabaseAvailable()) {
|
||
167 | return;
|
||
168 | } |
||
169 | |||
170 | List<Pair<org.apache.log4j.Logger, Level>> loggers = get_loggers(JDBCUtils.class, ResulSetControlerBase.class, "org.gvsig.fmap.dal.store.h2.H2SpatialHelper"); |
||
171 | set_level(loggers, Level.INFO);
|
||
172 | |||
173 | FeatureStore sourceStore = utils().openCSVStore("/org/gvsig/fmap/dal/store/jdbc2/esp_poblaciones-nogeom.csv");
|
||
174 | JDBCServerExplorer explorer = utils().openServerExplorer("testFRI2FI");
|
||
175 | 45472 | jjdelcerro | |
176 | 46317 | fdiaz | utils().info_jdbc(explorer); |
177 | |||
178 | // Importamos el CSV en la bbdd
|
||
179 | LOGGER.info("Creating table 'poblaciones' in h2 temporary database.");
|
||
180 | utils().drop_tables(explorer, "poblaciones", DatabaseWorkspaceManager.TABLE_RESOURCES_NAME);
|
||
181 | utils().create_table_from(explorer, "poblaciones", sourceStore);
|
||
182 | utils().insert_into_from(explorer, "poblaciones", sourceStore, FeatureStore.MODE_APPEND);
|
||
183 | |||
184 | LOGGER.info("Loading table 'poblaciones' in in memory and populate selection.");
|
||
185 | FeatureStore dbstore = utils().openStore(explorer, "poblaciones");
|
||
186 | Map<Integer,Feature> deletedMap = new HashMap<>(); |
||
187 | Observer observer = new Observer() { |
||
188 | @Override
|
||
189 | public void update(Observable observable, Object notification) { |
||
190 | if(notification instanceof FeatureStoreNotification) { |
||
191 | FeatureStoreNotification n = (FeatureStoreNotification) notification; |
||
192 | if (n.isOfType(FeatureStoreNotification.BEFORE_FINISHEDITING)) {
|
||
193 | doTest_FRI2FI2(dbstore, n.getDeletedsFeatures(), deletedMap); |
||
194 | } |
||
195 | } |
||
196 | } |
||
197 | |||
198 | }; |
||
199 | dbstore.addObserver(observer); |
||
200 | |||
201 | |||
202 | dbstore.edit(); |
||
203 | //Nuevo + borrado
|
||
204 | |||
205 | for(int i=100000; i<=100999; i++){ |
||
206 | EditableFeature feature = dbstore.createNewFeature(); |
||
207 | feature.set("POB_NOMBRE", "N_"+i); |
||
208 | feature.set("COD_PROVINCIA", 100); |
||
209 | feature.set("COD_POBLACION", i);
|
||
210 | dbstore.insert(feature); |
||
211 | } |
||
212 | |||
213 | ExpressionBuilder builder; |
||
214 | for(int i = 100100; i <= 100299; i++) { |
||
215 | builder = ExpressionUtils.createExpressionBuilder(); |
||
216 | String filter = builder.eq(
|
||
217 | builder.column("COD_POBLACION"),
|
||
218 | builder.constant(i) |
||
219 | ).toString(); |
||
220 | Feature feature = dbstore.findFirst(filter); |
||
221 | if(feature!=null){ |
||
222 | dbstore.delete(feature); |
||
223 | } |
||
224 | } |
||
225 | |||
226 | //Nuevo + modificado + borrado
|
||
227 | for(int i=100300; i<=100599; i++){ |
||
228 | builder = ExpressionUtils.createExpressionBuilder(); |
||
229 | String filter = builder.eq(
|
||
230 | builder.column("COD_POBLACION"),
|
||
231 | builder.constant(i) |
||
232 | ).toString(); |
||
233 | Feature feature = dbstore.findFirst(filter); |
||
234 | EditableFeature efeature = feature.getEditable(); |
||
235 | efeature.set("POB_NOMBRE", "NM_"+i); |
||
236 | dbstore.update(efeature); |
||
237 | } |
||
238 | for(int i=100300; i<=100599; i++){ |
||
239 | builder = ExpressionUtils.createExpressionBuilder(); |
||
240 | String filter = builder.eq(
|
||
241 | builder.column("COD_POBLACION"),
|
||
242 | builder.constant(i) |
||
243 | ).toString(); |
||
244 | Feature feature = dbstore.findFirst(filter); |
||
245 | if(feature != null){ |
||
246 | dbstore.delete(feature); |
||
247 | } |
||
248 | } |
||
249 | |||
250 | //modificado existente
|
||
251 | for(int i=49000; i<=49300; i++){ |
||
252 | builder = ExpressionUtils.createExpressionBuilder(); |
||
253 | String filter = builder.eq(
|
||
254 | builder.column("COD_POBLACION"),
|
||
255 | builder.constant(i) |
||
256 | ).toString(); |
||
257 | Feature feature = dbstore.findFirst(filter); |
||
258 | if(feature == null){ |
||
259 | continue;
|
||
260 | } |
||
261 | EditableFeature efeature = feature.getEditable(); |
||
262 | efeature.set("POB_NOMBRE", feature.get("POB_NOMBRE")+"_M"); |
||
263 | dbstore.update(efeature); |
||
264 | } |
||
265 | |||
266 | //borrado existente ==> add to deteledMap
|
||
267 | |||
268 | for(int i=50000; i<=50300; i++){ |
||
269 | builder = ExpressionUtils.createExpressionBuilder(); |
||
270 | String filter = builder.eq(
|
||
271 | builder.column("COD_POBLACION"),
|
||
272 | builder.constant(i) |
||
273 | ).toString(); |
||
274 | Feature feature = dbstore.findFirst(filter); |
||
275 | if (feature == null) { |
||
276 | continue;
|
||
277 | } |
||
278 | |||
279 | deletedMap.put(i, feature.getCopy()); |
||
280 | dbstore.delete(feature); |
||
281 | } |
||
282 | |||
283 | //modificado existente + borrado ==> add to deteledMap
|
||
284 | for(int i=48000; i<=48300; i++){ |
||
285 | builder = ExpressionUtils.createExpressionBuilder(); |
||
286 | String filter = builder.eq(
|
||
287 | builder.column("COD_POBLACION"),
|
||
288 | builder.constant(i) |
||
289 | ).toString(); |
||
290 | Feature feature = dbstore.findFirst(filter); |
||
291 | if(feature == null){ |
||
292 | continue;
|
||
293 | } |
||
294 | EditableFeature efeature = feature.getEditable(); |
||
295 | efeature.set("POB_NOMBRE", feature.get("POB_NOMBRE")+"_M"); |
||
296 | dbstore.update(efeature); |
||
297 | } |
||
298 | for(int i=48000; i<=48300; i++){ |
||
299 | builder = ExpressionUtils.createExpressionBuilder(); |
||
300 | String filter = builder.eq(
|
||
301 | builder.column("COD_POBLACION"),
|
||
302 | builder.constant(i) |
||
303 | ).toString(); |
||
304 | Feature feature = dbstore.findFirst(filter); |
||
305 | if (feature == null) { |
||
306 | continue;
|
||
307 | } |
||
308 | |||
309 | deletedMap.put(i, feature.getCopy()); |
||
310 | dbstore.delete(feature); |
||
311 | } |
||
312 | |||
313 | dbstore.finishEditing(); |
||
314 | |||
315 | |||
316 | restore_level(loggers); |
||
317 | |||
318 | DisposeUtils.dispose(dbstore); |
||
319 | |||
320 | } catch (Throwable th) { |
||
321 | LOGGER.warn("", th);
|
||
322 | throw th;
|
||
323 | } |
||
324 | |||
325 | } |
||
326 | |||
327 | private void doTest_FRI2FI2(FeatureStore dbstore, Iterator<FeatureReference> deletedsFeatures, Map<Integer,Feature> deletedMap) { |
||
328 | Set<Integer> found2 = new HashSet<>(); |
||
329 | int i = 0; |
||
330 | for (Feature feature : dbstore.getFeaturesIterable(deletedsFeatures)) {
|
||
331 | Feature f = feature; |
||
332 | int pk = f.getInt("COD_POBLACION"); |
||
333 | assertNotNull("row " + i++, deletedMap.get(pk));
|
||
334 | found2.add(pk); |
||
335 | |||
336 | } |
||
337 | assertEquals("features recuperadas (2)", deletedMap.size(), found2.size());
|
||
338 | |||
339 | } |
||
340 | |||
341 | |||
342 | 45472 | jjdelcerro | } |