root / trunk / libraries / libFMap / src-test / com / iver / cit / gvsig / fmap / core / symbols / TestILegend.java @ 18622
History | View | Annotate | Download (7.54 KB)
1 |
/* gvSIG. Sistema de Información Geográfica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ibáñez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package com.iver.cit.gvsig.fmap.core.symbols; |
42 |
|
43 |
import java.awt.Color; |
44 |
import java.lang.reflect.Field; |
45 |
import java.util.ArrayList; |
46 |
|
47 |
import junit.framework.TestCase; |
48 |
|
49 |
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException; |
50 |
import com.iver.cit.gvsig.fmap.layers.XMLException; |
51 |
import com.iver.cit.gvsig.fmap.rendering.IClassifiedVectorLegend; |
52 |
import com.iver.cit.gvsig.fmap.rendering.ILegend; |
53 |
import com.iver.cit.gvsig.fmap.rendering.IVectorLegend; |
54 |
import com.iver.cit.gvsig.fmap.rendering.IVectorialIntervalLegend; |
55 |
import com.iver.cit.gvsig.fmap.rendering.IVectorialUniqueValueLegend; |
56 |
import com.iver.cit.gvsig.fmap.rendering.SingleSymbolLegend; |
57 |
import com.iver.cit.gvsig.fmap.rendering.VectorialIntervalLegend; |
58 |
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend; |
59 |
|
60 |
/**
|
61 |
* Integration test to ensure that the legends follow the rules that follow the
|
62 |
* managing of them by the application.
|
63 |
*
|
64 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
65 |
*/
|
66 |
public class TestILegend extends TestCase { |
67 |
private static ArrayList classesToTest; |
68 |
transient private ILegend[] legends; |
69 |
|
70 |
private static ArrayList getClassesToTest() { |
71 |
if (classesToTest == null) { |
72 |
classesToTest = new ArrayList(); |
73 |
|
74 |
TestILegend.addLegendToTest(VectorialIntervalLegend.class); |
75 |
TestILegend.addLegendToTest(VectorialUniqueValueLegend.class); |
76 |
TestILegend.addLegendToTest(SingleSymbolLegend.class); |
77 |
|
78 |
// TestILegend.addLegendToTest(QuantityByCategoryLegend.class);
|
79 |
// TestILegend.addLegendToTest(DotDensityLegend.class);
|
80 |
// TestILegend.addLegendToTest(ProportionalSymbolsLegend.class);
|
81 |
// TestILegend.addLegendToTest(GraduatedSymbolLegend.class);
|
82 |
|
83 |
|
84 |
} |
85 |
|
86 |
return classesToTest;
|
87 |
} |
88 |
|
89 |
/**
|
90 |
* This method fills the attributes of a legend.The purpose is to ensure that the new
|
91 |
* legend is 'full' and ready to be used for a test.
|
92 |
* @param le
|
93 |
* @return
|
94 |
*/
|
95 |
|
96 |
private static ILegend fillthelegend(ILegend le) { |
97 |
|
98 |
SimpleFillSymbol mfs = new SimpleFillSymbol();
|
99 |
mfs.setFillColor(Color.ORANGE.darker());
|
100 |
mfs.setDescription("hola");
|
101 |
mfs.setIsShapeVisible(true);
|
102 |
String[] cad = new String[] {"Pepe","Juan"}; |
103 |
|
104 |
if(le instanceof IVectorLegend) { |
105 |
IVectorLegend ssl=(IVectorLegend)le; |
106 |
ssl.setDefaultSymbol(mfs); |
107 |
} |
108 |
|
109 |
if(le instanceof IVectorialUniqueValueLegend) { |
110 |
IVectorialUniqueValueLegend ivuvl= (IVectorialUniqueValueLegend)le; |
111 |
ivuvl.setClassifyingFieldNames(cad); |
112 |
} |
113 |
|
114 |
if(le instanceof IVectorialIntervalLegend) { |
115 |
IVectorialIntervalLegend ivil=(IVectorialIntervalLegend)le; |
116 |
ivil.setClassifyingFieldNames(cad); |
117 |
} |
118 |
|
119 |
if(le instanceof IClassifiedVectorLegend) { |
120 |
IClassifiedVectorLegend icvl=(IClassifiedVectorLegend)le; |
121 |
icvl.setClassifyingFieldNames(cad); |
122 |
} |
123 |
|
124 |
|
125 |
|
126 |
return le;
|
127 |
} |
128 |
|
129 |
|
130 |
public static void addLegendToTest(Class legendClass) { |
131 |
try {
|
132 |
ILegend len = (ILegend) legendClass.newInstance(); |
133 |
fillthelegend(len); |
134 |
len.getXMLEntity(); |
135 |
|
136 |
} catch (InstantiationException e) { |
137 |
// TODO Auto-generated catch block
|
138 |
fail("Instantiating class, cannot test a non-instantiable symbol");
|
139 |
} catch (IllegalAccessException e) { |
140 |
// TODO Auto-generated catch block
|
141 |
fail("Class not instantiable");
|
142 |
} catch (ClassCastException ccEx) { |
143 |
fail("Cannot test a non symbol class");
|
144 |
} |
145 |
getClassesToTest().add(legendClass); |
146 |
} |
147 |
|
148 |
/**
|
149 |
* The main purpose of this method is to create new legend instances to be used for
|
150 |
* other test methods.The new instances will be filled for the fillthelegend method.
|
151 |
* @return
|
152 |
* @throws FieldNotFoundException
|
153 |
*/
|
154 |
public static ILegend[] getNewLegendInstances() throws FieldNotFoundException{ |
155 |
ILegend[] legends = new ILegend[getClassesToTest().size()]; |
156 |
for (int i = 0; i < legends.length; i++) { |
157 |
|
158 |
try {
|
159 |
legends[i] = (ILegend) ((Class) getClassesToTest().get(i)).newInstance();
|
160 |
fillthelegend(legends[i]); |
161 |
|
162 |
} catch (InstantiationException e) { |
163 |
fail("Instantiating class");
|
164 |
} catch (IllegalAccessException e) { |
165 |
fail("Class not instantiable");
|
166 |
} |
167 |
|
168 |
} |
169 |
return legends;
|
170 |
} |
171 |
|
172 |
|
173 |
protected void setUp() throws Exception { |
174 |
legends = getNewLegendInstances(); |
175 |
} |
176 |
|
177 |
/**
|
178 |
* this test ensures that the legend is self-defining. Checks that
|
179 |
* the symbols contained by it can be replicated, and the rules for
|
180 |
* such symbols as well.
|
181 |
* @throws XMLException
|
182 |
*/
|
183 |
public void testILegendSelfDefinition() throws XMLException{ |
184 |
for (int i = 0; i < legends.length; i++) { |
185 |
final ILegend theLegend = legends[i];
|
186 |
final ILegend cloneLegend =theLegend.cloneLegend();
|
187 |
assertTrue(theLegend.getClassName()+ " wrong class name declaration in getXMLEntity() ",
|
188 |
cloneLegend.getClass().equals(theLegend.getClass())); |
189 |
final Field[] theLegendFields = theLegend.getClass().getFields(); |
190 |
for (int j = 0; j < theLegendFields.length; j++) { |
191 |
final Field fi = theLegendFields[j]; |
192 |
final boolean wasAccessible = fi.isAccessible(); |
193 |
fi.setAccessible(true);
|
194 |
|
195 |
try {
|
196 |
assertTrue(theLegend.getClassName() + " fails or misses clonning the field " +fi.getName(),
|
197 |
fi.get(theLegend).equals(fi.get(cloneLegend))); |
198 |
} catch (IllegalArgumentException e) { |
199 |
fail(); |
200 |
} catch (IllegalAccessException e) { |
201 |
fail(); |
202 |
} |
203 |
fi.setAccessible(wasAccessible); |
204 |
} |
205 |
} |
206 |
} |
207 |
|
208 |
/**
|
209 |
* this test ensures that any legend always have a symbol ready to be used.
|
210 |
* an empty legend is incorrect.
|
211 |
*
|
212 |
*/
|
213 |
public void testSymbolAvailability() { |
214 |
for (int i = 0; i < legends.length; i++) { |
215 |
assertNotNull("Legend no. "+i+" '"+legends[i].getClassName()+" does not have a symbol ready to be used", legends[i].getDefaultSymbol()); |
216 |
} |
217 |
|
218 |
for (int i = 0; i < legends.length; i++) { |
219 |
|
220 |
if (legends[i] instanceof IVectorLegend) { |
221 |
IVectorLegend vectLegend = (IVectorLegend) legends[i]; |
222 |
try {
|
223 |
vectLegend.setDefaultSymbol(null);
|
224 |
fail("setDefaultSymbol(ISymbol) should not accept null values");
|
225 |
} catch (NullPointerException e) { |
226 |
// correct
|
227 |
} |
228 |
} |
229 |
|
230 |
} |
231 |
} |
232 |
|
233 |
|
234 |
} |