KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > statistics > junit > DefaultStatisticalCategoryDatasetTests


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, 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  * DefaultStatisticalCategoryDatasetTests.java
29  * -------------------------------------------
30  * (C) Copyright 2005, 2006, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: DefaultStatisticalCategoryDatasetTests.java,v 1.1.2.1 2006/10/03 15:41:42 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 05-Feb-2005 : Version 1 (DG);
40  * 03-Aug-2006 : Added testGetRangeBounds() method (DG);
41  *
42  */

43
44 package org.jfree.data.statistics.junit;
45
46 import java.io.ByteArrayInputStream JavaDoc;
47 import java.io.ByteArrayOutputStream JavaDoc;
48 import java.io.ObjectInput JavaDoc;
49 import java.io.ObjectInputStream JavaDoc;
50 import java.io.ObjectOutput JavaDoc;
51 import java.io.ObjectOutputStream JavaDoc;
52
53 import junit.framework.Test;
54 import junit.framework.TestCase;
55 import junit.framework.TestSuite;
56
57 import org.jfree.data.Range;
58 import org.jfree.data.statistics.DefaultStatisticalCategoryDataset;
59
60 /**
61  * Tests for the {@link DefaultStatisticalCategoryDataset} class.
62  */

63 public class DefaultStatisticalCategoryDatasetTests extends TestCase {
64
65     /**
66      * Returns the tests as a test suite.
67      *
68      * @return The test suite.
69      */

70     public static Test suite() {
71         return new TestSuite(DefaultStatisticalCategoryDatasetTests.class);
72     }
73
74     /**
75      * Constructs a new set of tests.
76      *
77      * @param name the name of the tests.
78      */

79     public DefaultStatisticalCategoryDatasetTests(String JavaDoc name) {
80         super(name);
81     }
82
83     /**
84      * Some checks for the getRangeBounds() method.
85      */

86     public void testGetRangeBounds() {
87         DefaultStatisticalCategoryDataset d
88             = new DefaultStatisticalCategoryDataset();
89         
90         // an empty dataset should return null for bounds
91
assertNull(d.getRangeBounds(true));
92         
93         // try a dataset with a single value
94
d.add(4.5, 1.0, "R1", "C1");
95         assertEquals(new Range(4.5, 4.5), d.getRangeBounds(false));
96         assertEquals(new Range(3.5, 5.5), d.getRangeBounds(true));
97         
98         // try a dataset with two values
99
d.add(0.5, 2.0, "R1", "C2");
100         assertEquals(new Range(0.5, 4.5), d.getRangeBounds(false));
101         assertEquals(new Range(-1.5, 5.5), d.getRangeBounds(true));
102         
103         // try a Double.NaN
104
d.add(Double.NaN, 0.0, "R1", "C3");
105         assertEquals(new Range(0.5, 4.5), d.getRangeBounds(false));
106         assertEquals(new Range(-1.5, 5.5), d.getRangeBounds(true));
107
108         // try a Double.NEGATIVE_INFINITY
109
d.add(Double.NEGATIVE_INFINITY, 0.0, "R1", "C3");
110         assertEquals(new Range(Double.NEGATIVE_INFINITY, 4.5),
111                 d.getRangeBounds(false));
112         assertEquals(new Range(Double.NEGATIVE_INFINITY, 5.5),
113                 d.getRangeBounds(true));
114
115         // try a Double.POSITIVE_INFINITY
116
d.add(Double.POSITIVE_INFINITY, 0.0, "R1", "C3");
117         assertEquals(new Range(Double.NEGATIVE_INFINITY,
118                 Double.POSITIVE_INFINITY), d.getRangeBounds(false));
119         assertEquals(new Range(Double.NEGATIVE_INFINITY,
120                 Double.POSITIVE_INFINITY), d.getRangeBounds(true));
121     }
122     
123     /**
124      * Confirm that the equals method can distinguish all the required fields.
125      */

126     public void testEquals() {
127         DefaultStatisticalCategoryDataset d1
128             = new DefaultStatisticalCategoryDataset();
129         DefaultStatisticalCategoryDataset d2
130             = new DefaultStatisticalCategoryDataset();
131         assertTrue(d1.equals(d2));
132         assertTrue(d2.equals(d1));
133
134     }
135
136     /**
137      * Some checks for cloning.
138      */

139     public void testCloning() {
140         DefaultStatisticalCategoryDataset d1
141             = new DefaultStatisticalCategoryDataset();
142         d1.add(1.1, 2.2, "R1", "C1");
143         d1.add(3.3, 4.4, "R1", "C2");
144         d1.add(null, new Double JavaDoc(5.5), "R1", "C3");
145         d1.add(new Double JavaDoc(6.6), null, "R2", "C3");
146         DefaultStatisticalCategoryDataset d2 = null;
147         try {
148             d2 = (DefaultStatisticalCategoryDataset) d1.clone();
149         }
150         catch (CloneNotSupportedException JavaDoc e) {
151             fail(e.toString());
152         }
153         assertTrue(d1 != d2);
154         assertTrue(d1.getClass() == d2.getClass());
155         assertTrue(d1.equals(d2));
156     }
157
158     /**
159      * Check serialization of a default instance.
160      */

161     public void testSerialization1() {
162         DefaultStatisticalCategoryDataset d1
163             = new DefaultStatisticalCategoryDataset();
164         d1.add(1.1, 2.2, "R1", "C1");
165         d1.add(3.3, 4.4, "R1", "C2");
166         d1.add(null, new Double JavaDoc(5.5), "R1", "C3");
167         d1.add(new Double JavaDoc(6.6), null, "R2", "C3");
168         DefaultStatisticalCategoryDataset d2 = null;
169         try {
170             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
171             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
172             out.writeObject(d1);
173             out.close();
174
175             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
176                     new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
177             d2 = (DefaultStatisticalCategoryDataset) in.readObject();
178             in.close();
179         }
180         catch (Exception JavaDoc e) {
181             fail(e.toString());
182         }
183         assertEquals(d1, d2);
184     }
185     
186     /**
187      * Check serialization of a more complex instance.
188      */

189     public void testSerialization2() {
190         DefaultStatisticalCategoryDataset d1
191             = new DefaultStatisticalCategoryDataset();
192         d1.add(1.2, 3.4, "Row 1", "Column 1");
193         DefaultStatisticalCategoryDataset d2 = null;
194         try {
195             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
196             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
197             out.writeObject(d1);
198             out.close();
199
200             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
201                     new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
202             d2 = (DefaultStatisticalCategoryDataset) in.readObject();
203             in.close();
204         }
205         catch (Exception JavaDoc e) {
206             fail(e.toString());
207         }
208         assertEquals(d1, d2);
209     }
210     
211 }
212
Popular Tags