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 / TestISymbol.java @ 43510

History | View | Annotate | Download (14.8 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.awt.Color;
27
import java.lang.reflect.Field;
28
import java.util.ArrayList;
29
import java.util.Random;
30

    
31
import junit.framework.TestSuite;
32

    
33
import org.gvsig.fmap.geom.Geometry;
34
import org.gvsig.fmap.geom.GeometryLocator;
35
import org.gvsig.fmap.geom.GeometryManager;
36
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
37
import org.gvsig.fmap.geom.Geometry.TYPES;
38
import org.gvsig.fmap.geom.exception.CreateGeometryException;
39
import org.gvsig.fmap.geom.primitive.GeneralPathX;
40
import org.gvsig.fmap.geom.primitive.Line;
41
import org.gvsig.fmap.geom.primitive.Point;
42
import org.gvsig.fmap.geom.primitive.Surface;
43
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
44
import org.gvsig.fmap.mapcontext.rendering.symbols.ITextSymbol;
45
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol;
46
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
47
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
48
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
49

    
50
/**
51
 * Integration test to ensure that the symbols follow the rules that follow the
52
 * managing of them by the application.
53
 *
54
 * @author jaume dominguez faus - jaume.dominguez@iver.es
55
 */
56

    
57

    
58
public class TestISymbol extends AbstractLibraryAutoInitTestCase {
59
    private static ArrayList classesToTest;//<AbstractSymbolTestCase>
60
    transient private ISymbol[] symbols;
61

    
62
    public static TestSuite suite() {
63
            TestSuite suite = new TestSuite("Integration test for com.iver.cit.gvsig.fmap.core.ISymbol");
64
        suite.addTestSuite(TestISymbol.class);
65
        suite.addTestSuite(TestDrawMarkers.class);
66
        suite.addTestSuite(TestDrawLines.class);
67
        suite.addTestSuite(TestDrawFills.class);
68

    
69
        return suite;
70
    }
71

    
72
    protected void doSetUp() throws Exception {
73
            addSymbols();
74
            symbols = getNewSymbolInstances();
75
    }
76
    
77
    public static void addSymbols() {            
78
            addSymbolTest(new SimpleFillSymbolTest());
79
            addSymbolTest(new SimpleLineSymbolTest());
80
            addSymbolTest(new SimpleMarkerSymbolTest());
81
            addSymbolTest(new SimpleTextSymbolTest());
82
    }
83
    
84

    
85
    public static void addSymbolTest(AbstractSymbolTestCase symbolTestClass) {
86
            if (classesToTest == null) classesToTest = new ArrayList(); //<AbstractSymbolTestCase>
87

    
88
            classesToTest.add(symbolTestClass);
89
    }
90

    
91
    public static ISymbol[] getNewSymbolInstances() {
92
        ISymbol[] symbols = new ISymbol[classesToTest.size()];
93
        for (int i = 0; i < symbols.length; i++) {
94
                symbols[i] = (ISymbol)((AbstractSymbolTestCase)classesToTest.get(i)).newInstance();
95
        }
96
        return symbols;
97
    }
98

    
99
    public void testPointSuitability() throws InstantiationException, IllegalAccessException, CreateGeometryException {
100
            GeometryManager geomManager = GeometryLocator.getGeometryManager();
101
            Point point = (Point)geomManager.create(TYPES.POINT, SUBTYPES.GEOM2D);
102
            final Geometry dummyPointGeom = point;
103
        for (int i = 0; i < symbols.length; i++) {
104
            // text symbols are suitable for everything
105
            if (symbols[i] instanceof ITextSymbol) {
106
                assertTrue(symbols[i].getClass().getName()+" should be suitable for "+dummyPointGeom,
107
                        symbols[i].isSuitableFor(dummyPointGeom));
108
            } else
109

    
110
            // marker symbols are suitable for points
111
            if (symbols[i] instanceof IMarkerSymbol) {
112
                assertTrue(symbols[i].getClass().getName()+" should be suitable for "+dummyPointGeom,
113
                        symbols[i].isSuitableFor(dummyPointGeom));
114
            } else {
115
                assertFalse(symbols[i].getClass().getName()+" should NOT be suitable for "+dummyPointGeom,
116
                        symbols[i].isSuitableFor(dummyPointGeom));
117
            }
118
        }
119
    }
120

    
121
    public void testLineSuitability() throws InstantiationException, IllegalAccessException, CreateGeometryException {
122
            GeometryManager geomManager = GeometryLocator.getGeometryManager();
123
            Line line = geomManager.createLine(SUBTYPES.GEOM2D);
124
                line.setGeneralPath(new GeneralPathX());
125
            final Geometry dummyLineGeom = line;
126
        for (int i = 0; i < symbols.length; i++) {
127
            // text symbols are suitable for everything
128
            if (symbols[i] instanceof ITextSymbol) {
129
                assertTrue(symbols[i].getClass().getName()+" should be suitable for "+dummyLineGeom,
130
                        symbols[i].isSuitableFor(dummyLineGeom));
131
            } else
132

    
133
            // line symbols are suitable for line
134
            if (symbols[i] instanceof ILineSymbol) {
135
                assertTrue(symbols[i].getClass().getName()+" should be suitable for "+dummyLineGeom,
136
                        symbols[i].isSuitableFor(dummyLineGeom));
137
            } else {
138
                assertFalse(symbols[i].getClass().getName()+" should NOT be suitable for "+dummyLineGeom,
139
                        symbols[i].isSuitableFor(dummyLineGeom));
140
            }
141
        }
142
    }
143

    
144
    public void testPolygonSuitability() throws InstantiationException, IllegalAccessException, CreateGeometryException {
145
            GeometryManager geomManager = GeometryLocator.getGeometryManager();
146
            Surface surface = (Surface)geomManager.create(TYPES.SURFACE, SUBTYPES.GEOM2D);
147
            surface.setGeneralPath(new GeneralPathX());
148
        final Geometry dummyPolygonGeom = surface;
149
        for (int i = 0; i < symbols.length; i++) {
150

    
151
            // text symbols are suitable for everything
152
            if (symbols[i] instanceof ITextSymbol) {
153
                assertTrue(symbols[i].getClass().getName()+" should be suitable for "+dummyPolygonGeom,
154
                        symbols[i].isSuitableFor(dummyPolygonGeom));
155
            } else
156

    
157
            // fill symbols are suitable for polygons
158
            if (symbols[i] instanceof IFillSymbol) {
159
                assertTrue(symbols[i].getClass().getName()+" should be suitable for "+dummyPolygonGeom,
160
                        symbols[i].isSuitableFor(dummyPolygonGeom));
161
            } else {
162
                assertFalse(symbols[i].getClass().getName()+" should NOT be suitable for "+dummyPolygonGeom,
163
                        symbols[i].isSuitableFor(dummyPolygonGeom));
164
            }
165
        }
166
    }
167

    
168
    /**
169
     * tests whether the symbols were correctly configured to work with
170
     * different kinds of shapes.
171
     */
172
    public void testGeneralSuitability() {
173

    
174
        for (int i = 0; i < symbols.length; i++) {
175
            // text symbols are suitable for everything
176
            if (symbols[i] instanceof ITextSymbol) {
177
                assertTrue(symbols[i].getClass().getName()+" should be suitable for "+null,
178
                        symbols[i].isSuitableFor(null));
179
            } else {
180
                try {
181
                    symbols[i].isSuitableFor(null);
182
                    fail("Exception was not thrown");
183

    
184
                } catch (NullPointerException npEx) {
185
                    // this is correct!
186
                }
187
            }
188
        }
189
    }
190

    
191
    /**
192
     * ensures that symbols defined which can of FShape is the symbol
193
     */
194
    public void testSymbolTypeDefinition() {
195
        for (int i = 0; i < symbols.length; i++) {
196
            assertFalse("Symbol no. "+i+" '"+symbols[i].getClass().getName()+"'",
197
                        symbols[i].getSymbolType() == 0);
198

    
199
        }
200
    }
201

    
202
    /**
203
     * ensures that any symbol that is suitable for markers declares its type
204
     * as FShape.POINT or FShape.MULTI
205
     *
206
     */
207
    public void testMarkerSymbolTypeDefinition() {
208
        for (int i = 0; i < symbols.length; i++) {
209
            if (symbols[i] instanceof IMarkerSymbol) {
210
                assertTrue("Symbol no. "+i+" '"+symbols[i].getClass().getName()+"'",
211
                        symbols[i].getSymbolType() == Geometry.TYPES.POINT
212
                        || symbols[i].getSymbolType() == Geometry.TYPES.GEOMETRY);
213
            }
214
        }
215
    }
216

    
217
    /**
218
     * ensures that any symbol that is suitable for lines declares its type
219
     * as FShape.LINE or FShape.MULTI
220
     *
221
     */
222
    public void testLineSymbolTypeDefinition() {
223
        for (int i = 0; i < symbols.length; i++) {
224
            if (symbols[i] instanceof ILineSymbol) {
225
                assertTrue("Symbol no. "+i+" '"+symbols[i].getClass().getName()+"'",
226
                        symbols[i].getSymbolType() == Geometry.TYPES.CURVE
227
                        || symbols[i].getSymbolType() == Geometry.TYPES.GEOMETRY);
228
            }
229
        }
230
    }
231

    
232
    /**
233
     * ensures that any symbol that is suitable for fills declares its type
234
     * as POLYGON or Geometry.TYPES.GEOMETRY
235
     *
236
     */
237
    public void testFillSymbolTypeDefinition() {
238
        for (int i = 0; i < symbols.length; i++) {
239
            if (symbols[i] instanceof IFillSymbol) {
240
                assertTrue("Symbol no. "+i+" '"+symbols[i].getClass().getName()+"'",
241
                        symbols[i].getSymbolType() == Geometry.TYPES.SURFACE
242
                        || symbols[i].getSymbolType() == Geometry.TYPES.GEOMETRY);
243
            }
244
        }
245
    }
246

    
247
//    /**
248
//     * ensures that any symbol has a description in its persistence
249
//     * (very important)
250
//     */
251
//    public void testDescription() {
252
//        for (int i = 0; i < symbols.length; i++) {
253
//            try {
254
//                                assertTrue(symbols[i].getClass().getName() + " does not declare a description in its XMLEntity",
255
//                                        symbols[i].getXMLEntity().contains("desc"));
256
//                        } catch (XMLException e) {
257
//                                // TODO Auto-generated catch block
258
//                                e.printStackTrace();
259
//                        }
260
//        }
261
//    }
262

    
263
//    /**
264
//     * ensures that any symbol has an isShapeVisible field in its persistence
265
//     * (very important)
266
//     */
267
//    public void testIsShapeVisible() {
268
//        for (int i = 0; i < symbols.length; i++) {
269
//            try {
270
//                                assertTrue(symbols[i].getClass().getName() + " does not declare the isShapeVisible field in its XMLEntity",
271
//                                        symbols[i].getXMLEntity().contains("isShapeVisible"));
272
//                        } catch (XMLException e) {
273
//                                // TODO Auto-generated catch block
274
//                                e.printStackTrace();
275
//                        }
276
//        }
277
//    }
278

    
279
    /**
280
     * ensures that the symbol is self-defining
281
     * @throws Exception 
282
     */
283
    public void testSymbolSelfDefinition() throws Exception {
284
        for (int i = 0; i < symbols.length; i++) {
285
            final ISymbol theSymbol = symbols[i];
286
            ISymbol cloneSymbol= (ISymbol) theSymbol.clone();
287
                                //cloneSymbol = SymbologyFactory.createSymbolFromXML(theSymbol.getXMLEntity(), null);
288
            
289
            assertTrue(theSymbol.getClass().getName()+ " wrong class name declaration in getXMLEntity() ",
290
                    cloneSymbol.getClass().equals(theSymbol.getClass()));
291
            final Field[] theSymbolFields = theSymbol.getClass().getFields();
292
            for (int j = 0; j < theSymbolFields.length; j++) {
293
                final Field fi = theSymbolFields[j];
294
                final boolean wasAccessible = fi.isAccessible();
295
                fi.setAccessible(true);
296

    
297
                try {
298
                    assertTrue(theSymbol.getClass().getName() + " fails or misses clonning the field " +fi.getName(),
299
                            fi.get(theSymbol).equals(fi.get(cloneSymbol)));
300
                } catch (IllegalArgumentException e) {
301
                    fail();
302
                } catch (IllegalAccessException e) {
303
                    fail();
304
                }
305
                fi.setAccessible(wasAccessible);
306
            }
307
        }
308
    }
309

    
310
    /**
311
     * Check one pixel acceleration consistency. Checks that the color returned
312
     * in RGB matches the color of the symbol set.
313
     *
314
     */
315
    public void testOnePointRGB() {
316
            Random random = new Random(System.currentTimeMillis());
317

    
318
        for (int i = 0; i < symbols.length; i++) {
319
            if (symbols[i] instanceof IMarkerSymbol) {
320
                IMarkerSymbol marker = (IMarkerSymbol) symbols[i];
321
                marker.setColor(new Color(random.nextInt()>>8));
322
                assertTrue("Symbol no. "+i+" '"+symbols[i].getClass().getName()+"' RGB value mismatch for getOnePointRBG() and getColor().getRGB()",
323
                        symbols[i].getOnePointRgb() == marker.getColor().getRGB());
324

    
325
            }
326

    
327
            if (symbols[i] instanceof ILineSymbol) {
328
                ILineSymbol line = (ILineSymbol) symbols[i];
329
                line.setLineColor(new Color(random.nextInt()>>8));
330
                assertTrue("Symbol no. "+i+" '"+symbols[i].getClass().getName()+"' RGB value mismatch for getOnePointRBG() and getColor().getRGB()",
331
                        symbols[i].getOnePointRgb() == line.getColor().getRGB());
332
            }
333

    
334
            if (symbols[i] instanceof IFillSymbol) {
335
                IFillSymbol fill = (IFillSymbol) symbols[i];
336
                boolean outlined = fill.getOutline() != null;
337
                if (!outlined)
338
                        fill.setFillColor(new Color(random.nextInt()>>8));
339
                else
340
                        fill.getOutline().setLineColor(new Color(random.nextInt()>>8));
341
                assertTrue("Symbol no. "+i+" '"+symbols[i].getClass().getName()+"' RGB value mismatch for getOnePointRBG() and getColor().getRGB()",
342
                        symbols[i].getOnePointRgb() == ((outlined) ? fill.getOutline().getColor().getRGB() : fill.getFillColor().getRGB()));
343
            }
344

    
345
            if (symbols[i] instanceof ITextSymbol) {
346
                    ITextSymbol text = (ITextSymbol) symbols[i];
347
                    text.setTextColor(new Color(random.nextInt()>>8));
348
                assertTrue("Symbol no. "+i+" '"+symbols[i].getClass().getName()+"' RGB value mismatch for getOnePointRBG() and getColor().getRGB()",
349
                        symbols[i].getOnePointRgb() == text.getTextColor().getRGB());
350
            }
351

    
352
        }
353
    }
354

    
355
    /**
356
     * ensures that any symbol provides a version of itself to use when the
357
     * feature is selected
358
     *
359
     */
360
    public void testSymbolForSelection() {
361
            for (int i = 0; i < symbols.length; i++) {
362
                        assertNotNull("Symbol no. "+i+" '"+symbols[i].getClass().getName()+" does not define any derived symbol for selection", symbols[i].getSymbolForSelection());
363
                }
364
    }
365
}