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