KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DefaultIntervalXYDatasetTests.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: DefaultIntervalXYDatasetTests.java,v 1.1.2.2 2006/11/02 20:55:21 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 23-Oct-2006 : Version 1 (DG);
40  * 02-Nov-2006 : Added testAddSeries() method (DG);
41  *
42  */

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

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

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

78     public DefaultIntervalXYDatasetTests(String JavaDoc name) {
79         super(name);
80     }
81
82     /**
83      * Some checks for the getSeriesCount() method.
84      */

85     public void testGetSeriesCount() {
86         DefaultIntervalXYDataset d = new DefaultIntervalXYDataset();
87         assertEquals(0, d.getSeriesCount());
88         d = createSampleDataset1();
89         assertEquals(2, d.getSeriesCount());
90     }
91     
92     /**
93      * Some checks for the getSeriesKey(int) method.
94      */

95     public void testGetSeriesKey() {
96         DefaultIntervalXYDataset d = createSampleDataset1();
97         assertEquals("S1", d.getSeriesKey(0));
98         assertEquals("S2", d.getSeriesKey(1));
99         
100         // check for series key out of bounds
101
boolean pass = false;
102         try {
103             /*Comparable k =*/ d.getSeriesKey(-1);
104         }
105         catch (IllegalArgumentException JavaDoc e) {
106             pass = true;
107         }
108         assertTrue(pass);
109         
110         pass = false;
111         try {
112             /*Comparable k =*/ d.getSeriesKey(2);
113         }
114         catch (IllegalArgumentException JavaDoc e) {
115             pass = true;
116         }
117         assertTrue(pass);
118     }
119     
120     /**
121      * Some checks for the getItemCount() method.
122      */

123     public void testGetItemCount() {
124         DefaultIntervalXYDataset d = createSampleDataset1();
125         assertEquals(3, d.getItemCount(0));
126         assertEquals(3, d.getItemCount(1));
127         
128         // try an index out of bounds
129
boolean pass = false;
130         try {
131             d.getItemCount(2);
132         }
133         catch (IllegalArgumentException JavaDoc e) {
134             pass = true;
135         }
136         assertTrue(pass);
137     }
138     
139     private static final double EPSILON = 0.0000000001;
140     
141     /**
142      * Some checks for the getXValue() method.
143      */

144     public void testGetXValue() {
145         DefaultIntervalXYDataset d = createSampleDataset1();
146         assertEquals(1.0, d.getXValue(0, 0), EPSILON);
147         assertEquals(2.0, d.getXValue(0, 1), EPSILON);
148         assertEquals(3.0, d.getXValue(0, 2), EPSILON);
149         assertEquals(11.0, d.getXValue(1, 0), EPSILON);
150         assertEquals(12.0, d.getXValue(1, 1), EPSILON);
151         assertEquals(13.0, d.getXValue(1, 2), EPSILON);
152     }
153     
154     /**
155      * Some checks for the getYValue() method.
156      */

157     public void testGetYValue() {
158         DefaultIntervalXYDataset d = createSampleDataset1();
159         assertEquals(4.0, d.getYValue(0, 0), EPSILON);
160         assertEquals(5.0, d.getYValue(0, 1), EPSILON);
161         assertEquals(6.0, d.getYValue(0, 2), EPSILON);
162         assertEquals(14.0, d.getYValue(1, 0), EPSILON);
163         assertEquals(15.0, d.getYValue(1, 1), EPSILON);
164         assertEquals(16.0, d.getYValue(1, 2), EPSILON);
165     }
166
167     /**
168      * Some checks for the getStartXValue() method.
169      */

170     public void testGetStartXValue() {
171         DefaultIntervalXYDataset d = createSampleDataset1();
172         assertEquals(0.9, d.getStartXValue(0, 0), EPSILON);
173         assertEquals(1.9, d.getStartXValue(0, 1), EPSILON);
174         assertEquals(2.9, d.getStartXValue(0, 2), EPSILON);
175         assertEquals(10.9, d.getStartXValue(1, 0), EPSILON);
176         assertEquals(11.9, d.getStartXValue(1, 1), EPSILON);
177         assertEquals(12.9, d.getStartXValue(1, 2), EPSILON);
178     }
179     
180     /**
181      * Some checks for the getEndXValue() method.
182      */

183     public void testGetEndXValue() {
184         DefaultIntervalXYDataset d = createSampleDataset1();
185         assertEquals(1.1, d.getEndXValue(0, 0), EPSILON);
186         assertEquals(2.1, d.getEndXValue(0, 1), EPSILON);
187         assertEquals(3.1, d.getEndXValue(0, 2), EPSILON);
188         assertEquals(11.1, d.getEndXValue(1, 0), EPSILON);
189         assertEquals(12.1, d.getEndXValue(1, 1), EPSILON);
190         assertEquals(13.1, d.getEndXValue(1, 2), EPSILON);
191     }
192     
193     /**
194      * Some checks for the getStartYValue() method.
195      */

196     public void testGetStartYValue() {
197         DefaultIntervalXYDataset d = createSampleDataset1();
198         assertEquals(1.09, d.getStartYValue(0, 0), EPSILON);
199         assertEquals(2.09, d.getStartYValue(0, 1), EPSILON);
200         assertEquals(3.09, d.getStartYValue(0, 2), EPSILON);
201         assertEquals(11.09, d.getStartYValue(1, 0), EPSILON);
202         assertEquals(12.09, d.getStartYValue(1, 1), EPSILON);
203         assertEquals(13.09, d.getStartYValue(1, 2), EPSILON);
204     }
205     
206     /**
207      * Some checks for the getEndYValue() method.
208      */

209     public void testGetEndYValue() {
210         DefaultIntervalXYDataset d = createSampleDataset1();
211         assertEquals(1.11, d.getEndYValue(0, 0), EPSILON);
212         assertEquals(2.11, d.getEndYValue(0, 1), EPSILON);
213         assertEquals(3.11, d.getEndYValue(0, 2), EPSILON);
214         assertEquals(11.11, d.getEndYValue(1, 0), EPSILON);
215         assertEquals(12.11, d.getEndYValue(1, 1), EPSILON);
216         assertEquals(13.11, d.getEndYValue(1, 2), EPSILON);
217     }
218     
219     /**
220      * Confirm that the equals method can distinguish all the required fields.
221      */

222     public void testEquals() {
223         DefaultIntervalXYDataset d1 = new DefaultIntervalXYDataset();
224         DefaultIntervalXYDataset d2 = new DefaultIntervalXYDataset();
225         assertTrue(d1.equals(d2));
226         assertTrue(d2.equals(d1));
227
228         d1 = createSampleDataset1();
229         assertFalse(d1.equals(d2));
230         d2 = createSampleDataset1();
231         assertTrue(d1.equals(d2));
232     }
233
234     /**
235      * Confirm that cloning works.
236      */

237     public void testCloning() {
238         DefaultIntervalXYDataset d1 = new DefaultIntervalXYDataset();
239         DefaultIntervalXYDataset d2 = null;
240         try {
241             d2 = (DefaultIntervalXYDataset) d1.clone();
242         }
243         catch (CloneNotSupportedException JavaDoc e) {
244             e.printStackTrace();
245         }
246         assertTrue(d1 != d2);
247         assertTrue(d1.getClass() == d2.getClass());
248         assertTrue(d1.equals(d2));
249         
250         // try a dataset with some content...
251
d1 = createSampleDataset1();
252         try {
253             d2 = (DefaultIntervalXYDataset) d1.clone();
254         }
255         catch (CloneNotSupportedException JavaDoc e) {
256             e.printStackTrace();
257         }
258         assertTrue(d1 != d2);
259         assertTrue(d1.getClass() == d2.getClass());
260         assertTrue(d1.equals(d2));
261     }
262
263     /**
264      * Serialize an instance, restore it, and check for equality.
265      */

266     public void testSerialization() {
267
268         DefaultIntervalXYDataset d1 = new DefaultIntervalXYDataset();
269         DefaultIntervalXYDataset d2 = null;
270         
271         try {
272             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
273             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
274             out.writeObject(d1);
275             out.close();
276
277             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
278                     new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
279             d2 = (DefaultIntervalXYDataset) in.readObject();
280             in.close();
281         }
282         catch (Exception JavaDoc e) {
283             e.printStackTrace();
284         }
285         assertEquals(d1, d2);
286
287         // try a dataset with some content...
288
d1 = createSampleDataset1();
289         try {
290             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
291             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
292             out.writeObject(d1);
293             out.close();
294
295             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
296                     new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
297             d2 = (DefaultIntervalXYDataset) in.readObject();
298             in.close();
299         }
300         catch (Exception JavaDoc e) {
301             e.printStackTrace();
302         }
303         assertEquals(d1, d2);
304         
305     }
306     
307     /**
308      * Some checks for the indexOf(Comparable) method.
309      */

310     public void testIndexOf() {
311         DefaultIntervalXYDataset d = createSampleDataset1();
312         assertEquals(0, d.indexOf("S1"));
313         assertEquals(1, d.indexOf("S2"));
314         assertEquals(-1, d.indexOf("Green Eggs and Ham"));
315         assertEquals(-1, d.indexOf(null));
316     }
317     
318     /**
319      * Some tests for the addSeries() method.
320      */

321     public void testAddSeries() {
322         DefaultIntervalXYDataset d = new DefaultIntervalXYDataset();
323         d.addSeries("S1", new double[][] {{1.0}, {0.5}, {1.5}, {2.0}, {2.5},
324                 {1.5}});
325         assertEquals(1, d.getSeriesCount());
326         assertEquals("S1", d.getSeriesKey(0));
327         
328         // check that adding a series will overwrite the old series
329
d.addSeries("S1", new double[][] {{1.1}, {0.6}, {1.6}, {2.1}, {2.6},
330                 {1.6}});
331         assertEquals(1, d.getSeriesCount());
332         assertEquals(2.1, d.getYValue(0, 0), EPSILON);
333         
334         // check null key
335
boolean pass = false;
336         try
337         {
338           d.addSeries(null, new double[][] {{1.1}, {0.6}, {1.6}, {2.1}, {2.6},
339                   {1.6}});
340         }
341         catch (IllegalArgumentException JavaDoc e)
342         {
343           pass = true;
344         }
345         assertTrue(pass);
346     }
347
348     /**
349      * Creates a sample dataset for testing.
350      *
351      * @return A sample dataset.
352      */

353     public DefaultIntervalXYDataset createSampleDataset1() {
354         DefaultIntervalXYDataset d = new DefaultIntervalXYDataset();
355         double[] x1 = new double[] {1.0, 2.0, 3.0};
356         double[] x1Start = new double[] {0.9, 1.9, 2.9};
357         double[] x1End = new double[] {1.1, 2.1, 3.1};
358         double[] y1 = new double[] {4.0, 5.0, 6.0};
359         double[] y1Start = new double[] {1.09, 2.09, 3.09};
360         double[] y1End = new double[] {1.11, 2.11, 3.11};
361         double[][] data1 = new double[][] {x1, x1Start, x1End, y1, y1Start,
362                 y1End};
363         d.addSeries("S1", data1);
364         
365         double[] x2 = new double[] {11.0, 12.0, 13.0};
366         double[] x2Start = new double[] {10.9, 11.9, 12.9};
367         double[] x2End = new double[] {11.1, 12.1, 13.1};
368         double[] y2 = new double[] {14.0, 15.0, 16.0};
369         double[] y2Start = new double[] {11.09, 12.09, 13.09};
370         double[] y2End = new double[] {11.11, 12.11, 13.11};
371         double[][] data2 = new double[][] {x2, x2Start, x2End, y2, y2Start,
372                 y2End};
373         d.addSeries("S2", data2);
374         return d;
375     }
376     
377 }
378
Popular Tags