KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, 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  * XYIntervalSeriesTests.java
29  * --------------------------
30  * (C) Copyright 2006, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: XYIntervalSeriesTests.java,v 1.1.2.1 2006/10/20 15:23:22 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 20-Oct-2006 : Version 1, based on XYSeriesTests (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 import org.jfree.data.xy.XYIntervalSeries;
56
57 /**
58  * Tests for the {@link XYIntervalSeries} class.
59  */

60 public class XYIntervalSeriesTests extends TestCase {
61
62     /**
63      * Returns the tests as a test suite.
64      *
65      * @return The test suite.
66      */

67     public static Test suite() {
68         return new TestSuite(XYIntervalSeriesTests.class);
69     }
70
71     /**
72      * Constructs a new set of tests.
73      *
74      * @param name the name of the tests.
75      */

76     public XYIntervalSeriesTests(String JavaDoc name) {
77         super(name);
78     }
79
80     /**
81      * Confirm that the equals method can distinguish all the required fields.
82      */

83     public void testEquals() {
84         
85         XYIntervalSeries s1 = new XYIntervalSeries("s1");
86         XYIntervalSeries s2 = new XYIntervalSeries("s1");
87         assertTrue(s1.equals(s2));
88         
89         // seriesKey
90
s1 = new XYIntervalSeries("s2");
91         assertFalse(s1.equals(s2));
92         s2 = new XYIntervalSeries("s2");
93         assertTrue(s1.equals(s2));
94         
95         // autoSort
96
s1 = new XYIntervalSeries("s2", false, true);
97         assertFalse(s1.equals(s2));
98         s2 = new XYIntervalSeries("s2", false, true);
99         assertTrue(s1.equals(s2));
100         
101         // allowDuplicateValues
102
s1 = new XYIntervalSeries("s2", false, false);
103         assertFalse(s1.equals(s2));
104         s2 = new XYIntervalSeries("s2", false, false);
105         assertTrue(s1.equals(s2));
106         
107         // add a value
108
s1.add(1.0, 0.5, 1.5, 2.0, 1.9, 2.1);
109         assertFalse(s1.equals(s2));
110         s2.add(1.0, 0.5, 1.5, 2.0, 1.9, 2.1);
111         assertTrue(s2.equals(s1));
112
113         // add another value
114
s1.add(2.0, 0.5, 1.5, 2.0, 1.9, 2.1);
115         assertFalse(s1.equals(s2));
116         s2.add(2.0, 0.5, 1.5, 2.0, 1.9, 2.1);
117         assertTrue(s2.equals(s1));
118
119         // remove a value
120
s1.remove(new Double JavaDoc(1.0));
121         assertFalse(s1.equals(s2));
122         s2.remove(new Double JavaDoc(1.0));
123         assertTrue(s2.equals(s1));
124         
125     }
126
127     /**
128      * Confirm that cloning works.
129      */

130     public void testCloning() {
131         XYIntervalSeries s1 = new XYIntervalSeries("s1");
132         s1.add(1.0, 0.5, 1.5, 2.0, 1.9, 2.01);
133         XYIntervalSeries s2 = null;
134         try {
135             s2 = (XYIntervalSeries) s1.clone();
136         }
137         catch (CloneNotSupportedException JavaDoc e) {
138             e.printStackTrace();
139         }
140         assertTrue(s1 != s2);
141         assertTrue(s1.getClass() == s2.getClass());
142         assertTrue(s1.equals(s2));
143     }
144
145     /**
146      * Serialize an instance, restore it, and check for equality.
147      */

148     public void testSerialization() {
149
150         XYIntervalSeries s1 = new XYIntervalSeries("s1");
151         s1.add(1.0, 0.5, 1.5, 2.0, 1.9, 2.1);
152         XYIntervalSeries s2 = null;
153         
154         try {
155             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
156             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
157             out.writeObject(s1);
158             out.close();
159
160             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
161                     new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
162             s2 = (XYIntervalSeries) in.readObject();
163             in.close();
164         }
165         catch (Exception JavaDoc e) {
166             e.printStackTrace();
167         }
168         assertEquals(s1, s2);
169
170     }
171     
172     /**
173      * Simple test for the indexOf() method.
174      */

175     public void testIndexOf() {
176         XYIntervalSeries s1 = new XYIntervalSeries("Series 1");
177         s1.add(1.0, 1.0, 1.0, 2.0, 1.9, 2.1);
178         s1.add(2.0, 2.0, 2.0, 3.0, 2.9, 3.1);
179         s1.add(3.0, 3.0, 3.0, 4.0, 3.9, 4.1);
180         assertEquals(0, s1.indexOf(new Double JavaDoc(1.0)));
181     }
182     
183     /**
184      * A check for the indexOf() method for an unsorted series.
185      */

186     public void testIndexOf2() {
187         XYIntervalSeries s1 = new XYIntervalSeries("Series 1", false, true);
188         s1.add(1.0, 1.0, 1.0, 2.0, 1.9, 2.1);
189         s1.add(3.0, 3.0, 3.0, 3.0, 2.9, 3.1);
190         s1.add(2.0, 2.0, 2.0, 2.0, 1.9, 2.1);
191         assertEquals(0, s1.indexOf(new Double JavaDoc(1.0)));
192         assertEquals(1, s1.indexOf(new Double JavaDoc(3.0)));
193         assertEquals(2, s1.indexOf(new Double JavaDoc(2.0)));
194     }
195
196     /**
197      * Simple test for the remove() method.
198      */

199     public void testRemove() {
200         XYIntervalSeries s1 = new XYIntervalSeries("Series 1");
201         s1.add(1.0, 1.0, 1.0, 2.0, 1.9, 2.1);
202         s1.add(2.0, 2.0, 2.0, 2.0, 1.9, 2.1);
203         s1.add(3.0, 3.0, 3.0, 3.0, 2.9, 3.1);
204         assertEquals(3, s1.getItemCount());
205         
206         s1.remove(new Double JavaDoc(2.0));
207         assertEquals(new Double JavaDoc(3.0), s1.getX(1));
208         
209         s1.remove(new Double JavaDoc(1.0));
210         assertEquals(new Double JavaDoc(3.0), s1.getX(0));
211     }
212
213     private static final double EPSILON = 0.0000000001;
214     
215     /**
216      * When items are added with duplicate x-values, we expect them to remain
217      * in the order they were added.
218      */

219     public void testAdditionOfDuplicateXValues() {
220         XYIntervalSeries s1 = new XYIntervalSeries("Series 1");
221         s1.add(1.0, 1.0, 1.0, 1.0, 1.0, 1.0);
222         s1.add(2.0, 2.0, 2.0, 2.0, 2.0, 2.0);
223         s1.add(2.0, 3.0, 3.0, 3.0, 3.0, 3.0);
224         s1.add(2.0, 4.0, 4.0, 4.0, 4.0, 4.0);
225         s1.add(3.0, 5.0, 5.0, 5.0, 5.0, 5.0);
226         assertEquals(1.0, s1.getYValue(0), EPSILON);
227         assertEquals(2.0, s1.getYValue(1), EPSILON);
228         assertEquals(3.0, s1.getYValue(2), EPSILON);
229         assertEquals(4.0, s1.getYValue(3), EPSILON);
230         assertEquals(5.0, s1.getYValue(4), EPSILON);
231     }
232     
233     /**
234      * Some checks for the add() method for an UNSORTED series.
235      */

236     public void testAdd() {
237         XYIntervalSeries series = new XYIntervalSeries("Series", false, true);
238         series.add(5.0, 5.50, 5.50, 5.50, 5.50, 5.50);
239         series.add(5.1, 5.51, 5.51, 5.51, 5.51, 5.51);
240         series.add(6.0, 6.6, 6.6, 6.6, 6.6, 6.6);
241         series.add(3.0, 3.3, 3.3, 3.3, 3.3, 3.3);
242         series.add(4.0, 4.4, 4.4, 4.4, 4.4, 4.4);
243         series.add(2.0, 2.2, 2.2, 2.2, 2.2, 2.2);
244         series.add(1.0, 1.1, 1.1, 1.1, 1.1, 1.1);
245         assertEquals(5.5, series.getYValue(0), EPSILON);
246         assertEquals(5.51, series.getYValue(1), EPSILON);
247         assertEquals(6.6, series.getYValue(2), EPSILON);
248         assertEquals(3.3, series.getYValue(3), EPSILON);
249         assertEquals(4.4, series.getYValue(4), EPSILON);
250         assertEquals(2.2, series.getYValue(5), EPSILON);
251         assertEquals(1.1, series.getYValue(6), EPSILON);
252     }
253     
254     /**
255      * A simple check that the maximumItemCount attribute is working.
256      */

257     public void testSetMaximumItemCount() {
258         XYIntervalSeries s1 = new XYIntervalSeries("S1");
259         assertEquals(Integer.MAX_VALUE, s1.getMaximumItemCount());
260         s1.setMaximumItemCount(2);
261         assertEquals(2, s1.getMaximumItemCount());
262         s1.add(1.0, 1.1, 1.1, 1.1, 1.1, 1.1);
263         s1.add(2.0, 2.2, 2.2, 2.2, 2.2, 2.2);
264         s1.add(3.0, 3.3, 3.3, 3.3, 3.3, 3.3);
265         assertEquals(2.0, s1.getX(0).doubleValue(), EPSILON);
266         assertEquals(3.0, s1.getX(1).doubleValue(), EPSILON);
267     }
268     
269     /**
270      * Check that the maximum item count can be applied retrospectively.
271      */

272     public void testSetMaximumItemCount2() {
273         XYIntervalSeries s1 = new XYIntervalSeries("S1");
274         s1.add(1.0, 1.1, 1.1, 1.1, 1.1, 1.1);
275         s1.add(2.0, 2.2, 2.2, 2.2, 2.2, 2.2);
276         s1.add(3.0, 3.3, 3.3, 3.3, 2.2, 2.2);
277         s1.setMaximumItemCount(2);
278         assertEquals(2.0, s1.getX(0).doubleValue(), EPSILON);
279         assertEquals(3.0, s1.getX(1).doubleValue(), EPSILON);
280     }
281     
282 }
283
Popular Tags