Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src-test / org / gvsig / fmap / mapcontext / rendering / symbol / TestISymbol.java @ 27042

History | View | Annotate | Download (13.7 KB)

1
package org.gvsig.fmap.mapcontext.rendering.symbol;
2

    
3
import java.awt.Color;
4
import java.lang.reflect.Field;
5
import java.util.ArrayList;
6
import java.util.Random;
7

    
8
import junit.framework.TestCase;
9
import junit.framework.TestSuite;
10

    
11
import org.gvsig.fmap.geom.Geometry;
12
import org.gvsig.fmap.geom.GeometryLocator;
13
import org.gvsig.fmap.geom.GeometryManager;
14
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
15
import org.gvsig.fmap.geom.Geometry.TYPES;
16
import org.gvsig.fmap.geom.impl.DefaultGeometryLibrary;
17
import org.gvsig.fmap.geom.primitive.Curve;
18
import org.gvsig.fmap.geom.primitive.GeneralPathX;
19
import org.gvsig.fmap.geom.primitive.Point;
20
import org.gvsig.fmap.geom.primitive.Surface;
21
import org.gvsig.fmap.mapcontext.rendering.symbols.IFillSymbol;
22
import org.gvsig.fmap.mapcontext.rendering.symbols.ILineSymbol;
23
import org.gvsig.fmap.mapcontext.rendering.symbols.IMarkerSymbol;
24
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
25
import org.gvsig.fmap.mapcontext.rendering.symbols.ITextSymbol;
26
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbologyFactory;
27

    
28
import com.iver.utiles.XMLException;
29
/**
30
 * Integration test to ensure that the symbols follow the rules that follow the
31
 * managing of them by the application.
32
 *
33
 * @author jaume dominguez faus - jaume.dominguez@iver.es
34
 */
35

    
36

    
37
public class TestISymbol extends TestCase {
38
    private static ArrayList classesToTest;//<AbstractSymbolTestCase>
39
    transient private ISymbol[] symbols;
40

    
41

    
42
    public static TestSuite suite() {
43
            TestSuite suite = new TestSuite("Integration test for com.iver.cit.gvsig.fmap.core.ISymbol");
44
        suite.addTestSuite(TestISymbol.class);
45
        suite.addTestSuite(TestDrawMarkers.class);
46
        suite.addTestSuite(TestDrawLines.class);
47
        suite.addTestSuite(TestDrawFills.class);
48

    
49
        addSymbolTest(new SimpleFillSymbolTest());
50
        addSymbolTest(new SimpleLineSymbolTest());
51
        addSymbolTest(new SimpleMarkerSymbolTest());
52
        addSymbolTest(new SimpleTextSymbolTest());
53
        return suite;
54
    }
55

    
56
    public static void addSymbolTest(AbstractSymbolTestCase symbolTestClass) {
57
            if (classesToTest == null) classesToTest = new ArrayList(); //<AbstractSymbolTestCase>
58

    
59
            classesToTest.add(symbolTestClass);
60
    }
61

    
62
    public static ISymbol[] getNewSymbolInstances() {
63
        ISymbol[] symbols = new ISymbol[classesToTest.size()];
64
        for (int i = 0; i < symbols.length; i++) {
65
                symbols[i] = (ISymbol)((AbstractSymbolTestCase)classesToTest.get(i)).newInstance();
66
        }
67
        return symbols;
68
    }
69

    
70
    protected void setUp() throws Exception {
71
        symbols = getNewSymbolInstances();
72
        
73
        DefaultGeometryLibrary lib = new DefaultGeometryLibrary();                
74
                lib.initialize();
75
                lib.postInitialize();
76
    }
77

    
78
    public void testPointSuitability() throws InstantiationException, IllegalAccessException {
79
            GeometryManager geomManager = GeometryLocator.getGeometryManager();
80
            Point point = (Point)geomManager.create(TYPES.CURVE, SUBTYPES.GEOM2D);
81
            final Geometry dummyPointGeom = point;
82
        for (int i = 0; i < symbols.length; i++) {
83
            // text symbols are suitable for everything
84
            if (symbols[i] instanceof ITextSymbol) {
85
                assertTrue(symbols[i].getClassName()+" should be suitable for "+dummyPointGeom,
86
                        symbols[i].isSuitableFor(dummyPointGeom));
87
            } else
88

    
89
            // marker symbols are suitable for points
90
            if (symbols[i] instanceof IMarkerSymbol) {
91
                assertTrue(symbols[i].getClassName()+" should be suitable for "+dummyPointGeom,
92
                        symbols[i].isSuitableFor(dummyPointGeom));
93
            } else {
94
                assertFalse(symbols[i].getClassName()+" should NOT be suitable for "+dummyPointGeom,
95
                        symbols[i].isSuitableFor(dummyPointGeom));
96
            }
97
        }
98
    }
99

    
100
    public void testLineSuitability() throws InstantiationException, IllegalAccessException {
101
            GeometryManager geomManager = GeometryLocator.getGeometryManager();
102
            Curve curve = (Curve)geomManager.create(TYPES.CURVE, SUBTYPES.GEOM2D);
103
                curve.setGeneralPath(new GeneralPathX());
104
            final Geometry dummyLineGeom = curve;
105
        for (int i = 0; i < symbols.length; i++) {
106
            // text symbols are suitable for everything
107
            if (symbols[i] instanceof ITextSymbol) {
108
                assertTrue(symbols[i].getClassName()+" should be suitable for "+dummyLineGeom,
109
                        symbols[i].isSuitableFor(dummyLineGeom));
110
            } else
111

    
112
            // line symbols are suitable for line
113
            if (symbols[i] instanceof ILineSymbol) {
114
                assertTrue(symbols[i].getClassName()+" should be suitable for "+dummyLineGeom,
115
                        symbols[i].isSuitableFor(dummyLineGeom));
116
            } else {
117
                assertFalse(symbols[i].getClassName()+" should NOT be suitable for "+dummyLineGeom,
118
                        symbols[i].isSuitableFor(dummyLineGeom));
119
            }
120
        }
121
    }
122

    
123
    public void testPolygonSuitability() throws InstantiationException, IllegalAccessException {
124
            GeometryManager geomManager = GeometryLocator.getGeometryManager();
125
            Surface surface = (Surface)geomManager.create(TYPES.CURVE, SUBTYPES.GEOM2D);
126
            surface.setGeneralPath(new GeneralPathX());
127
        final Geometry dummyPolygonGeom = surface;
128
        for (int i = 0; i < symbols.length; i++) {
129

    
130
            // text symbols are suitable for everything
131
            if (symbols[i] instanceof ITextSymbol) {
132
                assertTrue(symbols[i].getClassName()+" should be suitable for "+dummyPolygonGeom,
133
                        symbols[i].isSuitableFor(dummyPolygonGeom));
134
            } else
135

    
136
            // fill symbols are suitable for polygons
137
            if (symbols[i] instanceof IFillSymbol) {
138
                assertTrue(symbols[i].getClassName()+" should be suitable for "+dummyPolygonGeom,
139
                        symbols[i].isSuitableFor(dummyPolygonGeom));
140
            } else {
141
                assertFalse(symbols[i].getClassName()+" should NOT be suitable for "+dummyPolygonGeom,
142
                        symbols[i].isSuitableFor(dummyPolygonGeom));
143
            }
144
        }
145
    }
146

    
147
    /**
148
     * tests whether the symbols were correctly configured to work with
149
     * different kinds of shapes.
150
     */
151
    public void testGeneralSuitability() {
152

    
153
        for (int i = 0; i < symbols.length; i++) {
154
            // text symbols are suitable for everything
155
            if (symbols[i] instanceof ITextSymbol) {
156
                assertTrue(symbols[i].getClassName()+" should be suitable for "+null,
157
                        symbols[i].isSuitableFor(null));
158
            } else {
159
                try {
160
                    symbols[i].isSuitableFor(null);
161
                    fail("Exception was not thrown");
162

    
163
                } catch (NullPointerException npEx) {
164
                    // this is correct!
165
                }
166
            }
167
        }
168
    }
169

    
170
    /**
171
     * ensures that symbols defined which can of FShape is the symbol
172
     */
173
    public void testSymbolTypeDefinition() {
174
        for (int i = 0; i < symbols.length; i++) {
175
            assertFalse("Symbol no. "+i+" '"+symbols[i].getClassName()+"'",
176
                        symbols[i].getSymbolType() == 0);
177

    
178
        }
179
    }
180

    
181
    /**
182
     * ensures that any symbol that is suitable for markers declares its type
183
     * as FShape.POINT or FShape.MULTI
184
     *
185
     */
186
    public void testMarkerSymbolTypeDefinition() {
187
        for (int i = 0; i < symbols.length; i++) {
188
            if (symbols[i] instanceof IMarkerSymbol) {
189
                assertTrue("Symbol no. "+i+" '"+symbols[i].getClassName()+"'",
190
                        symbols[i].getSymbolType() == Geometry.TYPES.POINT
191
                        || symbols[i].getSymbolType() == Geometry.TYPES.GEOMETRY);
192
            }
193
        }
194
    }
195

    
196
    /**
197
     * ensures that any symbol that is suitable for lines declares its type
198
     * as FShape.LINE or FShape.MULTI
199
     *
200
     */
201
    public void testLineSymbolTypeDefinition() {
202
        for (int i = 0; i < symbols.length; i++) {
203
            if (symbols[i] instanceof ILineSymbol) {
204
                assertTrue("Symbol no. "+i+" '"+symbols[i].getClassName()+"'",
205
                        symbols[i].getSymbolType() == Geometry.TYPES.CURVE
206
                        || symbols[i].getSymbolType() == Geometry.TYPES.GEOMETRY);
207
            }
208
        }
209
    }
210

    
211
    /**
212
     * ensures that any symbol that is suitable for fills declares its type
213
     * as POLYGON or Geometry.TYPES.GEOMETRY
214
     *
215
     */
216
    public void testFillSymbolTypeDefinition() {
217
        for (int i = 0; i < symbols.length; i++) {
218
            if (symbols[i] instanceof IFillSymbol) {
219
                assertTrue("Symbol no. "+i+" '"+symbols[i].getClassName()+"'",
220
                        symbols[i].getSymbolType() == Geometry.TYPES.SURFACE
221
                        || symbols[i].getSymbolType() == Geometry.TYPES.GEOMETRY);
222
            }
223
        }
224
    }
225

    
226
    /**
227
     * ensures that any symbol has a description in its persistence
228
     * (very important)
229
     */
230
    public void testDescription() {
231
        for (int i = 0; i < symbols.length; i++) {
232
            try {
233
                                assertTrue(symbols[i].getClassName() + " does not declare a description in its XMLEntity",
234
                                        symbols[i].getXMLEntity().contains("desc"));
235
                        } catch (XMLException e) {
236
                                // TODO Auto-generated catch block
237
                                e.printStackTrace();
238
                        }
239
        }
240
    }
241

    
242
    /**
243
     * ensures that any symbol has an isShapeVisible field in its persistence
244
     * (very important)
245
     */
246
    public void testIsShapeVisible() {
247
        for (int i = 0; i < symbols.length; i++) {
248
            try {
249
                                assertTrue(symbols[i].getClassName() + " does not declare the isShapeVisible field in its XMLEntity",
250
                                        symbols[i].getXMLEntity().contains("isShapeVisible"));
251
                        } catch (XMLException e) {
252
                                // TODO Auto-generated catch block
253
                                e.printStackTrace();
254
                        }
255
        }
256
    }
257

    
258
    /**
259
     * ensures that the symbol is self-defining
260
     */
261
    public void testSymbolSelfDefinition() {
262
        for (int i = 0; i < symbols.length; i++) {
263
            final ISymbol theSymbol = symbols[i];
264
            ISymbol cloneSymbol=null;
265
                        try {
266
                                cloneSymbol = SymbologyFactory.createSymbolFromXML(theSymbol.getXMLEntity(), null);
267
                        } catch (XMLException e1) {
268
                                // TODO Auto-generated catch block
269
                                e1.printStackTrace();
270
                        }
271
            assertTrue(theSymbol.getClassName()+ " wrong class name declaration in getXMLEntity() ",
272
                    cloneSymbol.getClass().equals(theSymbol.getClass()));
273
            final Field[] theSymbolFields = theSymbol.getClass().getFields();
274
            for (int j = 0; j < theSymbolFields.length; j++) {
275
                final Field fi = theSymbolFields[j];
276
                final boolean wasAccessible = fi.isAccessible();
277
                fi.setAccessible(true);
278

    
279
                try {
280
                    assertTrue(theSymbol.getClassName() + " fails or misses clonning the field " +fi.getName(),
281
                            fi.get(theSymbol).equals(fi.get(cloneSymbol)));
282
                } catch (IllegalArgumentException e) {
283
                    fail();
284
                } catch (IllegalAccessException e) {
285
                    fail();
286
                }
287
                fi.setAccessible(wasAccessible);
288
            }
289
        }
290
    }
291

    
292
    /**
293
     * Check one pixel acceleration consistency. Checks that the color returned
294
     * in RGB matches the color of the symbol set.
295
     *
296
     */
297
    public void testOnePointRGB() {
298
            Random random = new Random(System.currentTimeMillis());
299

    
300
        for (int i = 0; i < symbols.length; i++) {
301
            if (symbols[i] instanceof IMarkerSymbol) {
302
                IMarkerSymbol marker = (IMarkerSymbol) symbols[i];
303
                marker.setColor(new Color(random.nextInt()>>8));
304
                assertTrue("Symbol no. "+i+" '"+symbols[i].getClassName()+"' RGB value mismatch for getOnePointRBG() and getColor().getRGB()",
305
                        symbols[i].getOnePointRgb() == marker.getColor().getRGB());
306

    
307
            }
308

    
309
            if (symbols[i] instanceof ILineSymbol) {
310
                ILineSymbol line = (ILineSymbol) symbols[i];
311
                line.setLineColor(new Color(random.nextInt()>>8));
312
                assertTrue("Symbol no. "+i+" '"+symbols[i].getClassName()+"' RGB value mismatch for getOnePointRBG() and getColor().getRGB()",
313
                        symbols[i].getOnePointRgb() == line.getColor().getRGB());
314
            }
315

    
316
            if (symbols[i] instanceof IFillSymbol) {
317
                IFillSymbol fill = (IFillSymbol) symbols[i];
318
                boolean outlined = fill.getOutline() != null;
319
                if (!outlined)
320
                        fill.setFillColor(new Color(random.nextInt()>>8));
321
                else
322
                        fill.getOutline().setLineColor(new Color(random.nextInt()>>8));
323
                assertTrue("Symbol no. "+i+" '"+symbols[i].getClassName()+"' RGB value mismatch for getOnePointRBG() and getColor().getRGB()",
324
                        symbols[i].getOnePointRgb() == ((outlined) ? fill.getOutline().getColor().getRGB() : fill.getFillColor().getRGB()));
325
            }
326

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

    
334
        }
335
    }
336

    
337
    /**
338
     * ensures that any symbol provides a version of itself to use when the
339
     * feature is selected
340
     *
341
     */
342
    public void testSymbolForSelection() {
343
            for (int i = 0; i < symbols.length; i++) {
344
                        assertNotNull("Symbol no. "+i+" '"+symbols[i].getClassName()+" does not define any derived symbol for selection", symbols[i].getSymbolForSelection());
345
                }
346
    }
347
}