Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / org.gvsig.symbology / org.gvsig.symbology.lib / org.gvsig.symbology.lib.impl / src / test / java / org / gvsig / symbology / fmap / mapcontext / rendering / symbol / TestISymbol.java @ 34294

History | View | Annotate | Download (13.9 KB)

1
package org.gvsig.symbology.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.TestSuite;
9

    
10
import org.gvsig.fmap.geom.Geometry;
11
import org.gvsig.fmap.geom.GeometryLocator;
12
import org.gvsig.fmap.geom.GeometryManager;
13
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
14
import org.gvsig.fmap.geom.Geometry.TYPES;
15
import org.gvsig.fmap.geom.exception.CreateGeometryException;
16
import org.gvsig.fmap.geom.primitive.Curve;
17
import org.gvsig.fmap.geom.primitive.GeneralPathX;
18
import org.gvsig.fmap.geom.primitive.Point;
19
import org.gvsig.fmap.geom.primitive.Surface;
20
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
21
import org.gvsig.fmap.mapcontext.rendering.symbols.ITextSymbol;
22
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol;
23
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
24
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
25
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
26

    
27
/**
28
 * Integration test to ensure that the symbols follow the rules that follow the
29
 * managing of them by the application.
30
 *
31
 * @author jaume dominguez faus - jaume.dominguez@iver.es
32
 */
33

    
34

    
35
public class TestISymbol extends AbstractLibraryAutoInitTestCase {
36
    private static ArrayList classesToTest;//<AbstractSymbolTestCase>
37
    transient private ISymbol[] symbols;
38

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

    
46
        return suite;
47
    }
48

    
49
    protected void doSetUp() throws Exception {
50
            addSymbols();
51
            symbols = getNewSymbolInstances();
52
    }
53
    
54
    public static void addSymbols() {            
55
            addSymbolTest(new SimpleFillSymbolTest());
56
            addSymbolTest(new SimpleLineSymbolTest());
57
            addSymbolTest(new SimpleMarkerSymbolTest());
58
            addSymbolTest(new SimpleTextSymbolTest());
59
    }
60
    
61

    
62
    public static void addSymbolTest(AbstractSymbolTestCase symbolTestClass) {
63
            if (classesToTest == null) classesToTest = new ArrayList(); //<AbstractSymbolTestCase>
64

    
65
            classesToTest.add(symbolTestClass);
66
    }
67

    
68
    public static ISymbol[] getNewSymbolInstances() {
69
        ISymbol[] symbols = new ISymbol[classesToTest.size()];
70
        for (int i = 0; i < symbols.length; i++) {
71
                symbols[i] = (ISymbol)((AbstractSymbolTestCase)classesToTest.get(i)).newInstance();
72
        }
73
        return symbols;
74
    }
75

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

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

    
98
    public void testLineSuitability() throws InstantiationException, IllegalAccessException, CreateGeometryException {
99
            GeometryManager geomManager = GeometryLocator.getGeometryManager();
100
            Curve curve = (Curve)geomManager.create(TYPES.CURVE, SUBTYPES.GEOM2D);
101
                curve.setGeneralPath(new GeneralPathX());
102
            final Geometry dummyLineGeom = curve;
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 "+dummyLineGeom,
107
                        symbols[i].isSuitableFor(dummyLineGeom));
108
            } else
109

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

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

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

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

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

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

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

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

    
176
        }
177
    }
178

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

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

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

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

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

    
256
    /**
257
     * ensures that the symbol is self-defining
258
     * @throws Exception 
259
     */
260
    public void testSymbolSelfDefinition() throws Exception {
261
        for (int i = 0; i < symbols.length; i++) {
262
            final ISymbol theSymbol = symbols[i];
263
            ISymbol cloneSymbol= (ISymbol) theSymbol.clone();
264
                                //cloneSymbol = SymbologyFactory.createSymbolFromXML(theSymbol.getXMLEntity(), null);
265
            
266
            assertTrue(theSymbol.getClass().getName()+ " wrong class name declaration in getXMLEntity() ",
267
                    cloneSymbol.getClass().equals(theSymbol.getClass()));
268
            final Field[] theSymbolFields = theSymbol.getClass().getFields();
269
            for (int j = 0; j < theSymbolFields.length; j++) {
270
                final Field fi = theSymbolFields[j];
271
                final boolean wasAccessible = fi.isAccessible();
272
                fi.setAccessible(true);
273

    
274
                try {
275
                    assertTrue(theSymbol.getClass().getName() + " fails or misses clonning the field " +fi.getName(),
276
                            fi.get(theSymbol).equals(fi.get(cloneSymbol)));
277
                } catch (IllegalArgumentException e) {
278
                    fail();
279
                } catch (IllegalAccessException e) {
280
                    fail();
281
                }
282
                fi.setAccessible(wasAccessible);
283
            }
284
        }
285
    }
286

    
287
    /**
288
     * Check one pixel acceleration consistency. Checks that the color returned
289
     * in RGB matches the color of the symbol set.
290
     *
291
     */
292
    public void testOnePointRGB() {
293
            Random random = new Random(System.currentTimeMillis());
294

    
295
        for (int i = 0; i < symbols.length; i++) {
296
            if (symbols[i] instanceof IMarkerSymbol) {
297
                IMarkerSymbol marker = (IMarkerSymbol) symbols[i];
298
                marker.setColor(new Color(random.nextInt()>>8));
299
                assertTrue("Symbol no. "+i+" '"+symbols[i].getClass().getName()+"' RGB value mismatch for getOnePointRBG() and getColor().getRGB()",
300
                        symbols[i].getOnePointRgb() == marker.getColor().getRGB());
301

    
302
            }
303

    
304
            if (symbols[i] instanceof ILineSymbol) {
305
                ILineSymbol line = (ILineSymbol) symbols[i];
306
                line.setLineColor(new Color(random.nextInt()>>8));
307
                assertTrue("Symbol no. "+i+" '"+symbols[i].getClass().getName()+"' RGB value mismatch for getOnePointRBG() and getColor().getRGB()",
308
                        symbols[i].getOnePointRgb() == line.getColor().getRGB());
309
            }
310

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

    
322
            if (symbols[i] instanceof ITextSymbol) {
323
                    ITextSymbol text = (ITextSymbol) symbols[i];
324
                    text.setTextColor(new Color(random.nextInt()>>8));
325
                assertTrue("Symbol no. "+i+" '"+symbols[i].getClass().getName()+"' RGB value mismatch for getOnePointRBG() and getColor().getRGB()",
326
                        symbols[i].getOnePointRgb() == text.getTextColor().getRGB());
327
            }
328

    
329
        }
330
    }
331

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