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 / TestIClassifiedLegend.java @ 43510
History | View | Annotate | Download (9.62 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.Dimension; |
27 |
import java.awt.Rectangle; |
28 |
import java.util.ArrayList; |
29 |
import java.util.Hashtable; |
30 |
import org.gvsig.fmap.dal.exception.ReadException; |
31 |
import org.gvsig.fmap.dal.feature.Feature; |
32 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
33 |
import org.gvsig.fmap.geom.GeometryLocator; |
34 |
import org.gvsig.fmap.geom.GeometryManager; |
35 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
36 |
import org.gvsig.fmap.geom.primitive.Line; |
37 |
import org.gvsig.fmap.mapcontext.rendering.legend.IClassifiedLegend; |
38 |
import org.gvsig.fmap.mapcontext.rendering.legend.IClassifiedVectorLegend; |
39 |
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend; |
40 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
41 |
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.impl.AbstractIntervalLegend; |
42 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.TestISymbol; |
43 |
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase; |
44 |
|
45 |
|
46 |
/**
|
47 |
* Integration test to ensure that the legends which implements the
|
48 |
* IClassifiedLegend interface follow the rules that follow the managing of them
|
49 |
* by the application.
|
50 |
*
|
51 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
52 |
* @author pepe vidal salvador - jose.vidal.salvador@iver.es
|
53 |
*/
|
54 |
public class TestIClassifiedLegend extends AbstractLibraryAutoInitTestCase { |
55 |
|
56 |
private static final Integer v0 = new Integer(0); |
57 |
private static final Integer v1 = new Integer(1); |
58 |
private static final Integer v2 = new Integer(2); |
59 |
private static final Integer v3 = new Integer(3); |
60 |
|
61 |
private static final String FIELD3 = "field3"; |
62 |
private static final String FIELD2 = "field2"; |
63 |
private static final String FIELD1 = "field1"; |
64 |
private static final String FIELD0 = "field0"; |
65 |
private static final int FIELDID = 0; |
66 |
|
67 |
// private static final Integer[] feature0Values = new Integer[] { v0, v1, v2, v3, };
|
68 |
// private static final Integer[] feature1Values = new Integer[] { v3, v0, v1, v2, };
|
69 |
// private static final Integer[] feature2Values = new Integer[] { v2, v3, v0, v1, };
|
70 |
// private static final Integer[] feature3Values = new Integer[] { v1, v2, v3, v0, };
|
71 |
//
|
72 |
// private static final Integer[][] featureValues = new Integer[][] {
|
73 |
// feature0Values,
|
74 |
// feature1Values,
|
75 |
// feature2Values,
|
76 |
// feature3Values,};
|
77 |
|
78 |
private static String[] fieldNames = new String[] {FIELD0,FIELD1,FIELD2,FIELD3,}; |
79 |
|
80 |
|
81 |
// private static final Value v4 = (Value)ValueFactory.createValue(4);
|
82 |
// private static final Value v5 = (Value)ValueFactory.createValue(5);
|
83 |
// private static final Value v6 = (Value)ValueFactory.createValue(6);
|
84 |
// private static final Value v7 = (Value)ValueFactory.createValue(7);
|
85 |
// private static final Value v8 = (Value)ValueFactory.createValue(8);
|
86 |
// private static final Value v9 = (Value)ValueFactory.createValue(9);
|
87 |
|
88 |
private AbstractIntervalLegend[] intervalLegends; |
89 |
//private DummyFetureStore mockDataSource = new DummyFetureStore();
|
90 |
|
91 |
|
92 |
//private static final FInterval interval0=new FInterval(0,2);
|
93 |
//private static final FInterval interval1=new FInterval(3,5);
|
94 |
//private static final FInterval interval2=new FInterval(6,2);
|
95 |
//private static final FInterval interval3=new FInterval(9,2);
|
96 |
// private static final Value interval4;
|
97 |
// private static final Value interval5;
|
98 |
// private static final Value interval6;
|
99 |
// private static final Value interval7;
|
100 |
// private static final Value interval8;
|
101 |
// private static final Value interval9;
|
102 |
|
103 |
Hashtable symTable;
|
104 |
|
105 |
private IClassifiedVectorLegend[] classifiedLegends; |
106 |
private ISymbol[] symbols; |
107 |
private Object[] sampleValues = new Object[] { v0, v1, v2, v3, }; |
108 |
private Feature[] features; |
109 |
|
110 |
// private FInterval[] intervals = new FInterval[] {
|
111 |
// interval0,
|
112 |
// interval1,
|
113 |
// interval2,
|
114 |
// interval3,
|
115 |
// interval4,
|
116 |
// interval5,
|
117 |
// interval6,
|
118 |
// interval7,
|
119 |
// interval8,
|
120 |
// interval9,
|
121 |
// };
|
122 |
|
123 |
protected void doSetUp() throws Exception { |
124 |
|
125 |
features = new Feature[4]; |
126 |
|
127 |
// //Initializes the geometries library
|
128 |
// DefaultGeometryLibrary lib = new DefaultGeometryLibrary();
|
129 |
// lib.initialize();
|
130 |
// lib.postInitialize();
|
131 |
|
132 |
// initialize test values
|
133 |
for (int i = 0; i < features.length; i++) { |
134 |
// create the geometry associated to the feature
|
135 |
int size = 200; |
136 |
Dimension d = new Dimension(size, size); |
137 |
Rectangle aShape = new Rectangle(i * size, i * size, d.width, |
138 |
d.height); |
139 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
140 |
Line line = geomManager.createLine(SUBTYPES.GEOM2D);
|
141 |
line.setGeneralPath(new GeneralPathX(aShape.getPathIterator(null))); |
142 |
|
143 |
/*
|
144 |
* create a full-featured Feature with randomed values at its fields
|
145 |
* to avoid testing over the same values each time
|
146 |
*/
|
147 |
// features[i] = new DefaultFeature(geom, featureValues[i], "[" + i
|
148 |
// + "]");
|
149 |
} |
150 |
|
151 |
// initialize the symbol subset for this test
|
152 |
symbols = TestISymbol.getNewSymbolInstances(); |
153 |
|
154 |
// initialize the legends for this test
|
155 |
ILegend[] allLegends = TestILegend.getNewLegendInstances();
|
156 |
ArrayList clegends = new ArrayList(); |
157 |
ArrayList intervalLegends = new ArrayList(); |
158 |
for (int i = 0; i < allLegends.length; i++) { |
159 |
if (allLegends[i] instanceof AbstractIntervalLegend) { |
160 |
intervalLegends.add(allLegends[i]); |
161 |
} else if (allLegends[i] instanceof IClassifiedLegend) { |
162 |
clegends.add(allLegends[i]); |
163 |
} |
164 |
|
165 |
if (allLegends[i] instanceof IClassifiedVectorLegend) { |
166 |
IClassifiedVectorLegend cvl = (IClassifiedVectorLegend) allLegends[i]; |
167 |
cvl.setClassifyingFieldNames(new String[] { fieldNames[FIELDID] }); |
168 |
// cvl.setFeatureStore(mockDataSource);
|
169 |
|
170 |
} |
171 |
} |
172 |
|
173 |
this.classifiedLegends = (IClassifiedVectorLegend[]) clegends |
174 |
.toArray(new IClassifiedVectorLegend[clegends.size()]);
|
175 |
this.intervalLegends =
|
176 |
(AbstractIntervalLegend[]) intervalLegends
|
177 |
.toArray(new AbstractIntervalLegend[intervalLegends.size()]);
|
178 |
} |
179 |
|
180 |
/**
|
181 |
* This method is used to add symbols to a legend.That is, it takes an array
|
182 |
* of IClassifiedVectorialLegend which is empty andm, using a second array
|
183 |
* of objects (values), the first one is filled.Also, a hash table is filled
|
184 |
* too using the array of objects (it will be useful in some tests to check
|
185 |
* that a symbol can be taken using a feature) .
|
186 |
*
|
187 |
* @param legend
|
188 |
* @return
|
189 |
*/
|
190 |
private void fillClassifiedLegend(IClassifiedVectorLegend legend, |
191 |
Object[] values) { |
192 |
// initialize the hash table
|
193 |
symTable = new Hashtable(); |
194 |
|
195 |
// to add symbols to the legend and the hash table
|
196 |
for (int j = 0; j < values.length; j++) { |
197 |
|
198 |
ISymbol sym = symbols[j % symbols.length]; |
199 |
legend.addSymbol(values[j], sym); |
200 |
symTable.put(values[j], sym); |
201 |
} |
202 |
} |
203 |
|
204 |
/**
|
205 |
* This test ensures that when a legend is filled, the number of symbols
|
206 |
* added is correct. To do it, is checked that the number of symbols of a
|
207 |
* legend is the same as the length of the array of example values that we
|
208 |
* have.
|
209 |
*
|
210 |
* @throws ReadDriverException
|
211 |
*/
|
212 |
public void testICLAdittion() throws ReadException { |
213 |
|
214 |
// Fills the legend
|
215 |
for (int i = 0; i < classifiedLegends.length; i++) { |
216 |
fillClassifiedLegend(classifiedLegends[i], sampleValues); |
217 |
} |
218 |
|
219 |
for (int i = 0; i < classifiedLegends.length; i++) { |
220 |
assertEquals(classifiedLegends[i].getClass().getName() |
221 |
+ " fails with the comparation of the number of symbols",
|
222 |
classifiedLegends[i].getSymbols().length, |
223 |
sampleValues.length); |
224 |
} |
225 |
|
226 |
} |
227 |
|
228 |
/**
|
229 |
* This test ensures that the symbols that we have previously added to a
|
230 |
* legend are accessible using its features.To do it, this test compares the
|
231 |
* symbol taken from the legend with the symbol taken from the hashTable
|
232 |
* (using the same feature).
|
233 |
* @throws Exception
|
234 |
*/
|
235 |
public void testICLCheckValueSymbols() throws Exception { |
236 |
// fills the legends
|
237 |
for (int i = 0; i < classifiedLegends.length; i++) { |
238 |
fillClassifiedLegend(classifiedLegends[i], sampleValues); |
239 |
} |
240 |
|
241 |
for (int i = 0; i < classifiedLegends.length; i++) { |
242 |
// For each feature
|
243 |
for (int j = 0; j < features.length; j++) { |
244 |
Feature myFeature = features[i]; |
245 |
// takes the value of the field that identifies the feature
|
246 |
Object val = myFeature.get(FIELDID);
|
247 |
// the last value is used to access to the hash table to obtain
|
248 |
// a symbol
|
249 |
ISymbol tableSym = (ISymbol) symTable.get(val); |
250 |
|
251 |
IClassifiedVectorLegend leg = classifiedLegends[i]; |
252 |
// takes the symbol from a legend using the feature
|
253 |
ISymbol legendSym = leg.getSymbolByFeature(myFeature); |
254 |
// compares that both symbols are the same
|
255 |
assertEquals(legendSym.getClass().getName() |
256 |
+ " fails with the comparation of the class symbols",
|
257 |
legendSym, tableSym); |
258 |
} |
259 |
} |
260 |
} |
261 |
|
262 |
} |