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