001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * -------------------------------------------
028: * DefaultStatisticalCategoryDatasetTests.java
029: * -------------------------------------------
030: * (C) Copyright 2005, 2006, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: DefaultStatisticalCategoryDatasetTests.java,v 1.1.2.1 2006/10/03 15:41:42 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 05-Feb-2005 : Version 1 (DG);
040: * 03-Aug-2006 : Added testGetRangeBounds() method (DG);
041: *
042: */
043:
044: package org.jfree.data.statistics.junit;
045:
046: import java.io.ByteArrayInputStream;
047: import java.io.ByteArrayOutputStream;
048: import java.io.ObjectInput;
049: import java.io.ObjectInputStream;
050: import java.io.ObjectOutput;
051: import java.io.ObjectOutputStream;
052:
053: import junit.framework.Test;
054: import junit.framework.TestCase;
055: import junit.framework.TestSuite;
056:
057: import org.jfree.data.Range;
058: import org.jfree.data.statistics.DefaultStatisticalCategoryDataset;
059:
060: /**
061: * Tests for the {@link DefaultStatisticalCategoryDataset} class.
062: */
063: public class DefaultStatisticalCategoryDatasetTests extends TestCase {
064:
065: /**
066: * Returns the tests as a test suite.
067: *
068: * @return The test suite.
069: */
070: public static Test suite() {
071: return new TestSuite(
072: DefaultStatisticalCategoryDatasetTests.class);
073: }
074:
075: /**
076: * Constructs a new set of tests.
077: *
078: * @param name the name of the tests.
079: */
080: public DefaultStatisticalCategoryDatasetTests(String name) {
081: super (name);
082: }
083:
084: /**
085: * Some checks for the getRangeBounds() method.
086: */
087: public void testGetRangeBounds() {
088: DefaultStatisticalCategoryDataset d = new DefaultStatisticalCategoryDataset();
089:
090: // an empty dataset should return null for bounds
091: assertNull(d.getRangeBounds(true));
092:
093: // try a dataset with a single value
094: d.add(4.5, 1.0, "R1", "C1");
095: assertEquals(new Range(4.5, 4.5), d.getRangeBounds(false));
096: assertEquals(new Range(3.5, 5.5), d.getRangeBounds(true));
097:
098: // try a dataset with two values
099: 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), d
111: .getRangeBounds(false));
112: assertEquals(new Range(Double.NEGATIVE_INFINITY, 5.5), d
113: .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 = new DefaultStatisticalCategoryDataset();
128: DefaultStatisticalCategoryDataset d2 = new DefaultStatisticalCategoryDataset();
129: assertTrue(d1.equals(d2));
130: assertTrue(d2.equals(d1));
131:
132: }
133:
134: /**
135: * Some checks for cloning.
136: */
137: public void testCloning() {
138: DefaultStatisticalCategoryDataset d1 = new DefaultStatisticalCategoryDataset();
139: d1.add(1.1, 2.2, "R1", "C1");
140: d1.add(3.3, 4.4, "R1", "C2");
141: d1.add(null, new Double(5.5), "R1", "C3");
142: d1.add(new Double(6.6), null, "R2", "C3");
143: DefaultStatisticalCategoryDataset d2 = null;
144: try {
145: d2 = (DefaultStatisticalCategoryDataset) d1.clone();
146: } catch (CloneNotSupportedException e) {
147: fail(e.toString());
148: }
149: assertTrue(d1 != d2);
150: assertTrue(d1.getClass() == d2.getClass());
151: assertTrue(d1.equals(d2));
152: }
153:
154: /**
155: * Check serialization of a default instance.
156: */
157: public void testSerialization1() {
158: DefaultStatisticalCategoryDataset d1 = new DefaultStatisticalCategoryDataset();
159: d1.add(1.1, 2.2, "R1", "C1");
160: d1.add(3.3, 4.4, "R1", "C2");
161: d1.add(null, new Double(5.5), "R1", "C3");
162: d1.add(new Double(6.6), null, "R2", "C3");
163: DefaultStatisticalCategoryDataset d2 = null;
164: try {
165: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
166: ObjectOutput out = new ObjectOutputStream(buffer);
167: out.writeObject(d1);
168: out.close();
169:
170: ObjectInput in = new ObjectInputStream(
171: new ByteArrayInputStream(buffer.toByteArray()));
172: d2 = (DefaultStatisticalCategoryDataset) in.readObject();
173: in.close();
174: } catch (Exception e) {
175: fail(e.toString());
176: }
177: assertEquals(d1, d2);
178: }
179:
180: /**
181: * Check serialization of a more complex instance.
182: */
183: public void testSerialization2() {
184: DefaultStatisticalCategoryDataset d1 = new DefaultStatisticalCategoryDataset();
185: d1.add(1.2, 3.4, "Row 1", "Column 1");
186: DefaultStatisticalCategoryDataset d2 = null;
187: try {
188: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
189: ObjectOutput out = new ObjectOutputStream(buffer);
190: out.writeObject(d1);
191: out.close();
192:
193: ObjectInput in = new ObjectInputStream(
194: new ByteArrayInputStream(buffer.toByteArray()));
195: d2 = (DefaultStatisticalCategoryDataset) in.readObject();
196: in.close();
197: } catch (Exception e) {
198: fail(e.toString());
199: }
200: assertEquals(d1, d2);
201: }
202:
203: }
|