KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DefaultXYDatasetTests.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: DefaultXYDatasetTests.java,v 1.1.2.2 2006/11/02 20:55:21 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 06-Jul-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.DefaultXYDataset;
58
59 /**
60  * Tests for {@link DefaultXYDataset}.
61  */

62 public class DefaultXYDatasetTests 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(DefaultXYDatasetTests.class);
71     }
72
73     /**
74      * Constructs a new set of tests.
75      *
76      * @param name the name of the tests.
77      */

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

85     public void testEquals() {
86  
87         DefaultXYDataset d1 = new DefaultXYDataset();
88         DefaultXYDataset d2 = new DefaultXYDataset();
89         assertTrue(d1.equals(d2));
90         assertTrue(d2.equals(d1));
91
92         double[] x1 = new double[] {1.0, 2.0, 3.0};
93         double[] y1 = new double[] {4.0, 5.0, 6.0};
94         double[][] data1 = new double[][] {x1, y1};
95         double[] x2 = new double[] {1.0, 2.0, 3.0};
96         double[] y2 = new double[] {4.0, 5.0, 6.0};
97         double[][] data2 = new double[][] {x2, y2};
98         d1.addSeries("S1", data1);
99         assertFalse(d1.equals(d2));
100         d2.addSeries("S1", data2);
101         assertTrue(d1.equals(d2));
102     }
103
104     /**
105      * Confirm that cloning works.
106      */

107     public void testCloning() {
108         DefaultXYDataset d1 = new DefaultXYDataset();
109         DefaultXYDataset d2 = null;
110         try {
111             d2 = (DefaultXYDataset) d1.clone();
112         }
113         catch (CloneNotSupportedException JavaDoc e) {
114             e.printStackTrace();
115         }
116         assertTrue(d1 != d2);
117         assertTrue(d1.getClass() == d2.getClass());
118         assertTrue(d1.equals(d2));
119         
120         // try a dataset with some content...
121
double[] x1 = new double[] {1.0, 2.0, 3.0};
122         double[] y1 = new double[] {4.0, 5.0, 6.0};
123         double[][] data1 = new double[][] {x1, y1};
124         d1.addSeries("S1", data1);
125         try {
126             d2 = (DefaultXYDataset) d1.clone();
127         }
128         catch (CloneNotSupportedException JavaDoc e) {
129             e.printStackTrace();
130         }
131         assertTrue(d1 != d2);
132         assertTrue(d1.getClass() == d2.getClass());
133         assertTrue(d1.equals(d2));
134         
135         // check that the clone doesn't share the same underlying arrays.
136
x1[1] = 2.2;
137         assertFalse(d1.equals(d2));
138         x1[1] = 2.0;
139         assertTrue(d1.equals(d2));
140     }
141
142     /**
143      * Serialize an instance, restore it, and check for equality.
144      */

145     public void testSerialization() {
146
147         DefaultXYDataset d1 = new DefaultXYDataset();
148         DefaultXYDataset d2 = null;
149         
150         try {
151             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
152             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
153             out.writeObject(d1);
154             out.close();
155
156             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
157                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
158             );
159             d2 = (DefaultXYDataset) in.readObject();
160             in.close();
161         }
162         catch (Exception JavaDoc e) {
163             e.printStackTrace();
164         }
165         assertEquals(d1, d2);
166
167         // try a dataset with some content...
168
double[] x1 = new double[] {1.0, 2.0, 3.0};
169         double[] y1 = new double[] {4.0, 5.0, 6.0};
170         double[][] data1 = new double[][] {x1, y1};
171         d1.addSeries("S1", data1);
172         try {
173             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
174             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
175             out.writeObject(d1);
176             out.close();
177
178             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
179                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
180             );
181             d2 = (DefaultXYDataset) in.readObject();
182             in.close();
183         }
184         catch (Exception JavaDoc e) {
185             e.printStackTrace();
186         }
187         assertEquals(d1, d2);
188         
189     }
190     
191     /**
192      * Some checks for the getSeriesKey(int) method.
193      */

194     public void testGetSeriesKey() {
195         DefaultXYDataset d = createSampleDataset1();
196         assertEquals("S1", d.getSeriesKey(0));
197         assertEquals("S2", d.getSeriesKey(1));
198         
199         // check for series key out of bounds
200
boolean pass = false;
201         try {
202             /*Comparable k =*/ d.getSeriesKey(-1);
203         }
204         catch (IllegalArgumentException JavaDoc e) {
205             pass = true;
206         }
207         assertTrue(pass);
208         
209         pass = false;
210         try {
211             /*Comparable k =*/ d.getSeriesKey(2);
212         }
213         catch (IllegalArgumentException JavaDoc e) {
214             pass = true;
215         }
216         assertTrue(pass);
217     }
218     
219     /**
220      * Some checks for the indexOf(Comparable) method.
221      */

222     public void testIndexOf() {
223         DefaultXYDataset d = createSampleDataset1();
224         assertEquals(0, d.indexOf("S1"));
225         assertEquals(1, d.indexOf("S2"));
226         assertEquals(-1, d.indexOf("Green Eggs and Ham"));
227         assertEquals(-1, d.indexOf(null));
228     }
229     
230     static final double EPSILON = 0.0000000001;
231     
232     /**
233      * Some tests for the addSeries() method.
234      */

235     public void testAddSeries() {
236         DefaultXYDataset d = new DefaultXYDataset();
237         d.addSeries("S1", new double[][] {{1.0}, {2.0}});
238         assertEquals(1, d.getSeriesCount());
239         assertEquals("S1", d.getSeriesKey(0));
240         
241         // check that adding a series will overwrite the old series
242
d.addSeries("S1", new double[][] {{11.0}, {12.0}});
243         assertEquals(1, d.getSeriesCount());
244         assertEquals(12.0, d.getYValue(0, 0), EPSILON);
245         
246         // check null key
247
boolean pass = false;
248         try
249         {
250           d.addSeries(null, new double[][] {{1.0}, {2.0}});
251         }
252         catch (IllegalArgumentException JavaDoc e)
253         {
254           pass = true;
255         }
256         assertTrue(pass);
257     }
258     
259     /**
260      * Creates a sample dataset for testing.
261      *
262      * @return A sample dataset.
263      */

264     public DefaultXYDataset createSampleDataset1() {
265         DefaultXYDataset d = new DefaultXYDataset();
266         double[] x1 = new double[] {1.0, 2.0, 3.0};
267         double[] y1 = new double[] {4.0, 5.0, 6.0};
268         double[][] data1 = new double[][] {x1, y1};
269         d.addSeries("S1", data1);
270         
271         double[] x2 = new double[] {1.0, 2.0, 3.0};
272         double[] y2 = new double[] {4.0, 5.0, 6.0};
273         double[][] data2 = new double[][] {x2, y2};
274         d.addSeries("S2", data2);
275         return d;
276     }
277     
278 }
279
Popular Tags