Statistics
| Revision:

root / trunk / libraries / libFMap / src-test / com / iver / cit / gvsig / fmap / core / rendering / TestIntervalLegend.java @ 21534

History | View | Annotate | Download (8.39 KB)

1
 /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.fmap.core.rendering;
42

    
43
import java.awt.Dimension;
44
import java.awt.Rectangle;
45
import java.util.ArrayList;
46
import java.util.Hashtable;
47

    
48
import junit.framework.TestCase;
49

    
50
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
51
import com.hardcode.gdbms.engine.values.Value;
52
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
53
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
54
import com.iver.cit.gvsig.fmap.core.IFeature;
55
import com.iver.cit.gvsig.fmap.core.IGeometry;
56
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
57
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
58
import com.iver.cit.gvsig.fmap.core.symbols.TestISymbol;
59
import com.iver.cit.gvsig.fmap.rendering.AbstractIntervalLegend;
60
import com.iver.cit.gvsig.fmap.rendering.FInterval;
61
import com.iver.cit.gvsig.fmap.rendering.IClassifiedLegend;
62
import com.iver.cit.gvsig.fmap.rendering.IClassifiedVectorLegend;
63
import com.iver.cit.gvsig.fmap.rendering.ILegend;
64
import com.iver.cit.gvsig.fmap.rendering.IVectorialIntervalLegend;
65

    
66
/**
67
 * Integration test to ensure that the legends which implements the
68
 * IVectorialIntervalLegend interface (and extend AbstractIntervalLegend)
69
 * follow the rules that manage them by the application.
70
 *
71
 * @author pepe vidal salvador - jose.vidal.salvador@iver.es
72
 */
73
public class TestIntervalLegend extends TestCase {
74
        private AbstractIntervalLegend[] legends;
75

    
76
        protected void setUp() throws Exception {
77
                super.setUp();
78
                ILegend[] allLegends = TestILegend.getNewLegendInstances();
79
                // Filter the marker ones
80
                ArrayList<AbstractIntervalLegend> legends = new ArrayList<AbstractIntervalLegend>();
81

    
82
                for (int i = 0; i < allLegends.length; i++) {
83
                        if (allLegends[i] instanceof AbstractIntervalLegend) {
84
                                legends.add((AbstractIntervalLegend) allLegends[i]);
85

    
86
                        }
87
                }
88
                this.legends = (AbstractIntervalLegend[]) legends.
89
                        toArray(new AbstractIntervalLegend[legends.size()]);
90
        }
91

    
92

    
93
        public static final FInterval interval0=new FInterval(0,2);
94
        public static final FInterval interval1=new FInterval(3,5);
95
        public static final FInterval interval2=new FInterval(6,8);
96
        public static final FInterval interval3=new FInterval(9,11);
97
        private static final int FIELDID = 0;
98

    
99
        private Hashtable<FInterval, ISymbol> symTable;
100

    
101
        private ISymbol[] symbols = TestISymbol.getNewSymbolInstances();
102
        private FInterval[] sampleIntervals = new FInterval[] { interval0, interval1, interval2, interval3, };
103
        private IFeature[] features = AbstractVectorLegendTestCase.getFeatures();
104

    
105
        
106

    
107
        public void testGetSymbolByFeature() {
108
                ISymbol tableSym =null;
109

    
110
                // fills the legends
111
                for (int i = 0; i < legends.length; i++) {
112
                        fillClassifiedIntervalLegend(legends[i], sampleIntervals);
113
                }
114

    
115
                for (int i = 0; i < legends.length; i++) {
116
                        // For each feature
117
                        for (int j = 0; j < features.length; j++) {
118
                                IFeature myFeature = features[i];
119
                                // takes the value of the field that identifies the feature
120
                                Value val = myFeature.getAttributes()[FIELDID];
121
                                // the last value is used to access to the hash table to obtain
122
                                // a symbol
123

    
124
                                if(interval0.isInInterval(val))
125
                                        tableSym = symTable.get(interval0);
126
                                else if(interval1.isInInterval(val))
127
                                        tableSym = symTable.get(interval1);
128
                                else if(interval2.isInInterval(val))
129
                                        tableSym = symTable.get(interval2);
130
                                else if(interval3.isInInterval(val))
131
                                        tableSym = symTable.get(interval3);
132

    
133
                                // takes the symbol from a legend using the feature
134
                                ISymbol legendSym = legends[i].getSymbolByFeature(myFeature);
135
                                // compares that both symbols are the same
136
                                assertEquals(legendSym.getClassName()
137
                                                + " fails with the comparation of the class symbols",
138
                                                legendSym, tableSym);
139
                        }
140
                }
141
        }
142

    
143
        /**
144
         * This method is used to add symbols to a legend.That is, it takes an array
145
         * of AbstractIntervalLegend which is empty and, using a second array
146
         * of FIntervals(values), the first one is filled.Also, a hash table is filled
147
         * using the array of FIntervals (it will be useful in some tests to check
148
         * that a symbol can be taken using a feature) .
149
         *
150
         * @param legend
151
         * @return
152
         */
153
        private void fillClassifiedIntervalLegend(AbstractIntervalLegend legend,
154
                        FInterval[] values) {
155
                // initialize the hash table
156
                symTable = new Hashtable();
157

    
158
                // to add symbols to the legend and the hash table
159
                for (int j = 0; j < values.length; j++) {
160

    
161
                        ISymbol sym = symbols[j % symbols.length];
162
                        legend.addSymbol(values[j], sym);
163
                        symTable.put(values[j], sym);
164
                }
165
        }
166

    
167
//         public IInterval getInterval(Value v) ;
168
//            public int getIntervalType();
169
//           
170
//           
171
//                /**
172
//                 * 
173
//                 * Returns the symbol starting from an interval
174
//                 *
175
//                 * @param key interval.
176
//                 *
177
//                 * @return symbol.
178
//                 */
179
//            public ISymbol getSymbolByInterval(IInterval key);
180
//
181
//            /**
182
//             * Inserts the type of the classification of the intervals.
183
//                 *
184
//                 * @param tipoClasificacion type of the classification.
185
//                 */
186
//            public void setIntervalType(int tipoClasificacion);
187
        /**
188
         * This test ensures that when a legend is filled, the number of symbols
189
         * added is correct. To do it, is checked that the number of symbols of a
190
         * legend is the same as the length of the array of example values that we
191
         * have.
192
         *
193
         * @throws ReadDriverException
194
         */
195
        public void testICLAdittion() throws ReadDriverException {
196

    
197
                // Fills the legend
198
                for (int i = 0; i < legends.length; i++) {
199
                        fillClassifiedIntervalLegend(legends[i], sampleIntervals);
200
                }
201

    
202
                for (int i = 0; i < legends.length; i++)
203
                        assertEquals(legends[i].getClassName()
204
                                        + " fails with the comparation of the number of symbols",
205
                                        legends[i].getSymbols().length,
206
                                        sampleIntervals.length);
207

    
208
        }
209

    
210
        /**
211
         * This test ensures that the symbols that we have previously added to a
212
         * legend are accessible using its features.To do it, this test compares the
213
         * symbol taken from the legend with the symbol taken from the hashTable
214
         * (using the same feature).
215
         *
216
         * @throws ReadDriverException
217
         */
218

    
219
        public void testICLCheckValueSymbols() throws ReadDriverException {
220
                  ISymbol tableSym =null;
221

    
222
                  // fills the legends
223
                  for (int i = 0; i < legends.length; i++) {
224
                   fillClassifiedIntervalLegend(legends[i], sampleIntervals);
225
                  }
226

    
227
                  for (int i = 0; i < legends.length; i++) {
228
                   // For each feature
229
                   for (int j = 0; j < features.length; j++) {
230
                    IFeature myFeature = features[i];
231
                    // takes the value of the field that identifies the feature
232
                    Value val = myFeature.getAttributes()[FIELDID];
233
                    // the last value is used to access to the hash table to obtain
234
                    // a symbol
235

    
236
                    if(interval0.isInInterval(val))
237
                     tableSym = (ISymbol) symTable.get(interval0);
238
                    else if(interval1.isInInterval(val))
239
                     tableSym = (ISymbol) symTable.get(interval1);
240
                    else if(interval2.isInInterval(val))
241
                     tableSym = (ISymbol) symTable.get(interval2);
242
                    else if(interval3.isInInterval(val))
243
                     tableSym = (ISymbol) symTable.get(interval3);
244

    
245
                    AbstractIntervalLegend leg = (AbstractIntervalLegend) legends[i];
246
                    // takes the symbol from a legend using the feature
247
                    ISymbol legendSym = leg.getSymbolByFeature(myFeature);
248
                    // compares that both symbols are the same
249
                    assertEquals(legendSym.getClassName()
250
                      + " fails with the comparation of the class symbols",
251
                      legendSym, tableSym);
252
                   }
253
                  }
254
                
255
        }
256

    
257
}
258

    
259