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