KickJava   Java API By Example, From Geeks To Geeks.

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


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  * XIntervalSeriesTests.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: XIntervalSeriesTests.java,v 1.1.2.2 2006/10/21 05:11:04 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
56 import org.jfree.data.xy.XIntervalSeries;
57
58 /**
59  * Tests for the {@link XIntervalSeries} class.
60  */

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

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

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

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

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

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

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

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

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

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

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

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

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