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: * DefaultIntervalXYDatasetTests.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: DefaultIntervalXYDatasetTests.java,v 1.1.2.3 2006/11/28 14:06:14 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 23-Oct-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.DefaultIntervalXYDataset;
058:
059: /**
060: * Some tests for the {@link DefaultIntervalXYDataset} class.
061: */
062: public class DefaultIntervalXYDatasetTests 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(DefaultIntervalXYDatasetTests.class);
071: }
072:
073: /**
074: * Constructs a new set of tests.
075: *
076: * @param name the name of the tests.
077: */
078: public DefaultIntervalXYDatasetTests(String name) {
079: super (name);
080: }
081:
082: /**
083: * Some checks for the getSeriesCount() method.
084: */
085: public void testGetSeriesCount() {
086: DefaultIntervalXYDataset d = new DefaultIntervalXYDataset();
087: assertEquals(0, d.getSeriesCount());
088: d = createSampleDataset1();
089: assertEquals(2, d.getSeriesCount());
090: }
091:
092: /**
093: * Some checks for the getSeriesKey(int) method.
094: */
095: public void testGetSeriesKey() {
096: DefaultIntervalXYDataset d = createSampleDataset1();
097: assertEquals("S1", d.getSeriesKey(0));
098: assertEquals("S2", d.getSeriesKey(1));
099:
100: // check for series key out of bounds
101: boolean pass = false;
102: try {
103: /*Comparable k =*/d.getSeriesKey(-1);
104: } catch (IllegalArgumentException e) {
105: pass = true;
106: }
107: assertTrue(pass);
108:
109: pass = false;
110: try {
111: /*Comparable k =*/d.getSeriesKey(2);
112: } catch (IllegalArgumentException e) {
113: pass = true;
114: }
115: assertTrue(pass);
116: }
117:
118: /**
119: * Some checks for the getItemCount() method.
120: */
121: public void testGetItemCount() {
122: DefaultIntervalXYDataset d = createSampleDataset1();
123: assertEquals(3, d.getItemCount(0));
124: assertEquals(3, d.getItemCount(1));
125:
126: // try an index out of bounds
127: boolean pass = false;
128: try {
129: d.getItemCount(2);
130: } catch (IllegalArgumentException e) {
131: pass = true;
132: }
133: assertTrue(pass);
134: }
135:
136: private static final double EPSILON = 0.0000000001;
137:
138: /**
139: * Some checks for the getXValue() method.
140: */
141: public void testGetXValue() {
142: DefaultIntervalXYDataset d = createSampleDataset1();
143: assertEquals(1.0, d.getXValue(0, 0), EPSILON);
144: assertEquals(2.0, d.getXValue(0, 1), EPSILON);
145: assertEquals(3.0, d.getXValue(0, 2), EPSILON);
146: assertEquals(11.0, d.getXValue(1, 0), EPSILON);
147: assertEquals(12.0, d.getXValue(1, 1), EPSILON);
148: assertEquals(13.0, d.getXValue(1, 2), EPSILON);
149: }
150:
151: /**
152: * Some checks for the getYValue() method.
153: */
154: public void testGetYValue() {
155: DefaultIntervalXYDataset d = createSampleDataset1();
156: assertEquals(4.0, d.getYValue(0, 0), EPSILON);
157: assertEquals(5.0, d.getYValue(0, 1), EPSILON);
158: assertEquals(6.0, d.getYValue(0, 2), EPSILON);
159: assertEquals(14.0, d.getYValue(1, 0), EPSILON);
160: assertEquals(15.0, d.getYValue(1, 1), EPSILON);
161: assertEquals(16.0, d.getYValue(1, 2), EPSILON);
162: }
163:
164: /**
165: * Some checks for the getStartXValue() method.
166: */
167: public void testGetStartXValue() {
168: DefaultIntervalXYDataset d = createSampleDataset1();
169: assertEquals(0.9, d.getStartXValue(0, 0), EPSILON);
170: assertEquals(1.9, d.getStartXValue(0, 1), EPSILON);
171: assertEquals(2.9, d.getStartXValue(0, 2), EPSILON);
172: assertEquals(10.9, d.getStartXValue(1, 0), EPSILON);
173: assertEquals(11.9, d.getStartXValue(1, 1), EPSILON);
174: assertEquals(12.9, d.getStartXValue(1, 2), EPSILON);
175: }
176:
177: /**
178: * Some checks for the getEndXValue() method.
179: */
180: public void testGetEndXValue() {
181: DefaultIntervalXYDataset d = createSampleDataset1();
182: assertEquals(1.1, d.getEndXValue(0, 0), EPSILON);
183: assertEquals(2.1, d.getEndXValue(0, 1), EPSILON);
184: assertEquals(3.1, d.getEndXValue(0, 2), EPSILON);
185: assertEquals(11.1, d.getEndXValue(1, 0), EPSILON);
186: assertEquals(12.1, d.getEndXValue(1, 1), EPSILON);
187: assertEquals(13.1, d.getEndXValue(1, 2), EPSILON);
188: }
189:
190: /**
191: * Some checks for the getStartYValue() method.
192: */
193: public void testGetStartYValue() {
194: DefaultIntervalXYDataset d = createSampleDataset1();
195: assertEquals(1.09, d.getStartYValue(0, 0), EPSILON);
196: assertEquals(2.09, d.getStartYValue(0, 1), EPSILON);
197: assertEquals(3.09, d.getStartYValue(0, 2), EPSILON);
198: assertEquals(11.09, d.getStartYValue(1, 0), EPSILON);
199: assertEquals(12.09, d.getStartYValue(1, 1), EPSILON);
200: assertEquals(13.09, d.getStartYValue(1, 2), EPSILON);
201: }
202:
203: /**
204: * Some checks for the getEndYValue() method.
205: */
206: public void testGetEndYValue() {
207: DefaultIntervalXYDataset d = createSampleDataset1();
208: assertEquals(1.11, d.getEndYValue(0, 0), EPSILON);
209: assertEquals(2.11, d.getEndYValue(0, 1), EPSILON);
210: assertEquals(3.11, d.getEndYValue(0, 2), EPSILON);
211: assertEquals(11.11, d.getEndYValue(1, 0), EPSILON);
212: assertEquals(12.11, d.getEndYValue(1, 1), EPSILON);
213: assertEquals(13.11, d.getEndYValue(1, 2), EPSILON);
214: }
215:
216: /**
217: * Confirm that the equals method can distinguish all the required fields.
218: */
219: public void testEquals() {
220: DefaultIntervalXYDataset d1 = new DefaultIntervalXYDataset();
221: DefaultIntervalXYDataset d2 = new DefaultIntervalXYDataset();
222: assertTrue(d1.equals(d2));
223: assertTrue(d2.equals(d1));
224:
225: d1 = createSampleDataset1();
226: assertFalse(d1.equals(d2));
227: d2 = createSampleDataset1();
228: assertTrue(d1.equals(d2));
229: }
230:
231: /**
232: * Confirm that cloning works.
233: */
234: public void testCloning() {
235: DefaultIntervalXYDataset d1 = new DefaultIntervalXYDataset();
236: DefaultIntervalXYDataset d2 = null;
237: try {
238: d2 = (DefaultIntervalXYDataset) d1.clone();
239: } catch (CloneNotSupportedException e) {
240: e.printStackTrace();
241: }
242: assertTrue(d1 != d2);
243: assertTrue(d1.getClass() == d2.getClass());
244: assertTrue(d1.equals(d2));
245:
246: // try a dataset with some content...
247: d1 = createSampleDataset1();
248: try {
249: d2 = (DefaultIntervalXYDataset) d1.clone();
250: } catch (CloneNotSupportedException e) {
251: e.printStackTrace();
252: }
253: assertTrue(d1 != d2);
254: assertTrue(d1.getClass() == d2.getClass());
255: assertTrue(d1.equals(d2));
256: }
257:
258: /**
259: * Another test for cloning.
260: */
261: public void testCloning2() {
262: DefaultIntervalXYDataset d1 = new DefaultIntervalXYDataset();
263: double[] x1 = new double[] { 1.0, 2.0, 3.0 };
264: double[] x1Start = new double[] { 0.9, 1.9, 2.9 };
265: double[] x1End = new double[] { 1.1, 2.1, 3.1 };
266: double[] y1 = new double[] { 4.0, 5.0, 6.0 };
267: double[] y1Start = new double[] { 1.09, 2.09, 3.09 };
268: double[] y1End = new double[] { 1.11, 2.11, 3.11 };
269: double[][] data1 = new double[][] { x1, x1Start, x1End, y1,
270: y1Start, y1End };
271: d1.addSeries("S1", data1);
272: DefaultIntervalXYDataset d2 = null;
273: try {
274: d2 = (DefaultIntervalXYDataset) d1.clone();
275: } catch (CloneNotSupportedException e) {
276: e.printStackTrace();
277: }
278: assertTrue(d1 != d2);
279: assertTrue(d1.getClass() == d2.getClass());
280: assertTrue(d1.equals(d2));
281:
282: // check independence
283: x1[0] = 111.1;
284: assertFalse(d1.equals(d2));
285: }
286:
287: /**
288: * Serialize an instance, restore it, and check for equality.
289: */
290: public void testSerialization() {
291:
292: DefaultIntervalXYDataset d1 = new DefaultIntervalXYDataset();
293: DefaultIntervalXYDataset d2 = null;
294:
295: try {
296: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
297: ObjectOutput out = new ObjectOutputStream(buffer);
298: out.writeObject(d1);
299: out.close();
300:
301: ObjectInput in = new ObjectInputStream(
302: new ByteArrayInputStream(buffer.toByteArray()));
303: d2 = (DefaultIntervalXYDataset) in.readObject();
304: in.close();
305: } catch (Exception e) {
306: e.printStackTrace();
307: }
308: assertEquals(d1, d2);
309:
310: // try a dataset with some content...
311: d1 = createSampleDataset1();
312: try {
313: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
314: ObjectOutput out = new ObjectOutputStream(buffer);
315: out.writeObject(d1);
316: out.close();
317:
318: ObjectInput in = new ObjectInputStream(
319: new ByteArrayInputStream(buffer.toByteArray()));
320: d2 = (DefaultIntervalXYDataset) in.readObject();
321: in.close();
322: } catch (Exception e) {
323: e.printStackTrace();
324: }
325: assertEquals(d1, d2);
326:
327: }
328:
329: /**
330: * Some checks for the indexOf(Comparable) method.
331: */
332: public void testIndexOf() {
333: DefaultIntervalXYDataset d = createSampleDataset1();
334: assertEquals(0, d.indexOf("S1"));
335: assertEquals(1, d.indexOf("S2"));
336: assertEquals(-1, d.indexOf("Green Eggs and Ham"));
337: assertEquals(-1, d.indexOf(null));
338: }
339:
340: /**
341: * Some tests for the addSeries() method.
342: */
343: public void testAddSeries() {
344: DefaultIntervalXYDataset d = new DefaultIntervalXYDataset();
345: d.addSeries("S1", new double[][] { { 1.0 }, { 0.5 }, { 1.5 },
346: { 2.0 }, { 2.5 }, { 1.5 } });
347: assertEquals(1, d.getSeriesCount());
348: assertEquals("S1", d.getSeriesKey(0));
349:
350: // check that adding a series will overwrite the old series
351: d.addSeries("S1", new double[][] { { 1.1 }, { 0.6 }, { 1.6 },
352: { 2.1 }, { 2.6 }, { 1.6 } });
353: assertEquals(1, d.getSeriesCount());
354: assertEquals(2.1, d.getYValue(0, 0), EPSILON);
355:
356: // check null key
357: boolean pass = false;
358: try {
359: d.addSeries(null, new double[][] { { 1.1 }, { 0.6 },
360: { 1.6 }, { 2.1 }, { 2.6 }, { 1.6 } });
361: } catch (IllegalArgumentException e) {
362: pass = true;
363: }
364: assertTrue(pass);
365: }
366:
367: /**
368: * Creates a sample dataset for testing.
369: *
370: * @return A sample dataset.
371: */
372: public DefaultIntervalXYDataset createSampleDataset1() {
373: DefaultIntervalXYDataset d = new DefaultIntervalXYDataset();
374: double[] x1 = new double[] { 1.0, 2.0, 3.0 };
375: double[] x1Start = new double[] { 0.9, 1.9, 2.9 };
376: double[] x1End = new double[] { 1.1, 2.1, 3.1 };
377: double[] y1 = new double[] { 4.0, 5.0, 6.0 };
378: double[] y1Start = new double[] { 1.09, 2.09, 3.09 };
379: double[] y1End = new double[] { 1.11, 2.11, 3.11 };
380: double[][] data1 = new double[][] { x1, x1Start, x1End, y1,
381: y1Start, y1End };
382: d.addSeries("S1", data1);
383:
384: double[] x2 = new double[] { 11.0, 12.0, 13.0 };
385: double[] x2Start = new double[] { 10.9, 11.9, 12.9 };
386: double[] x2End = new double[] { 11.1, 12.1, 13.1 };
387: double[] y2 = new double[] { 14.0, 15.0, 16.0 };
388: double[] y2Start = new double[] { 11.09, 12.09, 13.09 };
389: double[] y2End = new double[] { 11.11, 12.11, 13.11 };
390: double[][] data2 = new double[][] { x2, x2Start, x2End, y2,
391: y2Start, y2End };
392: d.addSeries("S2", data2);
393: return d;
394: }
395:
396: }
|