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: * DefaultXYDatasetTests.java
029: * --------------------------
030: * (C) Copyright 2006, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): ;
034: *
035: * $Id: DefaultXYDatasetTests.java,v 1.1.2.2 2006/11/02 20:55:21 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 06-Jul-2006 : Version 1 (DG);
040: * 02-Nov-2006 : Added testAddSeries() method (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:
053: import junit.framework.Test;
054: import junit.framework.TestCase;
055: import junit.framework.TestSuite;
056:
057: import org.jfree.data.xy.DefaultXYDataset;
058:
059: /**
060: * Tests for {@link DefaultXYDataset}.
061: */
062: public class DefaultXYDatasetTests 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(DefaultXYDatasetTests.class);
071: }
072:
073: /**
074: * Constructs a new set of tests.
075: *
076: * @param name the name of the tests.
077: */
078: public DefaultXYDatasetTests(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:
087: DefaultXYDataset d1 = new DefaultXYDataset();
088: DefaultXYDataset d2 = new DefaultXYDataset();
089: assertTrue(d1.equals(d2));
090: assertTrue(d2.equals(d1));
091:
092: double[] x1 = new double[] { 1.0, 2.0, 3.0 };
093: double[] y1 = new double[] { 4.0, 5.0, 6.0 };
094: double[][] data1 = new double[][] { x1, y1 };
095: double[] x2 = new double[] { 1.0, 2.0, 3.0 };
096: double[] y2 = new double[] { 4.0, 5.0, 6.0 };
097: double[][] data2 = new double[][] { x2, y2 };
098: d1.addSeries("S1", data1);
099: assertFalse(d1.equals(d2));
100: d2.addSeries("S1", data2);
101: assertTrue(d1.equals(d2));
102: }
103:
104: /**
105: * Confirm that cloning works.
106: */
107: public void testCloning() {
108: DefaultXYDataset d1 = new DefaultXYDataset();
109: DefaultXYDataset d2 = null;
110: try {
111: d2 = (DefaultXYDataset) d1.clone();
112: } catch (CloneNotSupportedException e) {
113: e.printStackTrace();
114: }
115: assertTrue(d1 != d2);
116: assertTrue(d1.getClass() == d2.getClass());
117: assertTrue(d1.equals(d2));
118:
119: // try a dataset with some content...
120: double[] x1 = new double[] { 1.0, 2.0, 3.0 };
121: double[] y1 = new double[] { 4.0, 5.0, 6.0 };
122: double[][] data1 = new double[][] { x1, y1 };
123: d1.addSeries("S1", data1);
124: try {
125: d2 = (DefaultXYDataset) d1.clone();
126: } catch (CloneNotSupportedException e) {
127: e.printStackTrace();
128: }
129: assertTrue(d1 != d2);
130: assertTrue(d1.getClass() == d2.getClass());
131: assertTrue(d1.equals(d2));
132:
133: // check that the clone doesn't share the same underlying arrays.
134: x1[1] = 2.2;
135: assertFalse(d1.equals(d2));
136: x1[1] = 2.0;
137: assertTrue(d1.equals(d2));
138: }
139:
140: /**
141: * Serialize an instance, restore it, and check for equality.
142: */
143: public void testSerialization() {
144:
145: DefaultXYDataset d1 = new DefaultXYDataset();
146: DefaultXYDataset d2 = null;
147:
148: try {
149: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
150: ObjectOutput out = new ObjectOutputStream(buffer);
151: out.writeObject(d1);
152: out.close();
153:
154: ObjectInput in = new ObjectInputStream(
155: new ByteArrayInputStream(buffer.toByteArray()));
156: d2 = (DefaultXYDataset) in.readObject();
157: in.close();
158: } catch (Exception e) {
159: e.printStackTrace();
160: }
161: assertEquals(d1, d2);
162:
163: // try a dataset with some content...
164: double[] x1 = new double[] { 1.0, 2.0, 3.0 };
165: double[] y1 = new double[] { 4.0, 5.0, 6.0 };
166: double[][] data1 = new double[][] { x1, y1 };
167: d1.addSeries("S1", data1);
168: try {
169: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
170: ObjectOutput out = new ObjectOutputStream(buffer);
171: out.writeObject(d1);
172: out.close();
173:
174: ObjectInput in = new ObjectInputStream(
175: new ByteArrayInputStream(buffer.toByteArray()));
176: d2 = (DefaultXYDataset) in.readObject();
177: in.close();
178: } catch (Exception e) {
179: e.printStackTrace();
180: }
181: assertEquals(d1, d2);
182:
183: }
184:
185: /**
186: * Some checks for the getSeriesKey(int) method.
187: */
188: public void testGetSeriesKey() {
189: DefaultXYDataset d = createSampleDataset1();
190: assertEquals("S1", d.getSeriesKey(0));
191: assertEquals("S2", d.getSeriesKey(1));
192:
193: // check for series key out of bounds
194: boolean pass = false;
195: try {
196: /*Comparable k =*/d.getSeriesKey(-1);
197: } catch (IllegalArgumentException e) {
198: pass = true;
199: }
200: assertTrue(pass);
201:
202: pass = false;
203: try {
204: /*Comparable k =*/d.getSeriesKey(2);
205: } catch (IllegalArgumentException e) {
206: pass = true;
207: }
208: assertTrue(pass);
209: }
210:
211: /**
212: * Some checks for the indexOf(Comparable) method.
213: */
214: public void testIndexOf() {
215: DefaultXYDataset d = createSampleDataset1();
216: assertEquals(0, d.indexOf("S1"));
217: assertEquals(1, d.indexOf("S2"));
218: assertEquals(-1, d.indexOf("Green Eggs and Ham"));
219: assertEquals(-1, d.indexOf(null));
220: }
221:
222: static final double EPSILON = 0.0000000001;
223:
224: /**
225: * Some tests for the addSeries() method.
226: */
227: public void testAddSeries() {
228: DefaultXYDataset d = new DefaultXYDataset();
229: d.addSeries("S1", new double[][] { { 1.0 }, { 2.0 } });
230: assertEquals(1, d.getSeriesCount());
231: assertEquals("S1", d.getSeriesKey(0));
232:
233: // check that adding a series will overwrite the old series
234: d.addSeries("S1", new double[][] { { 11.0 }, { 12.0 } });
235: assertEquals(1, d.getSeriesCount());
236: assertEquals(12.0, d.getYValue(0, 0), EPSILON);
237:
238: // check null key
239: boolean pass = false;
240: try {
241: d.addSeries(null, new double[][] { { 1.0 }, { 2.0 } });
242: } catch (IllegalArgumentException e) {
243: pass = true;
244: }
245: assertTrue(pass);
246: }
247:
248: /**
249: * Creates a sample dataset for testing.
250: *
251: * @return A sample dataset.
252: */
253: public DefaultXYDataset createSampleDataset1() {
254: DefaultXYDataset d = new DefaultXYDataset();
255: double[] x1 = new double[] { 1.0, 2.0, 3.0 };
256: double[] y1 = new double[] { 4.0, 5.0, 6.0 };
257: double[][] data1 = new double[][] { x1, y1 };
258: d.addSeries("S1", data1);
259:
260: double[] x2 = new double[] { 1.0, 2.0, 3.0 };
261: double[] y2 = new double[] { 4.0, 5.0, 6.0 };
262: double[][] data2 = new double[][] { x2, y2 };
263: d.addSeries("S2", data2);
264: return d;
265: }
266:
267: }
|