Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap / src-test / org / gvsig / fmap / mapcontext / rendering / TestIClassifiedLegend.java @ 21156

History | View | Annotate | Download (19.3 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;
42

    
43
import java.awt.Dimension;
44
import java.awt.Rectangle;
45
import java.io.IOException;
46
import java.sql.Types;
47
import java.util.ArrayList;
48
import java.util.Hashtable;
49
import java.util.Iterator;
50
import java.util.List;
51

    
52
import org.gvsig.data.CloseException;
53
import org.gvsig.data.DataCollection;
54
import org.gvsig.data.DataException;
55
import org.gvsig.data.DataExplorer;
56
import org.gvsig.data.DataStoreParameters;
57
import org.gvsig.data.InitializeException;
58
import org.gvsig.data.OpenException;
59
import org.gvsig.data.ReadException;
60
import org.gvsig.data.WriteException;
61
import org.gvsig.data.commands.CommandsRecord;
62
import org.gvsig.data.vectorial.Feature;
63
import org.gvsig.data.vectorial.FeatureAttributeDescriptor;
64
import org.gvsig.data.vectorial.FeatureID;
65
import org.gvsig.data.vectorial.FeatureStore;
66
import org.gvsig.data.vectorial.FeatureType;
67
import org.gvsig.exceptions.BaseException;
68
import org.gvsig.fmap.geom.Geometry;
69
import org.gvsig.fmap.geom.GeometryFactory;
70
import org.gvsig.fmap.geom.GeometryManager;
71
import org.gvsig.fmap.geom.primitive.GeneralPathX;
72
import org.gvsig.fmap.mapcontext.rendering.legend.AbstractIntervalLegend;
73
import org.gvsig.fmap.mapcontext.rendering.legend.IClassifiedLegend;
74
import org.gvsig.fmap.mapcontext.rendering.legend.IClassifiedVectorLegend;
75
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
76
import org.gvsig.fmap.mapcontext.rendering.symbol.TestISymbol;
77
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
78
import org.gvsig.metadata.IMetadata;
79
import org.gvsig.util.observer.Observer;
80

    
81
import junit.framework.TestCase;
82

    
83

    
84
/**
85
 * Integration test to ensure that the legends which implements the
86
 * IClassifiedLegend interface follow the rules that follow the managing of them
87
 * by the application.
88
 *
89
 * @author jaume dominguez faus - jaume.dominguez@iver.es
90
 * @author pepe vidal salvador - jose.vidal.salvador@iver.es
91
 */
92
public class TestIClassifiedLegend extends TestCase {
93

    
94
        private static final Integer v0 = new Integer(0);
95
        private static final Integer v1 = new Integer(1);
96
        private static final Integer v2 = new Integer(2);
97
        private static final Integer v3 = new Integer(3);
98

    
99
        private static final String FIELD3 = "field3";
100
        private static final String FIELD2 = "field2";
101
        private static final String FIELD1 = "field1";
102
        private static final String FIELD0 = "field0";
103
        private static final int FIELDID = 0;
104

    
105
        private static final Integer[] feature0Values = new Integer[] { v0, v1, v2, v3, };
106
        private static final Integer[] feature1Values = new Integer[] { v3, v0, v1, v2, };
107
        private static final Integer[] feature2Values = new Integer[] { v2, v3, v0, v1, };
108
        private static final Integer[] feature3Values = new Integer[] { v1, v2, v3, v0, };
109

    
110
        private static final Integer[][] featureValues = new Integer[][] {
111
                feature0Values,
112
                feature1Values,
113
                feature2Values,
114
                feature3Values,};
115

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

    
118

    
119
        // private static final Value v4 = (Value)ValueFactory.createValue(4);
120
        // private static final Value v5 = (Value)ValueFactory.createValue(5);
121
        // private static final Value v6 = (Value)ValueFactory.createValue(6);
122
        // private static final Value v7 = (Value)ValueFactory.createValue(7);
123
        // private static final Value v8 = (Value)ValueFactory.createValue(8);
124
        // private static final Value v9 = (Value)ValueFactory.createValue(9);
125

    
126
        private AbstractIntervalLegend[] intervalLegends;
127
        private MockDataSource mockDataSource = new MockDataSource();
128

    
129
        /**
130
         * To avoid duplicated validation logic in the test a mock object is created
131
         * to use a DataSource for this test.
132
         *
133
         */
134
        private class MockDataSource implements FeatureStore {
135

    
136
                public DataCollection getDataCollection(FeatureType type, String filter, String order) throws ReadException {
137
                        // TODO Auto-generated method stub
138
                        return null;
139
                }
140

    
141
                public DataCollection getDataCollection(String[] fields, String filter, String order) throws ReadException {
142
                        // TODO Auto-generated method stub
143
                        return null;
144
                }
145

    
146
                public void getDataCollection(FeatureType type, String filter, String order, Observer observer) throws DataException {
147
                        // TODO Auto-generated method stub
148

    
149
                }
150

    
151
                public void getDataCollection(String[] fields, String filter, String order, Observer observer) throws DataException {
152
                        // TODO Auto-generated method stub
153

    
154
                }
155

    
156
                public void getDataCollection(Observer observer) {
157
                        // TODO Auto-generated method stub
158

    
159
                }
160

    
161
                public Feature getFeatureByID(FeatureID id) throws ReadException {
162
                        // TODO Auto-generated method stub
163
                        return null;
164
                }
165

    
166
                public Feature getFeatureByID(FeatureID id, FeatureType featureType) throws ReadException {
167
                        // TODO Auto-generated method stub
168
                        return null;
169
                }
170

    
171
                public FeatureType getDefaultFeatureType() {
172
                        // TODO Auto-generated method stub
173
                        return null;
174
                }
175

    
176
                public List getFeatureTypes() {
177
                        // TODO Auto-generated method stub
178
                        return null;
179
                }
180

    
181
                public Feature createFeature(FeatureType type, boolean defaultValues) throws InitializeException {
182
                        // TODO Auto-generated method stub
183
                        return null;
184
                }
185

    
186
                public Feature createDefaultFeature(boolean defaultValues) throws InitializeException {
187
                        // TODO Auto-generated method stub
188
                        return null;
189
                }
190

    
191
                public void update(Feature feature) throws DataException {
192
                        // TODO Auto-generated method stub
193

    
194
                }
195

    
196
                public void delete(Feature feature) throws DataException {
197
                        // TODO Auto-generated method stub
198

    
199
                }
200

    
201
                public void insert(Feature feature) throws DataException {
202
                        // TODO Auto-generated method stub
203

    
204
                }
205

    
206
                public void update(FeatureAttributeDescriptor attributeDescriptor) throws DataException {
207
                        // TODO Auto-generated method stub
208

    
209
                }
210

    
211
                public void delete(FeatureAttributeDescriptor attributeDescriptor) throws DataException {
212
                        // TODO Auto-generated method stub
213

    
214
                }
215

    
216
                public void insert(FeatureAttributeDescriptor attributeDescriptor) throws DataException {
217
                        // TODO Auto-generated method stub
218

    
219
                }
220

    
221
                public void disableNotifications() {
222
                        // TODO Auto-generated method stub
223

    
224
                }
225

    
226
                public void enableNotifications() {
227
                        // TODO Auto-generated method stub
228

    
229
                }
230

    
231
                public boolean isLocked(FeatureID id) {
232
                        // TODO Auto-generated method stub
233
                        return false;
234
                }
235

    
236
                public boolean lock(FeatureID id) {
237
                        // TODO Auto-generated method stub
238
                        return false;
239
                }
240

    
241
                public boolean isWithDefaultLegend() {
242
                        // TODO Auto-generated method stub
243
                        return false;
244
                }
245

    
246
                public Object getDefaultLegend() throws ReadException {
247
                        // TODO Auto-generated method stub
248
                        return null;
249
                }
250

    
251
                public Object getDefaultLabelingStrategy() {
252
                        // TODO Auto-generated method stub
253
                        return null;
254
                }
255

    
256
                public boolean canAlterFeatureType() {
257
                        // TODO Auto-generated method stub
258
                        return false;
259
                }
260

    
261
                public DataStoreParameters getParameters() {
262
                        // TODO Auto-generated method stub
263
                        return null;
264
                }
265

    
266
                public String getName() {
267
                        // TODO Auto-generated method stub
268
                        return null;
269
                }
270

    
271
                public void init(DataStoreParameters parameters) throws InitializeException {
272
                        // TODO Auto-generated method stub
273

    
274
                }
275

    
276
                public void open() throws OpenException {
277
                        // TODO Auto-generated method stub
278

    
279
                }
280

    
281
                public void refresh() throws OpenException, InitializeException {
282
                        // TODO Auto-generated method stub
283

    
284
                }
285

    
286
                public void close() throws CloseException {
287
                        // TODO Auto-generated method stub
288

    
289
                }
290

    
291
                public void dispose() throws CloseException {
292
                        // TODO Auto-generated method stub
293

    
294
                }
295

    
296
                public DataCollection getDataCollection() throws ReadException {
297
                        // TODO Auto-generated method stub
298
                        return null;
299
                }
300

    
301
                public DataCollection getSelection() {
302
                        // TODO Auto-generated method stub
303
                        return null;
304
                }
305

    
306
                public void setSelection(DataCollection selection) {
307
                        // TODO Auto-generated method stub
308

    
309
                }
310

    
311
                public DataCollection createSelection() {
312
                        // TODO Auto-generated method stub
313
                        return null;
314
                }
315

    
316
                public Iterator getChilds() {
317
                        // TODO Auto-generated method stub
318
                        return null;
319
                }
320

    
321
                public boolean isEditable() {
322
                        // TODO Auto-generated method stub
323
                        return false;
324
                }
325

    
326
                public void startEditing() throws ReadException {
327
                        // TODO Auto-generated method stub
328

    
329
                }
330

    
331
                public void cancelEditing() {
332
                        // TODO Auto-generated method stub
333

    
334
                }
335

    
336
                public void finishEditing() throws WriteException, ReadException {
337
                        // TODO Auto-generated method stub
338

    
339
                }
340

    
341
                public boolean isEditing() {
342
                        // TODO Auto-generated method stub
343
                        return false;
344
                }
345

    
346
                public void undo() {
347
                        // TODO Auto-generated method stub
348

    
349
                }
350

    
351
                public void redo() {
352
                        // TODO Auto-generated method stub
353

    
354
                }
355

    
356
                public CommandsRecord getCommandsRecord() {
357
                        // TODO Auto-generated method stub
358
                        return null;
359
                }
360

    
361
                public void beginComplexNotification() {
362
                        // TODO Auto-generated method stub
363

    
364
                }
365

    
366
                public void endComplexNotification() {
367
                        // TODO Auto-generated method stub
368

    
369
                }
370

    
371
                public DataCollection getLocked() {
372
                        // TODO Auto-generated method stub
373
                        return null;
374
                }
375

    
376
                public void setLocked(DataCollection locked) {
377
                        // TODO Auto-generated method stub
378

    
379
                }
380

    
381
                public DataCollection createLocked() {
382
                        // TODO Auto-generated method stub
383
                        return null;
384
                }
385

    
386
                public DataExplorer getExplorer() throws ReadException {
387
                        // TODO Auto-generated method stub
388
                        return null;
389
                }
390

    
391
                public void addObserver(Observer o) {
392
                        // TODO Auto-generated method stub
393

    
394
                }
395

    
396
                public void deleteObserver(Observer o) {
397
                        // TODO Auto-generated method stub
398

    
399
                }
400

    
401
                public void deleteObservers() {
402
                        // TODO Auto-generated method stub
403

    
404
                }
405

    
406
                public IMetadata getMetadata() throws BaseException {
407
                        // TODO Auto-generated method stub
408
                        return null;
409
                }
410
//                public void start() throws ReadDriverException                                                                 { }
411
//                public void stop() throws ReadDriverException                                                                 { }
412
//                public long[] getWhereFilter() throws IOException                                                         {return null;}
413
//                public DataSourceFactory getDataSourceFactory()                                                         {return null;}
414
//                public Memento getMemento() throws MementoException                                                 {return null;}
415
//                public void setDataSourceFactory(DataSourceFactory dsf)                                         { }
416
//                public void setSourceInfo(SourceInfo sourceInfo)                                                          { }
417
//                public SourceInfo getSourceInfo()                                                                                         {return null;}
418
//                public String getAsString() throws ReadDriverException                                                 {return null;}
419
//                public void remove() throws WriteDriverException                                                         { }
420
//                public int[] getPrimaryKeys() throws ReadDriverException                                         {return null;}
421
//                public ValueCollection getPKValue(long rowIndex)throws ReadDriverException         {return null;}
422
//                public String getPKName(int fieldId) throws ReadDriverException                         {return null;}
423
//                public String[] getPKNames() throws ReadDriverException                                         {return null;}
424
//                public int getPKType(int i) throws ReadDriverException                                                 {return 0;}
425
//                public int getPKCardinality() throws ReadDriverException                                         {return 0;}
426
//                public Value[] getRow(long rowIndex) throws ReadDriverException                         {return null;}
427
//                public DataWare getDataWare(int mode) throws ReadDriverException                         {return null;}
428
//                public boolean isVirtualField(int fieldId) throws ReadDriverException                 {return false;}
429
//                public Driver getDriver()                                                                                                         {return null;}
430
//                public void reload() throws ReloadDriverException                                                         { }
431
//                public void addDataSourceListener(IDataSourceListener listener)                         { }
432
//                public void removeDataSourceListener(IDataSourceListener listener)                         { }
433
//                public Value getFieldValue(long rowIndex, int fieldId)throws ReadDriverException {return null;}
434
//                public int getFieldCount() throws ReadDriverException                                                 {return 0;}
435
//                public long getRowCount() throws ReadDriverException                                                 {return 0;}
436
//                public int getFieldWidth(int i) throws ReadDriverException                                         {return 0;}
437
//
438
//                public String getFieldName(int fieldId) throws ReadDriverException {
439
//                        return fieldNames[fieldId];
440
//                }
441
//
442
//                public String getName(){
443
//                        return "Mock datasource used for testing only";
444
//                }
445
//
446
//                public String[] getFieldNames() throws ReadDriverException {
447
//                        return fieldNames;
448
//                }
449
//
450
//                public int getFieldIndexByName(String fieldName)throws ReadDriverException {
451
//                        for (int i = 0; i < fieldNames.length; i++) {
452
//                                if (fieldNames[i].equals(fieldName))
453
//                                        return i;
454
//                        }
455
//                        return -1;
456
//                }
457
//
458
//                public int getFieldType(int i) throws ReadDriverException {
459
//                        return Types.INTEGER;
460
//                }
461

    
462
        }
463

    
464
        //private static final FInterval interval0=new FInterval(0,2);
465
        //private static final FInterval interval1=new FInterval(3,5);
466
        //private static final FInterval interval2=new FInterval(6,2);
467
        //private static final FInterval interval3=new FInterval(9,2);
468
        // private static final Value interval4;
469
        // private static final Value interval5;
470
        // private static final Value interval6;
471
        // private static final Value interval7;
472
        // private static final Value interval8;
473
        // private static final Value interval9;
474

    
475
        Hashtable symTable;
476

    
477
        private IClassifiedVectorLegend[] classifiedLegends;
478
        private ISymbol[] symbols;
479
        private Object[] sampleValues = new Object[] { v0, v1, v2, v3, };
480
        private Feature[] features;
481

    
482
        // private FInterval[] intervals = new FInterval[] {
483
        // interval0,
484
        // interval1,
485
        // interval2,
486
        // interval3,
487
        // interval4,
488
        // interval5,
489
        // interval6,
490
        // interval7,
491
        // interval8,
492
        // interval9,
493
        // };
494

    
495
        protected void setUp() throws Exception {
496
                super.setUp();
497
                features = new Feature[4];
498

    
499
                // initialize test values
500
                for (int i = 0; i < features.length; i++) {
501
                        GeometryFactory geomFactory=GeometryManager.getInstance().getGeometryFactory();
502
                        // create the geometry associated to the feature
503
                        int size = 200;
504
                        Dimension d = new Dimension(size, size);
505
                        Rectangle aShape = new Rectangle(i * size, i * size, d.width,
506
                                        d.height);
507
                        Geometry geom = geomFactory.createPolyline2D(new GeneralPathX(
508
                                        aShape));
509

    
510
                        /*
511
                         * create a full-featured Feature with randomed values at its fields
512
                         * to avoid testing over the same values each time
513
                         */
514
//                        features[i] = new DefaultFeature(geom, featureValues[i], "[" + i
515
//                                        + "]");
516
                }
517

    
518
                // initialize the symbol subset for this test
519
                symbols = TestISymbol.getNewSymbolInstances();
520

    
521
                // initialize the legends for this test
522
                ILegend[] allLegends = TestILegend.getNewLegendInstances();
523
                ArrayList clegends = new ArrayList();
524
                ArrayList intervalLegends = new ArrayList();
525
                for (int i = 0; i < allLegends.length; i++) {
526
                        if (allLegends[i] instanceof AbstractIntervalLegend) // <- Tested
527
                                intervalLegends.add(allLegends[i]);
528
                        else if (allLegends[i] instanceof IClassifiedLegend) {
529
                                clegends.add(allLegends[i]);
530
                        }
531

    
532
                        if (allLegends[i] instanceof IClassifiedVectorLegend) {
533
                                IClassifiedVectorLegend cvl = (IClassifiedVectorLegend) allLegends[i];
534
                                cvl.setClassifyingFieldNames(new String[] { fieldNames[FIELDID] });
535
                                cvl.setFeatureStore(mockDataSource);
536

    
537
                        }
538
                }
539

    
540
                this.classifiedLegends = (IClassifiedVectorLegend[]) clegends
541
                                .toArray(new IClassifiedVectorLegend[clegends.size()]);
542
                this.intervalLegends = (AbstractIntervalLegend[]) intervalLegends
543
                                .toArray(new AbstractIntervalLegend[intervalLegends.size()]);
544
        }
545

    
546
        /**
547
         * This method is used to add symbols to a legend.That is, it takes an array
548
         * of IClassifiedVectorialLegend which is empty andm, using a second array
549
         * of objects (values), the first one is filled.Also, a hash table is filled
550
         * too using the array of objects (it will be useful in some tests to check
551
         * that a symbol can be taken using a feature) .
552
         *
553
         * @param legend
554
         * @return
555
         */
556
        private void fillClassifiedLegend(IClassifiedVectorLegend legend,
557
                        Object[] values) {
558
                // initialize the hash table
559
                symTable = new Hashtable();
560

    
561
                // to add symbols to the legend and the hash table
562
                for (int j = 0; j < values.length; j++) {
563

    
564
                        ISymbol sym = symbols[j % symbols.length];
565
                        legend.addSymbol(values[j], sym);
566
                        symTable.put(values[j], sym);
567
                }
568
        }
569

    
570
        /**
571
         * This test ensures that when a legend is filled, the number of symbols
572
         * added is correct. To do it, is checked that the number of symbols of a
573
         * legend is the same as the length of the array of example values that we
574
         * have.
575
         *
576
         * @throws ReadDriverException
577
         */
578
        public void testICLAdittion() throws ReadException {
579

    
580
                // Fills the legend
581
                for (int i = 0; i < classifiedLegends.length; i++) {
582
                        fillClassifiedLegend(classifiedLegends[i], sampleValues);
583
                }
584

    
585
                for (int i = 0; i < classifiedLegends.length; i++)
586
                        assertEquals(classifiedLegends[i].getClassName()
587
                                        + " fails with the comparation of the number of symbols",
588
                                        classifiedLegends[i].getSymbols().length,
589
                                        sampleValues.length);
590

    
591
        }
592

    
593
        /**
594
         * This test ensures that the symbols that we have previously added to a
595
         * legend are accessible using its features.To do it, this test compares the
596
         * symbol taken from the legend with the symbol taken from the hashTable
597
         * (using the same feature).
598
         *
599
         * @throws ReadDriverException
600
         */
601

    
602
        public void testICLCheckValueSymbols() throws ReadException {
603
                // fills the legends
604
                for (int i = 0; i < classifiedLegends.length; i++) {
605
                        fillClassifiedLegend(classifiedLegends[i], sampleValues);
606
                }
607

    
608
                for (int i = 0; i < classifiedLegends.length; i++) {
609
                        // For each feature
610
                        for (int j = 0; j < features.length; j++) {
611
                                Feature myFeature = features[i];
612
                                // takes the value of the field that identifies the feature
613
                                Object val = myFeature.get(FIELDID);
614
                                // the last value is used to access to the hash table to obtain
615
                                // a symbol
616
                                ISymbol tableSym = (ISymbol) symTable.get(val);
617

    
618
                                IClassifiedVectorLegend leg = (IClassifiedVectorLegend) classifiedLegends[i];
619
                                // takes the symbol from a legend using the feature
620
                                ISymbol legendSym = leg.getSymbolByFeature(myFeature);
621
                                // compares that both symbols are the same
622
                                assertEquals(legendSym.getClassName()
623
                                                + " fails with the comparation of the class symbols",
624
                                                legendSym, tableSym);
625
                        }
626
                }
627
        }
628

    
629
}