Statistics
| Revision:

svn-gvsig-desktop / branches / Mobile_Compatible_Hito_1 / libFMap_mapcontext / src-test / org / gvsig / fmap / mapcontext / rendering / TestIClassifiedLegend.java @ 21574

History | View | Annotate | Download (20.1 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
import org.opengis.filter.Filter;
81
import org.opengis.filter.sort.SortBy;
82

    
83
import junit.framework.TestCase;
84

    
85

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

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

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

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

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

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

    
120

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

    
128
        private AbstractIntervalLegend[] intervalLegends;
129
        private MockDataSource mockDataSource = new MockDataSource();
130

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

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

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

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

    
151
                }
152

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

    
156
                }
157

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

    
161
                }
162

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

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

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

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

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

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

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

    
196
                }
197

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

    
201
                }
202

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

    
206
                }
207

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

    
211
                }
212

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

    
216
                }
217

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

    
221
                }
222

    
223
                public void disableNotifications() {
224
                        // TODO Auto-generated method stub
225

    
226
                }
227

    
228
                public void enableNotifications() {
229
                        // TODO Auto-generated method stub
230

    
231
                }
232

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

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

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

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

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

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

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

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

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

    
276
                }
277

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

    
281
                }
282

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

    
286
                }
287

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

    
291
                }
292

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

    
296
                }
297

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

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

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

    
311
                }
312

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

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

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

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

    
331
                }
332

    
333
                public void cancelEditing() {
334
                        // TODO Auto-generated method stub
335

    
336
                }
337

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

    
341
                }
342

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

    
348
                public void undo() {
349
                        // TODO Auto-generated method stub
350

    
351
                }
352

    
353
                public void redo() {
354
                        // TODO Auto-generated method stub
355

    
356
                }
357

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

    
363
                public void beginComplexNotification() {
364
                        // TODO Auto-generated method stub
365

    
366
                }
367

    
368
                public void endComplexNotification() {
369
                        // TODO Auto-generated method stub
370

    
371
                }
372

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

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

    
381
                }
382

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

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

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

    
396
                }
397

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

    
401
                }
402

    
403
                public void deleteObservers() {
404
                        // TODO Auto-generated method stub
405

    
406
                }
407

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

    
464
                public DataCollection getDataCollection(FeatureType type,
465
                                Filter filter, SortBy order) throws ReadException {
466
                        // TODO Auto-generated method stub
467
                        return null;
468
                }
469

    
470
                public DataCollection getDataCollection(String[] fields, Filter filter,
471
                                SortBy order) throws ReadException {
472
                        // TODO Auto-generated method stub
473
                        return null;
474
                }
475

    
476
                public void getDataCollection(FeatureType type, Filter filter,
477
                                SortBy order, Observer observer) throws DataException {
478
                        // TODO Auto-generated method stub
479
                        
480
                }
481

    
482
                public void getDataCollection(String[] fields, Filter filter,
483
                                SortBy order, Observer observer) throws DataException {
484
                        // TODO Auto-generated method stub
485
                        
486
                }
487

    
488
        }
489

    
490
        //private static final FInterval interval0=new FInterval(0,2);
491
        //private static final FInterval interval1=new FInterval(3,5);
492
        //private static final FInterval interval2=new FInterval(6,2);
493
        //private static final FInterval interval3=new FInterval(9,2);
494
        // private static final Value interval4;
495
        // private static final Value interval5;
496
        // private static final Value interval6;
497
        // private static final Value interval7;
498
        // private static final Value interval8;
499
        // private static final Value interval9;
500

    
501
        Hashtable symTable;
502

    
503
        private IClassifiedVectorLegend[] classifiedLegends;
504
        private ISymbol[] symbols;
505
        private Object[] sampleValues = new Object[] { v0, v1, v2, v3, };
506
        private Feature[] features;
507

    
508
        // private FInterval[] intervals = new FInterval[] {
509
        // interval0,
510
        // interval1,
511
        // interval2,
512
        // interval3,
513
        // interval4,
514
        // interval5,
515
        // interval6,
516
        // interval7,
517
        // interval8,
518
        // interval9,
519
        // };
520

    
521
        protected void setUp() throws Exception {
522
                super.setUp();
523
                features = new Feature[4];
524

    
525
                // initialize test values
526
                for (int i = 0; i < features.length; i++) {
527
                        GeometryFactory geomFactory=GeometryManager.getInstance().getGeometryFactory();
528
                        // create the geometry associated to the feature
529
                        int size = 200;
530
                        Dimension d = new Dimension(size, size);
531
                        Rectangle aShape = new Rectangle(i * size, i * size, d.width,
532
                                        d.height);
533
                        Geometry geom = geomFactory.createPolyline2D(new GeneralPathX(
534
                                        aShape));
535

    
536
                        /*
537
                         * create a full-featured Feature with randomed values at its fields
538
                         * to avoid testing over the same values each time
539
                         */
540
//                        features[i] = new DefaultFeature(geom, featureValues[i], "[" + i
541
//                                        + "]");
542
                }
543

    
544
                // initialize the symbol subset for this test
545
                symbols = TestISymbol.getNewSymbolInstances();
546

    
547
                // initialize the legends for this test
548
                ILegend[] allLegends = TestILegend.getNewLegendInstances();
549
                ArrayList clegends = new ArrayList();
550
                ArrayList intervalLegends = new ArrayList();
551
                for (int i = 0; i < allLegends.length; i++) {
552
                        if (allLegends[i] instanceof AbstractIntervalLegend) // <- Tested
553
                                intervalLegends.add(allLegends[i]);
554
                        else if (allLegends[i] instanceof IClassifiedLegend) {
555
                                clegends.add(allLegends[i]);
556
                        }
557

    
558
                        if (allLegends[i] instanceof IClassifiedVectorLegend) {
559
                                IClassifiedVectorLegend cvl = (IClassifiedVectorLegend) allLegends[i];
560
                                cvl.setClassifyingFieldNames(new String[] { fieldNames[FIELDID] });
561
                                cvl.setFeatureStore(mockDataSource);
562

    
563
                        }
564
                }
565

    
566
                this.classifiedLegends = (IClassifiedVectorLegend[]) clegends
567
                                .toArray(new IClassifiedVectorLegend[clegends.size()]);
568
                this.intervalLegends = (AbstractIntervalLegend[]) intervalLegends
569
                                .toArray(new AbstractIntervalLegend[intervalLegends.size()]);
570
        }
571

    
572
        /**
573
         * This method is used to add symbols to a legend.That is, it takes an array
574
         * of IClassifiedVectorialLegend which is empty andm, using a second array
575
         * of objects (values), the first one is filled.Also, a hash table is filled
576
         * too using the array of objects (it will be useful in some tests to check
577
         * that a symbol can be taken using a feature) .
578
         *
579
         * @param legend
580
         * @return
581
         */
582
        private void fillClassifiedLegend(IClassifiedVectorLegend legend,
583
                        Object[] values) {
584
                // initialize the hash table
585
                symTable = new Hashtable();
586

    
587
                // to add symbols to the legend and the hash table
588
                for (int j = 0; j < values.length; j++) {
589

    
590
                        ISymbol sym = symbols[j % symbols.length];
591
                        legend.addSymbol(values[j], sym);
592
                        symTable.put(values[j], sym);
593
                }
594
        }
595

    
596
        /**
597
         * This test ensures that when a legend is filled, the number of symbols
598
         * added is correct. To do it, is checked that the number of symbols of a
599
         * legend is the same as the length of the array of example values that we
600
         * have.
601
         *
602
         * @throws ReadDriverException
603
         */
604
        public void testICLAdittion() throws ReadException {
605

    
606
                // Fills the legend
607
                for (int i = 0; i < classifiedLegends.length; i++) {
608
                        fillClassifiedLegend(classifiedLegends[i], sampleValues);
609
                }
610

    
611
                for (int i = 0; i < classifiedLegends.length; i++)
612
                        assertEquals(classifiedLegends[i].getClassName()
613
                                        + " fails with the comparation of the number of symbols",
614
                                        classifiedLegends[i].getSymbols().length,
615
                                        sampleValues.length);
616

    
617
        }
618

    
619
        /**
620
         * This test ensures that the symbols that we have previously added to a
621
         * legend are accessible using its features.To do it, this test compares the
622
         * symbol taken from the legend with the symbol taken from the hashTable
623
         * (using the same feature).
624
         *
625
         * @throws ReadDriverException
626
         */
627

    
628
        public void testICLCheckValueSymbols() throws ReadException {
629
                // fills the legends
630
                for (int i = 0; i < classifiedLegends.length; i++) {
631
                        fillClassifiedLegend(classifiedLegends[i], sampleValues);
632
                }
633

    
634
                for (int i = 0; i < classifiedLegends.length; i++) {
635
                        // For each feature
636
                        for (int j = 0; j < features.length; j++) {
637
                                Feature myFeature = features[i];
638
                                // takes the value of the field that identifies the feature
639
                                Object val = myFeature.get(FIELDID);
640
                                // the last value is used to access to the hash table to obtain
641
                                // a symbol
642
                                ISymbol tableSym = (ISymbol) symTable.get(val);
643

    
644
                                IClassifiedVectorLegend leg = (IClassifiedVectorLegend) classifiedLegends[i];
645
                                // takes the symbol from a legend using the feature
646
                                ISymbol legendSym = leg.getSymbolByFeature(myFeature);
647
                                // compares that both symbols are the same
648
                                assertEquals(legendSym.getClassName()
649
                                                + " fails with the comparation of the class symbols",
650
                                                legendSym, tableSym);
651
                        }
652
                }
653
        }
654

    
655
}