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: * DefaultOHLCDatasetTests.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: DefaultOHLCDatasetTests.java,v 1.1.2.2 2006/11/28 14:06:14 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 29-Apr-2005 : Version 1 (DG);
040: * 28-Nov-2006 : Extended equals() test (DG);
041: *
042: */
043:
044: package org.jfree.data.xy.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: import java.util.Date;
053:
054: import junit.framework.Test;
055: import junit.framework.TestCase;
056: import junit.framework.TestSuite;
057:
058: import org.jfree.data.Range;
059: import org.jfree.data.general.DatasetUtilities;
060: import org.jfree.data.xy.DefaultOHLCDataset;
061: import org.jfree.data.xy.OHLCDataItem;
062:
063: /**
064: * Tests for the {@link DefaultOHLCDataset} class.
065: */
066: public class DefaultOHLCDatasetTests extends TestCase {
067:
068: /**
069: * Returns the tests as a test suite.
070: *
071: * @return The test suite.
072: */
073: public static Test suite() {
074: return new TestSuite(DefaultOHLCDatasetTests.class);
075: }
076:
077: /**
078: * Constructs a new set of tests.
079: *
080: * @param name the name of the tests.
081: */
082: public DefaultOHLCDatasetTests(String name) {
083: super (name);
084: }
085:
086: private static final double EPSILON = 0.0000000001;
087:
088: /**
089: * A small test for the data range calculated on this dataset.
090: */
091: public void testDataRange() {
092: OHLCDataItem[] data = new OHLCDataItem[3];
093: data[0] = new OHLCDataItem(new Date(11L), 2.0, 4.0, 1.0, 3.0,
094: 100.0);
095: data[1] = new OHLCDataItem(new Date(22L), 4.0, 9.0, 2.0, 5.0,
096: 120.0);
097: data[2] = new OHLCDataItem(new Date(33L), 3.0, 7.0, 3.0, 6.0,
098: 140.0);
099: DefaultOHLCDataset d = new DefaultOHLCDataset("S1", data);
100: Range r = DatasetUtilities.findRangeBounds(d, false);
101: assertEquals(1.0, r.getLowerBound(), EPSILON);
102: assertEquals(9.0, r.getUpperBound(), EPSILON);
103: }
104:
105: /**
106: * Confirm that the equals method can distinguish all the required fields.
107: */
108: public void testEquals() {
109: DefaultOHLCDataset d1 = new DefaultOHLCDataset("Series 1",
110: new OHLCDataItem[0]);
111: DefaultOHLCDataset d2 = new DefaultOHLCDataset("Series 1",
112: new OHLCDataItem[0]);
113: assertTrue(d1.equals(d2));
114: assertTrue(d2.equals(d1));
115:
116: d1 = new DefaultOHLCDataset("Series 2", new OHLCDataItem[0]);
117: assertFalse(d1.equals(d2));
118: d2 = new DefaultOHLCDataset("Series 2", new OHLCDataItem[0]);
119: assertTrue(d1.equals(d2));
120:
121: d1 = new DefaultOHLCDataset("Series 2",
122: new OHLCDataItem[] { new OHLCDataItem(new Date(123L),
123: 1.2, 3.4, 5.6, 7.8, 99.9) });
124: assertFalse(d1.equals(d2));
125: d2 = new DefaultOHLCDataset("Series 2",
126: new OHLCDataItem[] { new OHLCDataItem(new Date(123L),
127: 1.2, 3.4, 5.6, 7.8, 99.9) });
128: assertTrue(d1.equals(d2));
129:
130: }
131:
132: /**
133: * Confirm that cloning works.
134: */
135: public void testCloning() {
136: DefaultOHLCDataset d1 = new DefaultOHLCDataset("Series 1",
137: new OHLCDataItem[0]);
138: DefaultOHLCDataset d2 = null;
139: try {
140: d2 = (DefaultOHLCDataset) d1.clone();
141: } catch (CloneNotSupportedException e) {
142: e.printStackTrace();
143: }
144: assertTrue(d1 != d2);
145: assertTrue(d1.getClass() == d2.getClass());
146: assertTrue(d1.equals(d2));
147: }
148:
149: /**
150: * Serialize an instance, restore it, and check for equality.
151: */
152: public void testSerialization() {
153: DefaultOHLCDataset d1 = new DefaultOHLCDataset("Series 1",
154: new OHLCDataItem[0]);
155: DefaultOHLCDataset d2 = null;
156:
157: try {
158: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
159: ObjectOutput out = new ObjectOutputStream(buffer);
160: out.writeObject(d1);
161: out.close();
162:
163: ObjectInput in = new ObjectInputStream(
164: new ByteArrayInputStream(buffer.toByteArray()));
165: d2 = (DefaultOHLCDataset) in.readObject();
166: in.close();
167: } catch (Exception e) {
168: System.out.println(e.toString());
169: }
170: assertEquals(d1, d2);
171: }
172:
173: }
|