Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src-test / com / iver / cit / gvsig / fmap / core / symbols / TestISymbol.java @ 22069

History | View | Annotate | Download (12.5 KB)

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

    
24

    
25
public class TestISymbol extends TestCase {
26
    private static ArrayList<AbstractSymbolTestCase> classesToTest;
27
    transient private ISymbol[] symbols;
28
    private static boolean init = true;
29

    
30
    public static TestSuite suite() {
31
            // NOTE: order is important for incremental testing
32

    
33
            TestSuite suite = new TestSuite("Integration test for com.iver.cit.gvsig.fmap.core.ISymbol");
34
        suite.addTestSuite(TestISymbol.class);
35
        suite.addTestSuite(TestIMarkerSymbol.class);
36
        suite.addTestSuite(TestILineSymbol.class);
37
        suite.addTestSuite(TestIFillSymbol.class);
38
        
39
        addSymbolTest(new SimpleFillSymbolTest());
40
        addSymbolTest(new SimpleLineSymbolTest());
41
        addSymbolTest(new SimpleMarkerSymbolTest());
42
        addSymbolTest(new SimpleTextSymbolTest());
43
        return suite;
44
    }
45

    
46
    public static void addSymbolTest(AbstractSymbolTestCase symbolTestClass) {
47
            if (classesToTest == null) classesToTest = new ArrayList<AbstractSymbolTestCase>();
48
            classesToTest.add(symbolTestClass);
49
    }
50

    
51
    public static ISymbol[] getNewSymbolInstances() {
52
            if (init) suite();
53
            init = false;
54
        ISymbol[] symbols = new ISymbol[classesToTest.size()];
55
        for (int i = 0; i < symbols.length; i++) {
56
                symbols[i] = classesToTest.get(i).newInstance();
57
        }
58
        return symbols;
59
    }
60

    
61
    protected void setUp() throws Exception {
62
        symbols = getNewSymbolInstances();
63
    }
64

    
65
    public void testPointSuitability() {
66
        final IGeometry dummyPointGeom = ShapeFactory.createPoint2D(new FPoint2D());
67
        for (int i = 0; i < symbols.length; i++) {
68
            // text symbols are suitable for everything
69
            if (symbols[i] instanceof ITextSymbol) {
70
                assertTrue(getNameForSymbol(symbols[i])+" should be suitable for "+dummyPointGeom,
71
                        symbols[i].isSuitableFor(dummyPointGeom));
72
            } else
73

    
74
            // marker symbols are suitable for points
75
            if (symbols[i] instanceof IMarkerSymbol) {
76
                assertTrue(getNameForSymbol(symbols[i])+" should be suitable for "+dummyPointGeom,
77
                        symbols[i].isSuitableFor(dummyPointGeom));
78
            } else {
79
                assertFalse(getNameForSymbol(symbols[i])+" should NOT be suitable for "+dummyPointGeom,
80
                        symbols[i].isSuitableFor(dummyPointGeom));
81
            }
82
        }
83
    }
84

    
85
    public void testLineSuitability() {
86
        final IGeometry dummyLineGeom = ShapeFactory.createPolyline2D(new GeneralPathX());
87
        for (int i = 0; i < symbols.length; i++) {
88
            // text symbols are suitable for everything
89
            if (symbols[i] instanceof ITextSymbol) {
90
                assertTrue(getNameForSymbol(symbols[i])+" should be suitable for "+dummyLineGeom,
91
                        symbols[i].isSuitableFor(dummyLineGeom));
92
            } else
93

    
94
            // line symbols are suitable for line
95
            if (symbols[i] instanceof ILineSymbol) {
96
                assertTrue(getNameForSymbol(symbols[i])+" should be suitable for "+dummyLineGeom,
97
                        symbols[i].isSuitableFor(dummyLineGeom));
98
            } else {
99
                assertFalse(getNameForSymbol(symbols[i])+" should NOT be suitable for "+dummyLineGeom,
100
                        symbols[i].isSuitableFor(dummyLineGeom));
101
            }
102
        }
103
    }
104

    
105
    public void testPolygonSuitability() {
106
        final IGeometry dummyPolygonGeom = ShapeFactory.createPolygon2D(new GeneralPathX());
107
        for (int i = 0; i < symbols.length; i++) {
108

    
109
            // text symbols are suitable for everything
110
            if (symbols[i] instanceof ITextSymbol) {
111
                assertTrue(getNameForSymbol(symbols[i])+" should be suitable for "+dummyPolygonGeom,
112
                        symbols[i].isSuitableFor(dummyPolygonGeom));
113
            } else
114

    
115
            // fill symbols are suitable for polygons
116
            if (symbols[i] instanceof IFillSymbol) {
117
                assertTrue(getNameForSymbol(symbols[i])+" should be suitable for "+dummyPolygonGeom,
118
                        symbols[i].isSuitableFor(dummyPolygonGeom));
119
            } else {
120
                assertFalse(getNameForSymbol(symbols[i])+" should NOT be suitable for "+dummyPolygonGeom,
121
                        symbols[i].isSuitableFor(dummyPolygonGeom));
122
            }
123
        }
124
    }
125

    
126
    /**
127
     * tests whether the symbols were correctly configured to work with
128
     * different kinds of shapes.
129
     */
130
    public void testGeneralSuitability() {
131

    
132
        for (int i = 0; i < symbols.length; i++) {
133
            // text symbols are suitable for everything
134
            if (symbols[i] instanceof ITextSymbol) {
135
                assertTrue(symbols[i].getClassName()+" should be suitable for "+null,
136
                        symbols[i].isSuitableFor(null));
137
            } else {
138
                try {
139
                    symbols[i].isSuitableFor(null);
140
                    fail("Exception was not thrown");
141

    
142
                } catch (NullPointerException npEx) {
143
                    // this is correct!
144
                }
145
            }
146
        }
147
    }
148

    
149
    /**
150
     * ensures that symbols defined which can of FShape is the symbol
151
     */
152
    public void testSymbolTypeDefinition() {
153
        for (int i = 0; i < symbols.length; i++) {
154
            assertFalse("Symbol no. "+i+" '"+getNameForSymbol(symbols[i])+"'",
155
                        symbols[i].getSymbolType() == 0);
156

    
157
        }
158
    }
159

    
160
    /**
161
     * ensures that any symbol that is suitable for markers declares its type
162
     * as FShape.POINT or FShape.MULTI
163
     *
164
     */
165
    public void testMarkerSymbolTypeDefinition() {
166
        for (int i = 0; i < symbols.length; i++) {
167
            if (symbols[i] instanceof IMarkerSymbol) {
168
                assertTrue("Symbol no. "+i+" '"+getNameForSymbol(symbols[i])+"'",
169
                        symbols[i].getSymbolType() == FShape.POINT
170
                        || symbols[i].getSymbolType() == FShape.MULTI);
171
            }
172
        }
173
    }
174

    
175
    /**
176
     * ensures that any symbol that is suitable for lines declares its type
177
     * as FShape.LINE or FShape.MULTI
178
     *
179
     */
180
    public void testLineSymbolTypeDefinition() {
181
        for (int i = 0; i < symbols.length; i++) {
182
            if (symbols[i] instanceof ILineSymbol) {
183
                assertTrue("Symbol no. "+i+" '"+getNameForSymbol(symbols[i])+"'",
184
                        symbols[i].getSymbolType() == FShape.LINE
185
                        || symbols[i].getSymbolType() == FShape.MULTI);
186
            }
187
        }
188
    }
189

    
190
    /**
191
     * ensures that any symbol that is suitable for fills declares its type
192
     * as POLYGON or FShape.MULTI
193
     *
194
     */
195
    public void testFillSymbolTypeDefinition() {
196
        for (int i = 0; i < symbols.length; i++) {
197
            if (symbols[i] instanceof IFillSymbol) {
198
                assertTrue("Symbol no. "+i+" '"+getNameForSymbol(symbols[i])+"'",
199
                        symbols[i].getSymbolType() == FShape.POLYGON
200
                        || symbols[i].getSymbolType() == FShape.MULTI);
201
            }
202
        }
203
    }
204

    
205
    /**
206
     * ensures that any symbol has a description in its persistence
207
     * (very important)
208
     */
209
    public void testDescription() {
210
        for (int i = 0; i < symbols.length; i++) {
211
            assertTrue(getNameForSymbol(symbols[i]) + " does not declare a description in its XMLEntity",
212
                    symbols[i].getXMLEntity().contains("desc"));
213
        }
214
    }
215

    
216
    /**
217
     * ensures that any symbol has an isShapeVisible field in its persistence
218
     * (very important)
219
     */
220
    public void testIsShapeVisible() {
221
        for (int i = 0; i < symbols.length; i++) {
222
            assertTrue(getNameForSymbol(symbols[i]) + " does not declare the isShapeVisible field in its XMLEntity",
223
                    symbols[i].getXMLEntity().contains("isShapeVisible"));
224
        }
225
    }
226

    
227
    /**
228
     * ensures that the symbol is self-defining
229
     */
230
    public void testSymbolSelfDefinition() {
231
        for (int i = 0; i < symbols.length; i++) {
232
            final ISymbol theSymbol = symbols[i];
233
            final ISymbol cloneSymbol = SymbologyFactory.createSymbolFromXML(theSymbol.getXMLEntity(), null);
234
            assertTrue(theSymbol.getClassName()+ " wrong class name declaration in getXMLEntity() ",
235
                    cloneSymbol.getClass().equals(theSymbol.getClass()));
236
            final Field[] theSymbolFields = theSymbol.getClass().getFields();
237
            for (int j = 0; j < theSymbolFields.length; j++) {
238
                final Field fi = theSymbolFields[j];
239
                final boolean wasAccessible = fi.isAccessible();
240
                fi.setAccessible(true);
241

    
242
                try {
243
                    assertTrue(theSymbol.getClassName() + " fails or misses clonning the field " +fi.getName(),
244
                            fi.get(theSymbol).equals(fi.get(cloneSymbol)));
245
                } catch (IllegalArgumentException e) {
246
                    fail();
247
                } catch (IllegalAccessException e) {
248
                    fail();
249
                }
250
                fi.setAccessible(wasAccessible);
251
            }
252
        }
253
    }
254

    
255
    /**
256
     * Check one pixel acceleration consistency. Checks that the color returned
257
     * in RGB matches the color of the symbol set.
258
     *
259
     */
260
    public void testOnePointRGB() {
261
            Random random = new Random(System.currentTimeMillis());
262

    
263
        for (int i = 0; i < symbols.length; i++) {
264
            if (symbols[i] instanceof IMarkerSymbol) {
265
                IMarkerSymbol marker = (IMarkerSymbol) symbols[i];
266
                marker.setColor(new Color(random.nextInt()>>8));
267
                assertTrue("Symbol no. "+i+" '"+getNameForSymbol(symbols[i])+"' RGB value mismatch for getOnePointRBG() and getColor().getRGB()",
268
                        symbols[i].getOnePointRgb() == marker.getColor().getRGB());
269

    
270
            }
271

    
272
            if (symbols[i] instanceof ILineSymbol) {
273
                ILineSymbol line = (ILineSymbol) symbols[i];
274
                line.setLineColor(new Color(random.nextInt()>>8));
275
                assertTrue("Symbol no. "+i+" '"+getNameForSymbol(symbols[i])+"' RGB value mismatch for getOnePointRBG() and getColor().getRGB()",
276
                        symbols[i].getOnePointRgb() == line.getColor().getRGB());
277
            }
278

    
279
            if (symbols[i] instanceof IFillSymbol) {
280
                IFillSymbol fill = (IFillSymbol) symbols[i];
281
                boolean outlined = fill.getOutline() != null;
282
                if (!outlined)
283
                        fill.setFillColor(new Color(random.nextInt()>>8));
284
                else
285
                        fill.getOutline().setLineColor(new Color(random.nextInt()>>8));
286
                assertTrue("Symbol no. "+i+" '"+getNameForSymbol(symbols[i])+"' RGB value mismatch for getOnePointRBG() and getColor().getRGB()",
287
                        symbols[i].getOnePointRgb() == ((outlined) ? fill.getOutline().getColor().getRGB() : fill.getFillColor().getRGB()));
288
            }
289

    
290
            if (symbols[i] instanceof ITextSymbol) {
291
                    ITextSymbol text = (ITextSymbol) symbols[i];
292
                    text.setTextColor(new Color(random.nextInt()>>8));
293
                assertTrue("Symbol no. "+i+" '"+getNameForSymbol(symbols[i])+"' RGB value mismatch for getOnePointRBG() and getColor().getRGB()",
294
                        symbols[i].getOnePointRgb() == text.getTextColor().getRGB());
295
            }
296

    
297
        }
298
    }
299

    
300
    /**
301
     * ensures that any symbol provides a version of itself to use when the
302
     * feature is selected
303
     *
304
     */
305
    public void testSymbolForSelection() {
306
            for (int i = 0; i < symbols.length; i++) {
307
                        assertNotNull("Symbol no. "+i+" '"+getNameForSymbol(symbols[i])+" does not define any derived symbol for selection", symbols[i].getSymbolForSelection());
308
                }
309
    }
310

    
311
        public static String getNameForSymbol(ISymbol testSymbol) {
312
                 return testSymbol.getClassName().substring(
313
                                        testSymbol.getClassName().lastIndexOf('.')+1,
314
                                        testSymbol.getClassName().length());
315
        }
316
}