Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap / src-test / org / gvsig / fmap / mapcontext / rendering / legend / TestILegend.java @ 20984

History | View | Annotate | Download (7.68 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 org.gvsig.fmap.mapcontext.rendering.legend;
42

    
43
import java.awt.Color;
44
import java.lang.reflect.Field;
45
import java.util.ArrayList;
46

    
47
import org.gvsig.fmap.mapcontext.layers.XMLException;
48
import org.gvsig.fmap.mapcontext.rendering.legend.IClassifiedVectorLegend;
49
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
50
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend;
51
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialIntervalLegend;
52
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialUniqueValueLegend;
53
import org.gvsig.fmap.mapcontext.rendering.legend.SingleSymbolLegend;
54
import org.gvsig.fmap.mapcontext.rendering.legend.VectorialIntervalLegend;
55
import org.gvsig.fmap.mapcontext.rendering.legend.VectorialUniqueValueLegend;
56
import org.gvsig.fmap.mapcontext.rendering.symbols.SimpleFillSymbol;
57

    
58
import junit.framework.TestCase;
59

    
60
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
61

    
62
/**
63
 * Integration test to ensure that the legends follow the rules that follow the
64
 * managing of them by the application.
65
 *
66
 * @author jaume dominguez faus - jaume.dominguez@iver.es
67
 */
68
public class TestILegend extends TestCase {
69
         private static ArrayList classesToTest;
70
         transient private ILegend[] legends;
71

    
72
         private static ArrayList getClassesToTest() {
73
                if (classesToTest == null) {
74
                    classesToTest = new ArrayList();
75

    
76
                  TestILegend.addLegendToTest(VectorialIntervalLegend.class);
77
                  TestILegend.addLegendToTest(VectorialUniqueValueLegend.class);
78
                  TestILegend.addLegendToTest(SingleSymbolLegend.class);
79

    
80
//                  TestILegend.addLegendToTest(QuantityByCategoryLegend.class);
81
//                  TestILegend.addLegendToTest(DotDensityLegend.class);
82
//                  TestILegend.addLegendToTest(ProportionalSymbolsLegend.class);
83
//                  TestILegend.addLegendToTest(GraduatedSymbolLegend.class);
84

    
85

    
86
                }
87

    
88
                return classesToTest;
89
            }
90

    
91
         /**
92
          * This method fills the attributes of a legend.The purpose is to ensure that the new
93
          * legend is 'full' and ready to be used for a test.
94
          * @param le
95
          * @return
96
          */
97

    
98
         private static ILegend fillthelegend(ILegend le) {
99

    
100
                 SimpleFillSymbol mfs = new SimpleFillSymbol();
101
                 mfs.setFillColor(Color.ORANGE.darker());
102
                 mfs.setDescription("hola");
103
                 mfs.setIsShapeVisible(true);
104
                 String[] cad = new String[] {"Pepe","Juan"};
105

    
106
                 if(le instanceof IVectorLegend) {
107
                         IVectorLegend ssl=(IVectorLegend)le;
108
                         ssl.setDefaultSymbol(mfs);
109
                 }
110

    
111
                 if(le instanceof IVectorialUniqueValueLegend) {
112
                         IVectorialUniqueValueLegend ivuvl= (IVectorialUniqueValueLegend)le;
113
                         ivuvl.setClassifyingFieldNames(cad);
114
                         }
115

    
116
                 if(le instanceof IVectorialIntervalLegend) {
117
                         IVectorialIntervalLegend ivil=(IVectorialIntervalLegend)le;
118
                         ivil.setClassifyingFieldNames(cad);
119
                         }
120

    
121
                 if(le instanceof IClassifiedVectorLegend) {
122
                         IClassifiedVectorLegend icvl=(IClassifiedVectorLegend)le;
123
                         icvl.setClassifyingFieldNames(cad);
124
                 }
125

    
126

    
127

    
128
                 return le;
129
         }
130

    
131

    
132
         public static void addLegendToTest(Class legendClass) {
133
                try {
134
                    ILegend len = (ILegend) legendClass.newInstance();
135
                    fillthelegend(len);
136
                    len.getXMLEntity();
137

    
138
                } catch (InstantiationException e) {
139
                    // TODO Auto-generated catch block
140
                    fail("Instantiating class, cannot test a non-instantiable symbol");
141
                } catch (IllegalAccessException e) {
142
                    // TODO Auto-generated catch block
143
                    fail("Class not instantiable");
144
                } catch (ClassCastException ccEx) {
145
                    fail("Cannot test a non symbol class");
146
                }
147
                getClassesToTest().add(legendClass);
148
            }
149

    
150
         /**
151
          * The main purpose of this method is to create new legend instances to be used for
152
          * other test methods.The new instances will be filled for the fillthelegend method.
153
          * @return
154
          * @throws FieldNotFoundException
155
          */
156
         public static ILegend[] getNewLegendInstances() throws FieldNotFoundException{
157
                ILegend[] legends = new ILegend[getClassesToTest().size()];
158
                for (int i = 0; i < legends.length; i++) {
159

    
160
                    try {
161
                            legends[i] = (ILegend) ((Class) getClassesToTest().get(i)).newInstance();
162
                            fillthelegend(legends[i]);
163

    
164
                    } catch (InstantiationException e) {
165
                        fail("Instantiating class");
166
                    } catch (IllegalAccessException e) {
167
                        fail("Class not instantiable");
168
                    }
169

    
170
                }
171
                return legends;
172
            }
173

    
174

    
175
          protected void setUp() throws Exception {
176
              legends = getNewLegendInstances();
177
            }
178

    
179
        /**
180
         * this test ensures that the legend is self-defining. Checks that
181
         * the symbols contained by it can be replicated, and the rules for
182
         * such symbols as well.
183
         * @throws XMLException
184
         */
185
        public void testILegendSelfDefinition() throws XMLException{
186
                for (int i = 0; i < legends.length; i++) {
187
                        final ILegend theLegend = legends[i];
188
                        final ILegend cloneLegend =theLegend.cloneLegend();
189
                        assertTrue(theLegend.getClassName()+ " wrong class name declaration in getXMLEntity() ",
190
                                        cloneLegend.getClass().equals(theLegend.getClass()));
191
                final Field[] theLegendFields = theLegend.getClass().getFields();
192
            for (int j = 0; j < theLegendFields.length; j++) {
193
                final Field fi = theLegendFields[j];
194
                final boolean wasAccessible = fi.isAccessible();
195
                fi.setAccessible(true);
196

    
197
                try {
198
                    assertTrue(theLegend.getClassName() + " fails or misses clonning the field " +fi.getName(),
199
                            fi.get(theLegend).equals(fi.get(cloneLegend)));
200
                } catch (IllegalArgumentException e) {
201
                    fail();
202
                } catch (IllegalAccessException e) {
203
                    fail();
204
                }
205
                fi.setAccessible(wasAccessible);
206
            }
207
                        }
208
                }
209

    
210
        /**
211
         * this test ensures that any legend always have a symbol ready to be used.
212
         * an empty legend is incorrect.
213
         *
214
         */
215
        public void testSymbolAvailability() {
216
                for (int i = 0; i < legends.length; i++) {
217
                        assertNotNull("Legend no. "+i+" '"+legends[i].getClassName()+" does not have a symbol ready to be used", legends[i].getDefaultSymbol());
218
                }
219

    
220
                for (int i = 0; i < legends.length; i++) {
221

    
222
                        if (legends[i] instanceof IVectorLegend) {
223
                                IVectorLegend vectLegend = (IVectorLegend) legends[i];
224
                                try {
225
                                        vectLegend.setDefaultSymbol(null);
226
                                        fail("setDefaultSymbol(ISymbol) should not accept null values");
227
                                } catch (NullPointerException e) {
228
                                        // correct
229
                                }
230
                        }
231

    
232
                }
233
        }
234

    
235

    
236
}