Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap / src-test / org / gvsig / fmap / mapcontext / layers / LayersIteratorTest.java @ 20984

History | View | Annotate | Download (8.96 KB)

1
package org.gvsig.fmap.mapcontext.layers;
2

    
3
import java.io.File;
4
import java.io.FileNotFoundException;
5
import java.net.URL;
6
import java.util.NoSuchElementException;
7

    
8
import junit.framework.TestCase;
9

    
10
import org.cresques.cts.IProjection;
11
import org.gvsig.fmap.crs.CRSFactory;
12
import org.gvsig.fmap.mapcontext.MapContext;
13
import org.gvsig.fmap.mapcontext.ViewPort;
14
import org.gvsig.fmap.mapcontext.layers.CancelationException;
15
import org.gvsig.fmap.mapcontext.layers.FLayer;
16
import org.gvsig.fmap.mapcontext.layers.FLayers;
17
import org.gvsig.fmap.mapcontext.layers.LayerFactory;
18
import org.gvsig.fmap.mapcontext.layers.LayersIterator;
19
import org.gvsig.fmap.mapcontext.layers.vectorial.FLayerFileVectorial;
20

    
21
import com.hardcode.driverManager.DriverLoadException;
22

    
23
public class LayersIteratorTest extends TestCase {
24
        static final String fwAndamiDriverPath = "../_fwAndami/gvSIG/extensiones/com.iver.cit.gvsig/drivers";
25
        private File baseDataPath;
26
        private File baseDriversPath;
27
        private String shpDriverName = "gvSIG shp driver";
28
        private IProjection projectionDefault = CRSFactory.getCRS("EPSG:23030");
29

    
30

    
31
        protected void setUp() throws Exception {
32
                super.setUp();
33
                URL url = this.getClass().getResource("LayersIteratorTest_data");
34
                if (url == null)
35
                        throw new Exception("Can't find 'LayersIteratorTest_data' dir");
36

    
37
                baseDataPath = new File(url.getFile());
38
                if (!baseDataPath.exists())
39
                        throw new Exception("Can't find 'LayersIteratorTest_data' dir");
40

    
41
                baseDriversPath = new File(fwAndamiDriverPath);
42
                if (!baseDriversPath.exists())
43
                        throw new Exception("Can't find drivers path: " + fwAndamiDriverPath);
44

    
45
                LayerFactory.setDriversPath(baseDriversPath.getAbsolutePath());
46
                if (LayerFactory.getDM().getDriverNames().length < 1)
47
                        throw new Exception("Can't find drivers in path: " + fwAndamiDriverPath);
48
        }
49

    
50

    
51
        private MapContext newMapContext() {
52
                ViewPort vp = new ViewPort(projectionDefault);
53
                return new MapContext(vp);
54
        }
55

    
56
        private FLayer newShpLayerToLayers(String shpFileName) {
57
                FLayerFileVectorial layerResult = new FLayerFileVectorial();
58
                layerResult.setName(shpFileName);
59
                try {
60
                        layerResult.setDriverByName(shpDriverName);
61
                } catch (DriverLoadException e2) {
62
                        // TODO Auto-generated catch block
63
                        e2.printStackTrace();
64
                }
65
                try {
66
                        layerResult.setProjection(projectionDefault);
67
                } catch (Exception e1) {
68
                        e1.printStackTrace();
69
                        return null;
70
                }
71
                try {
72
                        layerResult.setFileName(baseDataPath+File.separator+shpFileName);
73
                } catch (FileNotFoundException e) {
74
                        e.printStackTrace();
75
                        return null;
76
                }
77
                return layerResult;
78
        }
79

    
80
        private void testLayerIterator(LayersIterator iter,FLayer[] layers) {
81
                int i;
82
                FLayer aux;
83

    
84
                for (i=0;i<layers.length;i++) {
85
                        if (!iter.hasNext()) {
86
                                fail("iter count == "+i+", "+ (layers.length)+ "expected");
87
                        }
88
                        aux = iter.nextLayer();
89
                        assertEquals("element "+ i,aux,layers[i]);
90
                }
91

    
92
                assertFalse("inter hasNext",iter.hasNext());
93
                try {
94
                        iter.next();
95
                        fail("No Exception throw");
96
                } catch (NoSuchElementException e){
97

    
98
                } catch (Exception e ) {
99
                        fail("Exception throw is not a NoSuchElementException instance");
100
                }
101

    
102
                try {
103
                        iter.remove();
104
                        fail("No Exception throw");
105
                } catch (UnsupportedOperationException e){
106

    
107
                } catch (Exception e ) {
108
                        fail("Exception throw is not a UnsupportedOperationException instance");
109
                }
110

    
111

    
112
        }
113

    
114
        public void test1() {
115
                MapContext mapContext = newMapContext();
116
                FLayers root = mapContext.getLayers();
117
                FLayer layerX = newShpLayerToLayers("x.shp");
118
                assertNotNull("x.shp",layerX);
119
                FLayer layerX1 = newShpLayerToLayers("x1.shp");
120
                assertNotNull("x1.shp",layerX1);
121
                FLayer layerX2 = newShpLayerToLayers("x2.shp");
122
                assertNotNull("x2.shp",layerX1);
123

    
124
                try {
125
                        root.addLayer(layerX);
126
                        root.addLayer(layerX1);
127
                        root.addLayer(layerX2);
128
                } catch (CancelationException e) {
129
                        // TODO Auto-generated catch block
130
                        e.printStackTrace();
131
                }
132
                assertTrue("rootLayer.getLayersCount() == 3",root.getLayersCount() == 3);
133

    
134
                testLayerIterator(
135
                                new LayersIterator(root),
136
                                new FLayer[] {root,layerX,layerX1,layerX2}
137
                );
138
        }
139

    
140
        public void test2() {
141
                MapContext mapContext = newMapContext();
142
                FLayers root = mapContext.getLayers();
143

    
144
                FLayer layerX = newShpLayerToLayers("x.shp");
145
                assertNotNull("x.shp",layerX);
146
                FLayer layerX1 = newShpLayerToLayers("x1.shp");
147
                assertNotNull("x1.shp",layerX1);
148
                FLayer layerX2 = newShpLayerToLayers("x2.shp");
149
                assertNotNull("x2.shp",layerX1);
150

    
151
                FLayers group1 = new FLayers();//(mapContext,root);
152
                group1.setMapContext(mapContext);
153
                group1.setParentLayer(root);
154
                try {
155
                        root.addLayer(group1);
156
                        root.addLayer(layerX);
157
                        group1.addLayer(layerX1);
158
                        group1.addLayer(layerX2);
159
                } catch (CancelationException e) {
160
                        // TODO Auto-generated catch block
161
                        e.printStackTrace();
162
                }
163
                assertTrue("rootLayer.getLayersCount() == 2",root.getLayersCount() == 2);
164
                assertTrue("group1.getLayersCount() == 2",group1.getLayersCount() == 2);
165

    
166
                testLayerIterator(
167
                                new LayersIterator(root),
168
                                new FLayer[] {root,group1,layerX1,layerX2,layerX}
169
                );
170
        }
171

    
172
        public void test3() {
173
                MapContext mapContext = newMapContext();
174
                FLayers root = mapContext.getLayers();
175

    
176
                FLayer layerX = newShpLayerToLayers("x.shp");
177
                assertNotNull("x.shp",layerX);
178
                FLayer layerX1 = newShpLayerToLayers("x1.shp");
179
                assertNotNull("x1.shp",layerX1);
180
                FLayer layerX2 = newShpLayerToLayers("x2.shp");
181
                assertNotNull("x2.shp",layerX1);
182

    
183
                FLayers group1 = new FLayers();//(mapContext,root);
184
                group1.setMapContext(mapContext);
185
                group1.setParentLayer(root);
186
                try {
187
                        root.addLayer(group1);
188
                } catch (CancelationException e) {
189
                        // TODO Auto-generated catch block
190
                        e.printStackTrace();
191
                }
192
                FLayers group1_1 = new FLayers();//(mapContext,group1);
193
                group1_1.setMapContext(mapContext);
194
                group1_1.setParentLayer(group1);
195
                try {
196
                        group1.addLayer(group1_1);
197
                } catch (CancelationException e) {
198
                        // TODO Auto-generated catch block
199
                        e.printStackTrace();
200
                }
201
                FLayers group1_2 = new FLayers();//(mapContext,group1);
202
                group1_2.setMapContext(mapContext);
203
                group1_2.setParentLayer(group1);
204

    
205
                try {
206
                        group1.addLayer(group1_2);
207
                } catch (CancelationException e) {
208
                        // TODO Auto-generated catch block
209
                        e.printStackTrace();
210
                }
211

    
212
                try {
213
                        root.addLayer(layerX);
214
                        group1.addLayer(layerX1);
215
                        group1_1.addLayer(layerX2);
216
                } catch (CancelationException e) {
217
                        // TODO Auto-generated catch block
218
                        e.printStackTrace();
219
                }
220
                assertTrue("rootLayer.getLayersCount() == 2",root.getLayersCount() == 2);
221
                assertTrue("group1.getLayersCount() == 3",group1.getLayersCount() == 3);
222
                assertTrue("group1_1.getLayersCount() == 1",group1_1.getLayersCount() == 1);
223
                assertTrue("group1_2.getLayersCount() == 0",group1_2.getLayersCount() == 0);
224

    
225
                testLayerIterator(
226
                                new LayersIterator(root),
227
                                new FLayer[] {root,group1,group1_1,layerX2,group1_2,layerX1,layerX}
228
                );
229

    
230
                testLayerIterator(
231
                                new LayersIterator(group1),
232
                                new FLayer[] {group1,group1_1,layerX2,group1_2,layerX1}
233
                );
234

    
235
        }
236

    
237
        public void test4() {
238
                MapContext mapContext = newMapContext();
239
                FLayers root = mapContext.getLayers();
240

    
241
                FLayer layerX = newShpLayerToLayers("x.shp");
242
                assertNotNull("x.shp",layerX);
243
                FLayer layerX1 = newShpLayerToLayers("x1.shp");
244
                assertNotNull("x1.shp",layerX1);
245
                FLayer layerX2 = newShpLayerToLayers("x2.shp");
246
                assertNotNull("x2.shp",layerX1);
247

    
248
                FLayers group1 = new FLayers();//(mapContext,root);
249
                group1.setMapContext(mapContext);
250
                group1.setParentLayer(root);
251
                try {
252
                        root.addLayer(group1);
253
                } catch (CancelationException e) {
254
                        // TODO Auto-generated catch block
255
                        e.printStackTrace();
256
                }
257
                FLayers group1_1 = new FLayers();//(mapContext,root);
258
                group1_1.setMapContext(mapContext);
259
                group1_1.setParentLayer(group1);
260
                try {
261
                        group1.addLayer(group1_1);
262
                } catch (CancelationException e) {
263
                        // TODO Auto-generated catch block
264
                        e.printStackTrace();
265
                }
266
                FLayers group1_2 = new FLayers();//(mapContext,root);
267
                group1_2.setMapContext(mapContext);
268
                group1_2.setParentLayer(group1);
269
                try {
270
                        group1.addLayer(group1_2);
271
                } catch (CancelationException e) {
272
                        // TODO Auto-generated catch block
273
                        e.printStackTrace();
274
                }
275

    
276
                try {
277
                        root.addLayer(layerX);
278
                        group1.addLayer(layerX1);
279
                        group1_1.addLayer(layerX2);
280
                } catch (CancelationException e) {
281
                        // TODO Auto-generated catch block
282
                        e.printStackTrace();
283
                }
284
                assertTrue("rootLayer.getLayersCount() == 2",root.getLayersCount() == 2);
285
                assertTrue("group1.getLayersCount() == 3",group1.getLayersCount() == 3);
286
                assertTrue("group1_1.getLayersCount() == 1",group1_1.getLayersCount() == 1);
287
                assertTrue("group1_2.getLayersCount() == 0",group1_2.getLayersCount() == 0);
288

    
289
                testLayerIterator(
290
                                new myLayerIteratorFLayers(root),
291
                                new FLayer[] {root,group1,group1_1,group1_2}
292
                );
293

    
294
                LayersIterator iter;
295
                iter = new LayersIterator(root) {
296

    
297
                        public boolean evaluate(FLayer layer) {
298
                                return !(layer instanceof FLayers);
299
                        }
300

    
301
                };
302

    
303
                testLayerIterator(
304
                                iter,
305
                                new FLayer[] {layerX2,layerX1,layerX}
306
                );
307

    
308

    
309
        }
310

    
311
}
312

    
313
class myLayerIteratorFLayers extends LayersIterator {
314
        public myLayerIteratorFLayers(FLayer layer) {
315
                super(layer);
316
        }
317

    
318
        public boolean evaluate(FLayer layer) {
319
                return layer instanceof FLayers;
320
        }
321

    
322
}