KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DefaultXYZDatasetTests.java
29  * ---------------------------
30  * (C) Copyright 2006, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: DefaultXYZDatasetTests.java,v 1.1.2.2 2006/11/02 20:55:21 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 12-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.DefaultXYZDataset;
58
59 /**
60  * Tests for {@link DefaultXYZDataset}.
61  */

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

78     public DefaultXYZDatasetTests(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         DefaultXYZDataset d1 = new DefaultXYZDataset();
88         DefaultXYZDataset d2 = new DefaultXYZDataset();
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[] z1 = new double[] {7.0, 8.0, 9.0};
95         double[][] data1 = new double[][] {x1, y1, z1};
96         double[] x2 = new double[] {1.0, 2.0, 3.0};
97         double[] y2 = new double[] {4.0, 5.0, 6.0};
98         double[] z2 = new double[] {7.0, 8.0, 9.0};
99         double[][] data2 = new double[][] {x2, y2, z2};
100         d1.addSeries("S1", data1);
101         assertFalse(d1.equals(d2));
102         d2.addSeries("S1", data2);
103         assertTrue(d1.equals(d2));
104     }
105
106     /**
107      * Confirm that cloning works.
108      */

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

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

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

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

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

267     public DefaultXYZDataset createSampleDataset1() {
268         DefaultXYZDataset d = new DefaultXYZDataset();
269         double[] x1 = new double[] {1.0, 2.0, 3.0};
270         double[] y1 = new double[] {4.0, 5.0, 6.0};
271         double[] z1 = new double[] {7.0, 8.0, 9.0};
272         double[][] data1 = new double[][] {x1, y1, z1};
273         d.addSeries("S1", data1);
274         
275         double[] x2 = new double[] {1.0, 2.0, 3.0};
276         double[] y2 = new double[] {4.0, 5.0, 6.0};
277         double[] z2 = new double[] {7.0, 8.0, 9.0};
278         double[][] data2 = new double[][] {x2, y2, z2};
279         d.addSeries("S2", data2);
280         return d;
281     }
282     
283 }
284
Popular Tags