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: * XYSeriesTests.java
029: * ------------------
030: * (C) Copyright 2003-2007 by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: XYSeriesTests.java,v 1.1.2.3 2007/01/30 14:04:57 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 23-Dec-2003 : Version 1 (DG);
040: * 15-Jan-2007 : Added tests for new toArray() method (DG);
041: * 30-Jan-2007 : Fixed some code that won't compile with Java 1.4 (DG);
042: *
043: */
044:
045: package org.jfree.data.xy.junit;
046:
047: import java.io.ByteArrayInputStream;
048: import java.io.ByteArrayOutputStream;
049: import java.io.ObjectInput;
050: import java.io.ObjectInputStream;
051: import java.io.ObjectOutput;
052: import java.io.ObjectOutputStream;
053:
054: import junit.framework.Test;
055: import junit.framework.TestCase;
056: import junit.framework.TestSuite;
057:
058: import org.jfree.data.general.SeriesException;
059: import org.jfree.data.xy.DefaultXYDataset;
060: import org.jfree.data.xy.XYDataItem;
061: import org.jfree.data.xy.XYSeries;
062:
063: /**
064: * Tests for the {@link XYSeries} class.
065: */
066: public class XYSeriesTests 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(XYSeriesTests.class);
075: }
076:
077: /**
078: * Constructs a new set of tests.
079: *
080: * @param name the name of the tests.
081: */
082: public XYSeriesTests(String name) {
083: super (name);
084: }
085:
086: /**
087: * Confirm that the equals method can distinguish all the required fields.
088: */
089: public void testEquals() {
090:
091: XYSeries s1 = new XYSeries("Series");
092: s1.add(1.0, 1.1);
093: XYSeries s2 = new XYSeries("Series");
094: s2.add(1.0, 1.1);
095: assertTrue(s1.equals(s2));
096: assertTrue(s2.equals(s1));
097:
098: s1.setKey("Series X");
099: assertFalse(s1.equals(s2));
100:
101: s2.setKey("Series X");
102: assertTrue(s1.equals(s2));
103:
104: }
105:
106: /**
107: * Confirm that cloning works.
108: */
109: public void testCloning() {
110: XYSeries s1 = new XYSeries("Series");
111: s1.add(1.0, 1.1);
112: XYSeries s2 = null;
113: try {
114: s2 = (XYSeries) s1.clone();
115: } catch (CloneNotSupportedException e) {
116: e.printStackTrace();
117: }
118: assertTrue(s1 != s2);
119: assertTrue(s1.getClass() == s2.getClass());
120: assertTrue(s1.equals(s2));
121: }
122:
123: /**
124: * Serialize an instance, restore it, and check for equality.
125: */
126: public void testSerialization() {
127:
128: XYSeries s1 = new XYSeries("Series");
129: s1.add(1.0, 1.1);
130: XYSeries s2 = null;
131:
132: try {
133: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
134: ObjectOutput out = new ObjectOutputStream(buffer);
135: out.writeObject(s1);
136: out.close();
137:
138: ObjectInput in = new ObjectInputStream(
139: new ByteArrayInputStream(buffer.toByteArray()));
140: s2 = (XYSeries) in.readObject();
141: in.close();
142: } catch (Exception e) {
143: e.printStackTrace();
144: }
145: assertEquals(s1, s2);
146:
147: }
148:
149: /**
150: * Simple test for the indexOf() method.
151: */
152: public void testIndexOf() {
153: XYSeries s1 = new XYSeries("Series 1");
154: s1.add(1.0, 1.0);
155: s1.add(2.0, 2.0);
156: s1.add(3.0, 3.0);
157: assertEquals(0, s1.indexOf(new Double(1.0)));
158: }
159:
160: /**
161: * A check for the indexOf() method for an unsorted series.
162: */
163: public void testIndexOf2() {
164: XYSeries s1 = new XYSeries("Series 1", false, true);
165: s1.add(1.0, 1.0);
166: s1.add(3.0, 3.0);
167: s1.add(2.0, 2.0);
168: assertEquals(0, s1.indexOf(new Double(1.0)));
169: assertEquals(1, s1.indexOf(new Double(3.0)));
170: assertEquals(2, s1.indexOf(new Double(2.0)));
171: }
172:
173: /**
174: * Simple test for the remove() method.
175: */
176: public void testRemove() {
177:
178: XYSeries s1 = new XYSeries("Series 1");
179: s1.add(1.0, 1.0);
180: s1.add(2.0, 2.0);
181: s1.add(3.0, 3.0);
182:
183: assertEquals(3, s1.getItemCount());
184: s1.remove(new Double(2.0));
185: assertEquals(new Double(3.0), s1.getX(1));
186:
187: s1.remove(0);
188: assertEquals(new Double(3.0), s1.getX(0));
189:
190: }
191:
192: private static final double EPSILON = 0.0000000001;
193:
194: /**
195: * When items are added with duplicate x-values, we expect them to remain
196: * in the order they were added.
197: */
198: public void testAdditionOfDuplicateXValues() {
199: XYSeries s1 = new XYSeries("Series 1");
200: s1.add(1.0, 1.0);
201: s1.add(2.0, 2.0);
202: s1.add(2.0, 3.0);
203: s1.add(2.0, 4.0);
204: s1.add(3.0, 5.0);
205: assertEquals(1.0, s1.getY(0).doubleValue(), EPSILON);
206: assertEquals(2.0, s1.getY(1).doubleValue(), EPSILON);
207: assertEquals(3.0, s1.getY(2).doubleValue(), EPSILON);
208: assertEquals(4.0, s1.getY(3).doubleValue(), EPSILON);
209: assertEquals(5.0, s1.getY(4).doubleValue(), EPSILON);
210: }
211:
212: /**
213: * Some checks for the update(Number, Number) method.
214: */
215: public void testUpdate() {
216: XYSeries series = new XYSeries("S1");
217: series.add(new Integer(1), new Integer(2));
218: assertEquals(new Integer(2), series.getY(0));
219: series.update(new Integer(1), new Integer(3));
220: assertEquals(new Integer(3), series.getY(0));
221: try {
222: series.update(new Integer(2), new Integer(99));
223: assertTrue(false);
224: } catch (SeriesException e) {
225: // got the required exception
226: }
227: }
228:
229: /**
230: * Some checks for the update() method for an unsorted series.
231: */
232: public void testUpdate2() {
233: XYSeries series = new XYSeries("Series", false, true);
234: series.add(5.0, 55.0);
235: series.add(4.0, 44.0);
236: series.add(6.0, 66.0);
237: series.update(new Double(4.0), new Double(99.0));
238: assertEquals(new Double(99.0), series.getY(1));
239: }
240:
241: /**
242: * Some checks for the addOrUpdate() method.
243: */
244: public void testAddOrUpdate() {
245: XYSeries series = new XYSeries("S1");
246: XYDataItem old = series.addOrUpdate(new Long(1), new Long(2));
247: assertTrue(old == null);
248: assertEquals(1, series.getItemCount());
249: assertEquals(new Long(2), series.getY(0));
250:
251: old = series.addOrUpdate(new Long(2), new Long(3));
252: assertTrue(old == null);
253: assertEquals(2, series.getItemCount());
254: assertEquals(new Long(3), series.getY(1));
255:
256: old = series.addOrUpdate(new Long(1), new Long(99));
257: assertEquals(new XYDataItem(new Long(1), new Long(2)), old);
258: assertEquals(2, series.getItemCount());
259: assertEquals(new Long(99), series.getY(0));
260: assertEquals(new Long(3), series.getY(1));
261: }
262:
263: /**
264: * Some checks for the addOrUpdate() method for an UNSORTED series.
265: */
266: public void testAddOrUpdate2() {
267: XYSeries series = new XYSeries("Series", false, true);
268: series.add(5.0, 5.5);
269: series.add(6.0, 6.6);
270: series.add(3.0, 3.3);
271: series.add(4.0, 4.4);
272: series.add(2.0, 2.2);
273: series.add(1.0, 1.1);
274: series.addOrUpdate(new Double(3.0), new Double(33.3));
275: series.addOrUpdate(new Double(2.0), new Double(22.2));
276: assertEquals(33.3, series.getY(2).doubleValue(), EPSILON);
277: assertEquals(22.2, series.getY(4).doubleValue(), EPSILON);
278: }
279:
280: /**
281: * Some checks for the add() method for an UNSORTED series.
282: */
283: public void testAdd() {
284: XYSeries series = new XYSeries("Series", false, true);
285: series.add(5.0, 5.50);
286: series.add(5.1, 5.51);
287: series.add(6.0, 6.6);
288: series.add(3.0, 3.3);
289: series.add(4.0, 4.4);
290: series.add(2.0, 2.2);
291: series.add(1.0, 1.1);
292: assertEquals(5.5, series.getY(0).doubleValue(), EPSILON);
293: assertEquals(5.51, series.getY(1).doubleValue(), EPSILON);
294: assertEquals(6.6, series.getY(2).doubleValue(), EPSILON);
295: assertEquals(3.3, series.getY(3).doubleValue(), EPSILON);
296: assertEquals(4.4, series.getY(4).doubleValue(), EPSILON);
297: assertEquals(2.2, series.getY(5).doubleValue(), EPSILON);
298: assertEquals(1.1, series.getY(6).doubleValue(), EPSILON);
299: }
300:
301: /**
302: * A simple check that the maximumItemCount attribute is working.
303: */
304: public void testSetMaximumItemCount() {
305: XYSeries s1 = new XYSeries("S1");
306: assertEquals(Integer.MAX_VALUE, s1.getMaximumItemCount());
307: s1.setMaximumItemCount(2);
308: assertEquals(2, s1.getMaximumItemCount());
309: s1.add(1.0, 1.1);
310: s1.add(2.0, 2.2);
311: s1.add(3.0, 3.3);
312: assertEquals(2.0, s1.getX(0).doubleValue(), EPSILON);
313: assertEquals(3.0, s1.getX(1).doubleValue(), EPSILON);
314: }
315:
316: /**
317: * Check that the maximum item count can be applied retrospectively.
318: */
319: public void testSetMaximumItemCount2() {
320: XYSeries s1 = new XYSeries("S1");
321: s1.add(1.0, 1.1);
322: s1.add(2.0, 2.2);
323: s1.add(3.0, 3.3);
324: s1.setMaximumItemCount(2);
325: assertEquals(2.0, s1.getX(0).doubleValue(), EPSILON);
326: assertEquals(3.0, s1.getX(1).doubleValue(), EPSILON);
327: }
328:
329: /**
330: * Some checks for the toArray() method.
331: */
332: public void testToArray() {
333: XYSeries s = new XYSeries("S1");
334: double[][] array = s.toArray();
335: assertEquals(2, array.length);
336: assertEquals(0, array[0].length);
337: assertEquals(0, array[1].length);
338:
339: s.add(1.0, 2.0);
340: array = s.toArray();
341: assertEquals(1, array[0].length);
342: assertEquals(1, array[1].length);
343: assertEquals(2, array.length);
344: assertEquals(1.0, array[0][0], EPSILON);
345: assertEquals(2.0, array[1][0], EPSILON);
346:
347: s.add(2.0, null);
348: array = s.toArray();
349: assertEquals(2, array.length);
350: assertEquals(2, array[0].length);
351: assertEquals(2, array[1].length);
352: assertEquals(2.0, array[0][1], EPSILON);
353: assertTrue(Double.isNaN(array[1][1]));
354: }
355:
356: /**
357: * Some checks for an example using the toArray() method.
358: */
359: public void testToArrayExample() {
360: XYSeries s = new XYSeries("S");
361: s.add(1.0, 11.0);
362: s.add(2.0, 22.0);
363: s.add(3.5, 35.0);
364: s.add(5.0, null);
365: DefaultXYDataset dataset = new DefaultXYDataset();
366: dataset.addSeries("S", s.toArray());
367: assertEquals(1, dataset.getSeriesCount());
368: assertEquals(4, dataset.getItemCount(0));
369: assertEquals("S", dataset.getSeriesKey(0));
370: assertEquals(1.0, dataset.getXValue(0, 0), EPSILON);
371: assertEquals(2.0, dataset.getXValue(0, 1), EPSILON);
372: assertEquals(3.5, dataset.getXValue(0, 2), EPSILON);
373: assertEquals(5.0, dataset.getXValue(0, 3), EPSILON);
374: assertEquals(11.0, dataset.getYValue(0, 0), EPSILON);
375: assertEquals(22.0, dataset.getYValue(0, 1), EPSILON);
376: assertEquals(35.0, dataset.getYValue(0, 2), EPSILON);
377: assertTrue(Double.isNaN(dataset.getYValue(0, 3)));
378: }
379:
380: }
|