001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, 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: * SimpleHistogramDatasetTests.java
029: * --------------------------------
030: * (C) Copyright 2005, 2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: SimpleHistogramDatasetTests.java,v 1.1.2.2 2007/05/21 15:32:11 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 10-Jan-2005 : Version 1 (DG);
040: * 21-May-2007 : Added testClearObservations (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.statistics.SimpleHistogramBin;
058: import org.jfree.data.statistics.SimpleHistogramDataset;
059:
060: /**
061: * Tests for the {@link SimpleHistogramDataset} class.
062: */
063: public class SimpleHistogramDatasetTests 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(SimpleHistogramDatasetTests.class);
072: }
073:
074: /**
075: * Constructs a new set of tests.
076: *
077: * @param name the name of the tests.
078: */
079: public SimpleHistogramDatasetTests(String name) {
080: super (name);
081: }
082:
083: /**
084: * Ensure that the equals() method can distinguish all fields.
085: */
086: public void testEquals() {
087: SimpleHistogramDataset d1 = new SimpleHistogramDataset(
088: "Dataset 1");
089: SimpleHistogramDataset d2 = new SimpleHistogramDataset(
090: "Dataset 1");
091: assertTrue(d1.equals(d2));
092:
093: d1.addBin(new SimpleHistogramBin(1.0, 2.0));
094: assertFalse(d1.equals(d2));
095: d2.addBin(new SimpleHistogramBin(1.0, 2.0));
096: assertTrue(d1.equals(d2));
097: }
098:
099: /**
100: * Some checks for the clone() method.
101: */
102: public void testCloning() {
103: SimpleHistogramDataset d1 = new SimpleHistogramDataset(
104: "Dataset 1");
105: SimpleHistogramDataset d2 = null;
106: try {
107: d2 = (SimpleHistogramDataset) d1.clone();
108: } catch (CloneNotSupportedException e) {
109: e.printStackTrace();
110: }
111: assertTrue(d1 != d2);
112: assertTrue(d1.getClass() == d2.getClass());
113: assertTrue(d1.equals(d2));
114:
115: // check that clone is independent of the original
116: d2.addBin(new SimpleHistogramBin(2.0, 3.0));
117: d2.addObservation(2.3);
118: assertFalse(d1.equals(d2));
119: }
120:
121: /**
122: * Serialize an instance, restore it, and check for equality.
123: */
124: public void testSerialization() {
125: SimpleHistogramDataset d1 = new SimpleHistogramDataset("D1");
126: SimpleHistogramDataset d2 = null;
127: try {
128: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
129: ObjectOutput out = new ObjectOutputStream(buffer);
130: out.writeObject(d1);
131: out.close();
132: ObjectInput in = new ObjectInputStream(
133: new ByteArrayInputStream(buffer.toByteArray()));
134: d2 = (SimpleHistogramDataset) in.readObject();
135: in.close();
136: } catch (Exception e) {
137: e.printStackTrace();
138: }
139: assertEquals(d1, d2);
140: }
141:
142: private static final double EPSILON = 0.0000000001;
143:
144: /**
145: * Some checks for the clearObservations() method.
146: */
147: public void testClearObservations() {
148: SimpleHistogramDataset d1 = new SimpleHistogramDataset("D1");
149: d1.clearObservations();
150: assertEquals(0, d1.getItemCount(0));
151: d1.addBin(new SimpleHistogramBin(0.0, 1.0));
152: d1.addObservation(0.5);
153: assertEquals(1.0, d1.getYValue(0, 0), EPSILON);
154: }
155:
156: /**
157: * Some checks for the removeAllBins() method.
158: */
159: public void testRemoveAllBins() {
160: SimpleHistogramDataset d1 = new SimpleHistogramDataset("D1");
161: d1.addBin(new SimpleHistogramBin(0.0, 1.0));
162: d1.addObservation(0.5);
163: d1.addBin(new SimpleHistogramBin(2.0, 3.0));
164: assertEquals(2, d1.getItemCount(0));
165: d1.removeAllBins();
166: assertEquals(0, d1.getItemCount(0));
167: }
168:
169: }
|