Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap / src-test / org / gvsig / fmap / mapcontext / rendering / symbol / TestMultiLayerSymbol.java @ 21156

History | View | Annotate | Download (14.6 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.fmap.mapcontext.rendering.symbol;
42

    
43
import java.util.Random;
44

    
45
import junit.framework.TestCase;
46

    
47
import org.gvsig.fmap.mapcontext.rendering.symbols.IFillSymbol;
48
import org.gvsig.fmap.mapcontext.rendering.symbols.ILineSymbol;
49
import org.gvsig.fmap.mapcontext.rendering.symbols.IMarkerSymbol;
50
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
51
import org.gvsig.fmap.mapcontext.rendering.symbols.MultiLayerFillSymbol;
52
import org.gvsig.fmap.mapcontext.rendering.symbols.MultiLayerLineSymbol;
53
import org.gvsig.fmap.mapcontext.rendering.symbols.MultiLayerMarkerSymbol;
54
import org.gvsig.fmap.mapcontext.rendering.symbols.SimpleFillSymbol;
55
import org.gvsig.fmap.mapcontext.rendering.symbols.SimpleLineSymbol;
56
import org.gvsig.fmap.mapcontext.rendering.symbols.SimpleMarkerSymbol;
57

    
58
/**
59
 * @author jaume dominguez faus - jaume.dominguez@iver.es
60
 */
61
public class TestMultiLayerSymbol extends TestCase {
62
        ISymbol[] symbols;
63

    
64
        public void setUp() {
65
                symbols = TestISymbol.getNewSymbolInstances();
66
        }
67

    
68
        public void testLayerTypeAdditionFor_MARKERS() {
69
                MultiLayerMarkerSymbol multiLayer = new MultiLayerMarkerSymbol();
70
                for (int i = 0; i < symbols.length; i++) {
71
                        if (symbols[i] instanceof IMarkerSymbol) {
72
                                try {
73
                                IMarkerSymbol marker = (IMarkerSymbol) symbols[i];
74
                                multiLayer.addLayer(marker);
75
                                } catch (Exception e) {
76
                                        fail("MultiLayerMarkerSymbol failed adding a '"+symbols[i].getClassName());
77
                                }
78
                        } else {
79
                                try {
80
                                        multiLayer.addLayer(symbols[i]);
81
                                        fail("MultiLayerMarkerSymbol should not accept '"+symbols[i].getClassName()+"' symbols");
82
                                } catch (ClassCastException ccEx){
83
                                        // this is right
84
                                }
85
                        }
86
                }
87
        }
88

    
89
        public void testLayerTypeAdditionFor_LINES() {
90
                MultiLayerLineSymbol multiLayer = new MultiLayerLineSymbol();
91
                for (int i = 0; i < symbols.length; i++) {
92
                        if (symbols[i] instanceof ILineSymbol) {
93
                                try {
94
                                ILineSymbol line = (ILineSymbol) symbols[i];
95
                                multiLayer.addLayer(line);
96
                                } catch (Exception e) {
97
                                        fail("MultiLayerLineSymbol failed adding a '"+symbols[i].getClassName());
98
                                }
99
                        } else {
100
                                try {
101
                                        multiLayer.addLayer(symbols[i]);
102
                                        fail("MultiLayerLineSymbol should not accept '"+symbols[i].getClassName()+"' symbols");
103
                                } catch (ClassCastException ccEx){
104
                                        // this is right
105
                                }
106
                        }
107
                }
108
        }
109

    
110
        public void testLayerTypeAdditionFor_FILL() {
111
                MultiLayerFillSymbol multiLayer = new MultiLayerFillSymbol();
112
                for (int i = 0; i < symbols.length; i++) {
113
                        if (symbols[i] instanceof IFillSymbol) {
114
                                try {
115
                                        IFillSymbol fill = (IFillSymbol) symbols[i];
116
                                multiLayer.addLayer(fill);
117
                                } catch (Exception e) {
118
                                        fail("MultiLayerLineSymbol failed adding a '"+symbols[i].getClassName());
119
                                }
120
                        } else {
121
                                try {
122
                                        multiLayer.addLayer(symbols[i]);
123
                                        fail("MultiLayerLineSymbol should not accept '"+symbols[i].getClassName()+"' symbols");
124
                                } catch (ClassCastException ccEx){
125
                                        // this is right
126
                                }
127
                        }
128
                }
129
        }
130

    
131
        public void testLayerAdditionPositionConsistencyMultiLayer_MARKER_Symbol() {
132
                MultiLayerMarkerSymbol multiLayer = new MultiLayerMarkerSymbol();
133
                IMarkerSymbol sym1 = new SimpleMarkerSymbol();
134
                IMarkerSymbol sym2 = new SimpleMarkerSymbol();
135
                sym1.setDescription("sym1");
136
                sym2.setDescription("sym2");
137
                try {
138
                        multiLayer.addLayer(sym1, 0);
139
                } catch (IndexOutOfBoundsException ioobEx) {
140
                        fail("MultiLayer should always accept adding at index 0, even when it is empty");
141
                }
142

    
143
                try {
144
                        multiLayer.addLayer(sym2, 3);
145
                        fail("MultiLayer cannot accept adding at an index larger than the amount of layers already contained");
146
                } catch (IndexOutOfBoundsException ioobEx){
147
                        // this is right
148
                }
149

    
150
                try {
151
                        multiLayer.addLayer(sym1, multiLayer.getLayerCount());
152
                } catch (IndexOutOfBoundsException ioobEx) {
153
                        fail("MultiLayer should accept adding at index less or equal to the layer count");
154
                }
155

    
156
                assertTrue("layer returned does not correspond with the index provided", multiLayer.getLayer(1).getDescription().equals("sym2"));
157
                assertTrue("layer returned does not correspond with the index provided", multiLayer.getLayer(0).getDescription().equals("sym1"));
158
        }
159

    
160
        public void testLayerAdditionPositionConsistencyMultiLayer_LINE_Symbol() {
161
                MultiLayerLineSymbol multiLayer = new MultiLayerLineSymbol();
162
                ILineSymbol layer1 = new SimpleLineSymbol();
163
                ILineSymbol layer2 = new SimpleLineSymbol();
164
                layer1.setDescription("layer1");
165
                layer2.setDescription("layer2");
166

    
167
                try {
168
                        multiLayer.addLayer(layer1, 0);
169
                } catch (IndexOutOfBoundsException ioobEx) {
170
                        fail("MultiLayer should always accept adding at index 0, even when it is empty");
171
                }
172

    
173
                try {
174
                        multiLayer.addLayer(layer2, 3);
175
                        fail("MultiLayer cannot accept adding at an index larger than the amount of layers already contained");
176
                } catch (IndexOutOfBoundsException ioobEx){
177
                        // this is right
178
                }
179

    
180
                try {
181
                        multiLayer.addLayer(layer2, multiLayer.getLayerCount());
182
                } catch (IndexOutOfBoundsException ioobEx) {
183
                        fail("MultiLayer should accept adding at index less or equal to the layer count");
184
                }
185

    
186
                assertTrue("layer returned does not correspond with the index provided", multiLayer.getLayer(1).getDescription().equals("layer2"));
187
                assertTrue("layer returned does not correspond with the index provided", multiLayer.getLayer(0).getDescription().equals("layer1"));
188
        }
189

    
190
        public void testLayerAdditionPositionConsistencyMultiLayer_FILL_Symbol() {
191
                MultiLayerFillSymbol multiLayer = new MultiLayerFillSymbol();
192
                IFillSymbol layer1 = new SimpleFillSymbol();
193
                IFillSymbol layer2 = new SimpleFillSymbol();
194
                layer1.setDescription("layer1");
195
                layer2.setDescription("layer2");
196

    
197
                try {
198
                        multiLayer.addLayer(layer1, 0);
199
                } catch (IndexOutOfBoundsException ioobEx) {
200
                        fail("MultiLayer should always accept adding at index 0, even when it is empty");
201
                }
202

    
203
                try {
204
                        multiLayer.addLayer(layer2, 3);
205
                        fail("MultiLayer cannot accept adding at an index larger than the amount of layers already contained");
206
                } catch (IndexOutOfBoundsException ioobEx){
207
                        // this is right
208
                }
209

    
210
                try {
211
                        multiLayer.addLayer(layer2, multiLayer.getLayerCount());
212
                } catch (IndexOutOfBoundsException ioobEx) {
213
                        fail("MultiLayer should accept adding at index less or equal to the layer count");
214
                }
215

    
216
                assertTrue("layer returned does not correspond with the index provided", multiLayer.getLayer(1).getDescription().equals("layer2"));
217
                assertTrue("layer returned does not correspond with the index provided", multiLayer.getLayer(0).getDescription().equals("layer1"));
218
        }
219

    
220
        public void testMultiLayer_MARKER_SymbolLayerSwapping() {
221
                MultiLayerMarkerSymbol multiLayer = new MultiLayerMarkerSymbol();
222
                IMarkerSymbol layer1 = new SimpleMarkerSymbol();
223
                IMarkerSymbol layer2 = new SimpleMarkerSymbol();
224
                IMarkerSymbol layer3 = new SimpleMarkerSymbol();
225
                layer1.setDescription("layer1");
226
                layer2.setDescription("layer2");
227
                layer3.setDescription("layer3");
228

    
229
                multiLayer.addLayer(layer1);
230
                multiLayer.addLayer(layer2);
231
                multiLayer.addLayer(layer3);
232

    
233
                multiLayer.swapLayers(1,0);
234
                assertTrue("failed swapping layers 0 and 1",
235
                                   multiLayer.getLayer(0).getDescription().equals("layer2")
236
                                && multiLayer.getLayer(1).getDescription().equals("layer1"));
237

    
238
                multiLayer.swapLayers(1,2);
239
                assertTrue("failed swapping layers 0 and 1",
240
                                   multiLayer.getLayer(2).getDescription().equals("layer1")
241
                                && multiLayer.getLayer(1).getDescription().equals("layer3"));
242

    
243
                Random random = new Random(System.currentTimeMillis());
244
                int numTries = 50;
245
                for (int i = 0; i < numTries; i++) {
246
                        int n = random.nextInt(multiLayer.getLayerCount());
247
                        int m = random.nextInt(multiLayer.getLayerCount());
248

    
249
                        ISymbol aLayer = multiLayer.getLayer(n);
250
                        multiLayer.swapLayers(n, m);
251
                        ISymbol otherLayer = multiLayer.getLayer(m);
252
                        assertEquals("failed swapping layers "+n+" and "+m, aLayer, otherLayer);
253
                }
254

    
255
        }
256

    
257
        public void testMultiLayer_LINE_SymbolLayerSwapping() {
258
                MultiLayerLineSymbol multiLayer = new MultiLayerLineSymbol();
259
                ILineSymbol layer1 = new SimpleLineSymbol();
260
                ILineSymbol layer2 = new SimpleLineSymbol();
261
                ILineSymbol layer3 = new SimpleLineSymbol();
262
                layer1.setDescription("layer1");
263
                layer2.setDescription("layer2");
264
                layer3.setDescription("layer3");
265

    
266
                multiLayer.addLayer(layer1);
267
                multiLayer.addLayer(layer2);
268
                multiLayer.addLayer(layer3);
269

    
270
                multiLayer.swapLayers(1,0);
271
                assertTrue("failed swapping layers 0 and 1",
272
                                   multiLayer.getLayer(0).getDescription().equals("layer2")
273
                                && multiLayer.getLayer(1).getDescription().equals("layer1"));
274

    
275
                multiLayer.swapLayers(1,2);
276
                assertTrue("failed swapping layers 0 and 1",
277
                                   multiLayer.getLayer(2).getDescription().equals("layer1")
278
                                && multiLayer.getLayer(1).getDescription().equals("layer3"));
279

    
280
                Random random = new Random(System.currentTimeMillis());
281
                int numTries = 50;
282
                for (int i = 0; i < numTries; i++) {
283
                        int n = random.nextInt(multiLayer.getLayerCount());
284
                        int m = random.nextInt(multiLayer.getLayerCount());
285

    
286
                        ISymbol aLayer = multiLayer.getLayer(n);
287
                        multiLayer.swapLayers(n, m);
288
                        ISymbol otherLayer = multiLayer.getLayer(m);
289
                        assertEquals("failed swapping layers "+n+" and "+m, aLayer, otherLayer);
290
                }
291

    
292
        }
293

    
294
        public void testMultiLayer_FILL_SymbolLayerSwapping() {
295
                MultiLayerFillSymbol multiLayer = new MultiLayerFillSymbol();
296
                IFillSymbol layer1 = new SimpleFillSymbol();
297
                IFillSymbol layer2 = new SimpleFillSymbol();
298
                IFillSymbol layer3 = new SimpleFillSymbol();
299
                layer1.setDescription("layer1");
300
                layer2.setDescription("layer2");
301
                layer3.setDescription("layer3");
302

    
303
                multiLayer.addLayer(layer1);
304
                multiLayer.addLayer(layer2);
305
                multiLayer.addLayer(layer3);
306

    
307
                multiLayer.swapLayers(1,0);
308
                assertTrue("failed swapping layers 0 and 1",
309
                                   multiLayer.getLayer(0).getDescription().equals("layer2")
310
                                && multiLayer.getLayer(1).getDescription().equals("layer1"));
311

    
312
                multiLayer.swapLayers(1,2);
313
                assertTrue("failed swapping layers 0 and 1",
314
                                   multiLayer.getLayer(2).getDescription().equals("layer1")
315
                                && multiLayer.getLayer(1).getDescription().equals("layer3"));
316

    
317
                Random random = new Random(System.currentTimeMillis());
318
                int numTries = 50;
319
                for (int i = 0; i < numTries; i++) {
320
                        int n = random.nextInt(multiLayer.getLayerCount());
321
                        int m = random.nextInt(multiLayer.getLayerCount());
322

    
323
                        ISymbol aLayer = multiLayer.getLayer(n);
324
                        multiLayer.swapLayers(n, m);
325
                        ISymbol otherLayer = multiLayer.getLayer(m);
326
                        assertEquals("failed swapping layers "+n+" and "+m, aLayer, otherLayer);
327
                }
328
        }
329

    
330
        public void testMultiLayer_MARKER_IndexOutOfBounds() {
331
                MultiLayerMarkerSymbol multiLayer = new MultiLayerMarkerSymbol();
332
                IMarkerSymbol layer1 = new SimpleMarkerSymbol();
333
                IMarkerSymbol layer2 = new SimpleMarkerSymbol();
334
                IMarkerSymbol layer3 = new SimpleMarkerSymbol();
335
                layer1.setDescription("layer1");
336
                layer2.setDescription("layer2");
337
                layer3.setDescription("layer3");
338

    
339
                multiLayer.addLayer(layer1);
340
                multiLayer.addLayer(layer2);
341
                multiLayer.addLayer(layer3);
342

    
343
                for (int i = -4; i < multiLayer.getLayerCount()+4; i++) {
344
                        if (i<0 || i>=multiLayer.getLayerCount()) {
345
                                try {
346
                                        multiLayer.getLayer(i);
347
                                        fail("MultiLayerMarkerSymbol should throw an IndexOutOfBoundsException when accessing a layer at index "+i+", because layer count is "+multiLayer.getLayerCount());
348
                                } catch (IndexOutOfBoundsException ioobEx) {
349
                                        // this is correct;
350
                                }
351
                        } else {
352
                                try {
353
                                        multiLayer.getLayer(i);
354
                                } catch (IndexOutOfBoundsException ioobEx) {
355
                                        fail("MultiLayerMarkerSymbol threw an IndexOutOfBoundsException, but it has "+multiLayer.getLayerCount()+" layers");
356
                                }
357
                        }
358
                }
359
        }
360

    
361
        public void testMultiLayer_LINE_IndexOutOfBounds() {
362
                MultiLayerLineSymbol multiLayer = new MultiLayerLineSymbol();
363
                ILineSymbol layer1 = new SimpleLineSymbol();
364
                ILineSymbol layer2 = new SimpleLineSymbol();
365
                ILineSymbol layer3 = new SimpleLineSymbol();
366
                layer1.setDescription("layer1");
367
                layer2.setDescription("layer2");
368
                layer3.setDescription("layer3");
369

    
370
                multiLayer.addLayer(layer1);
371
                multiLayer.addLayer(layer2);
372
                multiLayer.addLayer(layer3);
373

    
374
                for (int i = -4; i < multiLayer.getLayerCount()+4; i++) {
375
                        if (i<0 || i>=multiLayer.getLayerCount()) {
376
                                try {
377
                                        multiLayer.getLayer(i);
378
                                        fail("MultiLayerLineSymbol should throw an IndexOutOfBoundsException when accessing a layer at index "+i+", because layer count is "+multiLayer.getLayerCount());
379
                                } catch (IndexOutOfBoundsException ioobEx) {
380
                                        // this is correct;
381
                                }
382
                        } else {
383
                                try {
384
                                        multiLayer.getLayer(i);
385
                                } catch (IndexOutOfBoundsException ioobEx) {
386
                                        fail("MultiLayerLineSymbol threw an IndexOutOfBoundsException, but it has "+multiLayer.getLayerCount()+" layers");
387
                                }
388
                        }
389
                }
390
        }
391

    
392
        public void testMultiLayer_FILL_IndexOutOfBounds() {
393
                MultiLayerFillSymbol multiLayer = new MultiLayerFillSymbol();
394
                IFillSymbol layer1 = new SimpleFillSymbol();
395
                IFillSymbol layer2 = new SimpleFillSymbol();
396
                IFillSymbol layer3 = new SimpleFillSymbol();
397
                layer1.setDescription("layer1");
398
                layer2.setDescription("layer2");
399
                layer3.setDescription("layer3");
400

    
401
                multiLayer.addLayer(layer1);
402
                multiLayer.addLayer(layer2);
403
                multiLayer.addLayer(layer3);
404
                for (int i = -4; i < multiLayer.getLayerCount()+4; i++) {
405
                        if (i<0 || i>=multiLayer.getLayerCount()) {
406
                                try {
407
                                        multiLayer.getLayer(i);
408
                                        fail("MultiLayerFillSymbol should throw an IndexOutOfBoundsException when accessing a layer at index "+i+", because layer count is "+multiLayer.getLayerCount());
409
                                } catch (IndexOutOfBoundsException ioobEx) {
410
                                        // this is correct;
411
                                }
412
                        } else {
413
                                try {
414
                                        multiLayer.getLayer(i);
415
                                } catch (IndexOutOfBoundsException ioobEx) {
416
                                        fail("MultiLayerFillSymbol threw an IndexOutOfBoundsException, but it has "+multiLayer.getLayerCount()+" layers");
417
                                }
418
                        }
419
                }
420

    
421
        }
422
}