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