Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.api / src / test / java / org / gvsig / fmap / mapcontext / persistence / MapContextPersistenceTest.java @ 40559

History | View | Annotate | Download (11.5 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.mapcontext.persistence;
25

    
26
import java.awt.Color;
27
import java.awt.Dimension;
28
import java.awt.geom.Rectangle2D;
29
import java.io.File;
30
import java.io.FileInputStream;
31
import java.io.FileOutputStream;
32

    
33
import org.gvsig.fmap.crs.CRSFactory;
34
import org.gvsig.fmap.dal.feature.FeatureStore;
35
import org.gvsig.fmap.geom.Geometry;
36
import org.gvsig.fmap.geom.Geometry.TYPES;
37
import org.gvsig.fmap.geom.GeometryLocator;
38
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
39
import org.gvsig.fmap.geom.primitive.Envelope;
40
import org.gvsig.fmap.mapcontext.MapContext;
41
import org.gvsig.fmap.mapcontext.MapContextLocator;
42
import org.gvsig.fmap.mapcontext.ViewPort;
43
import org.gvsig.fmap.mapcontext.exceptions.LegendLayerException;
44
import org.gvsig.fmap.mapcontext.layers.FLayer;
45
import org.gvsig.fmap.mapcontext.layers.FLayers;
46
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
47
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
48
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend;
49
import org.gvsig.fmap.mapcontext.rendering.symbol.DummyVectorLegend;
50
import org.gvsig.tools.ToolsLocator;
51
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
52
import org.gvsig.tools.locator.LocatorException;
53
import org.gvsig.tools.persistence.Persistent;
54
import org.gvsig.tools.persistence.PersistentState;
55
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
57

    
58
public class MapContextPersistenceTest extends AbstractLibraryAutoInitTestCase {
59

    
60
        final static private Logger logger =
61
                        LoggerFactory.getLogger(MapContextPersistenceTest.class);
62

    
63
        protected void doSetUp() throws Exception {
64
                DummyVectorLegend.registerPersistent();
65
                DummyDBFeatureStore.registerPersistent();
66
                DummyFileFeatureStore.registerPersistent();
67

    
68
                MapContextLocator.getMapContextManager().registerLegend("DummyLegend",
69
                                DummyVectorLegend.class);
70
                MapContextLocator.getMapContextManager().setDefaultVectorLegend(
71
                                "DummyLegend");
72
        }
73
        
74
        private File getTempFile() throws Exception {
75

    
76
                File tempFile = File.createTempFile("persisted_mapcontext", ".xml");
77
                if (tempFile.exists())
78
                        tempFile.delete();
79
                tempFile.createNewFile();
80
                
81
                System.out.println("TEMP FILE: " + tempFile.getAbsolutePath());
82
                return tempFile;
83
        }
84
        
85
        public void testMapContext() throws Exception {
86
                ViewPort vp = getViewPort();
87
                logger.debug("Creating mapcontext...");
88
                MapContext mc = new MapContext(vp);
89
                addDummyLayers(mc);
90
                doTestPersist(mc);
91
        }
92

    
93
        public void noTestFLyrs() throws Exception {
94
                
95
                int lyr_count = 3;
96
                // testPersist(mc);
97
                FLyrVect[] lyr = new FLyrVect[lyr_count];
98
                FeatureStore[] fs = new FeatureStore[lyr_count];
99
                FLayers lyrs = new FLayers();
100
                
101
                for (int i=0; i<lyr_count; i++) {
102
                        fs[i] = new DummyFileFeatureStore("" + System.currentTimeMillis());
103
                        lyr[i] = createLayer(fs[i]);
104
                        lyr[i].setName("Layer " + (i+System.currentTimeMillis()));
105
                        lyr[i].setIsLabeled(false);
106
                        try { lyr[i].setLegend(new DummyVectorLegend(TYPES.SOLID)); } catch (LegendLayerException e) {
107
                                assertTrue("Error while creating legend: " + e.getMessage(), false);
108
                        }
109
                        lyrs.addLayer(lyr[i]);
110
                        lyr[i].dispose();
111
                }
112
                
113
                for (int i=0; i<lyr_count; i++) {
114
                        fs[i] = new DummyDBFeatureStore("" + System.currentTimeMillis());
115
                        lyr[i] = createLayer(fs[i]);
116
                        lyr[i].setName("Layer " + (10000 + i + System.currentTimeMillis()));
117
                        lyr[i].setIsLabeled(false);
118
                        try { lyr[i].setLegend(new DummyVectorLegend(TYPES.SOLID)); } catch (LegendLayerException e) {
119
                                assertTrue("Error while creating legend: " + e.getMessage(), false);
120
                        }
121
                        lyrs.addLayer(lyr[i]);
122
                        lyr[i].dispose();
123
                }
124
                
125
                doTestPersist(lyrs);
126
        }
127
        
128
        
129
        
130
        private ViewPort getViewPort() throws LocatorException, CreateEnvelopeException {
131
                
132
                ViewPort vp = new ViewPort(CRSFactory.getCRS("EPSG:4326"));
133
                vp.setImageSize(new Dimension(640, 480));
134
                Envelope env = createEnvelope2D(0, 0, 1000000, 1000000);
135
                vp.setEnvelope(env);
136
                env = createEnvelope2D(200000, 200000, 500000, 500000);
137
                vp.setEnvelope(env);
138
                env = createEnvelope2D(300000, 300000, 300000, 300000);
139
                vp.setEnvelope(env);
140
                env = createEnvelope2D(400000, 400000, 200000, 200000);
141
                vp.setEnvelope(env);
142
                env = createEnvelope2D(440000, 440000, 100000, 100000);
143
                vp.setEnvelope(env);
144
                env = createEnvelope2D(480000, 480000, 30000, 30000);
145
                vp.setEnvelope(env);
146
                vp.setBackColor(Color.YELLOW);
147
                vp.setClipRect(new Rectangle2D.Double(0, 0, 10, 10));
148
                return vp;
149
        }
150
        
151
        private Envelope createEnvelope2D(double minX,double minY,double maxX, double maxY) throws LocatorException, CreateEnvelopeException {
152
                return GeometryLocator.getGeometryManager().createEnvelope(minX, minY, maxX, maxY, Geometry.SUBTYPES.GEOM2D);
153
        }
154

    
155
        private void addDummyLayers(MapContext mcon) throws Exception {
156
                
157
                FLayers resp = new FLayers();
158
                resp.setMapContext(mcon);
159
                resp.setName("root layer");
160
                
161
                FLayers aux = new FLayers();
162
                aux.setMapContext(mcon);
163
                aux.setName("Group");
164
                
165
                FLyrVect lyr = null;
166
                IVectorLegend lgn = null;
167
                FeatureStore fs = null;
168
                
169
                logger.debug("Adding dummy layers...");
170
                        
171
                fs = new DummyFileFeatureStore("1");
172
                lyr = createLayer(fs);
173
                lyr.setName("Layer 1");
174
                try { lyr.setLegend(new DummyVectorLegend(TYPES.SOLID)); } catch (LegendLayerException e) { }
175
                
176
                aux.addLayer(lyr);
177
                lyr.dispose();
178
                
179
                fs = new DummyDBFeatureStore("A");
180
                lyr = createLayer(fs);
181
                lyr.setName("Layer A");
182
                try { lyr.setLegend(new DummyVectorLegend(TYPES.SOLID)); } catch (LegendLayerException e) { }
183

    
184
                aux.addLayer(lyr);
185
                lyr.dispose();
186
                
187
                fs = new DummyFileFeatureStore("2");
188
                lyr = createLayer(fs);
189
                lyr.setName("Layer 2");
190
                try { lyr.setLegend(new DummyVectorLegend(TYPES.SOLID)); } catch (LegendLayerException e) { }
191

    
192
                aux.addLayer(lyr);
193
                lyr.dispose();
194
                
195
                fs = new DummyDBFeatureStore("B");
196
                lyr = createLayer(fs);
197
                lyr.setName("Layer 1");
198
                try { lyr.setLegend(new DummyVectorLegend(TYPES.SOLID)); } catch (LegendLayerException e) { }
199
                
200
                resp.addLayer(lyr);
201
                resp.addLayer(aux);
202
                lyr.dispose();
203
                aux.dispose();
204
                
205
                mcon.getLayers().addLayer(resp);
206
                resp.dispose();
207
        }
208

    
209
        private FLyrVect createLayer(FeatureStore fs) throws Exception {
210
                
211
                FLyrVect resp = new FLyrVect();
212
                resp.setDataStore(fs);
213
                resp.wakeUp();
214
                resp.load();
215
                return resp;
216
        }
217

    
218
        private void doTestPersist(Persistent p) throws Exception {
219
                
220
                System.out.println("Starting persistence test for class: " + p.getClass().getName());
221

    
222
                File perfile = getTempFile();
223
                FileOutputStream fos = new FileOutputStream(perfile);
224
                System.out.println("Getting state for class: " + p.getClass().getName());
225
                PersistentState pst = ToolsLocator.getPersistenceManager().getState(p);
226
                System.out.println("Saving state...");
227

    
228
                System.out.println("Saving state...");
229
                ToolsLocator.getPersistenceManager().saveState(pst, fos);
230
                System.out.println("Saving state... done.");
231

    
232
                fos.close();
233

    
234
                // ==============================================
235
                // ==============================================
236
                System.out.println("Is now persisted: " + p.getClass().getName());
237
                // ==============================================
238

    
239
                // if (true) return;
240

    
241
                System.out.println("Opening persistence file...");
242
                FileInputStream fis = new FileInputStream(perfile);
243

    
244
                System.out.println("Loading state from file...");
245
                pst = ToolsLocator.getPersistenceManager().loadState(fis);
246

    
247
                System.out.println("Instantiating " + p.getClass().getName()
248
                                + " from state, pst = " + pst);
249
                Object p2 = ToolsLocator.getPersistenceManager().create(pst);
250

    
251
                System.out.println("Comparing original and current...");
252
                comparePersistent(p, p2);
253

    
254
                System.out.println("Successful mapcontext persistence test!");
255
                
256
        }
257

    
258
        
259
        // FeatureStore, ILegend, ViewPort
260
        private void comparePersistent(Persistent p, Object p2) {
261
                
262
                if (p2.getClass().getName().compareTo(p.getClass().getName()) == 0) {
263
                        
264
                        if (p instanceof MapContext) {
265
                                compareMapContexts((MapContext) p, (MapContext) p2);
266
                        } else {
267
                                if (p instanceof FLayer) {
268
                                        compareLayers((FLayer) p, (FLayer) p2);
269
                                } else {
270
                                        if (p instanceof FeatureStore) {
271
                                                compareStore((FeatureStore) p, (FeatureStore) p2);
272
                                        } else {
273
                                                if (p instanceof ILegend) {
274
                                                        compareLegend((ILegend) p, (ILegend) p2);
275
                                                } else {
276
                                                        if (p instanceof ViewPort) {
277
                                                                compareViewPorts((ViewPort) p, (ViewPort) p2);
278
                                                        } else {
279
                                                                fail("Did not compare: " + p.getClass().getName());
280
                                                        }
281
                                                }
282
                                        }
283
                                }
284
                        }
285
                } else {
286
                        fail("Comparing different classes (?)");
287
                }
288
                
289
        }
290

    
291
        private void compareMapContexts(MapContext m1, MapContext m2) {
292
                
293
                logger.debug("Getting viewports to compare...");
294
                ViewPort vp1 = m1.getViewPort();
295
                ViewPort vp2 = m2.getViewPort();
296
                
297
                compareViewPorts(vp1, vp2);
298

    
299
                logger.debug("Getting root flayers to compare...");
300
                FLayers lyr1 = m1.getLayers();
301
                FLayers lyr2 = m2.getLayers();
302
                compareLayers(lyr1, lyr2);
303
        }
304

    
305
        private void compareLayers(FLayer l1, FLayer l2) {
306
                
307
                logger.debug("Comparing layers...");
308

    
309
                assertTrue("Layers of diff type!", l1.getClass().getName().compareTo(l2.getClass().getName()) == 0);
310
                if (l1 instanceof FLayers) {
311
                        
312
                        logger.debug("(type FLayers)...");
313
                        FLayers ls1 = (FLayers) l1;
314
                        FLayers ls2 = (FLayers) l2;
315
                        assertEquals(ls1.getLayersCount(), ls2.getLayersCount());
316
                        int sz = ls2.getLayersCount();
317
                        for (int i=0; i<sz; i++) {
318
                                compareLayers(ls1.getLayer(i), ls2.getLayer(i));
319
                        }
320
                        
321
                } else {
322
                        if (l1 instanceof FLyrVect) {
323
                                
324
                                logger.debug("(type FLyrVect)...");
325
                                FLyrVect ls1 = (FLyrVect) l1;
326
                                FLyrVect ls2 = (FLyrVect) l2;
327
                                compareVectLayers(ls1, ls2);
328
                                
329
                        } else {
330
                                
331
                        }
332
                }
333

    
334
        }
335

    
336
        private void compareVectLayers(FLyrVect l1, FLyrVect l2) {
337
                compareLegend(l1.getLegend(), l2.getLegend());
338
                compareStore(l1.getFeatureStore(), l2.getFeatureStore());
339
        }
340

    
341
        private void compareStore(FeatureStore fs1,
342
                        FeatureStore fs2) {
343
                
344
                logger.debug("Comparing stores...");
345
                assertTrue("Diff stores!", fs1.getName().compareTo(fs2.getName()) == 0);
346
        }
347

    
348
        private void compareLegend(ILegend l1, ILegend l2) {
349

    
350
                logger.debug("Comparing legends...");
351
                IVectorLegend vl1 = (IVectorLegend) l1;
352
                IVectorLegend vl2 = (IVectorLegend) l2;
353
                assertTrue("Diff legends!", vl1.getShapeType() == vl2.getShapeType());
354
                
355
        }
356

    
357
        private void compareViewPorts(ViewPort vp1, ViewPort vp2) {
358

    
359

    
360
                logger.debug("Comparing viewports...");
361
                
362
                assertEquals(
363
                                vp1.getAdjustedEnvelope().getMinimum(0),
364
                                vp2.getAdjustedEnvelope().getMinimum(0),
365
                                0.00000000000001);
366
                assertEquals(
367
                                vp1.getAdjustedEnvelope().getMinimum(1),
368
                                vp2.getAdjustedEnvelope().getMinimum(1),
369
                                0.00000000000001);
370
                assertEquals(
371
                                vp1.getAdjustedEnvelope().getMaximum(0),
372
                                vp2.getAdjustedEnvelope().getMaximum(0),
373
                                0.00000000000001);
374
                assertEquals(
375
                                vp1.getAdjustedEnvelope().getMaximum(1),
376
                                vp2.getAdjustedEnvelope().getMaximum(1),
377
                                0.00000000000001);
378
        }
379
        
380
}