svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src-test / org / gvsig / fmap / mapcontext / persistence / MapContextPersistenceTest.java @ 40373
History | View | Annotate | Download (10.6 KB)
1 |
package org.gvsig.fmap.mapcontext.persistence; |
---|---|
2 |
|
3 |
import java.awt.Color; |
4 |
import java.awt.Dimension; |
5 |
import java.awt.geom.Rectangle2D; |
6 |
import java.io.File; |
7 |
import java.io.FileInputStream; |
8 |
import java.io.FileOutputStream; |
9 |
|
10 |
import org.gvsig.fmap.crs.CRSFactory; |
11 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
12 |
import org.gvsig.fmap.geom.Geometry; |
13 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
14 |
import org.gvsig.fmap.geom.GeometryLocator; |
15 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
16 |
import org.gvsig.fmap.geom.primitive.Envelope; |
17 |
import org.gvsig.fmap.mapcontext.MapContext; |
18 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
19 |
import org.gvsig.fmap.mapcontext.ViewPort; |
20 |
import org.gvsig.fmap.mapcontext.exceptions.LegendLayerException; |
21 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
22 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
23 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
24 |
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend; |
25 |
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend; |
26 |
import org.gvsig.fmap.mapcontext.rendering.symbol.DummyVectorLegend; |
27 |
import org.gvsig.tools.ToolsLocator; |
28 |
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase; |
29 |
import org.gvsig.tools.locator.LocatorException; |
30 |
import org.gvsig.tools.persistence.Persistent; |
31 |
import org.gvsig.tools.persistence.PersistentState; |
32 |
import org.slf4j.Logger; |
33 |
import org.slf4j.LoggerFactory; |
34 |
|
35 |
public class MapContextPersistenceTest extends AbstractLibraryAutoInitTestCase { |
36 |
|
37 |
final static private Logger logger = |
38 |
LoggerFactory.getLogger(MapContextPersistenceTest.class); |
39 |
|
40 |
protected void doSetUp() throws Exception { |
41 |
DummyVectorLegend.registerPersistent(); |
42 |
DummyDBFeatureStore.registerPersistent(); |
43 |
DummyFileFeatureStore.registerPersistent(); |
44 |
|
45 |
MapContextLocator.getMapContextManager().registerLegend("DummyLegend",
|
46 |
DummyVectorLegend.class); |
47 |
MapContextLocator.getMapContextManager().setDefaultVectorLegend( |
48 |
"DummyLegend");
|
49 |
} |
50 |
|
51 |
private File getTempFile() throws Exception { |
52 |
|
53 |
File tempFile = File.createTempFile("persisted_mapcontext", ".xml"); |
54 |
if (tempFile.exists())
|
55 |
tempFile.delete(); |
56 |
tempFile.createNewFile(); |
57 |
|
58 |
System.out.println("TEMP FILE: " + tempFile.getAbsolutePath()); |
59 |
return tempFile;
|
60 |
} |
61 |
|
62 |
public void testMapContext() throws Exception { |
63 |
ViewPort vp = getViewPort(); |
64 |
logger.debug("Creating mapcontext...");
|
65 |
MapContext mc = new MapContext(vp);
|
66 |
addDummyLayers(mc); |
67 |
doTestPersist(mc); |
68 |
} |
69 |
|
70 |
public void noTestFLyrs() throws Exception { |
71 |
|
72 |
int lyr_count = 3; |
73 |
// testPersist(mc);
|
74 |
FLyrVect[] lyr = new FLyrVect[lyr_count]; |
75 |
FeatureStore[] fs = new FeatureStore[lyr_count]; |
76 |
FLayers lyrs = new FLayers();
|
77 |
|
78 |
for (int i=0; i<lyr_count; i++) { |
79 |
fs[i] = new DummyFileFeatureStore("" + System.currentTimeMillis()); |
80 |
lyr[i] = createLayer(fs[i]); |
81 |
lyr[i].setName("Layer " + (i+System.currentTimeMillis())); |
82 |
lyr[i].setIsLabeled(false);
|
83 |
try { lyr[i].setLegend(new DummyVectorLegend(TYPES.SOLID)); } catch (LegendLayerException e) { |
84 |
assertTrue("Error while creating legend: " + e.getMessage(), false); |
85 |
} |
86 |
lyrs.addLayer(lyr[i]); |
87 |
lyr[i].dispose(); |
88 |
} |
89 |
|
90 |
for (int i=0; i<lyr_count; i++) { |
91 |
fs[i] = new DummyDBFeatureStore("" + System.currentTimeMillis()); |
92 |
lyr[i] = createLayer(fs[i]); |
93 |
lyr[i].setName("Layer " + (10000 + i + System.currentTimeMillis())); |
94 |
lyr[i].setIsLabeled(false);
|
95 |
try { lyr[i].setLegend(new DummyVectorLegend(TYPES.SOLID)); } catch (LegendLayerException e) { |
96 |
assertTrue("Error while creating legend: " + e.getMessage(), false); |
97 |
} |
98 |
lyrs.addLayer(lyr[i]); |
99 |
lyr[i].dispose(); |
100 |
} |
101 |
|
102 |
doTestPersist(lyrs); |
103 |
} |
104 |
|
105 |
|
106 |
|
107 |
private ViewPort getViewPort() throws LocatorException, CreateEnvelopeException { |
108 |
|
109 |
ViewPort vp = new ViewPort(CRSFactory.getCRS("EPSG:4326")); |
110 |
vp.setImageSize(new Dimension(640, 480)); |
111 |
Envelope env = createEnvelope2D(0, 0, 1000000, 1000000); |
112 |
vp.setEnvelope(env); |
113 |
env = createEnvelope2D(200000, 200000, 500000, 500000); |
114 |
vp.setEnvelope(env); |
115 |
env = createEnvelope2D(300000, 300000, 300000, 300000); |
116 |
vp.setEnvelope(env); |
117 |
env = createEnvelope2D(400000, 400000, 200000, 200000); |
118 |
vp.setEnvelope(env); |
119 |
env = createEnvelope2D(440000, 440000, 100000, 100000); |
120 |
vp.setEnvelope(env); |
121 |
env = createEnvelope2D(480000, 480000, 30000, 30000); |
122 |
vp.setEnvelope(env); |
123 |
vp.setBackColor(Color.YELLOW);
|
124 |
vp.setClipRect(new Rectangle2D.Double(0, 0, 10, 10)); |
125 |
return vp;
|
126 |
} |
127 |
|
128 |
private Envelope createEnvelope2D(double minX,double minY,double maxX, double maxY) throws LocatorException, CreateEnvelopeException { |
129 |
return GeometryLocator.getGeometryManager().createEnvelope(minX, minY, maxX, maxY, Geometry.SUBTYPES.GEOM2D);
|
130 |
} |
131 |
|
132 |
private void addDummyLayers(MapContext mcon) throws Exception { |
133 |
|
134 |
FLayers resp = new FLayers();
|
135 |
resp.setMapContext(mcon); |
136 |
resp.setName("root layer");
|
137 |
|
138 |
FLayers aux = new FLayers();
|
139 |
aux.setMapContext(mcon); |
140 |
aux.setName("Group");
|
141 |
|
142 |
FLyrVect lyr = null;
|
143 |
IVectorLegend lgn = null;
|
144 |
FeatureStore fs = null;
|
145 |
|
146 |
logger.debug("Adding dummy layers...");
|
147 |
|
148 |
fs = new DummyFileFeatureStore("1"); |
149 |
lyr = createLayer(fs); |
150 |
lyr.setName("Layer 1");
|
151 |
try { lyr.setLegend(new DummyVectorLegend(TYPES.SOLID)); } catch (LegendLayerException e) { } |
152 |
|
153 |
aux.addLayer(lyr); |
154 |
lyr.dispose(); |
155 |
|
156 |
fs = new DummyDBFeatureStore("A"); |
157 |
lyr = createLayer(fs); |
158 |
lyr.setName("Layer A");
|
159 |
try { lyr.setLegend(new DummyVectorLegend(TYPES.SOLID)); } catch (LegendLayerException e) { } |
160 |
|
161 |
aux.addLayer(lyr); |
162 |
lyr.dispose(); |
163 |
|
164 |
fs = new DummyFileFeatureStore("2"); |
165 |
lyr = createLayer(fs); |
166 |
lyr.setName("Layer 2");
|
167 |
try { lyr.setLegend(new DummyVectorLegend(TYPES.SOLID)); } catch (LegendLayerException e) { } |
168 |
|
169 |
aux.addLayer(lyr); |
170 |
lyr.dispose(); |
171 |
|
172 |
fs = new DummyDBFeatureStore("B"); |
173 |
lyr = createLayer(fs); |
174 |
lyr.setName("Layer 1");
|
175 |
try { lyr.setLegend(new DummyVectorLegend(TYPES.SOLID)); } catch (LegendLayerException e) { } |
176 |
|
177 |
resp.addLayer(lyr); |
178 |
resp.addLayer(aux); |
179 |
lyr.dispose(); |
180 |
aux.dispose(); |
181 |
|
182 |
mcon.getLayers().addLayer(resp); |
183 |
resp.dispose(); |
184 |
} |
185 |
|
186 |
private FLyrVect createLayer(FeatureStore fs) throws Exception { |
187 |
|
188 |
FLyrVect resp = new FLyrVect();
|
189 |
resp.setDataStore(fs); |
190 |
resp.wakeUp(); |
191 |
resp.load(); |
192 |
return resp;
|
193 |
} |
194 |
|
195 |
private void doTestPersist(Persistent p) throws Exception { |
196 |
|
197 |
System.out.println("Starting persistence test for class: " + p.getClass().getName()); |
198 |
|
199 |
File perfile = getTempFile();
|
200 |
FileOutputStream fos = new FileOutputStream(perfile); |
201 |
System.out.println("Getting state for class: " + p.getClass().getName()); |
202 |
PersistentState pst = ToolsLocator.getPersistenceManager().getState(p); |
203 |
System.out.println("Saving state..."); |
204 |
|
205 |
System.out.println("Saving state..."); |
206 |
ToolsLocator.getPersistenceManager().saveState(pst, fos); |
207 |
System.out.println("Saving state... done."); |
208 |
|
209 |
fos.close(); |
210 |
|
211 |
// ==============================================
|
212 |
// ==============================================
|
213 |
System.out.println("Is now persisted: " + p.getClass().getName()); |
214 |
// ==============================================
|
215 |
|
216 |
// if (true) return;
|
217 |
|
218 |
System.out.println("Opening persistence file..."); |
219 |
FileInputStream fis = new FileInputStream(perfile); |
220 |
|
221 |
System.out.println("Loading state from file..."); |
222 |
pst = ToolsLocator.getPersistenceManager().loadState(fis); |
223 |
|
224 |
System.out.println("Instantiating " + p.getClass().getName() |
225 |
+ " from state, pst = " + pst);
|
226 |
Object p2 = ToolsLocator.getPersistenceManager().create(pst);
|
227 |
|
228 |
System.out.println("Comparing original and current..."); |
229 |
comparePersistent(p, p2); |
230 |
|
231 |
System.out.println("Successful mapcontext persistence test!"); |
232 |
|
233 |
} |
234 |
|
235 |
|
236 |
// FeatureStore, ILegend, ViewPort
|
237 |
private void comparePersistent(Persistent p, Object p2) { |
238 |
|
239 |
if (p2.getClass().getName().compareTo(p.getClass().getName()) == 0) { |
240 |
|
241 |
if (p instanceof MapContext) { |
242 |
compareMapContexts((MapContext) p, (MapContext) p2); |
243 |
} else {
|
244 |
if (p instanceof FLayer) { |
245 |
compareLayers((FLayer) p, (FLayer) p2); |
246 |
} else {
|
247 |
if (p instanceof FeatureStore) { |
248 |
compareStore((FeatureStore) p, (FeatureStore) p2); |
249 |
} else {
|
250 |
if (p instanceof ILegend) { |
251 |
compareLegend((ILegend) p, (ILegend) p2); |
252 |
} else {
|
253 |
if (p instanceof ViewPort) { |
254 |
compareViewPorts((ViewPort) p, (ViewPort) p2); |
255 |
} else {
|
256 |
fail("Did not compare: " + p.getClass().getName());
|
257 |
} |
258 |
} |
259 |
} |
260 |
} |
261 |
} |
262 |
} else {
|
263 |
fail("Comparing different classes (?)");
|
264 |
} |
265 |
|
266 |
} |
267 |
|
268 |
private void compareMapContexts(MapContext m1, MapContext m2) { |
269 |
|
270 |
logger.debug("Getting viewports to compare...");
|
271 |
ViewPort vp1 = m1.getViewPort(); |
272 |
ViewPort vp2 = m2.getViewPort(); |
273 |
|
274 |
compareViewPorts(vp1, vp2); |
275 |
|
276 |
logger.debug("Getting root flayers to compare...");
|
277 |
FLayers lyr1 = m1.getLayers(); |
278 |
FLayers lyr2 = m2.getLayers(); |
279 |
compareLayers(lyr1, lyr2); |
280 |
} |
281 |
|
282 |
private void compareLayers(FLayer l1, FLayer l2) { |
283 |
|
284 |
logger.debug("Comparing layers...");
|
285 |
|
286 |
assertTrue("Layers of diff type!", l1.getClass().getName().compareTo(l2.getClass().getName()) == 0); |
287 |
if (l1 instanceof FLayers) { |
288 |
|
289 |
logger.debug("(type FLayers)...");
|
290 |
FLayers ls1 = (FLayers) l1; |
291 |
FLayers ls2 = (FLayers) l2; |
292 |
assertEquals(ls1.getLayersCount(), ls2.getLayersCount()); |
293 |
int sz = ls2.getLayersCount();
|
294 |
for (int i=0; i<sz; i++) { |
295 |
compareLayers(ls1.getLayer(i), ls2.getLayer(i)); |
296 |
} |
297 |
|
298 |
} else {
|
299 |
if (l1 instanceof FLyrVect) { |
300 |
|
301 |
logger.debug("(type FLyrVect)...");
|
302 |
FLyrVect ls1 = (FLyrVect) l1; |
303 |
FLyrVect ls2 = (FLyrVect) l2; |
304 |
compareVectLayers(ls1, ls2); |
305 |
|
306 |
} else {
|
307 |
|
308 |
} |
309 |
} |
310 |
|
311 |
} |
312 |
|
313 |
private void compareVectLayers(FLyrVect l1, FLyrVect l2) { |
314 |
compareLegend(l1.getLegend(), l2.getLegend()); |
315 |
compareStore(l1.getFeatureStore(), l2.getFeatureStore()); |
316 |
} |
317 |
|
318 |
private void compareStore(FeatureStore fs1, |
319 |
FeatureStore fs2) { |
320 |
|
321 |
logger.debug("Comparing stores...");
|
322 |
assertTrue("Diff stores!", fs1.getName().compareTo(fs2.getName()) == 0); |
323 |
} |
324 |
|
325 |
private void compareLegend(ILegend l1, ILegend l2) { |
326 |
|
327 |
logger.debug("Comparing legends...");
|
328 |
IVectorLegend vl1 = (IVectorLegend) l1; |
329 |
IVectorLegend vl2 = (IVectorLegend) l2; |
330 |
assertTrue("Diff legends!", vl1.getShapeType() == vl2.getShapeType());
|
331 |
|
332 |
} |
333 |
|
334 |
private void compareViewPorts(ViewPort vp1, ViewPort vp2) { |
335 |
|
336 |
|
337 |
logger.debug("Comparing viewports...");
|
338 |
|
339 |
assertEquals( |
340 |
vp1.getAdjustedEnvelope().getMinimum(0),
|
341 |
vp2.getAdjustedEnvelope().getMinimum(0),
|
342 |
0.00000000000001);
|
343 |
assertEquals( |
344 |
vp1.getAdjustedEnvelope().getMinimum(1),
|
345 |
vp2.getAdjustedEnvelope().getMinimum(1),
|
346 |
0.00000000000001);
|
347 |
assertEquals( |
348 |
vp1.getAdjustedEnvelope().getMaximum(0),
|
349 |
vp2.getAdjustedEnvelope().getMaximum(0),
|
350 |
0.00000000000001);
|
351 |
assertEquals( |
352 |
vp1.getAdjustedEnvelope().getMaximum(1),
|
353 |
vp2.getAdjustedEnvelope().getMaximum(1),
|
354 |
0.00000000000001);
|
355 |
} |
356 |
|
357 |
} |