Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.symbology / org.gvsig.symbology.lib / org.gvsig.symbology.lib.impl / src / test / java / org / gvsig / symbology / fmap / mapcontext / rendering / symbol / TestMultiLayerSymbol.java @ 40560

History | View | Annotate | Download (14.6 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.symbology.fmap.mapcontext.rendering.symbol;
25

    
26
import java.util.Random;
27

    
28
import junit.framework.TestCase;
29

    
30
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
31
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol;
32
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.impl.MultiLayerFillSymbol;
33
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.impl.SimpleFillSymbol;
34
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
35
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.impl.MultiLayerLineSymbol;
36
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.impl.SimpleLineSymbol;
37
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
38
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.impl.MultiLayerMarkerSymbol;
39
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.impl.SimpleMarkerSymbol;
40

    
41
/**
42
 * @author jaume dominguez faus - jaume.dominguez@iver.es
43
 */
44
public class TestMultiLayerSymbol extends TestCase {
45
        ISymbol[] symbols;
46

    
47
        public void setUp() {
48
                symbols = TestISymbol.getNewSymbolInstances();
49
        }
50

    
51
        public void testLayerTypeAdditionFor_MARKERS() {
52
                MultiLayerMarkerSymbol multiLayer = new MultiLayerMarkerSymbol();
53
                for (int i = 0; i < symbols.length; i++) {
54
                        if (symbols[i] instanceof IMarkerSymbol) {
55
                                try {
56
                                IMarkerSymbol marker = (IMarkerSymbol) symbols[i];
57
                                multiLayer.addLayer(marker);
58
                                } catch (Exception e) {
59
                                        fail("MultiLayerMarkerSymbol failed adding a '"+symbols[i].getClass().getName());
60
                                }
61
                        } else {
62
                                try {
63
                                        multiLayer.addLayer(symbols[i]);
64
                                        fail("MultiLayerMarkerSymbol should not accept '"+symbols[i].getClass().getName()+"' symbols");
65
                                } catch (ClassCastException ccEx){
66
                                        // this is right
67
                                }
68
                        }
69
                }
70
        }
71

    
72
        public void testLayerTypeAdditionFor_LINES() {
73
                MultiLayerLineSymbol multiLayer = new MultiLayerLineSymbol();
74
                for (int i = 0; i < symbols.length; i++) {
75
                        if (symbols[i] instanceof ILineSymbol) {
76
                                try {
77
                                ILineSymbol line = (ILineSymbol) symbols[i];
78
                                multiLayer.addLayer(line);
79
                                } catch (Exception e) {
80
                                        fail("MultiLayerLineSymbol failed adding a '"+symbols[i].getClass().getName());
81
                                }
82
                        } else {
83
                                try {
84
                                        multiLayer.addLayer(symbols[i]);
85
                                        fail("MultiLayerLineSymbol should not accept '"+symbols[i].getClass().getName()+"' symbols");
86
                                } catch (ClassCastException ccEx){
87
                                        // this is right
88
                                }
89
                        }
90
                }
91
        }
92

    
93
        public void testLayerTypeAdditionFor_FILL() {
94
                MultiLayerFillSymbol multiLayer = new MultiLayerFillSymbol();
95
                for (int i = 0; i < symbols.length; i++) {
96
                        if (symbols[i] instanceof IFillSymbol) {
97
                                try {
98
                                        IFillSymbol fill = (IFillSymbol) symbols[i];
99
                                multiLayer.addLayer(fill);
100
                                } catch (Exception e) {
101
                                        fail("MultiLayerLineSymbol failed adding a '"+symbols[i].getClass().getName());
102
                                }
103
                        } else {
104
                                try {
105
                                        multiLayer.addLayer(symbols[i]);
106
                                        fail("MultiLayerLineSymbol should not accept '"+symbols[i].getClass().getName()+"' symbols");
107
                                } catch (ClassCastException ccEx){
108
                                        // this is right
109
                                }
110
                        }
111
                }
112
        }
113

    
114
        public void testLayerAdditionPositionConsistencyMultiLayer_MARKER_Symbol() {
115
                MultiLayerMarkerSymbol multiLayer = new MultiLayerMarkerSymbol();
116
                IMarkerSymbol sym1 = new SimpleMarkerSymbol();
117
                IMarkerSymbol sym2 = new SimpleMarkerSymbol();
118
                sym1.setDescription("sym1");
119
                sym2.setDescription("sym2");
120
                try {
121
                        multiLayer.addLayer(sym1, 0);
122
                } catch (IndexOutOfBoundsException ioobEx) {
123
                        fail("MultiLayer should always accept adding at index 0, even when it is empty");
124
                }
125

    
126
                try {
127
                        multiLayer.addLayer(sym2, 3);
128
                        fail("MultiLayer cannot accept adding at an index larger than the amount of layers already contained");
129
                } catch (IndexOutOfBoundsException ioobEx){
130
                        // this is right
131
                }
132

    
133
                try {
134
                        multiLayer.addLayer(sym1, multiLayer.getLayerCount());
135
                } catch (IndexOutOfBoundsException ioobEx) {
136
                        fail("MultiLayer should accept adding at index less or equal to the layer count");
137
                }
138

    
139
                assertTrue("layer returned does not correspond with the index provided", multiLayer.getLayer(1).getDescription().equals("sym2"));
140
                assertTrue("layer returned does not correspond with the index provided", multiLayer.getLayer(0).getDescription().equals("sym1"));
141
        }
142

    
143
        public void testLayerAdditionPositionConsistencyMultiLayer_LINE_Symbol() {
144
                MultiLayerLineSymbol multiLayer = new MultiLayerLineSymbol();
145
                ILineSymbol layer1 = new SimpleLineSymbol();
146
                ILineSymbol layer2 = new SimpleLineSymbol();
147
                layer1.setDescription("layer1");
148
                layer2.setDescription("layer2");
149

    
150
                try {
151
                        multiLayer.addLayer(layer1, 0);
152
                } catch (IndexOutOfBoundsException ioobEx) {
153
                        fail("MultiLayer should always accept adding at index 0, even when it is empty");
154
                }
155

    
156
                try {
157
                        multiLayer.addLayer(layer2, 3);
158
                        fail("MultiLayer cannot accept adding at an index larger than the amount of layers already contained");
159
                } catch (IndexOutOfBoundsException ioobEx){
160
                        // this is right
161
                }
162

    
163
                try {
164
                        multiLayer.addLayer(layer2, multiLayer.getLayerCount());
165
                } catch (IndexOutOfBoundsException ioobEx) {
166
                        fail("MultiLayer should accept adding at index less or equal to the layer count");
167
                }
168

    
169
                assertTrue("layer returned does not correspond with the index provided", multiLayer.getLayer(1).getDescription().equals("layer2"));
170
                assertTrue("layer returned does not correspond with the index provided", multiLayer.getLayer(0).getDescription().equals("layer1"));
171
        }
172

    
173
        public void testLayerAdditionPositionConsistencyMultiLayer_FILL_Symbol() {
174
                MultiLayerFillSymbol multiLayer = new MultiLayerFillSymbol();
175
                IFillSymbol layer1 = new SimpleFillSymbol();
176
                IFillSymbol layer2 = new SimpleFillSymbol();
177
                layer1.setDescription("layer1");
178
                layer2.setDescription("layer2");
179

    
180
                try {
181
                        multiLayer.addLayer(layer1, 0);
182
                } catch (IndexOutOfBoundsException ioobEx) {
183
                        fail("MultiLayer should always accept adding at index 0, even when it is empty");
184
                }
185

    
186
                try {
187
                        multiLayer.addLayer(layer2, 3);
188
                        fail("MultiLayer cannot accept adding at an index larger than the amount of layers already contained");
189
                } catch (IndexOutOfBoundsException ioobEx){
190
                        // this is right
191
                }
192

    
193
                try {
194
                        multiLayer.addLayer(layer2, multiLayer.getLayerCount());
195
                } catch (IndexOutOfBoundsException ioobEx) {
196
                        fail("MultiLayer should accept adding at index less or equal to the layer count");
197
                }
198

    
199
                assertTrue("layer returned does not correspond with the index provided", multiLayer.getLayer(1).getDescription().equals("layer2"));
200
                assertTrue("layer returned does not correspond with the index provided", multiLayer.getLayer(0).getDescription().equals("layer1"));
201
        }
202

    
203
        public void testMultiLayer_MARKER_SymbolLayerSwapping() {
204
                MultiLayerMarkerSymbol multiLayer = new MultiLayerMarkerSymbol();
205
                IMarkerSymbol layer1 = new SimpleMarkerSymbol();
206
                IMarkerSymbol layer2 = new SimpleMarkerSymbol();
207
                IMarkerSymbol layer3 = new SimpleMarkerSymbol();
208
                layer1.setDescription("layer1");
209
                layer2.setDescription("layer2");
210
                layer3.setDescription("layer3");
211

    
212
                multiLayer.addLayer(layer1);
213
                multiLayer.addLayer(layer2);
214
                multiLayer.addLayer(layer3);
215

    
216
                multiLayer.swapLayers(1,0);
217
                assertTrue("failed swapping layers 0 and 1",
218
                                   multiLayer.getLayer(0).getDescription().equals("layer2")
219
                                && multiLayer.getLayer(1).getDescription().equals("layer1"));
220

    
221
                multiLayer.swapLayers(1,2);
222
                assertTrue("failed swapping layers 0 and 1",
223
                                   multiLayer.getLayer(2).getDescription().equals("layer1")
224
                                && multiLayer.getLayer(1).getDescription().equals("layer3"));
225

    
226
                Random random = new Random(System.currentTimeMillis());
227
                int numTries = 50;
228
                for (int i = 0; i < numTries; i++) {
229
                        int n = random.nextInt(multiLayer.getLayerCount());
230
                        int m = random.nextInt(multiLayer.getLayerCount());
231

    
232
                        ISymbol aLayer = multiLayer.getLayer(n);
233
                        multiLayer.swapLayers(n, m);
234
                        ISymbol otherLayer = multiLayer.getLayer(m);
235
                        assertEquals("failed swapping layers "+n+" and "+m, aLayer, otherLayer);
236
                }
237

    
238
        }
239

    
240
        public void testMultiLayer_LINE_SymbolLayerSwapping() {
241
                MultiLayerLineSymbol multiLayer = new MultiLayerLineSymbol();
242
                ILineSymbol layer1 = new SimpleLineSymbol();
243
                ILineSymbol layer2 = new SimpleLineSymbol();
244
                ILineSymbol layer3 = new SimpleLineSymbol();
245
                layer1.setDescription("layer1");
246
                layer2.setDescription("layer2");
247
                layer3.setDescription("layer3");
248

    
249
                multiLayer.addLayer(layer1);
250
                multiLayer.addLayer(layer2);
251
                multiLayer.addLayer(layer3);
252

    
253
                multiLayer.swapLayers(1,0);
254
                assertTrue("failed swapping layers 0 and 1",
255
                                   multiLayer.getLayer(0).getDescription().equals("layer2")
256
                                && multiLayer.getLayer(1).getDescription().equals("layer1"));
257

    
258
                multiLayer.swapLayers(1,2);
259
                assertTrue("failed swapping layers 0 and 1",
260
                                   multiLayer.getLayer(2).getDescription().equals("layer1")
261
                                && multiLayer.getLayer(1).getDescription().equals("layer3"));
262

    
263
                Random random = new Random(System.currentTimeMillis());
264
                int numTries = 50;
265
                for (int i = 0; i < numTries; i++) {
266
                        int n = random.nextInt(multiLayer.getLayerCount());
267
                        int m = random.nextInt(multiLayer.getLayerCount());
268

    
269
                        ISymbol aLayer = multiLayer.getLayer(n);
270
                        multiLayer.swapLayers(n, m);
271
                        ISymbol otherLayer = multiLayer.getLayer(m);
272
                        assertEquals("failed swapping layers "+n+" and "+m, aLayer, otherLayer);
273
                }
274

    
275
        }
276

    
277
        public void testMultiLayer_FILL_SymbolLayerSwapping() {
278
                MultiLayerFillSymbol multiLayer = new MultiLayerFillSymbol();
279
                IFillSymbol layer1 = new SimpleFillSymbol();
280
                IFillSymbol layer2 = new SimpleFillSymbol();
281
                IFillSymbol layer3 = new SimpleFillSymbol();
282
                layer1.setDescription("layer1");
283
                layer2.setDescription("layer2");
284
                layer3.setDescription("layer3");
285

    
286
                multiLayer.addLayer(layer1);
287
                multiLayer.addLayer(layer2);
288
                multiLayer.addLayer(layer3);
289

    
290
                multiLayer.swapLayers(1,0);
291
                assertTrue("failed swapping layers 0 and 1",
292
                                   multiLayer.getLayer(0).getDescription().equals("layer2")
293
                                && multiLayer.getLayer(1).getDescription().equals("layer1"));
294

    
295
                multiLayer.swapLayers(1,2);
296
                assertTrue("failed swapping layers 0 and 1",
297
                                   multiLayer.getLayer(2).getDescription().equals("layer1")
298
                                && multiLayer.getLayer(1).getDescription().equals("layer3"));
299

    
300
                Random random = new Random(System.currentTimeMillis());
301
                int numTries = 50;
302
                for (int i = 0; i < numTries; i++) {
303
                        int n = random.nextInt(multiLayer.getLayerCount());
304
                        int m = random.nextInt(multiLayer.getLayerCount());
305

    
306
                        ISymbol aLayer = multiLayer.getLayer(n);
307
                        multiLayer.swapLayers(n, m);
308
                        ISymbol otherLayer = multiLayer.getLayer(m);
309
                        assertEquals("failed swapping layers "+n+" and "+m, aLayer, otherLayer);
310
                }
311
        }
312

    
313
        public void testMultiLayer_MARKER_IndexOutOfBounds() {
314
                MultiLayerMarkerSymbol multiLayer = new MultiLayerMarkerSymbol();
315
                IMarkerSymbol layer1 = new SimpleMarkerSymbol();
316
                IMarkerSymbol layer2 = new SimpleMarkerSymbol();
317
                IMarkerSymbol layer3 = new SimpleMarkerSymbol();
318
                layer1.setDescription("layer1");
319
                layer2.setDescription("layer2");
320
                layer3.setDescription("layer3");
321

    
322
                multiLayer.addLayer(layer1);
323
                multiLayer.addLayer(layer2);
324
                multiLayer.addLayer(layer3);
325

    
326
                for (int i = -4; i < multiLayer.getLayerCount()+4; i++) {
327
                        if (i<0 || i>=multiLayer.getLayerCount()) {
328
                                try {
329
                                        multiLayer.getLayer(i);
330
                                        fail("MultiLayerMarkerSymbol should throw an IndexOutOfBoundsException when accessing a layer at index "+i+", because layer count is "+multiLayer.getLayerCount());
331
                                } catch (IndexOutOfBoundsException ioobEx) {
332
                                        // this is correct;
333
                                }
334
                        } else {
335
                                try {
336
                                        multiLayer.getLayer(i);
337
                                } catch (IndexOutOfBoundsException ioobEx) {
338
                                        fail("MultiLayerMarkerSymbol threw an IndexOutOfBoundsException, but it has "+multiLayer.getLayerCount()+" layers");
339
                                }
340
                        }
341
                }
342
        }
343

    
344
        public void testMultiLayer_LINE_IndexOutOfBounds() {
345
                MultiLayerLineSymbol multiLayer = new MultiLayerLineSymbol();
346
                ILineSymbol layer1 = new SimpleLineSymbol();
347
                ILineSymbol layer2 = new SimpleLineSymbol();
348
                ILineSymbol layer3 = new SimpleLineSymbol();
349
                layer1.setDescription("layer1");
350
                layer2.setDescription("layer2");
351
                layer3.setDescription("layer3");
352

    
353
                multiLayer.addLayer(layer1);
354
                multiLayer.addLayer(layer2);
355
                multiLayer.addLayer(layer3);
356

    
357
                for (int i = -4; i < multiLayer.getLayerCount()+4; i++) {
358
                        if (i<0 || i>=multiLayer.getLayerCount()) {
359
                                try {
360
                                        multiLayer.getLayer(i);
361
                                        fail("MultiLayerLineSymbol should throw an IndexOutOfBoundsException when accessing a layer at index "+i+", because layer count is "+multiLayer.getLayerCount());
362
                                } catch (IndexOutOfBoundsException ioobEx) {
363
                                        // this is correct;
364
                                }
365
                        } else {
366
                                try {
367
                                        multiLayer.getLayer(i);
368
                                } catch (IndexOutOfBoundsException ioobEx) {
369
                                        fail("MultiLayerLineSymbol threw an IndexOutOfBoundsException, but it has "+multiLayer.getLayerCount()+" layers");
370
                                }
371
                        }
372
                }
373
        }
374

    
375
        public void testMultiLayer_FILL_IndexOutOfBounds() {
376
                MultiLayerFillSymbol multiLayer = new MultiLayerFillSymbol();
377
                IFillSymbol layer1 = new SimpleFillSymbol();
378
                IFillSymbol layer2 = new SimpleFillSymbol();
379
                IFillSymbol layer3 = new SimpleFillSymbol();
380
                layer1.setDescription("layer1");
381
                layer2.setDescription("layer2");
382
                layer3.setDescription("layer3");
383

    
384
                multiLayer.addLayer(layer1);
385
                multiLayer.addLayer(layer2);
386
                multiLayer.addLayer(layer3);
387
                for (int i = -4; i < multiLayer.getLayerCount()+4; i++) {
388
                        if (i<0 || i>=multiLayer.getLayerCount()) {
389
                                try {
390
                                        multiLayer.getLayer(i);
391
                                        fail("MultiLayerFillSymbol should throw an IndexOutOfBoundsException when accessing a layer at index "+i+", because layer count is "+multiLayer.getLayerCount());
392
                                } catch (IndexOutOfBoundsException ioobEx) {
393
                                        // this is correct;
394
                                }
395
                        } else {
396
                                try {
397
                                        multiLayer.getLayer(i);
398
                                } catch (IndexOutOfBoundsException ioobEx) {
399
                                        fail("MultiLayerFillSymbol threw an IndexOutOfBoundsException, but it has "+multiLayer.getLayerCount()+" layers");
400
                                }
401
                        }
402
                }
403

    
404
        }
405
}