Revision 18626

View differences:

trunk/libraries/libFMap/src-test/com/iver/cit/gvsig/fmap/core/symbols/TestMultiLayerSymbol.java
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 com.iver.cit.gvsig.fmap.core.symbols;
42

  
43
import java.util.Random;
44

  
45
import junit.framework.TestCase;
46

  
47
/**
48
 * @author jaume dominguez faus - jaume.dominguez@iver.es
49
 */
50
public class TestMultiLayerSymbol extends TestCase {
51
	ISymbol[] symbols;
52

  
53
	public void setUp() {
54
		symbols = TestISymbol.getNewSymbolInstances();
55
	}
56

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

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

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

  
120
	public void testLayerAdditionPositionConsistencyMultiLayer_MARKER_Symbol() {
121
		MultiLayerMarkerSymbol multiLayer = new MultiLayerMarkerSymbol();
122
		try {
123
			multiLayer.addLayer(new SimpleMarkerSymbol(), 0);
124
		} catch (IndexOutOfBoundsException ioobEx) {
125
			fail("MultiLayer should always accept adding at index 0, even when it is empty");
126
		}
127

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

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

  
141
		assertTrue("layer returned does not correspond with the index provided", multiLayer.getLayer(1) instanceof CharacterMarkerSymbol);
142
		assertTrue("layer returned does not correspond with the index provided", multiLayer.getLayer(0) instanceof SimpleMarkerSymbol);
143
	}
144

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

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

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

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

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

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

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

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

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

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

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

  
214
		multiLayer.addLayer(layer1);
215
		multiLayer.addLayer(layer2);
216
		multiLayer.addLayer(layer3);
217

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

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

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

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

  
240
	}
241

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

  
251
		multiLayer.addLayer(layer1);
252
		multiLayer.addLayer(layer2);
253
		multiLayer.addLayer(layer3);
254

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

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

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

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

  
277
	}
278

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

  
288
		multiLayer.addLayer(layer1);
289
		multiLayer.addLayer(layer2);
290
		multiLayer.addLayer(layer3);
291

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

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

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

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

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

  
324
		multiLayer.addLayer(layer1);
325
		multiLayer.addLayer(layer2);
326
		multiLayer.addLayer(layer3);
327

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

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

  
355
		multiLayer.addLayer(layer1);
356
		multiLayer.addLayer(layer2);
357
		multiLayer.addLayer(layer3);
358

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

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

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

  
406
	}
407
}
trunk/libraries/libFMap/src-test/com/iver/cit/gvsig/fmap/core/symbols/TestISymbol.java
1
package com.iver.cit.gvsig.fmap.core.symbols;
2

  
3
import java.awt.Color;
4
import java.io.File;
5
import java.io.IOException;
6
import java.lang.reflect.Field;
7
import java.util.ArrayList;
8
import java.util.Random;
9

  
10
import junit.framework.TestCase;
11
import junit.framework.TestSuite;
12

  
13
import com.iver.cit.gvsig.fmap.core.FPoint2D;
14
import com.iver.cit.gvsig.fmap.core.FShape;
15
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
16
import com.iver.cit.gvsig.fmap.core.IGeometry;
17
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
18
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
19
/**
20
 * Integration test to ensure that the symbols follow the rules that follow the
21
 * managing of them by the application.
22
 *
23
 * @author jaume dominguez faus - jaume.dominguez@iver.es
24
 */
25

  
26

  
27
public class TestISymbol extends TestCase {
28
    private static ArrayList classesToTest;
29
    transient private ISymbol[] symbols;
30

  
31

  
32
    public static TestSuite suite() {
33
    	TestSuite suite = new TestSuite("Integration test for com.iver.cit.gvsig.fmap.core.ISymbol");
34
        suite.addTestSuite(TestISymbol.class);
35
        suite.addTestSuite(TestDrawMarkers.class);
36
        suite.addTestSuite(TestDrawLines.class);
37
        suite.addTestSuite(TestDrawFills.class);
38
        suite.addTestSuite(TestMultiLayerSymbol.class);
39
        return suite;
40
    }
41

  
42
    private static ArrayList getClassesToTest() {
43
        if (classesToTest == null) {
44
            classesToTest = new ArrayList();
45
            TestISymbol.addSymbolToTest(SimpleMarkerSymbol.class);
46
            TestISymbol.addSymbolToTest(CharacterMarkerSymbol.class);
47
            TestISymbol.addSymbolToTest(PictureMarkerSymbol.class);
48
            TestISymbol.addSymbolToTest(DotDensityFillSymbol.class);
49
            TestISymbol.addSymbolToTest(MarkerFillSymbol.class);
50
            TestISymbol.addSymbolToTest(SimpleFillSymbol.class);
51
            TestISymbol.addSymbolToTest(SimpleLineSymbol.class);
52
            TestISymbol.addSymbolToTest(SimpleTextSymbol.class);
53
            TestISymbol.addSymbolToTest(SmartTextSymbol.class);
54
            TestISymbol.addSymbolToTest(SimpleMarkerSymbol.class);
55
            TestISymbol.addSymbolToTest(PictureLineSymbol.class);
56
            TestISymbol.addSymbolToTest(SimpleTextSymbol.class);
57
            TestISymbol.addSymbolToTest(SmartTextSymbol.class);
58
            TestISymbol.addSymbolToTest(LineFillSymbol.class);
59
            TestISymbol.addSymbolToTest(PictureFillSymbol.class);
60
            TestISymbol.addSymbolToTest(MarkerLineSymbol.class);
61
            TestISymbol.addSymbolToTest(ArrowMarkerSymbol.class);
62
        }
63

  
64
        return classesToTest;
65
    }
66

  
67
    public static void addSymbolToTest(Class symbolClass) {
68
        try {
69
            ISymbol sym = (ISymbol) symbolClass.newInstance();
70
            sym.getXMLEntity();
71
        } catch (InstantiationException e) {
72
            // TODO Auto-generated catch block
73
            fail("Instantiating class, cannot test a non-instantiable symbol");
74
        } catch (IllegalAccessException e) {
75
            // TODO Auto-generated catch block
76
            fail("Class not instantiable");
77
        } catch (ClassCastException ccEx) {
78
            fail("Cannot test a non symbol class");
79
        }
80
        getClassesToTest().add(symbolClass);
81
    }
82

  
83
    public static ISymbol[] getNewSymbolInstances() {
84
        ISymbol[] symbols = new ISymbol[getClassesToTest().size()];
85
        for (int i = 0; i < symbols.length; i++) {
86

  
87
            try {
88
                symbols[i] = (ISymbol) ((Class) getClassesToTest().get(i)).newInstance();
89
                if (symbols[i] instanceof PictureMarkerSymbol) {
90
                    PictureMarkerSymbol pms = (PictureMarkerSymbol) symbols[i];
91
                    try {
92
                        pms.setImage(new File(TestISymbol.class.getResource("picture-marker.png").getFile()));
93
                        pms.setSelImage(new File(TestISymbol.class.getResource("picture-marker-selected.png").getFile()));
94
                    } catch (IOException e) {
95
                        fail("setting up a PictureMarkerSymbol");
96
                    }
97
                }
98

  
99
                if (symbols[i] instanceof PictureLineSymbol) {
100
                	PictureLineSymbol pls = (PictureLineSymbol) symbols[i];
101
                    try {
102
                        pls.setImage(new File(TestISymbol.class.getResource("picture-line.png").getFile()));
103
                        pls.setSelImage(new File(TestISymbol.class.getResource("picture-line-selected.png").getFile()));
104
                    } catch (IOException e) {
105
                        fail("setting up a PictureLineSymbol");
106
                    }
107
                }
108
                
109
                if (symbols[i] instanceof PictureFillSymbol) {
110
                	PictureFillSymbol pls = (PictureFillSymbol) symbols[i];
111
                    try {
112
                        pls.setImage(new File(TestISymbol.class.getResource("picture-line.png").getFile()));
113
                       
114
                    } catch (IOException e) {
115
                        fail("setting up a PictureLineSymbol");
116
                    }
117
                }
118
            } catch (InstantiationException e) {
119
                fail("Instantiating class");
120
            } catch (IllegalAccessException e) {
121
                fail("Class not instantiable");
122
            }
123

  
124
        }
125
        return symbols;
126
    }
127

  
128
    protected void setUp() throws Exception {
129
        symbols = getNewSymbolInstances();
130
    }
131

  
132
    public void testPointSuitability() {
133
        final IGeometry dummyPointGeom = ShapeFactory.createPoint2D(new FPoint2D());
134
        for (int i = 0; i < symbols.length; i++) {
135
            // text symbols are suitable for everything
136
            if (symbols[i] instanceof ITextSymbol) {
137
                assertTrue(symbols[i].getClassName()+" should be suitable for "+dummyPointGeom,
138
                        symbols[i].isSuitableFor(dummyPointGeom));
139
            } else
140

  
141
            // marker symbols are suitable for points
142
            if (symbols[i] instanceof IMarkerSymbol) {
143
                assertTrue(symbols[i].getClassName()+" should be suitable for "+dummyPointGeom,
144
                        symbols[i].isSuitableFor(dummyPointGeom));
145
            } else {
146
                assertFalse(symbols[i].getClassName()+" should NOT be suitable for "+dummyPointGeom,
147
                        symbols[i].isSuitableFor(dummyPointGeom));
148
            }
149
        }
150
    }
151

  
152
    public void testLineSuitability() {
153
        final IGeometry dummyLineGeom = ShapeFactory.createPolyline2D(new GeneralPathX());
154
        for (int i = 0; i < symbols.length; i++) {
155
            // text symbols are suitable for everything
156
            if (symbols[i] instanceof ITextSymbol) {
157
                assertTrue(symbols[i].getClassName()+" should be suitable for "+dummyLineGeom,
158
                        symbols[i].isSuitableFor(dummyLineGeom));
159
            } else
160

  
161
            // line symbols are suitable for line
162
            if (symbols[i] instanceof ILineSymbol) {
163
                assertTrue(symbols[i].getClassName()+" should be suitable for "+dummyLineGeom,
164
                        symbols[i].isSuitableFor(dummyLineGeom));
165
            } else {
166
                assertFalse(symbols[i].getClassName()+" should NOT be suitable for "+dummyLineGeom,
167
                        symbols[i].isSuitableFor(dummyLineGeom));
168
            }
169
        }
170
    }
171

  
172
    public void testPolygonSuitability() {
173
        final IGeometry dummyPolygonGeom = ShapeFactory.createPolygon2D(new GeneralPathX());
174
        for (int i = 0; i < symbols.length; i++) {
175

  
176
            // text symbols are suitable for everything
177
            if (symbols[i] instanceof ITextSymbol) {
178
                assertTrue(symbols[i].getClassName()+" should be suitable for "+dummyPolygonGeom,
179
                        symbols[i].isSuitableFor(dummyPolygonGeom));
180
            } else
181

  
182
            // fill symbols are suitable for polygons
183
            if (symbols[i] instanceof IFillSymbol) {
184
                assertTrue(symbols[i].getClassName()+" should be suitable for "+dummyPolygonGeom,
185
                        symbols[i].isSuitableFor(dummyPolygonGeom));
186
            } else {
187
                assertFalse(symbols[i].getClassName()+" should NOT be suitable for "+dummyPolygonGeom,
188
                        symbols[i].isSuitableFor(dummyPolygonGeom));
189
            }
190
        }
191
    }
192

  
193
    /**
194
     * tests whether the symbols were correctly configured to work with
195
     * different kinds of shapes.
196
     */
197
    public void testGeneralSuitability() {
198

  
199
        for (int i = 0; i < symbols.length; i++) {
200
            // text symbols are suitable for everything
201
            if (symbols[i] instanceof ITextSymbol) {
202
                assertTrue(symbols[i].getClassName()+" should be suitable for "+null,
203
                        symbols[i].isSuitableFor(null));
204
            } else {
205
                try {
206
                    symbols[i].isSuitableFor(null);
207
                    fail("Exception was not thrown");
208

  
209
                } catch (NullPointerException npEx) {
210
                    // this is correct!
211
                }
212
            }
213
        }
214
    }
215

  
216
    /**
217
     * ensures that symbols defined which can of FShape is the symbol
218
     */
219
    public void testSymbolTypeDefinition() {
220
        for (int i = 0; i < symbols.length; i++) {
221
            assertFalse("Symbol no. "+i+" '"+symbols[i].getClassName()+"'",
222
                        symbols[i].getSymbolType() == 0);
223

  
224
        }
225
    }
226

  
227
    /**
228
     * ensures that any symbol that is suitable for markers declares its type
229
     * as FShape.POINT or FShape.MULTI
230
     *
231
     */
232
    public void testMarkerSymbolTypeDefinition() {
233
        for (int i = 0; i < symbols.length; i++) {
234
            if (symbols[i] instanceof IMarkerSymbol) {
235
                assertTrue("Symbol no. "+i+" '"+symbols[i].getClassName()+"'",
236
                        symbols[i].getSymbolType() == FShape.POINT
237
                        || symbols[i].getSymbolType() == FShape.MULTI);
238
            }
239
        }
240
    }
241

  
242
    /**
243
     * ensures that any symbol that is suitable for lines declares its type
244
     * as FShape.LINE or FShape.MULTI
245
     *
246
     */
247
    public void testLineSymbolTypeDefinition() {
248
        for (int i = 0; i < symbols.length; i++) {
249
            if (symbols[i] instanceof ILineSymbol) {
250
                assertTrue("Symbol no. "+i+" '"+symbols[i].getClassName()+"'",
251
                        symbols[i].getSymbolType() == FShape.LINE
252
                        || symbols[i].getSymbolType() == FShape.MULTI);
253
            }
254
        }
255
    }
256

  
257
    /**
258
     * ensures that any symbol that is suitable for fills declares its type
259
     * as POLYGON or FShape.MULTI
260
     *
261
     */
262
    public void testFillSymbolTypeDefinition() {
263
        for (int i = 0; i < symbols.length; i++) {
264
            if (symbols[i] instanceof IFillSymbol) {
265
                assertTrue("Symbol no. "+i+" '"+symbols[i].getClassName()+"'",
266
                        symbols[i].getSymbolType() == FShape.POLYGON
267
                        || symbols[i].getSymbolType() == FShape.MULTI);
268
            }
269
        }
270
    }
271

  
272
    /**
273
     * ensures that any symbol has a description in its persistence
274
     * (very important)
275
     */
276
    public void testDescription() {
277
        for (int i = 0; i < symbols.length; i++) {
278
            assertTrue(symbols[i].getClassName() + " does not declare a description in its XMLEntity",
279
                    symbols[i].getXMLEntity().contains("desc"));
280
        }
281
    }
282

  
283
    /**
284
     * ensures that any symbol has an isShapeVisible field in its persistence
285
     * (very important)
286
     */
287
    public void testIsShapeVisible() {
288
        for (int i = 0; i < symbols.length; i++) {
289
            assertTrue(symbols[i].getClassName() + " does not declare the isShapeVisible field in its XMLEntity",
290
                    symbols[i].getXMLEntity().contains("isShapeVisible"));
291
        }
292
    }
293

  
294
    /**
295
     * ensures that the symbol is self-defining
296
     */
297
    public void testSymbolSelfDefinition() {
298
        for (int i = 0; i < symbols.length; i++) {
299
            final ISymbol theSymbol = symbols[i];
300
            final ISymbol cloneSymbol = SymbologyFactory.createSymbolFromXML(theSymbol.getXMLEntity(), null);
301
            assertTrue(theSymbol.getClassName()+ " wrong class name declaration in getXMLEntity() ",
302
                    cloneSymbol.getClass().equals(theSymbol.getClass()));
303
            final Field[] theSymbolFields = theSymbol.getClass().getFields();
304
            for (int j = 0; j < theSymbolFields.length; j++) {
305
                final Field fi = theSymbolFields[j];
306
                final boolean wasAccessible = fi.isAccessible();
307
                fi.setAccessible(true);
308

  
309
                try {
310
                    assertTrue(theSymbol.getClassName() + " fails or misses clonning the field " +fi.getName(),
311
                            fi.get(theSymbol).equals(fi.get(cloneSymbol)));
312
                } catch (IllegalArgumentException e) {
313
                    fail();
314
                } catch (IllegalAccessException e) {
315
                    fail();
316
                }
317
                fi.setAccessible(wasAccessible);
318
            }
319
        }
320
    }
321

  
322
    /**
323
     * Check one pixel acceleration consistency. Checks that the color returned
324
     * in RGB matches the color of the symbol set.
325
     *
326
     */
327
    public void testOnePointRGB() {
328
    	Random random = new Random(System.currentTimeMillis());
329

  
330
        for (int i = 0; i < symbols.length; i++) {
331
            if (symbols[i] instanceof IMarkerSymbol) {
332
                IMarkerSymbol marker = (IMarkerSymbol) symbols[i];
333
                marker.setColor(new Color(random.nextInt()>>8));
334
                assertTrue("Symbol no. "+i+" '"+symbols[i].getClassName()+"' RGB value mismatch for getOnePointRBG() and getColor().getRGB()",
335
                        symbols[i].getOnePointRgb() == marker.getColor().getRGB());
336

  
337
            }
338

  
339
            if (symbols[i] instanceof ILineSymbol) {
340
                ILineSymbol line = (ILineSymbol) symbols[i];
341
                line.setLineColor(new Color(random.nextInt()>>8));
342
                assertTrue("Symbol no. "+i+" '"+symbols[i].getClassName()+"' RGB value mismatch for getOnePointRBG() and getColor().getRGB()",
343
                        symbols[i].getOnePointRgb() == line.getColor().getRGB());
344
            }
345

  
346
            if (symbols[i] instanceof IFillSymbol) {
347
                IFillSymbol fill = (IFillSymbol) symbols[i];
348
                boolean outlined = fill.getOutline() != null;
349
                if (!outlined)
350
                	fill.setFillColor(new Color(random.nextInt()>>8));
351
                else
352
                	fill.getOutline().setLineColor(new Color(random.nextInt()>>8));
353
                assertTrue("Symbol no. "+i+" '"+symbols[i].getClassName()+"' RGB value mismatch for getOnePointRBG() and getColor().getRGB()",
354
                        symbols[i].getOnePointRgb() == ((outlined) ? fill.getOutline().getColor().getRGB() : fill.getFillColor().getRGB()));
355
            }
356

  
357
            if (symbols[i] instanceof ITextSymbol) {
358
            	ITextSymbol text = (ITextSymbol) symbols[i];
359
            	text.setTextColor(new Color(random.nextInt()>>8));
360
                assertTrue("Symbol no. "+i+" '"+symbols[i].getClassName()+"' RGB value mismatch for getOnePointRBG() and getColor().getRGB()",
361
                        symbols[i].getOnePointRgb() == text.getTextColor().getRGB());
362
            }
363

  
364
        }
365
    }
366

  
367
    /**
368
     * ensures that any symbol provides a version of itself to use when the
369
     * feature is selected
370
     *
371
     */
372
    public void testSymbolForSelection() {
373
    	for (int i = 0; i < symbols.length; i++) {
374
			assertNotNull("Symbol no. "+i+" '"+symbols[i].getClassName()+" does not define any derived symbol for selection", symbols[i].getSymbolForSelection());
375
		}
376
    }
377
}
trunk/libraries/libFMap/src-test/com/iver/cit/gvsig/fmap/core/symbols/TestDrawMarkers.java
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 com.iver.cit.gvsig.fmap.core.symbols;
42

  
43
import java.awt.Color;
44
import java.awt.Dimension;
45
import java.awt.Graphics2D;
46
import java.awt.Rectangle;
47
import java.awt.geom.AffineTransform;
48
import java.awt.geom.Point2D;
49
import java.awt.image.BufferedImage;
50
import java.util.ArrayList;
51
import java.util.Random;
52

  
53
import junit.framework.TestCase;
54

  
55
import com.iver.cit.gvsig.fmap.core.FPoint2D;
56

  
57
/**
58
 * Integration test to test that Marker symbols always draw in the same
59
 * place respecting size constraints.
60
 *
61
 * @author jaume dominguez faus - jaume.dominguez@iver.es
62
 */
63
public class TestDrawMarkers extends TestCase {
64
	private IMarkerSymbol[] symbols;
65
	private final Dimension sz = new Dimension(400, 400);
66
	private final FPoint2D centerP = new FPoint2D(sz.width/2, sz.height/2);
67
	private static final int OUTTER_TOLERANCE = 1;
68
	private static final int INNER_TOLERANCE = 1;
69

  
70
	private static final double sizes[] = new double[] {
71
		200,
72
		100,
73
		50,
74
		30,
75
		16,
76
		5,
77
		3,
78
		2,
79
		// smaller sizes don't make any sense
80

  
81
	};
82

  
83

  
84

  
85
	protected void setUp() throws Exception {
86
		// get all the symbols in the Test Suite
87
		super.setUp();
88

  
89
		ISymbol[] allSymbols = TestISymbol.getNewSymbolInstances();
90
		// Filter the marker ones
91
		ArrayList symbols = new ArrayList();
92

  
93
		for (int i = 0; i < allSymbols.length; i++) {
94
			if (allSymbols[i] instanceof IMarkerSymbol) {
95
				IMarkerSymbol sym = (IMarkerSymbol) allSymbols[i];
96
				symbols.add(sym);
97

  
98
			}
99
		}
100
		this.symbols = (IMarkerSymbol[]) symbols.toArray(new IMarkerSymbol[symbols.size()]);
101
	}
102

  
103
	public void testDrawingSize() {
104
		for (int i = 0; i < symbols.length; i++) {
105
			for (int j = 0; j < sizes.length; j++) {
106
				// new blank buffered image
107
				BufferedImage bi = new BufferedImage(sz.width, sz.height, BufferedImage.TYPE_INT_ARGB);
108

  
109
				// the graphics for the image, so we can draw onto the buffered image
110
				Graphics2D g = bi.createGraphics();
111

  
112
				IMarkerSymbol testSymbol = symbols[i];
113
				testSymbol.setColor(Color.YELLOW);
114
				testSymbol.setSize(sizes[j]);
115
				String name = testSymbol.getClassName().substring(
116
						testSymbol.getClassName().lastIndexOf('.')+1,
117
						testSymbol.getClassName().length());
118

  
119
				testSymbol.draw(g, new AffineTransform(), centerP, null);
120

  
121
				Rectangle wrappingRect = new Rectangle(
122
						(int) (centerP.getX()-sizes[j]/2),
123
						(int) (centerP.getY()-sizes[j]/2),
124
						(int) (sizes[j]),
125
						(int) (sizes[j]));
126

  
127
				if (testSymbol instanceof CharacterMarkerSymbol)
128
					continue;
129

  
130
				assertFalse("fails sizing marker, too big ("+name+", "+sizes[j]+"px)", isOutsideRect(bi, wrappingRect, OUTTER_TOLERANCE ));
131
				assertTrue("fails sizing marker, too small ("+name+", "+sizes[j]+"px) \n" +
132
						"\t - forgot to enable ANTIALIASING?", fitsInsideRect(bi, wrappingRect, INNER_TOLERANCE));
133

  
134

  
135
			}
136
		}
137
	}
138

  
139
	public void testDrawingOffset() {
140
		Random random = new Random(System.currentTimeMillis());
141
		for (int i = 0; i < symbols.length; i++) {
142
			for (int j = 0; j < sizes.length; j++) {
143
				// new blank buffered image
144
				BufferedImage bi = new BufferedImage(sz.width, sz.height, BufferedImage.TYPE_INT_ARGB);
145

  
146
				// the graphics for the image, so we can draw onto the buffered image
147
				Graphics2D g = bi.createGraphics();
148

  
149
				IMarkerSymbol testSymbol = symbols[i];
150
				testSymbol.setColor(Color.YELLOW);
151
				testSymbol.setSize(sizes[j]);
152

  
153
				String name = testSymbol.getClassName().substring(
154
						testSymbol.getClassName().lastIndexOf('.')+1,
155
						testSymbol.getClassName().length());
156

  
157
				double xOffset = (random.nextDouble()-0.5)*sz.width;
158
				double yOffset = (random.nextDouble()-0.5)*sz.height;
159

  
160
				testSymbol.setOffset(new Point2D.Double(xOffset, yOffset));
161

  
162
				Rectangle wrappingRect = new Rectangle(
163
						(int) ((centerP.getX()-sizes[j]/2) + xOffset),
164
						(int) ((centerP.getY()-sizes[j]/2) + yOffset),
165
						(int) (sizes[j]),
166
						(int) (sizes[j]));
167

  
168
				if (testSymbol instanceof CharacterMarkerSymbol)
169
					continue;
170

  
171
				testSymbol.draw(g, new AffineTransform(), centerP, null);
172
				assertFalse("fails sizing marker, too big ("+name+", "+sizes[j]+"px)", isOutsideRect(bi, wrappingRect, OUTTER_TOLERANCE ));
173
				assertTrue("fails sizing marker, too small ("+name+", "+sizes[j]+"px) \n" +
174
						"\t - forgot to enable ANTIALIASING?", fitsInsideRect(bi, wrappingRect, INNER_TOLERANCE));
175

  
176
			}
177
		}
178
	}
179
	
180
	public void testDrawingRotation() {
181
		// TODO implement it
182
		
183
	}
184

  
185
	private boolean fitsInsideRect(BufferedImage bi, Rectangle wrappingRect, int tolerance) {
186
		int myTolerance = Math.min(tolerance, wrappingRect.width);
187
		Rectangle myRect;
188
		if (myTolerance >= INNER_TOLERANCE) {
189
			myRect = new Rectangle(
190
					wrappingRect.x+myTolerance,
191
					wrappingRect.y+myTolerance,
192
					wrappingRect.width-myTolerance-myTolerance,
193
					wrappingRect.height-myTolerance-myTolerance);
194
		} else {
195
			myRect = new Rectangle(-1, -1, 0, 0); // just ensure it does not disturb the check
196
		}
197

  
198
		for (int i = 0; i < bi.getWidth(); i++) {
199
			for (int j = 0; j < bi.getHeight(); j++) {
200
				if (wrappingRect.contains(i,j)
201
						&& !myRect.contains(i,j)) {
202

  
203
					if (bi.getRGB(i,j) != 0)
204
						return true;
205
				}
206
			}
207
		}
208
		return false;
209
	}
210

  
211
	private boolean isOutsideRect(BufferedImage bi, Rectangle wrappingRect, int tolerance) {
212
		Rectangle myRect = new Rectangle(
213
				wrappingRect.x-tolerance,
214
				wrappingRect.y-tolerance,
215
				wrappingRect.width+tolerance+tolerance,
216
				wrappingRect.height+tolerance+tolerance);
217
		for (int i = 0; i < bi.getWidth(); i++) {
218
			for (int j = 0; j < bi.getHeight(); j++) {
219
				if (!myRect.contains(i,j))
220
					continue;
221

  
222
				if (bi.getRGB(i,j) != 0) return false;
223
			}
224
		}
225
		return true;
226
	}
227
}
228

  
229
/// paste this piece of code to produce some png files to check visually what is happening
230
//g.setColor(Color.BLUE);
231
//Rectangle myRect = new Rectangle(
232
//		wrappingRect.x+INNER_TOLERANCE,
233
//		wrappingRect.y+INNER_TOLERANCE,
234
//		wrappingRect.width-INNER_TOLERANCE-INNER_TOLERANCE,
235
//		wrappingRect.height-INNER_TOLERANCE-INNER_TOLERANCE);
236
//
237
//g.draw(myRect);
238
//g.setColor(Color.RED);
239
//g.draw(wrappingRect);
240
//try {
241
//
242
//	File dstDir = new File (System.getProperty("java.io.tmpdir")+"/prova-imatges/");
243
//	if (!dstDir.exists()) dstDir.mkdir();
244
//	ImageIO.write(bi, "png",
245
//			new File(dstDir.getAbsoluteFile()+File.separator+
246
//					name+"_size_"+sizes[j]
247
//					                    +".png"));
248
//} catch (IOException e) {
249
//	e.printStackTrace();
250
//	fail();
251
//}
252
/// 
trunk/libraries/libFMap/src-test/com/iver/cit/gvsig/fmap/core/symbols/TestDrawFills.java
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 com.iver.cit.gvsig.fmap.core.symbols;
42

  
43
import java.util.ArrayList;
44

  
45
import junit.framework.TestCase;
46

  
47
import com.iver.cit.gvsig.fmap.AllTests;
48
import com.iver.cit.gvsig.fmap.MapContext;
49
/**
50
 * Integration test to test that Fill symbols always draw in the same
51
 * place respecting size constraints.
52
 *
53
 * @author jaume dominguez faus - jaume.dominguez@iver.es
54
 */
55
public class TestDrawFills extends TestCase {
56
	private IFillSymbol[] symbols;
57

  
58

  
59
	protected void setUp() throws Exception {
60
		// get all the symbols in the Test Suite
61
		super.setUp();
62

  
63
		ISymbol[] allSymbols = TestISymbol.getNewSymbolInstances();
64
		// Filter the marker ones
65
		ArrayList symbols = new ArrayList();
66

  
67
		for (int i = 0; i < allSymbols.length; i++) {
68
			if (allSymbols[i] instanceof IFillSymbol) {
69
				IFillSymbol sym = (IFillSymbol) allSymbols[i];
70
				symbols.add(sym);
71

  
72
			}
73
		}
74
		this.symbols = (IFillSymbol[]) symbols.toArray(new IFillSymbol[symbols.size()]);
75
	}
76

  
77
	public void testDraw() {
78
		MapContext mc = AllTests.newMapContext(AllTests.TEST_DEFAULT_MERCATOR_PROJECTION);
79

  
80
	}
81

  
82
	public void testDrawInsideRectangle() {
83
		// TODO Implement it
84

  
85
	}
86
}
trunk/libraries/libFMap/src-test/com/iver/cit/gvsig/fmap/core/symbols/TestDrawLines.java
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 com.iver.cit.gvsig.fmap.core.symbols;
42

  
43
import java.awt.Color;
44
import java.awt.Dimension;
45
import java.awt.Graphics2D;
46
import java.awt.geom.AffineTransform;
47
import java.awt.geom.Point2D;
48
import java.awt.image.BufferedImage;
49
import java.util.ArrayList;
50

  
51
import junit.framework.TestCase;
52

  
53
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
54
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
55

  
56
/**
57
 * Integration test to test that Line symbols always draw in the same
58
 * place respecting size constraints.
59
 *
60
 * @author jaume dominguez faus - jaume.dominguez@iver.es
61
 */
62
public class TestDrawLines extends TestCase {
63
	private ILineSymbol[] symbols;
64
	private final Dimension sz = new Dimension(401, 401);
65
	private final FPolyline2D centerL;
66

  
67
	{
68
		GeneralPathX gp = new GeneralPathX();
69
		gp.moveTo(0, sz.height / 2 );
70
		gp.lineTo(sz.width, sz.height /2 );
71

  
72
		centerL = new FPolyline2D(gp);
73
	}
74
	private static final double sizes[] = new double[] {
75
		300,
76
		250,
77
		225,
78
		200,
79
		100,
80
		50,
81
		30,
82
		15,
83
		5,
84
		3,
85
		2,
86
		1,
87
		// smaller sizes don't make any sense
88
	};
89

  
90
	private static final float INNER_TOLERANCE = 1F;
91
	private static final float OUTTER_TOLERANCE = 1F;
92

  
93
	protected void setUp() throws Exception {
94
		// get all the symbols in the Test Suite
95
		super.setUp();
96

  
97
		ISymbol[] allSymbols = TestISymbol.getNewSymbolInstances();
98
		// Filter the marker ones
99
		ArrayList symbols = new ArrayList();
100

  
101
		for (int i = 0; i < allSymbols.length; i++) {
102
			if (allSymbols[i] instanceof ILineSymbol) {
103
				ILineSymbol sym = (ILineSymbol) allSymbols[i];
104
				symbols.add(sym);
105

  
106
			}
107
		}
108
		this.symbols = (ILineSymbol[]) symbols.toArray(new ILineSymbol[symbols.size()]);
109
	}
110

  
111
	public void testDrawingSize() {
112
		for (int i = 0; i < symbols.length; i++) {
113
			for (int j = 0; j < sizes.length; j++) {
114
				// new blank buffered image
115
				BufferedImage bi = new BufferedImage(sz.width, sz.height, BufferedImage.TYPE_INT_ARGB);
116

  
117
				// the graphics for the image, so we can draw onto the buffered image
118
				Graphics2D g = bi.createGraphics();
119

  
120
				ILineSymbol testSymbol = symbols[i];
121
				testSymbol.setLineColor(Color.YELLOW);
122
				testSymbol.setLineWidth(sizes[j]);
123
				String name = testSymbol.getClassName().substring(
124
						testSymbol.getClassName().lastIndexOf('.')+1,
125
						testSymbol.getClassName().length());
126

  
127
				Point2D upperP1 = new Point2D.Double(0, centerL.getBounds().getY() - sizes[j]*0.5);
128
				Point2D upperP2 = new Point2D.Double(centerL.getBounds().getWidth(), centerL.getBounds().getY() - sizes[j]*0.5);
129
				GeneralPathX gpUp = new GeneralPathX();
130
				gpUp.moveTo(upperP1.getX(), upperP1.getY());
131
				gpUp.lineTo(upperP2.getX(), upperP2.getY());
132

  
133

  
134
				GeneralPathX gpDown = new GeneralPathX();
135
				Point2D lowerP1 = new Point2D.Double(0, centerL.getBounds().getY() + sizes[j]*0.5);
136
				Point2D lowerP2 = new Point2D.Double(centerL.getBounds().getWidth(), centerL.getBounds().getY() + sizes[j]*0.5);
137
				gpDown.moveTo(lowerP1.getX(), lowerP1.getY());
138
				gpDown.lineTo(lowerP2.getX(), lowerP2.getY());
139

  
140

  
141
				testSymbol.draw(g, new AffineTransform(), centerL, null);
142

  
143
				/// per a borrar
144
//				g.setStroke(new BasicStroke());
145
//				g.setColor(Color.RED);
146
//
147
//				g.draw(gpUp);
148
//
149
//				g.setColor(Color.BLUE);
150
//				g.draw(gpDown);
151
//
152
//				try {
153
//
154
//					File dstDir = new File (System.getProperty("java.io.tmpdir")+"/prova-imatges/");
155
//					if (!dstDir.exists()) dstDir.mkdir();
156
//				ImageIO.write(bi, "png",
157
//						new File(dstDir.getAbsoluteFile()+File.separator+
158
//									name+"_size_"+sizes[j]
159
//									                    +".png"));
160
//				} catch (IOException e) {
161
//					e.printStackTrace();
162
//					fail();
163
//				}
164
				/// fi per a borrar
165
				assertFalse("fails sizing line, too big ("+name+", "+sizes[j]+"px)", isOutsideRect(bi, upperP1, lowerP1, OUTTER_TOLERANCE ));
166
				assertTrue("fails sizing line, too small ("+name+", "+sizes[j]+"px) \n" +
167
						"\t - forgot to enable ANTIALIASING?", fitsInsideRect(bi, upperP1, lowerP1, INNER_TOLERANCE));
168

  
169
			}
170
		}
171
	}
172

  
173
	private boolean isOutsideRect(BufferedImage bi, Point2D upper, Point2D lower, float outterTolerance) {
174
		for (int i = 0; i < bi.getWidth(); i++) {
175
			for (int j = 0; j < bi.getHeight(); j++) {
176
				if (j<upper.getY()-outterTolerance && j>lower.getY()+outterTolerance)
177
					if (bi.getRGB(i,j) != 0) {
178
						System.out.println("too big In pixel ("+i+", "+j+")");
179
						return true;
180
					}
181

  
182
			}
183
		}
184
		return false;
185
	}
186

  
187
	private boolean fitsInsideRect(BufferedImage bi,Point2D upper, Point2D lower , float innerTolerance) {
188
		for (int i = 0; i < bi.getWidth(); i++) {
189
			for (int j = 0; j < bi.getHeight(); j++) {
190
				if (j<upper.getY()+innerTolerance && j>lower.getY()-innerTolerance)
191
					if (bi.getRGB(i,j) == 0) {
192
						System.out.println("does not fit big In pixel ("+i+", "+j+")");
193
						return false;
194
					}
195

  
196
			}
197
		}
198
		return true;
199
	}
200

  
201
}

Also available in: Unified diff