KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, 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  * TableXYDatasetTests.java
29  * ------------------------
30  * (C) Copyright 2003-2005, by Richard Atkinson and Contributors.
31  *
32  * Original Author: Richard Atkinson;
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  *
35  * $Id: TableXYDatasetTests.java,v 1.1.2.1 2006/10/03 15:41:38 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 11-Aug-2003 : Version 1 (RA);
40  * 18-Aug-2003 : Added tests for event notification when removing and updating
41  * series (RA);
42  * 22-Sep-2003 : Changed to recognise that empty values are now null rather
43  * than zero (RA);
44  * 16-Feb-2004 : Added some additional tests (DG);
45  * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
46  * getYValue() (DG);
47  *
48  */

49
50 package org.jfree.data.xy.junit;
51
52 import java.io.ByteArrayInputStream JavaDoc;
53 import java.io.ByteArrayOutputStream JavaDoc;
54 import java.io.ObjectInput JavaDoc;
55 import java.io.ObjectInputStream JavaDoc;
56 import java.io.ObjectOutput JavaDoc;
57 import java.io.ObjectOutputStream JavaDoc;
58
59 import junit.framework.Test;
60 import junit.framework.TestCase;
61 import junit.framework.TestSuite;
62
63 import org.jfree.data.xy.DefaultTableXYDataset;
64 import org.jfree.data.xy.XYSeries;
65
66 /**
67  * Tests for {@link DefaultTableXYDataset}.
68  *
69  * @author Richard Atkinson
70  */

71 public class TableXYDatasetTests extends TestCase {
72
73     /**
74      * Returns the tests as a test suite.
75      *
76      * @return The test suite.
77      */

78     public static Test suite() {
79         return new TestSuite(TableXYDatasetTests.class);
80     }
81
82     /**
83      * Constructs a new set of tests.
84      *
85      * @param name the name of the tests.
86      */

87     public TableXYDatasetTests(String JavaDoc name) {
88         super(name);
89     }
90
91     /**
92      * Confirm that the equals method can distinguish all the required fields.
93      */

94     public void testEquals() {
95         
96         DefaultTableXYDataset d1 = new DefaultTableXYDataset();
97         DefaultTableXYDataset d2 = new DefaultTableXYDataset();
98         assertTrue(d1.equals(d2));
99         assertTrue(d2.equals(d1));
100
101         d1.addSeries(createSeries1());
102         assertFalse(d1.equals(d2));
103
104         d2.addSeries(createSeries1());
105         assertTrue(d1.equals(d2));
106
107     }
108
109     /**
110      * Confirm that cloning works.
111      */

112     public void testCloning() {
113         DefaultTableXYDataset d1 = new DefaultTableXYDataset();
114         d1.addSeries(createSeries1());
115         DefaultTableXYDataset d2 = null;
116         try {
117             d2 = (DefaultTableXYDataset) d1.clone();
118         }
119         catch (CloneNotSupportedException JavaDoc e) {
120             System.err.println("Failed to clone.");
121         }
122         assertTrue(d1 != d2);
123         assertTrue(d1.getClass() == d2.getClass());
124         assertTrue(d1.equals(d2));
125     }
126
127     /**
128      * Serialize an instance, restore it, and check for equality.
129      */

130     public void testSerialization() {
131
132         DefaultTableXYDataset d1 = new DefaultTableXYDataset();
133         d1.addSeries(createSeries2());
134         DefaultTableXYDataset d2 = null;
135         
136         try {
137             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
138             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
139             out.writeObject(d1);
140             out.close();
141
142             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
143                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
144             );
145             d2 = (DefaultTableXYDataset) in.readObject();
146             in.close();
147         }
148         catch (Exception JavaDoc e) {
149             System.out.println(e.toString());
150         }
151         assertEquals(d1, d2);
152
153     }
154
155     /**
156      * Assorted tests.
157      */

158     public void testTableXYDataset() {
159         
160         XYSeries series1 = createSeries1();
161         XYSeries series2 = createSeries2();
162         
163         DefaultTableXYDataset dataset = new DefaultTableXYDataset();
164         dataset.addSeries(series1);
165         dataset.addSeries(series2);
166
167         // Test that there are 6 X points and some specific values
168
assertEquals(6, dataset.getItemCount());
169         assertEquals(6, dataset.getX(0, 5).intValue());
170         assertEquals(null, dataset.getY(0, 5));
171         assertEquals(6, dataset.getX(1, 5).intValue());
172         assertEquals(2, dataset.getY(1, 5).intValue());
173
174         // after adding a point to a series, check that there are now 7
175
// items in each series
176
series2.add(7, 2);
177         assertEquals(7, dataset.getItemCount());
178         assertEquals(null, dataset.getY(0, 6));
179         assertEquals(2, dataset.getY(1, 6).intValue());
180
181         // Remove series 1
182
dataset.removeSeries(series1);
183         // Test that there are still 7 X points
184
assertEquals(7, dataset.getItemCount());
185
186         // Remove series 2 and add new series
187
dataset.removeSeries(series2);
188         series1 = createSeries1();
189         dataset.addSeries(series1);
190
191         // Test that there are now 4 X points
192
assertEquals(4, dataset.getItemCount());
193
194     }
195     
196     /**
197      * A test for bug report 788597.
198      */

199     public void test788597() {
200         DefaultTableXYDataset dataset = new DefaultTableXYDataset();
201         dataset.addSeries(createSeries1());
202         assertEquals(4, dataset.getItemCount());
203         dataset.removeAllSeries();
204         assertEquals(0, dataset.getItemCount());
205     }
206     
207     /**
208      * Test that removing all values for a given x works.
209      */

210     public void testRemoveAllValuesForX() {
211         DefaultTableXYDataset dataset = new DefaultTableXYDataset();
212         dataset.addSeries(createSeries1());
213         dataset.addSeries(createSeries2());
214         dataset.removeAllValuesForX(new Double JavaDoc(2.0));
215         assertEquals(5, dataset.getItemCount());
216         assertEquals(new Double JavaDoc(1.0), dataset.getX(0, 0));
217         assertEquals(new Double JavaDoc(3.0), dataset.getX(0, 1));
218         assertEquals(new Double JavaDoc(4.0), dataset.getX(0, 2));
219         assertEquals(new Double JavaDoc(5.0), dataset.getX(0, 3));
220         assertEquals(new Double JavaDoc(6.0), dataset.getX(0, 4));
221     }
222     
223     /**
224      * Tests to see that pruning removes unwanted x values.
225      */

226     public void testPrune() {
227         DefaultTableXYDataset dataset = new DefaultTableXYDataset();
228         dataset.addSeries(createSeries1());
229         dataset.addSeries(createSeries2());
230         dataset.removeSeries(1);
231         dataset.prune();
232         assertEquals(4, dataset.getItemCount());
233     }
234     
235     /**
236      * Tests the auto-pruning feature.
237      */

238     public void testAutoPrune() {
239         
240         // WITH AUTOPRUNING
241
DefaultTableXYDataset dataset = new DefaultTableXYDataset(true);
242         dataset.addSeries(createSeriesA());
243         assertEquals(2, dataset.getItemCount()); // should be 2 items
244
dataset.addSeries(createSeriesB());
245         assertEquals(2, dataset.getItemCount()); // still 2
246
dataset.removeSeries(1);
247         assertEquals(1, dataset.getItemCount()); // 1 value pruned.
248

249         // WITHOUT AUTOPRUNING
250
DefaultTableXYDataset dataset2 = new DefaultTableXYDataset(true);
251         dataset2.addSeries(createSeriesA());
252         assertEquals(2, dataset2.getItemCount()); // should be 2 items
253
dataset2.addSeries(createSeriesB());
254         assertEquals(2, dataset2.getItemCount()); // still 2
255
dataset2.removeSeries(1);
256         assertEquals(1, dataset2.getItemCount()); // still 2.
257

258     }
259     
260     /**
261      * Creates a series for testing.
262      *
263      * @return A series.
264      */

265     private XYSeries createSeriesA() {
266         XYSeries s = new XYSeries("A", true, false);
267         s.add(1.0, 1.1);
268         s.add(2.0, null);
269         return s;
270     }
271     
272     /**
273      * Creates a series for testing.
274      *
275      * @return A series.
276      */

277     private XYSeries createSeriesB() {
278         XYSeries s = new XYSeries("B", true, false);
279         s.add(1.0, null);
280         s.add(2.0, 2.2);
281         return s;
282     }
283     
284     /**
285      * Creates a series for testing.
286      *
287      * @return A series.
288      */

289     private XYSeries createSeries1() {
290         XYSeries series1 = new XYSeries("Series 1", true, false);
291         series1.add(1.0, 1.0);
292         series1.add(2.0, 1.0);
293         series1.add(4.0, 1.0);
294         series1.add(5.0, 1.0);
295         return series1;
296     }
297     
298     /**
299      * Creates a series for testing.
300      *
301      * @return A series.
302      */

303     private XYSeries createSeries2() {
304         XYSeries series2 = new XYSeries("Series 2", true, false);
305         series2.add(2.0, 2.0);
306         series2.add(3.0, 2.0);
307         series2.add(4.0, 2.0);
308         series2.add(5.0, 2.0);
309         series2.add(6.0, 2.0);
310         return series2;
311     }
312     
313 }
314
Popular Tags