Statistics
| Revision:

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 @ 42928

History | View | Annotate | Download (9.63 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.Geometry.TYPES;
35
import org.gvsig.fmap.geom.GeometryLocator;
36
import org.gvsig.fmap.geom.GeometryManager;
37
import org.gvsig.fmap.geom.primitive.Curve;
38
import org.gvsig.fmap.geom.primitive.GeneralPathX;
39
import org.gvsig.fmap.mapcontext.rendering.legend.IClassifiedVectorLegend;
40
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
41
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
42
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.impl.AbstractIntervalLegend;
43
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.impl.FInterval;
44
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.TestISymbol;
45
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
46

    
47

    
48
/**
49
 * Integration test to ensure that the legends which implements the
50
 * IVectorialIntervalLegend interface follow the rules that follow the managing
51
 * of them by the application.
52
 *
53
 * @author pepe vidal salvador - jose.vidal.salvador@iver.es
54
 */
55
public class TestAbstractIntervalLegend extends AbstractLibraryAutoInitTestCase {
56

    
57
        private static final Integer v0 = new Integer(0);
58
        private static final Integer v1 = new Integer(1);
59
        private static final Integer v2 = new Integer(2);
60
        private static final Integer v3 = new Integer(3);
61

    
62
        private static final String FIELD3 = "field3";
63
        private static final String FIELD2 = "field2";
64
        private static final String FIELD1 = "field1";
65
        private static final String FIELD0 = "field0";
66
        private static final int FIELDID = 0;
67

    
68
        private static final Integer[] feature0Values = new Integer[] { v0, v1, v2, v3, };
69
        private static final Integer[] feature1Values = new Integer[] { v3, v0, v1, v2, };
70
        private static final Integer[] feature2Values = new Integer[] { v2, v3, v0, v1, };
71
        private static final Integer[] feature3Values = new Integer[] { v1, v2, v3, v0, };
72

    
73
        private static final Integer[][] featureValues = new Integer[][] {
74
                feature0Values,
75
                feature1Values,
76
                feature2Values,
77
                feature3Values,};
78

    
79
        private static String[] fieldNames = new String[] {FIELD0,FIELD1,FIELD2,FIELD3,};
80

    
81

    
82
        // private static final Value v4 = (Value)ValueFactory.createValue(4);
83
        // private static final Value v5 = (Value)ValueFactory.createValue(5);
84
        // private static final Value v6 = (Value)ValueFactory.createValue(6);
85
        // private static final Value v7 = (Value)ValueFactory.createValue(7);
86
        // private static final Value v8 = (Value)ValueFactory.createValue(8);
87
        // private static final Value v9 = (Value)ValueFactory.createValue(9);
88

    
89
        private AbstractIntervalLegend[] intervalLegends;
90
        private DummyFetureStore mockDataSource = new DummyFetureStore();
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,8);
95
         private static final FInterval interval3=new FInterval(9,11);
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 ISymbol[] symbols;
106
        private FInterval[] sampleIntervals = new FInterval[] { interval0, interval1, interval2, interval3, };
107
        private Feature[] features;
108

    
109
        // private FInterval[] intervals = new FInterval[] {
110
        // interval0,
111
        // interval1,
112
        // interval2,
113
        // interval3,
114
        // interval4,
115
        // interval5,
116
        // interval6,
117
        // interval7,
118
        // interval8,
119
        // interval9,
120
        // };
121

    
122
        protected void doSetUp() throws Exception {
123
                features = new Feature[4];
124

    
125
                // initialize test values
126
                for (int i = 0; i < features.length; i++) {
127

    
128
                        // create the geometry associated to the feature
129
                        int size = 200;
130
                        Dimension d = new Dimension(size, size);
131
                        Rectangle aShape = new Rectangle(i * size, i * size, d.width,
132
                                        d.height);
133
                        GeometryManager geomManager = GeometryLocator.getGeometryManager();
134
                        Curve curve = (Curve)geomManager.create(TYPES.CURVE, SUBTYPES.GEOM2D);
135
                        curve.setGeneralPath(new GeneralPathX(aShape.getPathIterator(null)));
136

    
137
                        /*
138
                         * create a full-featured Feature with randomed values at its fields
139
                         * to avoid testing over the same values each time
140
                         */
141
                        //TODO
142
//                        features[i] = new MemoryFeature(geom, featureValues[i], "[" + i
143
//                                        + "]");
144
                }
145

    
146
                // initialize the symbol subset for this test
147
                TestISymbol.addSymbols();
148
                symbols = TestISymbol.getNewSymbolInstances();
149

    
150
                // initialize the legends for this test
151
                ILegend[] allLegends = TestILegend.getNewLegendInstances();
152
                ArrayList intervalLegends = new ArrayList();
153
                for (int i = 0; i < allLegends.length; i++) {
154

    
155
                        if (allLegends[i] instanceof AbstractIntervalLegend) {
156
                                intervalLegends.add(allLegends[i]);
157
                        }
158

    
159
                        if (allLegends[i] instanceof IClassifiedVectorLegend) {
160
                                IClassifiedVectorLegend cvl = (IClassifiedVectorLegend) allLegends[i];
161
                                cvl.setClassifyingFieldNames(new String[] { fieldNames[FIELDID] });
162
//                                cvl.setFeatureStore(mockDataSource);
163

    
164
                        }
165
                }
166

    
167
                this.intervalLegends = (AbstractIntervalLegend[]) intervalLegends
168
                                .toArray(new AbstractIntervalLegend[intervalLegends.size()]);
169
        }
170

    
171
        /**
172
         * This method is used to add symbols to a legend.That is, it takes an array
173
         * of AbstractIntervalLegend which is empty and, using a second array
174
         * of FIntervals(values), the first one is filled.Also, a hash table is filled
175
         * using the array of FIntervals (it will be useful in some tests to check
176
         * that a symbol can be taken using a feature) .
177
         *
178
         * @param legend
179
         * @return
180
         */
181
        private void fillClassifiedLegend(AbstractIntervalLegend legend,
182
                        FInterval[] values) {
183
                // initialize the hash table
184
                symTable = new Hashtable();
185

    
186
                // to add symbols to the legend and the hash table
187
                for (int j = 0; j < values.length; j++) {
188

    
189
                        ISymbol sym = symbols[j % symbols.length];
190
                        legend.addSymbol(values[j], sym);
191
                        symTable.put(values[j], sym);
192
                }
193
        }
194

    
195
        /**
196
         * This test ensures that when a legend is filled, the number of symbols
197
         * added is correct. To do it, is checked that the number of symbols of a
198
         * legend is the same as the length of the array of example values that we
199
         * have.
200
         *
201
         * @throws ReadDriverException
202
         */
203
        public void testICLAdittion() throws ReadException {
204

    
205
                // Fills the legend
206
                for (int i = 0; i < intervalLegends.length; i++) {
207
                        fillClassifiedLegend(intervalLegends[i], sampleIntervals);
208
                }
209

    
210
                for (int i = 0; i < intervalLegends.length; i++) {
211
                        assertEquals(intervalLegends[i].getClass().getName()
212
                                        + " fails with the comparation of the number of symbols",
213
                                        intervalLegends[i].getSymbols().length,
214
                                        sampleIntervals.length);
215
                }
216

    
217
        }
218

    
219
        /**
220
         * This test ensures that the symbols that we have previously added to a
221
         * legend are accessible using its features.To do it, this test compares the
222
         * symbol taken from the legend with the symbol taken from the hashTable
223
         * (using the same feature).
224
         * @throws Exception
225
         */
226
        public void testICLCheckValueSymbols() throws Exception {
227

    
228
                ISymbol tableSym =null;
229

    
230
                // fills the legends
231
                for (int i = 0; i < intervalLegends.length; i++) {
232
                        fillClassifiedLegend(intervalLegends[i], sampleIntervals);
233
                }
234

    
235
                for (int i = 0; i < intervalLegends.length; i++) {
236
                        // For each feature
237
                        for (int j = 0; j < features.length; j++) {
238
                                Feature myFeature = features[i];
239
                                // takes the value of the field that identifies the feature
240
                                Object val = myFeature.get(FIELDID);
241
                                // the last value is used to access to the hash table to obtain
242
                                // a symbol
243

    
244
                                if(interval0.isInInterval(val)) {
245
                                        tableSym = (ISymbol) symTable.get(interval0);
246
                                } else if(interval1.isInInterval(val)) {
247
                                        tableSym = (ISymbol) symTable.get(interval1);
248
                                } else if(interval2.isInInterval(val)) {
249
                                        tableSym = (ISymbol) symTable.get(interval2);
250
                                } else if(interval3.isInInterval(val)) {
251
                                        tableSym = (ISymbol) symTable.get(interval3);
252
                                }
253

    
254
                                AbstractIntervalLegend leg = intervalLegends[i];
255
                                // takes the symbol from a legend using the feature
256
                                ISymbol legendSym = leg.getSymbolByFeature(myFeature);
257
                                // compares that both symbols are the same
258
                                assertEquals(legendSym.getClass().getName()
259
                                                + " fails with the comparation of the class symbols",
260
                                                legendSym, tableSym);
261
                        }
262
                }
263
        }
264

    
265
}
266

    
267