KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > xy > junit > XYSeriesTests


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ------------------
28  * XYSeriesTests.java
29  * ------------------
30  * (C) Copyright 2003, 2004, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: XYSeriesTests.java,v 1.1.2.1 2006/10/03 15:41:38 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 23-Dec-2003 : Version 1 (DG);
40  *
41  */

42
43 package org.jfree.data.xy.junit;
44
45 import java.io.ByteArrayInputStream JavaDoc;
46 import java.io.ByteArrayOutputStream JavaDoc;
47 import java.io.ObjectInput JavaDoc;
48 import java.io.ObjectInputStream JavaDoc;
49 import java.io.ObjectOutput JavaDoc;
50 import java.io.ObjectOutputStream JavaDoc;
51
52 import junit.framework.Test;
53 import junit.framework.TestCase;
54 import junit.framework.TestSuite;
55
56 import org.jfree.data.general.SeriesException;
57 import org.jfree.data.xy.XYDataItem;
58 import org.jfree.data.xy.XYSeries;
59
60 /**
61  * Tests for the {@link XYSeries} class.
62  */

63 public class XYSeriesTests extends TestCase {
64
65     /**
66      * Returns the tests as a test suite.
67      *
68      * @return The test suite.
69      */

70     public static Test suite() {
71         return new TestSuite(XYSeriesTests.class);
72     }
73
74     /**
75      * Constructs a new set of tests.
76      *
77      * @param name the name of the tests.
78      */

79     public XYSeriesTests(String JavaDoc name) {
80         super(name);
81     }
82
83     /**
84      * Confirm that the equals method can distinguish all the required fields.
85      */

86     public void testEquals() {
87         
88         XYSeries s1 = new XYSeries("Series");
89         s1.add(1.0, 1.1);
90         XYSeries s2 = new XYSeries("Series");
91         s2.add(1.0, 1.1);
92         assertTrue(s1.equals(s2));
93         assertTrue(s2.equals(s1));
94
95         s1.setKey("Series X");
96         assertFalse(s1.equals(s2));
97
98         s2.setKey("Series X");
99         assertTrue(s1.equals(s2));
100
101     }
102
103     /**
104      * Confirm that cloning works.
105      */

106     public void testCloning() {
107         XYSeries s1 = new XYSeries("Series");
108         s1.add(1.0, 1.1);
109         XYSeries s2 = null;
110         try {
111             s2 = (XYSeries) s1.clone();
112         }
113         catch (CloneNotSupportedException JavaDoc e) {
114             System.err.println("XYSeriesTests.testCloning: failed to clone.");
115         }
116         assertTrue(s1 != s2);
117         assertTrue(s1.getClass() == s2.getClass());
118         assertTrue(s1.equals(s2));
119     }
120
121     /**
122      * Serialize an instance, restore it, and check for equality.
123      */

124     public void testSerialization() {
125
126         XYSeries s1 = new XYSeries("Series");
127         s1.add(1.0, 1.1);
128         XYSeries s2 = null;
129         
130         try {
131             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
132             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
133             out.writeObject(s1);
134             out.close();
135
136             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
137                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
138             );
139             s2 = (XYSeries) in.readObject();
140             in.close();
141         }
142         catch (Exception JavaDoc e) {
143             System.out.println(e.toString());
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 JavaDoc(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 JavaDoc(1.0)));
169         assertEquals(1, s1.indexOf(new Double JavaDoc(3.0)));
170         assertEquals(2, s1.indexOf(new Double JavaDoc(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 JavaDoc(2.0));
185         assertEquals(new Double JavaDoc(3.0), s1.getX(1));
186         
187         s1.remove(0);
188         assertEquals(new Double JavaDoc(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 JavaDoc(1), new Integer JavaDoc(2));
218         assertEquals(new Integer JavaDoc(2), series.getY(0));
219         series.update(new Integer JavaDoc(1), new Integer JavaDoc(3));
220         assertEquals(new Integer JavaDoc(3), series.getY(0));
221         try {
222             series.update(new Integer JavaDoc(2), new Integer JavaDoc(99));
223             assertTrue(false);
224         }
225         catch (SeriesException e) {
226             // got the required exception
227
}
228     }
229     
230     /**
231      * Some checks for the update() method for an unsorted series.
232      */

233     public void testUpdate2() {
234        XYSeries series = new XYSeries("Series", false, true);
235        series.add(5.0, 55.0);
236        series.add(4.0, 44.0);
237        series.add(6.0, 66.0);
238        series.update(new Double JavaDoc(4.0), new Double JavaDoc(99.0));
239        assertEquals(new Double JavaDoc(99.0), series.getY(1));
240     }
241     
242     /**
243      * Some checks for the addOrUpdate() method.
244      */

245     public void testAddOrUpdate() {
246         XYSeries series = new XYSeries("S1");
247         XYDataItem old = series.addOrUpdate(new Long JavaDoc(1), new Long JavaDoc(2));
248         assertTrue(old == null);
249         assertEquals(1, series.getItemCount());
250         assertEquals(new Long JavaDoc(2), series.getY(0));
251         
252         old = series.addOrUpdate(new Long JavaDoc(2), new Long JavaDoc(3));
253         assertTrue(old == null);
254         assertEquals(2, series.getItemCount());
255         assertEquals(new Long JavaDoc(3), series.getY(1));
256
257         old = series.addOrUpdate(new Long JavaDoc(1), new Long JavaDoc(99));
258         assertEquals(new XYDataItem(new Long JavaDoc(1), new Long JavaDoc(2)), old);
259         assertEquals(2, series.getItemCount());
260         assertEquals(new Long JavaDoc(99), series.getY(0));
261         assertEquals(new Long JavaDoc(3), series.getY(1));
262     }
263     
264     /**
265      * Some checks for the addOrUpdate() method for an UNSORTED series.
266      */

267     public void testAddOrUpdate2() {
268         XYSeries series = new XYSeries("Series", false, true);
269         series.add(5.0, 5.5);
270         series.add(6.0, 6.6);
271         series.add(3.0, 3.3);
272         series.add(4.0, 4.4);
273         series.add(2.0, 2.2);
274         series.add(1.0, 1.1);
275         series.addOrUpdate(new Double JavaDoc(3.0), new Double JavaDoc(33.3));
276         series.addOrUpdate(new Double JavaDoc(2.0), new Double JavaDoc(22.2));
277         assertEquals(33.3, series.getY(2).doubleValue(), EPSILON);
278         assertEquals(22.2, series.getY(4).doubleValue(), EPSILON);
279     }
280     
281     /**
282      * Some checks for the add() method for an UNSORTED series.
283      */

284     public void testAdd() {
285         XYSeries series = new XYSeries("Series", false, true);
286         series.add(5.0, 5.50);
287         series.add(5.1, 5.51);
288         series.add(6.0, 6.6);
289         series.add(3.0, 3.3);
290         series.add(4.0, 4.4);
291         series.add(2.0, 2.2);
292         series.add(1.0, 1.1);
293         assertEquals(5.5, series.getY(0).doubleValue(), EPSILON);
294         assertEquals(5.51, series.getY(1).doubleValue(), EPSILON);
295         assertEquals(6.6, series.getY(2).doubleValue(), EPSILON);
296         assertEquals(3.3, series.getY(3).doubleValue(), EPSILON);
297         assertEquals(4.4, series.getY(4).doubleValue(), EPSILON);
298         assertEquals(2.2, series.getY(5).doubleValue(), EPSILON);
299         assertEquals(1.1, series.getY(6).doubleValue(), EPSILON);
300     }
301     
302     /**
303      * A simple check that the maximumItemCount attribute is working.
304      */

305     public void testSetMaximumItemCount() {
306         XYSeries s1 = new XYSeries("S1");
307         assertEquals(Integer.MAX_VALUE, s1.getMaximumItemCount());
308         s1.setMaximumItemCount(2);
309         assertEquals(2, s1.getMaximumItemCount());
310         s1.add(1.0, 1.1);
311         s1.add(2.0, 2.2);
312         s1.add(3.0, 3.3);
313         assertEquals(2.0, s1.getX(0).doubleValue(), EPSILON);
314         assertEquals(3.0, s1.getX(1).doubleValue(), EPSILON);
315     }
316     
317     /**
318      * Check that the maximum item count can be applied retrospectively.
319      */

320     public void testSetMaximumItemCount2() {
321         XYSeries s1 = new XYSeries("S1");
322         s1.add(1.0, 1.1);
323         s1.add(2.0, 2.2);
324         s1.add(3.0, 3.3);
325         s1.setMaximumItemCount(2);
326         assertEquals(2.0, s1.getX(0).doubleValue(), EPSILON);
327         assertEquals(3.0, s1.getX(1).doubleValue(), EPSILON);
328     }
329     
330 }
331
Popular Tags