Revision 10627 trunk/libraries/libFMap/src-test/com/iver/cit/gvsig/fmap/layers/LayersIteratorTest.java

View differences:

LayersIteratorTest.java
8 8

  
9 9
import org.cresques.cts.IProjection;
10 10

  
11
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
11 12
import com.iver.cit.gvsig.fmap.MapContext;
12 13
import com.iver.cit.gvsig.fmap.ViewPort;
13 14
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
......
19 20
	private File baseDataPath;
20 21
	private File baseDriversPath;
21 22
	private String shpDriverName = "gvSIG shp driver";
22
	private IProjection projectionDefault = CRSFactory.getCRS("EPSG:23030"); 
23
	
23
	private IProjection projectionDefault = CRSFactory.getCRS("EPSG:23030");
24 24

  
25
	protected void setUp() throws Exception {		
25

  
26
	protected void setUp() throws Exception {
26 27
		super.setUp();
27 28
		URL url = this.getClass().getResource("LayersIteratorTest_data");
28
		if (url == null) 
29
		if (url == null)
29 30
			throw new Exception("Can't find 'LayersIteratorTest_data' dir");
30
		
31

  
31 32
		baseDataPath = new File(url.getFile());
32
		if (!baseDataPath.exists()) 
33
		if (!baseDataPath.exists())
33 34
			throw new Exception("Can't find 'LayersIteratorTest_data' dir");
34
		
35

  
35 36
		baseDriversPath = new File(fwAndamiDriverPath);
36
		if (!baseDriversPath.exists()) 
37
		if (!baseDriversPath.exists())
37 38
			throw new Exception("Can't find drivers path: " + fwAndamiDriverPath);
38
		
39

  
39 40
		LayerFactory.setDriversPath(baseDriversPath.getAbsolutePath());
40
		if (LayerFactory.getDM().getDriverNames().length < 1) 
41
		if (LayerFactory.getDM().getDriverNames().length < 1)
41 42
			throw new Exception("Can't find drivers in path: " + fwAndamiDriverPath);
42 43
	}
43 44

  
44
	
45

  
45 46
	private MapContext newMapContext() {
46 47
		ViewPort vp = new ViewPort(projectionDefault);
47 48
		return new MapContext(vp);
48 49
	}
49
	
50

  
50 51
	private FLayer newShpLayerToLayers(String shpFileName) {
51 52
		FLayerFileVectorial layerResult = new FLayerFileVectorial();
52 53
		layerResult.setName(shpFileName);
53 54
		layerResult.setDriverByName(shpDriverName);
54 55
		try {
55 56
			layerResult.setProjection(projectionDefault);
56
		} catch (Exception e1) {			
57
		} catch (Exception e1) {
57 58
			e1.printStackTrace();
58 59
			return null;
59 60
		}
......
63 64
			e.printStackTrace();
64 65
			return null;
65 66
		}
66
		return layerResult;		
67
		return layerResult;
67 68
	}
68
	
69

  
69 70
	private void testLayerIterator(LayersIterator iter,FLayer[] layers) {
70 71
		int i;
71 72
		FLayer aux;
72
	
73

  
73 74
		for (i=0;i<layers.length;i++) {
74 75
			if (!iter.hasNext()) {
75 76
				fail("iter count == "+i+", "+ (layers.length)+ "expected");
......
77 78
			aux = iter.nextLayer();
78 79
			assertEquals("element "+ i,aux,layers[i]);
79 80
		}
80
				
81

  
81 82
		assertFalse("inter hasNext",iter.hasNext());
82 83
		try {
83 84
			iter.next();
84 85
			fail("No Exception throw");
85 86
		} catch (NoSuchElementException e){
86
			
87

  
87 88
		} catch (Exception e ) {
88 89
			fail("Exception throw is not a NoSuchElementException instance");
89 90
		}
90
		
91

  
91 92
		try {
92 93
			iter.remove();
93 94
			fail("No Exception throw");
94 95
		} catch (UnsupportedOperationException e){
95
			
96

  
96 97
		} catch (Exception e ) {
97 98
			fail("Exception throw is not a UnsupportedOperationException instance");
98 99
		}
99 100

  
100
		
101

  
101 102
	}
102 103

  
103 104
	public void test1() {
......
109 110
		assertNotNull("x1.shp",layerX1);
110 111
		FLayer layerX2 = newShpLayerToLayers("x2.shp");
111 112
		assertNotNull("x2.shp",layerX1);
112
	
113
		root.addLayer(layerX);
114
		root.addLayer(layerX1);
115
		root.addLayer(layerX2);
116
		
113

  
114
		try {
115
			root.addLayer(layerX);
116
			root.addLayer(layerX1);
117
			root.addLayer(layerX2);
118
		} catch (CancelationException e) {
119
			// TODO Auto-generated catch block
120
			e.printStackTrace();
121
		}
117 122
		assertTrue("rootLayer.getLayersCount() == 3",root.getLayersCount() == 3);
118
		
123

  
119 124
		testLayerIterator(
120
				new LayersIterator(root), 
125
				new LayersIterator(root),
121 126
				new FLayer[] {root,layerX,layerX1,layerX2}
122
		);		
127
		);
123 128
	}
124 129

  
125 130
	public void test2() {
126 131
		MapContext mapContext = newMapContext();
127 132
		FLayers root = mapContext.getLayers();
128 133

  
129
		FLayer layerX = newShpLayerToLayers("x.shp");		
134
		FLayer layerX = newShpLayerToLayers("x.shp");
130 135
		assertNotNull("x.shp",layerX);
131 136
		FLayer layerX1 = newShpLayerToLayers("x1.shp");
132 137
		assertNotNull("x1.shp",layerX1);
133 138
		FLayer layerX2 = newShpLayerToLayers("x2.shp");
134 139
		assertNotNull("x2.shp",layerX1);
135
	
140

  
136 141
		FLayers group1 = new FLayers(mapContext,root);
137
		root.addLayer(group1);
138
		root.addLayer(layerX);
139
		group1.addLayer(layerX1);
140
		group1.addLayer(layerX2);
141
		
142
		try {
143
			root.addLayer(group1);
144
			root.addLayer(layerX);
145
			group1.addLayer(layerX1);
146
			group1.addLayer(layerX2);
147
		} catch (CancelationException e) {
148
			// TODO Auto-generated catch block
149
			e.printStackTrace();
150
		}
142 151
		assertTrue("rootLayer.getLayersCount() == 2",root.getLayersCount() == 2);
143 152
		assertTrue("group1.getLayersCount() == 2",group1.getLayersCount() == 2);
144
		
153

  
145 154
		testLayerIterator(
146
				new LayersIterator(root), 
155
				new LayersIterator(root),
147 156
				new FLayer[] {root,group1,layerX1,layerX2,layerX}
148
		);		
157
		);
149 158
	}
150 159

  
151 160
	public void test3() {
152 161
		MapContext mapContext = newMapContext();
153 162
		FLayers root = mapContext.getLayers();
154 163

  
155
		FLayer layerX = newShpLayerToLayers("x.shp");		
164
		FLayer layerX = newShpLayerToLayers("x.shp");
156 165
		assertNotNull("x.shp",layerX);
157 166
		FLayer layerX1 = newShpLayerToLayers("x1.shp");
158 167
		assertNotNull("x1.shp",layerX1);
159 168
		FLayer layerX2 = newShpLayerToLayers("x2.shp");
160 169
		assertNotNull("x2.shp",layerX1);
161
	
170

  
162 171
		FLayers group1 = new FLayers(mapContext,root);
163
		root.addLayer(group1);
172
		try {
173
			root.addLayer(group1);
174
		} catch (CancelationException e) {
175
			// TODO Auto-generated catch block
176
			e.printStackTrace();
177
		}
164 178
		FLayers group1_1 = new FLayers(mapContext,group1);
165
		group1.addLayer(group1_1);
179
		try {
180
			group1.addLayer(group1_1);
181
		} catch (CancelationException e) {
182
			// TODO Auto-generated catch block
183
			e.printStackTrace();
184
		}
166 185
		FLayers group1_2 = new FLayers(mapContext,group1);
167
		group1.addLayer(group1_2);
168
		
169
		root.addLayer(layerX);
170
		group1.addLayer(layerX1);
171
		group1_1.addLayer(layerX2);
172
		
186
		try {
187
			group1.addLayer(group1_2);
188
		} catch (CancelationException e) {
189
			// TODO Auto-generated catch block
190
			e.printStackTrace();
191
		}
192

  
193
		try {
194
			root.addLayer(layerX);
195
			group1.addLayer(layerX1);
196
			group1_1.addLayer(layerX2);
197
		} catch (CancelationException e) {
198
			// TODO Auto-generated catch block
199
			e.printStackTrace();
200
		}
173 201
		assertTrue("rootLayer.getLayersCount() == 2",root.getLayersCount() == 2);
174 202
		assertTrue("group1.getLayersCount() == 3",group1.getLayersCount() == 3);
175 203
		assertTrue("group1_1.getLayersCount() == 1",group1_1.getLayersCount() == 1);
176 204
		assertTrue("group1_2.getLayersCount() == 0",group1_2.getLayersCount() == 0);
177
		
205

  
178 206
		testLayerIterator(
179
				new LayersIterator(root), 
207
				new LayersIterator(root),
180 208
				new FLayer[] {root,group1,group1_1,layerX2,group1_2,layerX1,layerX}
181
		);	
209
		);
182 210

  
183 211
		testLayerIterator(
184
				new LayersIterator(group1), 
212
				new LayersIterator(group1),
185 213
				new FLayer[] {group1,group1_1,layerX2,group1_2,layerX1}
186
		);	
187
		
214
		);
215

  
188 216
	}
189 217

  
190 218
	public void test4() {
191 219
		MapContext mapContext = newMapContext();
192 220
		FLayers root = mapContext.getLayers();
193 221

  
194
		FLayer layerX = newShpLayerToLayers("x.shp");		
222
		FLayer layerX = newShpLayerToLayers("x.shp");
195 223
		assertNotNull("x.shp",layerX);
196 224
		FLayer layerX1 = newShpLayerToLayers("x1.shp");
197 225
		assertNotNull("x1.shp",layerX1);
198 226
		FLayer layerX2 = newShpLayerToLayers("x2.shp");
199 227
		assertNotNull("x2.shp",layerX1);
200
	
228

  
201 229
		FLayers group1 = new FLayers(mapContext,root);
202
		root.addLayer(group1);
230
		try {
231
			root.addLayer(group1);
232
		} catch (CancelationException e) {
233
			// TODO Auto-generated catch block
234
			e.printStackTrace();
235
		}
203 236
		FLayers group1_1 = new FLayers(mapContext,group1);
204
		group1.addLayer(group1_1);
237
		try {
238
			group1.addLayer(group1_1);
239
		} catch (CancelationException e) {
240
			// TODO Auto-generated catch block
241
			e.printStackTrace();
242
		}
205 243
		FLayers group1_2 = new FLayers(mapContext,group1);
206
		group1.addLayer(group1_2);
207
		
208
		root.addLayer(layerX);
209
		group1.addLayer(layerX1);
210
		group1_1.addLayer(layerX2);
211
		
244
		try {
245
			group1.addLayer(group1_2);
246
		} catch (CancelationException e) {
247
			// TODO Auto-generated catch block
248
			e.printStackTrace();
249
		}
250

  
251
		try {
252
			root.addLayer(layerX);
253
			group1.addLayer(layerX1);
254
			group1_1.addLayer(layerX2);
255
		} catch (CancelationException e) {
256
			// TODO Auto-generated catch block
257
			e.printStackTrace();
258
		}
212 259
		assertTrue("rootLayer.getLayersCount() == 2",root.getLayersCount() == 2);
213 260
		assertTrue("group1.getLayersCount() == 3",group1.getLayersCount() == 3);
214 261
		assertTrue("group1_1.getLayersCount() == 1",group1_1.getLayersCount() == 1);
215 262
		assertTrue("group1_2.getLayersCount() == 0",group1_2.getLayersCount() == 0);
216
		
263

  
217 264
		testLayerIterator(
218
				new myLayerIteratorFLayers(root), 
265
				new myLayerIteratorFLayers(root),
219 266
				new FLayer[] {root,group1,group1_1,group1_2}
220 267
		);
221
		
222
		LayersIterator iter; 
268

  
269
		LayersIterator iter;
223 270
		iter = new LayersIterator(root) {
224 271

  
225 272
			public boolean evaluate(FLayer layer) {
226 273
				return !(layer instanceof FLayers);
227 274
			}
228
			
275

  
229 276
		};
230
		
277

  
231 278
		testLayerIterator(
232
				iter, 
279
				iter,
233 280
				new FLayer[] {layerX2,layerX1,layerX}
234 281
		);
235 282

  
236
		
283

  
237 284
	}
238 285

  
239 286
}
......
243 290
		super(layer);
244 291
	}
245 292

  
246
	public boolean evaluate(FLayer layer) {		
293
	public boolean evaluate(FLayer layer) {
247 294
		return layer instanceof FLayers;
248 295
	}
249 296

  

Also available in: Unified diff