svn-gvsig-desktop / trunk / libraries / libFMap / src-test / com / iver / cit / gvsig / fmap / core / symbols / TestISymbol.java @ 12043
History | View | Annotate | Download (7.9 KB)
1 |
package com.iver.cit.gvsig.fmap.core.symbols; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
import java.lang.reflect.Field; |
6 |
import java.util.ArrayList; |
7 |
|
8 |
import junit.framework.Test; |
9 |
import junit.framework.TestCase; |
10 |
import junit.framework.TestSuite; |
11 |
|
12 |
import com.iver.cit.gvsig.fmap.core.FPoint2D; |
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 |
public class TestISymbol extends TestCase { |
24 |
private static ArrayList classesToTest; |
25 |
transient private ISymbol[] symbols; |
26 |
|
27 |
public static Test suite() { |
28 |
TestSuite suite = new TestSuite("Integration test for com.iver.cit.gvsig.fmap.core.ISymbol"); |
29 |
suite.addTestSuite(TestDrawMarkers.class); |
30 |
suite.addTestSuite(TestDrawLines.class); |
31 |
suite.addTestSuite(TestDrawFills.class); |
32 |
|
33 |
return suite;
|
34 |
} |
35 |
|
36 |
private static ArrayList getClassesToTest() { |
37 |
if (classesToTest == null) { |
38 |
classesToTest = new ArrayList(); |
39 |
TestISymbol.addSymbolToTest(SimpleMarkerSymbol.class); |
40 |
TestISymbol.addSymbolToTest(CharacterMarkerSymbol.class); |
41 |
TestISymbol.addSymbolToTest(PictureMarkerSymbol.class); |
42 |
TestISymbol.addSymbolToTest(DotDensityFillSymbol.class); |
43 |
TestISymbol.addSymbolToTest(MarkerFillSymbol.class); |
44 |
TestISymbol.addSymbolToTest(SimpleFillSymbol.class); |
45 |
TestISymbol.addSymbolToTest(SimpleLineSymbol.class); |
46 |
TestISymbol.addSymbolToTest(SimpleTextSymbol.class); |
47 |
TestISymbol.addSymbolToTest(SmartTextSymbol.class); |
48 |
TestISymbol.addSymbolToTest(SimpleMarkerSymbol.class); |
49 |
|
50 |
} |
51 |
|
52 |
return classesToTest;
|
53 |
} |
54 |
|
55 |
public static void addSymbolToTest(Class symbolClass) { |
56 |
try {
|
57 |
ISymbol sym = (ISymbol) symbolClass.newInstance(); |
58 |
sym.getXMLEntity(); |
59 |
} catch (InstantiationException e) { |
60 |
// TODO Auto-generated catch block
|
61 |
fail("Instantiating class, cannot test a non-instantiable symbol");
|
62 |
} catch (IllegalAccessException e) { |
63 |
// TODO Auto-generated catch block
|
64 |
fail("Class not instantiable");
|
65 |
} catch (ClassCastException ccEx) { |
66 |
fail("Cannot test a non symbol class");
|
67 |
} |
68 |
getClassesToTest().add(symbolClass); |
69 |
} |
70 |
|
71 |
public static ISymbol[] getNewSymbolInstances() { |
72 |
ISymbol[] symbols = new ISymbol[getClassesToTest().size()]; |
73 |
for (int i = 0; i < symbols.length; i++) { |
74 |
|
75 |
try {
|
76 |
symbols[i] = (ISymbol) ((Class) getClassesToTest().get(i)).newInstance();
|
77 |
if (symbols[i] instanceof PictureMarkerSymbol) { |
78 |
PictureMarkerSymbol pms = (PictureMarkerSymbol) symbols[i]; |
79 |
try {
|
80 |
pms.setImage(new File(TestISymbol.class.getResource("picture-marker.png").getFile())); |
81 |
} catch (IOException e) { |
82 |
fail("setting up a PictureMarkerSymbol");
|
83 |
} |
84 |
} |
85 |
} catch (InstantiationException e) { |
86 |
fail("Instantiating class");
|
87 |
} catch (IllegalAccessException e) { |
88 |
fail("Class not instantiable");
|
89 |
} |
90 |
|
91 |
} |
92 |
return symbols;
|
93 |
} |
94 |
|
95 |
protected void setUp() throws Exception { |
96 |
symbols = getNewSymbolInstances(); |
97 |
} |
98 |
|
99 |
public void testPointSuitability() { |
100 |
final IGeometry dummyPointGeom = ShapeFactory.createPoint2D(new FPoint2D()); |
101 |
for (int i = 0; i < symbols.length; i++) { |
102 |
// text symbols are suitable for everything
|
103 |
if (symbols[i] instanceof ITextSymbol) { |
104 |
assertTrue(symbols[i].getClassName()+" should be suitable for "+dummyPointGeom,
|
105 |
symbols[i].isSuitableFor(dummyPointGeom)); |
106 |
} else
|
107 |
|
108 |
// marker symbols are suitable for points
|
109 |
if (symbols[i] instanceof IMarkerSymbol) { |
110 |
assertTrue(symbols[i].getClassName()+" should be suitable for "+dummyPointGeom,
|
111 |
symbols[i].isSuitableFor(dummyPointGeom)); |
112 |
} else {
|
113 |
assertFalse(symbols[i].getClassName()+" should NOT be suitable for "+dummyPointGeom,
|
114 |
symbols[i].isSuitableFor(dummyPointGeom)); |
115 |
} |
116 |
} |
117 |
} |
118 |
|
119 |
public void testLineSuitability() { |
120 |
final IGeometry dummyLineGeom = ShapeFactory.createPolyline2D(new GeneralPathX()); |
121 |
for (int i = 0; i < symbols.length; i++) { |
122 |
// text symbols are suitable for everything
|
123 |
if (symbols[i] instanceof ITextSymbol) { |
124 |
assertTrue(symbols[i].getClassName()+" should be suitable for "+dummyLineGeom,
|
125 |
symbols[i].isSuitableFor(dummyLineGeom)); |
126 |
} else
|
127 |
|
128 |
// line symbols are suitable for line
|
129 |
if (symbols[i] instanceof ILineSymbol) { |
130 |
assertTrue(symbols[i].getClassName()+" should be suitable for "+dummyLineGeom,
|
131 |
symbols[i].isSuitableFor(dummyLineGeom)); |
132 |
} else {
|
133 |
assertFalse(symbols[i].getClassName()+" should NOT be suitable for "+dummyLineGeom,
|
134 |
symbols[i].isSuitableFor(dummyLineGeom)); |
135 |
} |
136 |
} |
137 |
} |
138 |
|
139 |
public void testPolygonSuitability() { |
140 |
final IGeometry dummyPolygonGeom = ShapeFactory.createPolygon2D(new GeneralPathX()); |
141 |
for (int i = 0; i < symbols.length; i++) { |
142 |
|
143 |
// text symbols are suitable for everything
|
144 |
if (symbols[i] instanceof ITextSymbol) { |
145 |
assertTrue(symbols[i].getClassName()+" should be suitable for "+dummyPolygonGeom,
|
146 |
symbols[i].isSuitableFor(dummyPolygonGeom)); |
147 |
} else
|
148 |
|
149 |
// fill symbols are suitable for polygons
|
150 |
if (symbols[i] instanceof IFillSymbol) { |
151 |
assertTrue(symbols[i].getClassName()+" should be suitable for "+dummyPolygonGeom,
|
152 |
symbols[i].isSuitableFor(dummyPolygonGeom)); |
153 |
} else {
|
154 |
assertFalse(symbols[i].getClassName()+" should NOT be suitable for "+dummyPolygonGeom,
|
155 |
symbols[i].isSuitableFor(dummyPolygonGeom)); |
156 |
} |
157 |
} |
158 |
} |
159 |
|
160 |
/**
|
161 |
* tests whether the symbols were correctly configured to work with
|
162 |
* different kinds of shapes.
|
163 |
*/
|
164 |
public void testGeneralSuitability() { |
165 |
|
166 |
for (int i = 0; i < symbols.length; i++) { |
167 |
// text symbols are suitable for everything
|
168 |
if (symbols[i] instanceof ITextSymbol) { |
169 |
assertTrue(symbols[i].getClassName()+" should be suitable for "+null, |
170 |
symbols[i].isSuitableFor(null));
|
171 |
} else {
|
172 |
try {
|
173 |
symbols[i].isSuitableFor(null);
|
174 |
fail("Exception was not thrown");
|
175 |
|
176 |
} catch (NullPointerException npEx) { |
177 |
// this is correct!
|
178 |
} |
179 |
} |
180 |
} |
181 |
} |
182 |
|
183 |
/**
|
184 |
* ensures that symbols defined which can of FShape is the symbol
|
185 |
*/
|
186 |
public void testSymbolTypeDefinition() { |
187 |
for (int i = 0; i < symbols.length; i++) { |
188 |
assertFalse("Symbol no. "+i+" '"+symbols[i].getClassName()+"'", |
189 |
symbols[i].getSymbolType() == 0);
|
190 |
|
191 |
} |
192 |
} |
193 |
|
194 |
/**
|
195 |
* ensures that any symbol has a description in its persistence
|
196 |
* (very important)
|
197 |
*/
|
198 |
public void testDescription() { |
199 |
for (int i = 0; i < symbols.length; i++) { |
200 |
assertTrue(symbols[i].getClassName() + " does not declare a description in its XMLEntity",
|
201 |
symbols[i].getXMLEntity().contains("desc"));
|
202 |
} |
203 |
} |
204 |
|
205 |
/**
|
206 |
* ensures that any symbol has an isShapeVisible field in its persistence
|
207 |
* (very important)
|
208 |
*/
|
209 |
public void testIsShapeVisible() { |
210 |
for (int i = 0; i < symbols.length; i++) { |
211 |
assertTrue(symbols[i].getClassName() + " does not declare a description in its XMLEntity",
|
212 |
symbols[i].getXMLEntity().contains("isShapeVisible"));
|
213 |
} |
214 |
} |
215 |
|
216 |
/**
|
217 |
* ensures that the symbol is self-defining
|
218 |
*/
|
219 |
public void testSymbolSelfDefinition() { |
220 |
for (int i = 0; i < symbols.length; i++) { |
221 |
final ISymbol theSymbol = symbols[i];
|
222 |
final ISymbol cloneSymbol = SymbologyFactory.createSymbolFromXML(theSymbol.getXMLEntity(), null); |
223 |
assertTrue(theSymbol.getClassName()+ " wrong class name declaration in getXMLEntity() ",
|
224 |
cloneSymbol.getClass().equals(theSymbol.getClass())); |
225 |
final Field[] theSymbolFields = theSymbol.getClass().getFields(); |
226 |
for (int j = 0; j < theSymbolFields.length; j++) { |
227 |
final Field fi = theSymbolFields[j]; |
228 |
final boolean wasAccessible = fi.isAccessible(); |
229 |
fi.setAccessible(true);
|
230 |
|
231 |
try {
|
232 |
assertTrue(theSymbol.getClassName() + " fails or misses clonning the field " +fi.getName(),
|
233 |
fi.get(theSymbol).equals(fi.get(cloneSymbol))); |
234 |
} catch (IllegalArgumentException e) { |
235 |
fail(); |
236 |
} catch (IllegalAccessException e) { |
237 |
fail(); |
238 |
} |
239 |
fi.setAccessible(wasAccessible); |
240 |
} |
241 |
} |
242 |
} |
243 |
|
244 |
|
245 |
} |