svn-gvsig-desktop / trunk / libraries / libFMap / src-test / com / iver / cit / gvsig / fmap / layers / LayersIteratorTest.java @ 10665
History | View | Annotate | Download (8.21 KB)
1 |
package com.iver.cit.gvsig.fmap.layers; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.FileNotFoundException; |
5 |
import java.io.IOException; |
6 |
import java.net.URL; |
7 |
import java.util.NoSuchElementException; |
8 |
|
9 |
import org.cresques.cts.IProjection; |
10 |
|
11 |
import com.hardcode.driverManager.DriverLoadException; |
12 |
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException; |
13 |
import com.iver.cit.gvsig.fmap.MapContext; |
14 |
import com.iver.cit.gvsig.fmap.ViewPort; |
15 |
import com.iver.cit.gvsig.fmap.crs.CRSFactory; |
16 |
|
17 |
import junit.framework.TestCase; |
18 |
|
19 |
public class LayersIteratorTest extends TestCase { |
20 |
static final String fwAndamiDriverPath = "../_fwAndami/gvSIG/extensiones/com.iver.cit.gvsig/drivers"; |
21 |
private File baseDataPath; |
22 |
private File baseDriversPath; |
23 |
private String shpDriverName = "gvSIG shp driver"; |
24 |
private IProjection projectionDefault = CRSFactory.getCRS("EPSG:23030"); |
25 |
|
26 |
|
27 |
protected void setUp() throws Exception { |
28 |
super.setUp();
|
29 |
URL url = this.getClass().getResource("LayersIteratorTest_data"); |
30 |
if (url == null) |
31 |
throw new Exception("Can't find 'LayersIteratorTest_data' dir"); |
32 |
|
33 |
baseDataPath = new File(url.getFile()); |
34 |
if (!baseDataPath.exists())
|
35 |
throw new Exception("Can't find 'LayersIteratorTest_data' dir"); |
36 |
|
37 |
baseDriversPath = new File(fwAndamiDriverPath); |
38 |
if (!baseDriversPath.exists())
|
39 |
throw new Exception("Can't find drivers path: " + fwAndamiDriverPath); |
40 |
|
41 |
LayerFactory.setDriversPath(baseDriversPath.getAbsolutePath()); |
42 |
if (LayerFactory.getDM().getDriverNames().length < 1) |
43 |
throw new Exception("Can't find drivers in path: " + fwAndamiDriverPath); |
44 |
} |
45 |
|
46 |
|
47 |
private MapContext newMapContext() {
|
48 |
ViewPort vp = new ViewPort(projectionDefault);
|
49 |
return new MapContext(vp); |
50 |
} |
51 |
|
52 |
private FLayer newShpLayerToLayers(String shpFileName) { |
53 |
FLayerFileVectorial layerResult = new FLayerFileVectorial();
|
54 |
layerResult.setName(shpFileName); |
55 |
try {
|
56 |
layerResult.setDriverByName(shpDriverName); |
57 |
} catch (DriverLoadException e2) {
|
58 |
// TODO Auto-generated catch block
|
59 |
e2.printStackTrace(); |
60 |
} |
61 |
try {
|
62 |
layerResult.setProjection(projectionDefault); |
63 |
} catch (Exception e1) { |
64 |
e1.printStackTrace(); |
65 |
return null; |
66 |
} |
67 |
try {
|
68 |
layerResult.setFileName(baseDataPath+File.separator+shpFileName);
|
69 |
} catch (FileNotFoundException e) { |
70 |
e.printStackTrace(); |
71 |
return null; |
72 |
} |
73 |
return layerResult;
|
74 |
} |
75 |
|
76 |
private void testLayerIterator(LayersIterator iter,FLayer[] layers) { |
77 |
int i;
|
78 |
FLayer aux; |
79 |
|
80 |
for (i=0;i<layers.length;i++) { |
81 |
if (!iter.hasNext()) {
|
82 |
fail("iter count == "+i+", "+ (layers.length)+ "expected"); |
83 |
} |
84 |
aux = iter.nextLayer(); |
85 |
assertEquals("element "+ i,aux,layers[i]);
|
86 |
} |
87 |
|
88 |
assertFalse("inter hasNext",iter.hasNext());
|
89 |
try {
|
90 |
iter.next(); |
91 |
fail("No Exception throw");
|
92 |
} catch (NoSuchElementException e){ |
93 |
|
94 |
} catch (Exception e ) { |
95 |
fail("Exception throw is not a NoSuchElementException instance");
|
96 |
} |
97 |
|
98 |
try {
|
99 |
iter.remove(); |
100 |
fail("No Exception throw");
|
101 |
} catch (UnsupportedOperationException e){ |
102 |
|
103 |
} catch (Exception e ) { |
104 |
fail("Exception throw is not a UnsupportedOperationException instance");
|
105 |
} |
106 |
|
107 |
|
108 |
} |
109 |
|
110 |
public void test1() { |
111 |
MapContext mapContext = newMapContext(); |
112 |
FLayers root = mapContext.getLayers(); |
113 |
FLayer layerX = newShpLayerToLayers("x.shp");
|
114 |
assertNotNull("x.shp",layerX);
|
115 |
FLayer layerX1 = newShpLayerToLayers("x1.shp");
|
116 |
assertNotNull("x1.shp",layerX1);
|
117 |
FLayer layerX2 = newShpLayerToLayers("x2.shp");
|
118 |
assertNotNull("x2.shp",layerX1);
|
119 |
|
120 |
try {
|
121 |
root.addLayer(layerX); |
122 |
root.addLayer(layerX1); |
123 |
root.addLayer(layerX2); |
124 |
} catch (CancelationException e) {
|
125 |
// TODO Auto-generated catch block
|
126 |
e.printStackTrace(); |
127 |
} |
128 |
assertTrue("rootLayer.getLayersCount() == 3",root.getLayersCount() == 3); |
129 |
|
130 |
testLayerIterator( |
131 |
new LayersIterator(root),
|
132 |
new FLayer[] {root,layerX,layerX1,layerX2} |
133 |
); |
134 |
} |
135 |
|
136 |
public void test2() { |
137 |
MapContext mapContext = newMapContext(); |
138 |
FLayers root = mapContext.getLayers(); |
139 |
|
140 |
FLayer layerX = newShpLayerToLayers("x.shp");
|
141 |
assertNotNull("x.shp",layerX);
|
142 |
FLayer layerX1 = newShpLayerToLayers("x1.shp");
|
143 |
assertNotNull("x1.shp",layerX1);
|
144 |
FLayer layerX2 = newShpLayerToLayers("x2.shp");
|
145 |
assertNotNull("x2.shp",layerX1);
|
146 |
|
147 |
FLayers group1 = new FLayers(mapContext,root);
|
148 |
try {
|
149 |
root.addLayer(group1); |
150 |
root.addLayer(layerX); |
151 |
group1.addLayer(layerX1); |
152 |
group1.addLayer(layerX2); |
153 |
} catch (CancelationException e) {
|
154 |
// TODO Auto-generated catch block
|
155 |
e.printStackTrace(); |
156 |
} |
157 |
assertTrue("rootLayer.getLayersCount() == 2",root.getLayersCount() == 2); |
158 |
assertTrue("group1.getLayersCount() == 2",group1.getLayersCount() == 2); |
159 |
|
160 |
testLayerIterator( |
161 |
new LayersIterator(root),
|
162 |
new FLayer[] {root,group1,layerX1,layerX2,layerX} |
163 |
); |
164 |
} |
165 |
|
166 |
public void test3() { |
167 |
MapContext mapContext = newMapContext(); |
168 |
FLayers root = mapContext.getLayers(); |
169 |
|
170 |
FLayer layerX = newShpLayerToLayers("x.shp");
|
171 |
assertNotNull("x.shp",layerX);
|
172 |
FLayer layerX1 = newShpLayerToLayers("x1.shp");
|
173 |
assertNotNull("x1.shp",layerX1);
|
174 |
FLayer layerX2 = newShpLayerToLayers("x2.shp");
|
175 |
assertNotNull("x2.shp",layerX1);
|
176 |
|
177 |
FLayers group1 = new FLayers(mapContext,root);
|
178 |
try {
|
179 |
root.addLayer(group1); |
180 |
} catch (CancelationException e) {
|
181 |
// TODO Auto-generated catch block
|
182 |
e.printStackTrace(); |
183 |
} |
184 |
FLayers group1_1 = new FLayers(mapContext,group1);
|
185 |
try {
|
186 |
group1.addLayer(group1_1); |
187 |
} catch (CancelationException e) {
|
188 |
// TODO Auto-generated catch block
|
189 |
e.printStackTrace(); |
190 |
} |
191 |
FLayers group1_2 = new FLayers(mapContext,group1);
|
192 |
try {
|
193 |
group1.addLayer(group1_2); |
194 |
} catch (CancelationException e) {
|
195 |
// TODO Auto-generated catch block
|
196 |
e.printStackTrace(); |
197 |
} |
198 |
|
199 |
try {
|
200 |
root.addLayer(layerX); |
201 |
group1.addLayer(layerX1); |
202 |
group1_1.addLayer(layerX2); |
203 |
} catch (CancelationException e) {
|
204 |
// TODO Auto-generated catch block
|
205 |
e.printStackTrace(); |
206 |
} |
207 |
assertTrue("rootLayer.getLayersCount() == 2",root.getLayersCount() == 2); |
208 |
assertTrue("group1.getLayersCount() == 3",group1.getLayersCount() == 3); |
209 |
assertTrue("group1_1.getLayersCount() == 1",group1_1.getLayersCount() == 1); |
210 |
assertTrue("group1_2.getLayersCount() == 0",group1_2.getLayersCount() == 0); |
211 |
|
212 |
testLayerIterator( |
213 |
new LayersIterator(root),
|
214 |
new FLayer[] {root,group1,group1_1,layerX2,group1_2,layerX1,layerX} |
215 |
); |
216 |
|
217 |
testLayerIterator( |
218 |
new LayersIterator(group1),
|
219 |
new FLayer[] {group1,group1_1,layerX2,group1_2,layerX1} |
220 |
); |
221 |
|
222 |
} |
223 |
|
224 |
public void test4() { |
225 |
MapContext mapContext = newMapContext(); |
226 |
FLayers root = mapContext.getLayers(); |
227 |
|
228 |
FLayer layerX = newShpLayerToLayers("x.shp");
|
229 |
assertNotNull("x.shp",layerX);
|
230 |
FLayer layerX1 = newShpLayerToLayers("x1.shp");
|
231 |
assertNotNull("x1.shp",layerX1);
|
232 |
FLayer layerX2 = newShpLayerToLayers("x2.shp");
|
233 |
assertNotNull("x2.shp",layerX1);
|
234 |
|
235 |
FLayers group1 = new FLayers(mapContext,root);
|
236 |
try {
|
237 |
root.addLayer(group1); |
238 |
} catch (CancelationException e) {
|
239 |
// TODO Auto-generated catch block
|
240 |
e.printStackTrace(); |
241 |
} |
242 |
FLayers group1_1 = new FLayers(mapContext,group1);
|
243 |
try {
|
244 |
group1.addLayer(group1_1); |
245 |
} catch (CancelationException e) {
|
246 |
// TODO Auto-generated catch block
|
247 |
e.printStackTrace(); |
248 |
} |
249 |
FLayers group1_2 = new FLayers(mapContext,group1);
|
250 |
try {
|
251 |
group1.addLayer(group1_2); |
252 |
} catch (CancelationException e) {
|
253 |
// TODO Auto-generated catch block
|
254 |
e.printStackTrace(); |
255 |
} |
256 |
|
257 |
try {
|
258 |
root.addLayer(layerX); |
259 |
group1.addLayer(layerX1); |
260 |
group1_1.addLayer(layerX2); |
261 |
} catch (CancelationException e) {
|
262 |
// TODO Auto-generated catch block
|
263 |
e.printStackTrace(); |
264 |
} |
265 |
assertTrue("rootLayer.getLayersCount() == 2",root.getLayersCount() == 2); |
266 |
assertTrue("group1.getLayersCount() == 3",group1.getLayersCount() == 3); |
267 |
assertTrue("group1_1.getLayersCount() == 1",group1_1.getLayersCount() == 1); |
268 |
assertTrue("group1_2.getLayersCount() == 0",group1_2.getLayersCount() == 0); |
269 |
|
270 |
testLayerIterator( |
271 |
new myLayerIteratorFLayers(root),
|
272 |
new FLayer[] {root,group1,group1_1,group1_2} |
273 |
); |
274 |
|
275 |
LayersIterator iter; |
276 |
iter = new LayersIterator(root) {
|
277 |
|
278 |
public boolean evaluate(FLayer layer) { |
279 |
return !(layer instanceof FLayers); |
280 |
} |
281 |
|
282 |
}; |
283 |
|
284 |
testLayerIterator( |
285 |
iter, |
286 |
new FLayer[] {layerX2,layerX1,layerX} |
287 |
); |
288 |
|
289 |
|
290 |
} |
291 |
|
292 |
} |
293 |
|
294 |
class myLayerIteratorFLayers extends LayersIterator { |
295 |
public myLayerIteratorFLayers(FLayer layer) {
|
296 |
super(layer);
|
297 |
} |
298 |
|
299 |
public boolean evaluate(FLayer layer) { |
300 |
return layer instanceof FLayers; |
301 |
} |
302 |
|
303 |
} |