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