KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > general > junit > DatasetUtilitiesTests


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * --------------------------
28  * DatasetUtilitiesTests.java
29  * --------------------------
30  * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: DatasetUtilitiesTests.java,v 1.1.2.1 2006/10/03 15:41:44 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 18-Sep-2003 : Version 1 (DG);
40  * 23-Mar-2004 : Added test for maximumStackedRangeValue() method (DG);
41  * 04-Oct-2004 : Eliminated NumberUtils usage (DG);
42  * 07-Jan-2005 : Updated for method name changes (DG);
43  * 03-Feb-2005 : Added testFindStackedRangeBounds2() method (DG);
44  *
45  */

46
47 package org.jfree.data.general.junit;
48
49 import junit.framework.Test;
50 import junit.framework.TestCase;
51 import junit.framework.TestSuite;
52
53 import org.jfree.data.KeyToGroupMap;
54 import org.jfree.data.Range;
55 import org.jfree.data.category.CategoryDataset;
56 import org.jfree.data.category.DefaultCategoryDataset;
57 import org.jfree.data.general.DatasetUtilities;
58 import org.jfree.data.general.DefaultPieDataset;
59 import org.jfree.data.general.PieDataset;
60 import org.jfree.data.xy.DefaultTableXYDataset;
61 import org.jfree.data.xy.TableXYDataset;
62 import org.jfree.data.xy.XYDataset;
63 import org.jfree.data.xy.XYSeries;
64 import org.jfree.data.xy.XYSeriesCollection;
65
66 /**
67  * Tests for the {@link DatasetUtilities} class.
68  */

69 public class DatasetUtilitiesTests extends TestCase {
70
71     private static final double EPSILON = 0.0000000001;
72     
73     /**
74      * Returns the tests as a test suite.
75      *
76      * @return The test suite.
77      */

78     public static Test suite() {
79         return new TestSuite(DatasetUtilitiesTests.class);
80     }
81
82     /**
83      * Constructs a new set of tests.
84      *
85      * @param name the name of the tests.
86      */

87     public DatasetUtilitiesTests(String JavaDoc name) {
88         super(name);
89     }
90     
91     /**
92      * Some tests to verify that Java does what I think it does!
93      */

94     public void testJava() {
95         assertTrue(Double.isNaN(Math.min(1.0, Double.NaN)));
96         assertTrue(Double.isNaN(Math.max(1.0, Double.NaN)));
97     }
98     
99     /**
100      * Some tests for the calculatePieDatasetTotal() method.
101      */

102     public void testCalculatePieDatasetTotal() {
103         DefaultPieDataset d = new DefaultPieDataset();
104         assertEquals(
105             0.0, DatasetUtilities.calculatePieDatasetTotal(d), EPSILON
106         );
107         d.setValue("A", 1.0);
108         assertEquals(
109             1.0, DatasetUtilities.calculatePieDatasetTotal(d), EPSILON
110         );
111         d.setValue("B", 3.0);
112         assertEquals(
113             4.0, DatasetUtilities.calculatePieDatasetTotal(d), EPSILON
114         );
115     }
116
117     /**
118      * Some tests for the findDomainBounds() method.
119      */

120     public void testFindDomainBounds() {
121         XYDataset dataset = createXYDataset1();
122         Range r = DatasetUtilities.findDomainBounds(dataset);
123         assertEquals(1.0, r.getLowerBound(), EPSILON);
124         assertEquals(3.0, r.getUpperBound(), EPSILON);
125     }
126     
127     /**
128      * Some tests for the iterateDomainBounds() method.
129      */

130     public void testIterateDomainBounds() {
131         XYDataset dataset = createXYDataset1();
132         Range r = DatasetUtilities.iterateDomainBounds(dataset);
133         assertEquals(1.0, r.getLowerBound(), EPSILON);
134         assertEquals(3.0, r.getUpperBound(), EPSILON);
135     }
136     
137     /**
138      * Some tests for the findRangeExtent() method.
139      */

140     public void testFindRangeBounds1() {
141         CategoryDataset dataset = createCategoryDataset1();
142         Range r = DatasetUtilities.findRangeBounds(dataset);
143         assertEquals(1.0, r.getLowerBound(), EPSILON);
144         assertEquals(6.0, r.getUpperBound(), EPSILON);
145     }
146     
147     /**
148      * Some tests for the findRangeBounds() method.
149      */

150     public void testFindRangeBounds2() {
151         XYDataset dataset = createXYDataset1();
152         Range r = DatasetUtilities.findRangeBounds(dataset);
153         assertEquals(100.0, r.getLowerBound(), EPSILON);
154         assertEquals(105.0, r.getUpperBound(), EPSILON);
155     }
156     
157     /**
158      * Some tests for the iterateCategoryRangeBounds() method.
159      */

160     public void testIterateCategoryRangeBounds() {
161         CategoryDataset dataset = createCategoryDataset1();
162         Range r = DatasetUtilities.iterateCategoryRangeBounds(dataset, false);
163         assertEquals(1.0, r.getLowerBound(), EPSILON);
164         assertEquals(6.0, r.getUpperBound(), EPSILON);
165     }
166
167     /**
168      * Some tests for the iterateXYRangeBounds() method.
169      */

170     public void testIterateXYRangeBounds() {
171         XYDataset dataset = createXYDataset1();
172         Range r = DatasetUtilities.iterateXYRangeBounds(dataset);
173         assertEquals(100.0, r.getLowerBound(), EPSILON);
174         assertEquals(105.0, r.getUpperBound(), EPSILON);
175     }
176
177     /**
178      * Check the range returned when a series contains a null value.
179      */

180     public void testIterateXYRangeBounds2() {
181         XYSeries s1 = new XYSeries("S1");
182         s1.add(1.0, 1.1);
183         s1.add(2.0, null);
184         s1.add(3.0, 3.3);
185         XYSeriesCollection dataset = new XYSeriesCollection(s1);
186         Range r = DatasetUtilities.iterateXYRangeBounds(dataset);
187         assertEquals(1.1, r.getLowerBound(), EPSILON);
188         assertEquals(3.3, r.getUpperBound(), EPSILON);
189     }
190     
191     /**
192      * Some tests for the findMinimumDomainValue() method.
193      */

194     public void testFindMinimumDomainValue() {
195         XYDataset dataset = createXYDataset1();
196         Number JavaDoc minimum = DatasetUtilities.findMinimumDomainValue(dataset);
197         assertEquals(new Double JavaDoc(1.0), minimum);
198     }
199     
200     /**
201      * Some tests for the findMaximumDomainValue() method.
202      */

203     public void testFindMaximumDomainValue() {
204         XYDataset dataset = createXYDataset1();
205         Number JavaDoc maximum = DatasetUtilities.findMaximumDomainValue(dataset);
206         assertEquals(new Double JavaDoc(3.0), maximum);
207     }
208     
209     /**
210      * Some tests for the findMinimumRangeValue() method.
211      */

212     public void testFindMinimumRangeValue() {
213         CategoryDataset d1 = createCategoryDataset1();
214         Number JavaDoc min1 = DatasetUtilities.findMinimumRangeValue(d1);
215         assertEquals(new Double JavaDoc(1.0), min1);
216         
217         XYDataset d2 = createXYDataset1();
218         Number JavaDoc min2 = DatasetUtilities.findMinimumRangeValue(d2);
219         assertEquals(new Double JavaDoc(100.0), min2);
220     }
221     
222     /**
223      * Some tests for the findMaximumRangeValue() method.
224      */

225     public void testFindMaximumRangeValue() {
226         CategoryDataset d1 = createCategoryDataset1();
227         Number JavaDoc max1 = DatasetUtilities.findMaximumRangeValue(d1);
228         assertEquals(new Double JavaDoc(6.0), max1);
229
230         XYDataset dataset = createXYDataset1();
231         Number JavaDoc maximum = DatasetUtilities.findMaximumRangeValue(dataset);
232         assertEquals(new Double JavaDoc(105.0), maximum);
233     }
234     
235     /**
236      * A quick test of the min and max range value methods.
237      */

238     public void testMinMaxRange() {
239         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
240         dataset.addValue(100.0, "Series 1", "Type 1");
241         dataset.addValue(101.1, "Series 1", "Type 2");
242         Number JavaDoc min = DatasetUtilities.findMinimumRangeValue(dataset);
243         assertTrue(min.doubleValue() < 100.1);
244         Number JavaDoc max = DatasetUtilities.findMaximumRangeValue(dataset);
245         assertTrue(max.doubleValue() > 101.0);
246     }
247
248     /**
249      * A test to reproduce bug report 803660.
250      */

251     public void test803660() {
252         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
253         dataset.addValue(100.0, "Series 1", "Type 1");
254         dataset.addValue(101.1, "Series 1", "Type 2");
255         Number JavaDoc n = DatasetUtilities.findMaximumRangeValue(dataset);
256         assertTrue(n.doubleValue() > 101.0);
257     }
258     
259     /**
260      * A simple test for the cumulative range calculation. The sequence of
261      * "cumulative" values are considered to be { 0.0, 10.0, 25.0, 18.0 } so
262      * the range should be 0.0 -> 25.0.
263      */

264     public void testCumulativeRange1() {
265         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
266         dataset.addValue(10.0, "Series 1", "Start");
267         dataset.addValue(15.0, "Series 1", "Delta 1");
268         dataset.addValue(-7.0, "Series 1", "Delta 2");
269         Range range = DatasetUtilities.findCumulativeRangeBounds(dataset);
270         assertEquals(0.0, range.getLowerBound(), 0.00000001);
271         assertEquals(25.0, range.getUpperBound(), 0.00000001);
272     }
273     
274     /**
275      * A further test for the cumulative range calculation.
276      */

277     public void testCumulativeRange2() {
278         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
279         dataset.addValue(-21.4, "Series 1", "Start Value");
280         dataset.addValue(11.57, "Series 1", "Delta 1");
281         dataset.addValue(3.51, "Series 1", "Delta 2");
282         dataset.addValue(-12.36, "Series 1", "Delta 3");
283         dataset.addValue(3.39, "Series 1", "Delta 4");
284         dataset.addValue(38.68, "Series 1", "Delta 5");
285         dataset.addValue(-43.31, "Series 1", "Delta 6");
286         dataset.addValue(-29.59, "Series 1", "Delta 7");
287         dataset.addValue(35.30, "Series 1", "Delta 8");
288         dataset.addValue(5.0, "Series 1", "Delta 9");
289         Range range = DatasetUtilities.findCumulativeRangeBounds(dataset);
290         assertEquals(-49.51, range.getLowerBound(), 0.00000001);
291         assertEquals(23.39, range.getUpperBound(), 0.00000001);
292     }
293     
294     /**
295      * Test the creation of a dataset from an array.
296      */

297     public void testCreateCategoryDataset1() {
298         String JavaDoc[] rowKeys = {"R1", "R2", "R3"};
299         String JavaDoc[] columnKeys = {"C1", "C2"};
300         double[][] data = new double[3][];
301         data[0] = new double[] {1.1, 1.2};
302         data[1] = new double[] {2.1, 2.2};
303         data[2] = new double[] {3.1, 3.2};
304         CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
305             rowKeys, columnKeys, data
306         );
307         assertTrue(dataset.getRowCount() == 3);
308         assertTrue(dataset.getColumnCount() == 2);
309     }
310
311     /**
312      * Test the creation of a dataset from an array. This time is should fail
313      * because the array dimensions are around the wrong way.
314      */

315     public void testCreateCategoryDataset2() {
316         boolean pass = false;
317         String JavaDoc[] rowKeys = {"R1", "R2", "R3"};
318         String JavaDoc[] columnKeys = {"C1", "C2"};
319         double[][] data = new double[2][];
320         data[0] = new double[] {1.1, 1.2, 1.3};
321         data[1] = new double[] {2.1, 2.2, 2.3};
322         CategoryDataset dataset = null;
323         try {
324             dataset = DatasetUtilities.createCategoryDataset(
325                rowKeys, columnKeys, data
326             );
327         }
328         catch (IllegalArgumentException JavaDoc e) {
329             pass = true; // got it!
330
}
331         assertTrue(pass);
332         assertTrue(dataset == null);
333     }
334     
335     /**
336      * Test for a bug reported in the forum:
337      *
338      * http://www.jfree.org/phpBB2/viewtopic.php?t=7903
339      */

340     public void testMaximumStackedRangeValue() {
341         double v1 = 24.3;
342         double v2 = 14.2;
343         double v3 = 33.2;
344         double v4 = 32.4;
345         double v5 = 26.3;
346         double v6 = 22.6;
347         Number JavaDoc answer = new Double JavaDoc(Math.max(v1 + v2 + v3, v4 + v5 + v6));
348         DefaultCategoryDataset d = new DefaultCategoryDataset();
349         d.addValue(v1, "Row 0", "Column 0");
350         d.addValue(v2, "Row 1", "Column 0");
351         d.addValue(v3, "Row 2", "Column 0");
352         d.addValue(v4, "Row 0", "Column 1");
353         d.addValue(v5, "Row 1", "Column 1");
354         d.addValue(v6, "Row 2", "Column 1");
355         Number JavaDoc max = DatasetUtilities.findMaximumStackedRangeValue(d);
356         assertTrue(max.equals(answer));
357     }
358     
359     /**
360      * Some checks for the findStackedRangeBounds() method.
361      */

362     public void testFindStackedRangeBoundsForCategoryDataset1() {
363         CategoryDataset d1 = createCategoryDataset1();
364         Range r = DatasetUtilities.findStackedRangeBounds(d1);
365         assertEquals(0.0, r.getLowerBound(), EPSILON);
366         assertEquals(15.0, r.getUpperBound(), EPSILON);
367         
368         d1 = createCategoryDataset2();
369         r = DatasetUtilities.findStackedRangeBounds(d1);
370         assertEquals(-2.0, r.getLowerBound(), EPSILON);
371         assertEquals(2.0, r.getUpperBound(), EPSILON);
372     }
373     
374     /**
375      * Some checks for the findStackedRangeBounds() method.
376      */

377     public void testFindStackedRangeBoundsForCategoryDataset2() {
378         CategoryDataset d1 = new DefaultCategoryDataset();
379         Range r = DatasetUtilities.findStackedRangeBounds(d1);
380         assertTrue(r == null);
381     }
382     
383     /**
384      * Some checks for the findStackedRangeBounds() method.
385      */

386     public void testFindStackedRangeBoundsForTableXYDataset1() {
387         TableXYDataset d2 = createTableXYDataset1();
388         Range r = DatasetUtilities.findStackedRangeBounds(d2);
389         assertEquals(-2.0, r.getLowerBound(), EPSILON);
390         assertEquals(2.0, r.getUpperBound(), EPSILON);
391     }
392     
393     /**
394      * Some checks for the findStackedRangeBounds() method.
395      */

396     public void testFindStackedRangeBoundsForTableXYDataset2() {
397         DefaultTableXYDataset d = new DefaultTableXYDataset();
398         Range r = DatasetUtilities.findStackedRangeBounds(d);
399         assertEquals(r, new Range(0.0, 0.0));
400     }
401     
402     /**
403      * Tests the stacked range extent calculation.
404      */

405     public void testStackedRangeWithMap() {
406         CategoryDataset d = createCategoryDataset1();
407         KeyToGroupMap map = new KeyToGroupMap("G0");
408         map.mapKeyToGroup("R2", "G1");
409         Range r = DatasetUtilities.findStackedRangeBounds(d, map);
410         assertEquals(0.0, r.getLowerBound(), EPSILON);
411         assertEquals(9.0, r.getUpperBound(), EPSILON);
412     }
413     
414     /**
415      * Some checks for the limitPieDataset() methods.
416      */

417     public void testLimitPieDataset() {
418         
419         // check that empty dataset is handled OK
420
DefaultPieDataset d1 = new DefaultPieDataset();
421         PieDataset d2 = DatasetUtilities.createConsolidatedPieDataset(
422             d1, "Other", 0.05
423         );
424         assertEquals(0, d2.getItemCount());
425         
426         // check that minItem limit is observed
427
d1.setValue("Item 1", 1.0);
428         d1.setValue("Item 2", 49.50);
429         d1.setValue("Item 3", 49.50);
430         d2 = DatasetUtilities.createConsolidatedPieDataset(d1, "Other", 0.05);
431         assertEquals(3, d2.getItemCount());
432         assertEquals("Item 1", d2.getKey(0));
433         assertEquals("Item 2", d2.getKey(1));
434         assertEquals("Item 3", d2.getKey(2));
435
436         // check that minItem limit is observed
437
d1.setValue("Item 4", 1.0);
438         d2 = DatasetUtilities.createConsolidatedPieDataset(
439             d1, "Other", 0.05, 2
440         );
441         
442         // and that simple aggregation works
443
assertEquals(3, d2.getItemCount());
444         assertEquals("Item 2", d2.getKey(0));
445         assertEquals("Item 3", d2.getKey(1));
446         assertEquals("Other", d2.getKey(2));
447         assertEquals(new Double JavaDoc(2.0), d2.getValue("Other"));
448         
449     }
450     
451     /**
452      * Creates a dataset for testing.
453      *
454      * @return A dataset.
455      */

456     private CategoryDataset createCategoryDataset1() {
457         DefaultCategoryDataset result = new DefaultCategoryDataset();
458         result.addValue(1.0, "R0", "C0");
459         result.addValue(1.0, "R1", "C0");
460         result.addValue(1.0, "R2", "C0");
461         result.addValue(4.0, "R0", "C1");
462         result.addValue(5.0, "R1", "C1");
463         result.addValue(6.0, "R2", "C1");
464         return result;
465     }
466     
467     /**
468      * Creates a dataset for testing.
469      *
470      * @return A dataset.
471      */

472     private CategoryDataset createCategoryDataset2() {
473         DefaultCategoryDataset result = new DefaultCategoryDataset();
474         result.addValue(1.0, "R0", "C0");
475         result.addValue(-2.0, "R1", "C0");
476         result.addValue(2.0, "R0", "C1");
477         result.addValue(-1.0, "R1", "C1");
478         return result;
479     }
480     
481
482     /**
483      * Creates a dataset for testing.
484      *
485      * @return A dataset.
486      */

487     private XYDataset createXYDataset1() {
488         XYSeries series1 = new XYSeries("S1");
489         series1.add(1.0, 100.0);
490         series1.add(2.0, 101.0);
491         series1.add(3.0, 102.0);
492         XYSeries series2 = new XYSeries("S2");
493         series2.add(1.0, 103.0);
494         series2.add(2.0, null);
495         series2.add(3.0, 105.0);
496         XYSeriesCollection result = new XYSeriesCollection();
497         result.addSeries(series1);
498         result.addSeries(series2);
499         result.setIntervalWidth(0.0);
500         return result;
501     }
502     
503     /**
504      * Creates a sample dataset for testing purposes.
505      *
506      * @return A sample dataset.
507      */

508     private TableXYDataset createTableXYDataset1() {
509         DefaultTableXYDataset dataset = new DefaultTableXYDataset();
510         
511         XYSeries s1 = new XYSeries("Series 1", true, false);
512         s1.add(1.0, 1.0);
513         s1.add(2.0, 2.0);
514         dataset.addSeries(s1);
515         
516         XYSeries s2 = new XYSeries("Series 2", true, false);
517         s2.add(1.0, -2.0);
518         s2.add(2.0, -1.0);
519         dataset.addSeries(s2);
520         
521         return dataset;
522     }
523     
524 }
525
Popular Tags