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: * DefaultHighLowDatasetTests.java
029: * -------------------------------
030: * (C) Copyright 2006, 2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: DefaultHighLowDatasetTests.java,v 1.1.2.2 2007/02/02 13:54:18 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 28-Nov-2006 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.data.xy.junit;
044:
045: import java.io.ByteArrayInputStream;
046: import java.io.ByteArrayOutputStream;
047: import java.io.ObjectInput;
048: import java.io.ObjectInputStream;
049: import java.io.ObjectOutput;
050: import java.io.ObjectOutputStream;
051: import java.util.Date;
052:
053: import junit.framework.Test;
054: import junit.framework.TestCase;
055: import junit.framework.TestSuite;
056:
057: import org.jfree.data.xy.DefaultHighLowDataset;
058:
059: /**
060: * Tests for the {@link DefaultHighLowDataset} class.
061: */
062: public class DefaultHighLowDatasetTests extends TestCase {
063:
064: /**
065: * Returns the tests as a test suite.
066: *
067: * @return The test suite.
068: */
069: public static Test suite() {
070: return new TestSuite(DefaultHighLowDatasetTests.class);
071: }
072:
073: /**
074: * Constructs a new set of tests.
075: *
076: * @param name the name of the tests.
077: */
078: public DefaultHighLowDatasetTests(String name) {
079: super (name);
080: }
081:
082: /**
083: * Confirm that the equals method can distinguish all the required fields.
084: */
085: public void testEquals() {
086: DefaultHighLowDataset d1 = new DefaultHighLowDataset(
087: "Series 1", new Date[0], new double[0], new double[0],
088: new double[0], new double[0], new double[0]);
089: DefaultHighLowDataset d2 = new DefaultHighLowDataset(
090: "Series 1", new Date[0], new double[0], new double[0],
091: new double[0], new double[0], new double[0]);
092: assertTrue(d1.equals(d2));
093: assertTrue(d2.equals(d1));
094:
095: d1 = new DefaultHighLowDataset("Series 2", new Date[0],
096: new double[0], new double[0], new double[0],
097: new double[0], new double[0]);
098: assertFalse(d1.equals(d2));
099: d2 = new DefaultHighLowDataset("Series 2", new Date[0],
100: new double[0], new double[0], new double[0],
101: new double[0], new double[0]);
102: assertTrue(d1.equals(d2));
103:
104: d1 = new DefaultHighLowDataset("Series 2",
105: new Date[] { new Date(123L) }, new double[1],
106: new double[1], new double[1], new double[1],
107: new double[1]);
108: assertFalse(d1.equals(d2));
109: d2 = new DefaultHighLowDataset("Series 2",
110: new Date[] { new Date(123L) }, new double[1],
111: new double[1], new double[1], new double[1],
112: new double[1]);
113: assertTrue(d1.equals(d2));
114:
115: d1 = new DefaultHighLowDataset("Series 2",
116: new Date[] { new Date(123L) }, new double[] { 1.2 },
117: new double[1], new double[1], new double[1],
118: new double[1]);
119: assertFalse(d1.equals(d2));
120: d2 = new DefaultHighLowDataset("Series 2",
121: new Date[] { new Date(123L) }, new double[] { 1.2 },
122: new double[1], new double[1], new double[1],
123: new double[1]);
124: assertTrue(d1.equals(d2));
125:
126: d1 = new DefaultHighLowDataset("Series 2",
127: new Date[] { new Date(123L) }, new double[] { 1.2 },
128: new double[] { 3.4 }, new double[1], new double[1],
129: new double[1]);
130: assertFalse(d1.equals(d2));
131: d2 = new DefaultHighLowDataset("Series 2",
132: new Date[] { new Date(123L) }, new double[] { 1.2 },
133: new double[] { 3.4 }, new double[1], new double[1],
134: new double[1]);
135: assertTrue(d1.equals(d2));
136:
137: d1 = new DefaultHighLowDataset("Series 2",
138: new Date[] { new Date(123L) }, new double[] { 1.2 },
139: new double[] { 3.4 }, new double[] { 5.6 },
140: new double[1], new double[1]);
141: assertFalse(d1.equals(d2));
142: d2 = new DefaultHighLowDataset("Series 2",
143: new Date[] { new Date(123L) }, new double[] { 1.2 },
144: new double[] { 3.4 }, new double[] { 5.6 },
145: new double[1], new double[1]);
146: assertTrue(d1.equals(d2));
147:
148: d1 = new DefaultHighLowDataset("Series 2",
149: new Date[] { new Date(123L) }, new double[] { 1.2 },
150: new double[] { 3.4 }, new double[] { 5.6 },
151: new double[] { 7.8 }, new double[1]);
152: assertFalse(d1.equals(d2));
153: d2 = new DefaultHighLowDataset("Series 2",
154: new Date[] { new Date(123L) }, new double[] { 1.2 },
155: new double[] { 3.4 }, new double[] { 5.6 },
156: new double[] { 7.8 }, new double[1]);
157: assertTrue(d1.equals(d2));
158:
159: d1 = new DefaultHighLowDataset("Series 2",
160: new Date[] { new Date(123L) }, new double[] { 1.2 },
161: new double[] { 3.4 }, new double[] { 5.6 },
162: new double[] { 7.8 }, new double[] { 99.9 });
163: assertFalse(d1.equals(d2));
164: d2 = new DefaultHighLowDataset("Series 2",
165: new Date[] { new Date(123L) }, new double[] { 1.2 },
166: new double[] { 3.4 }, new double[] { 5.6 },
167: new double[] { 7.8 }, new double[] { 99.9 });
168: assertTrue(d1.equals(d2));
169:
170: }
171:
172: /**
173: * Confirm that cloning works.
174: */
175: public void testCloning() {
176: DefaultHighLowDataset d1 = new DefaultHighLowDataset(
177: "Series 1", new Date[] { new Date(123L) },
178: new double[] { 1.2 }, new double[] { 3.4 },
179: new double[] { 5.6 }, new double[] { 7.8 },
180: new double[] { 99.9 });
181: DefaultHighLowDataset d2 = null;
182: try {
183: d2 = (DefaultHighLowDataset) d1.clone();
184: } catch (CloneNotSupportedException e) {
185: e.printStackTrace();
186: }
187: assertTrue(d1 != d2);
188: assertTrue(d1.getClass() == d2.getClass());
189: assertTrue(d1.equals(d2));
190: }
191:
192: /**
193: * Serialize an instance, restore it, and check for equality.
194: */
195: public void testSerialization() {
196: DefaultHighLowDataset d1 = new DefaultHighLowDataset(
197: "Series 1", new Date[] { new Date(123L) },
198: new double[] { 1.2 }, new double[] { 3.4 },
199: new double[] { 5.6 }, new double[] { 7.8 },
200: new double[] { 99.9 });
201: DefaultHighLowDataset d2 = null;
202:
203: try {
204: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
205: ObjectOutput out = new ObjectOutputStream(buffer);
206: out.writeObject(d1);
207: out.close();
208:
209: ObjectInput in = new ObjectInputStream(
210: new ByteArrayInputStream(buffer.toByteArray()));
211: d2 = (DefaultHighLowDataset) in.readObject();
212: in.close();
213: } catch (Exception e) {
214: e.printStackTrace();
215: }
216: assertEquals(d1, d2);
217: }
218:
219: }
|