root / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src-test / org / gvsig / fmap / mapcontext / persistence / MapContextPersistenceTestNo.java @ 30580
History | View | Annotate | Download (8.68 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 junit.framework.TestCase; |
11 |
|
12 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
13 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
14 |
import org.gvsig.fmap.geom.primitive.Envelope; |
15 |
import org.gvsig.fmap.geom.primitive.impl.Envelope2D; |
16 |
import org.gvsig.fmap.mapcontext.MapContext; |
17 |
import org.gvsig.fmap.mapcontext.ViewPort; |
18 |
import org.gvsig.fmap.mapcontext.exceptions.LegendLayerException; |
19 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
20 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
21 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
22 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
23 |
import org.gvsig.fmap.mapcontext.rendering.legend.DummyVectorLegend; |
24 |
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend; |
25 |
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend; |
26 |
import org.gvsig.tools.ToolsLocator; |
27 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
28 |
import org.gvsig.tools.persistence.PersistentState; |
29 |
|
30 |
public class MapContextPersistenceTestNo extends TestCase { |
31 |
|
32 |
private MapContext mc = null; |
33 |
private File tempFile = null; |
34 |
|
35 |
public void setUp() { |
36 |
|
37 |
try {
|
38 |
System.out.println("Setting up parent..."); |
39 |
|
40 |
super.setUp();
|
41 |
|
42 |
System.out.println("Init all library..."); |
43 |
new DefaultLibrariesInitializer().fullInitialize();
|
44 |
|
45 |
// LibrariesInitializer lini = new LibrariesInitializer();
|
46 |
// lini.initializeAndPostinitializeAll();
|
47 |
/*
|
48 |
System.out.println("Init Projection library...");
|
49 |
ProjectionLibrary crel = new ProjectionLibrary();
|
50 |
// CresquesCtsLibrary crel = new CresquesCtsLibrary();
|
51 |
crel.initialize();
|
52 |
crel.postInitialize();
|
53 |
*/
|
54 |
|
55 |
// System.out.println("Register DefaultDynObjectManager...");
|
56 |
// ToolsLocator.registerDynObjectManager(DefaultDynObjectManager.class);
|
57 |
// System.out.println("Register XMLPersistenceManager...");
|
58 |
// ToolsLocator.registerDefaultPersistenceManager(XMLPersistenceManager.class);
|
59 |
//
|
60 |
// System.out.println("Register DefaultMapContextManager...");
|
61 |
// MapContextLocator.registerMapContextManager(DefaultMapContextManager.class);
|
62 |
|
63 |
System.out.println("Creating temp file for persistence..."); |
64 |
tempFile = File.createTempFile("persisted_mapcontext", ".xml"); |
65 |
if (tempFile.exists()) tempFile.delete();
|
66 |
tempFile.createNewFile(); |
67 |
|
68 |
System.out.println("Creating viewport..."); |
69 |
ViewPort vp = getViewPort(); |
70 |
System.out.println("Creating mapcontext..."); |
71 |
mc = new MapContext(vp);
|
72 |
|
73 |
addDummyLayers(mc); |
74 |
|
75 |
} catch (Exception ex) { |
76 |
System.err.println("Error while setting up mapcontext persistence test: " + ex.getMessage()); |
77 |
assertTrue(false);
|
78 |
} |
79 |
} |
80 |
|
81 |
private ViewPort getViewPort() {
|
82 |
|
83 |
ViewPort vp = new ViewPort(null); |
84 |
|
85 |
vp.setImageSize(new Dimension(640, 480)); |
86 |
Envelope env = new Envelope2D(0, 0, 1000000, 1000000); |
87 |
vp.setEnvelope(env); |
88 |
env = new Envelope2D(200000, 200000, 500000, 500000); |
89 |
vp.setEnvelope(env); |
90 |
env = new Envelope2D(300000, 300000, 300000, 300000); |
91 |
vp.setEnvelope(env); |
92 |
env = new Envelope2D(400000, 400000, 200000, 200000); |
93 |
vp.setEnvelope(env); |
94 |
env = new Envelope2D(440000, 440000, 100000, 100000); |
95 |
vp.setEnvelope(env); |
96 |
env = new Envelope2D(480000, 480000, 30000, 30000); |
97 |
vp.setEnvelope(env); |
98 |
|
99 |
vp.setBackColor(Color.YELLOW);
|
100 |
vp.setClipRect(new Rectangle2D.Double(0, 0, 10, 10)); |
101 |
return vp;
|
102 |
} |
103 |
|
104 |
private void addDummyLayers(MapContext mcon) { |
105 |
|
106 |
FLayers resp = new FLayers();
|
107 |
resp.setMapContext(mcon); |
108 |
resp.setName("root layer");
|
109 |
|
110 |
FLayers aux = new FLayers();
|
111 |
aux.setMapContext(mcon); |
112 |
aux.setName("Group");
|
113 |
|
114 |
FLyrVect lyr = null;
|
115 |
IVectorLegend lgn = null;
|
116 |
FeatureStore fs = null;
|
117 |
|
118 |
System.out.println("Adding dummy layers..."); |
119 |
|
120 |
fs = new DummyFileFeatureStore("1"); |
121 |
lyr = createLayer(fs); |
122 |
lyr.setName("Layer 1");
|
123 |
try { lyr.setLegend(new DummyVectorLegend(TYPES.SOLID)); } catch (LegendLayerException e) { } |
124 |
|
125 |
aux.addLayer(lyr); |
126 |
|
127 |
fs = new DummyDBFeatureStore("A"); |
128 |
lyr = createLayer(fs); |
129 |
lyr.setName("Layer A");
|
130 |
try { lyr.setLegend(new DummyVectorLegend(TYPES.SOLID)); } catch (LegendLayerException e) { } |
131 |
|
132 |
aux.addLayer(lyr); |
133 |
|
134 |
fs = new DummyFileFeatureStore("2"); |
135 |
lyr = createLayer(fs); |
136 |
lyr.setName("Layer 2");
|
137 |
try { lyr.setLegend(new DummyVectorLegend(TYPES.SOLID)); } catch (LegendLayerException e) { } |
138 |
|
139 |
aux.addLayer(lyr); |
140 |
|
141 |
fs = new DummyDBFeatureStore("B"); |
142 |
lyr = createLayer(fs); |
143 |
lyr.setName("Layer 1");
|
144 |
try { lyr.setLegend(new DummyVectorLegend(TYPES.SOLID)); } catch (LegendLayerException e) { } |
145 |
|
146 |
resp.addLayer(lyr); |
147 |
resp.addLayer(aux); |
148 |
|
149 |
mcon.getLayers().addLayer(resp); |
150 |
} |
151 |
|
152 |
private FLyrVect createLayer(FeatureStore fs) {
|
153 |
|
154 |
FLyrVect resp = new FLyrVect();
|
155 |
try {
|
156 |
resp.setDataStore(fs); |
157 |
resp.wakeUp(); |
158 |
resp.load(); |
159 |
} catch (LoadLayerException e) {
|
160 |
assertTrue("While creating dummy layer: " + e.getMessage(), false); |
161 |
} |
162 |
return resp;
|
163 |
} |
164 |
|
165 |
public void testPersist() { |
166 |
|
167 |
if (true) return; |
168 |
|
169 |
|
170 |
System.out.println("Starting persistence test..."); |
171 |
|
172 |
try {
|
173 |
FileOutputStream fos = new FileOutputStream(tempFile); |
174 |
|
175 |
System.out.println("Getting mapcontext state..."); |
176 |
PersistentState pst = ToolsLocator.getPersistenceManager().getState(mc); |
177 |
System.out.println("Saving mapcontext state..."); |
178 |
ToolsLocator.getPersistenceManager().saveState(pst, fos); |
179 |
fos.close(); |
180 |
|
181 |
// ==============================================
|
182 |
// ==============================================
|
183 |
System.out.println("Mapcontext is now persisted!"); |
184 |
// ==============================================
|
185 |
|
186 |
System.out.println("Opening persistence file..."); |
187 |
FileInputStream fis = new FileInputStream(tempFile); |
188 |
|
189 |
System.out.println("Loading mapcontext state from file..."); |
190 |
pst = ToolsLocator.getPersistenceManager().loadState(fis); |
191 |
|
192 |
System.out.println("Instantiating mapcontext from state..."); |
193 |
MapContext mc2 = (MapContext) ToolsLocator.getPersistenceManager().create(pst); |
194 |
|
195 |
System.out.println("Comparing original and current mapcontext..."); |
196 |
compareMapContexts(mc, mc2); |
197 |
|
198 |
System.out.println("Successful mapcontext persistence test!"); |
199 |
|
200 |
} catch (Exception ex) { |
201 |
System.err.println("Error while testing persistence: " + ex.getMessage()); |
202 |
assertTrue(false);
|
203 |
} |
204 |
|
205 |
|
206 |
} |
207 |
|
208 |
private void compareMapContexts(MapContext m1, MapContext m2) { |
209 |
|
210 |
System.out.println("Getting viewports to compare..."); |
211 |
ViewPort vp1 = m1.getViewPort(); |
212 |
ViewPort vp2 = m2.getViewPort(); |
213 |
|
214 |
compareViewPorts(vp1, vp2); |
215 |
|
216 |
System.out.println("Getting root flayers to compare..."); |
217 |
FLayers lyr1 = m1.getLayers(); |
218 |
FLayers lyr2 = m2.getLayers(); |
219 |
compareLayers(lyr1, lyr2); |
220 |
} |
221 |
|
222 |
private void compareLayers(FLayer l1, FLayer l2) { |
223 |
|
224 |
System.out.println("Comparing layers..."); |
225 |
|
226 |
assertTrue("Layers of diff type!", l1.getClass().getName().compareTo(l2.getClass().getName()) == 0); |
227 |
if (l1 instanceof FLayers) { |
228 |
|
229 |
System.out.println("(type FLayers)..."); |
230 |
FLayers ls1 = (FLayers) l1; |
231 |
FLayers ls2 = (FLayers) l2; |
232 |
assertEquals(ls1.getLayersCount(), ls2.getLayersCount()); |
233 |
int sz = ls2.getLayersCount();
|
234 |
for (int i=0; i<sz; i++) { |
235 |
compareLayers(ls1.getLayer(i), ls2.getLayer(i)); |
236 |
} |
237 |
|
238 |
} else {
|
239 |
if (l1 instanceof FLyrVect) { |
240 |
|
241 |
System.out.println("(type FLyrVect)..."); |
242 |
FLyrVect ls1 = (FLyrVect) l1; |
243 |
FLyrVect ls2 = (FLyrVect) l2; |
244 |
compareVectLayers(ls1, ls2); |
245 |
|
246 |
} else {
|
247 |
|
248 |
} |
249 |
} |
250 |
|
251 |
} |
252 |
|
253 |
private void compareVectLayers(FLyrVect l1, FLyrVect l2) { |
254 |
compareLegend(l1.getLegend(), l2.getLegend()); |
255 |
compareStore(l1.getFeatureStore(), l2.getFeatureStore()); |
256 |
} |
257 |
|
258 |
private void compareStore(FeatureStore fs1, |
259 |
FeatureStore fs2) { |
260 |
|
261 |
System.out.println("Comparing stores..."); |
262 |
assertTrue("Diff stores!", fs1.getName().compareTo(fs2.getName()) == 0); |
263 |
} |
264 |
|
265 |
private void compareLegend(ILegend l1, ILegend l2) { |
266 |
|
267 |
System.out.println("Comparing legends..."); |
268 |
IVectorLegend vl1 = (IVectorLegend) l1; |
269 |
IVectorLegend vl2 = (IVectorLegend) l2; |
270 |
assertTrue("Diff legends!", vl1.getShapeType() == vl2.getShapeType());
|
271 |
|
272 |
} |
273 |
|
274 |
private void compareViewPorts(ViewPort vp1, ViewPort vp2) { |
275 |
|
276 |
|
277 |
System.out.println("Comparing viewports..."); |
278 |
|
279 |
assertEquals( |
280 |
vp1.getAdjustedEnvelope().getMinimum(0),
|
281 |
vp2.getAdjustedEnvelope().getMinimum(0),
|
282 |
0.00000000000001);
|
283 |
assertEquals( |
284 |
vp1.getAdjustedEnvelope().getMinimum(1),
|
285 |
vp2.getAdjustedEnvelope().getMinimum(1),
|
286 |
0.00000000000001);
|
287 |
assertEquals( |
288 |
vp1.getAdjustedEnvelope().getMaximum(0),
|
289 |
vp2.getAdjustedEnvelope().getMaximum(0),
|
290 |
0.00000000000001);
|
291 |
assertEquals( |
292 |
vp1.getAdjustedEnvelope().getMaximum(1),
|
293 |
vp2.getAdjustedEnvelope().getMaximum(1),
|
294 |
0.00000000000001);
|
295 |
} |
296 |
|
297 |
|
298 |
} |