Statistics
| Revision:

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

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.symbols;
42

    
43
import java.util.Random;
44

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

    
56

    
57
import junit.framework.TestCase;
58

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
256
        }
257

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

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

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

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

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

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

    
293
        }
294

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
422
        }
423
}