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 License
20 * along with this library; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 *
23 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24 * in the United States and other countries.]
25 *
26 * ------------------------
27 * TableXYDatasetTests.java
28 * ------------------------
29 * (C) Copyright 2003-2005, by Richard Atkinson and Contributors.
30 *
31 * Original Author: Richard Atkinson;
32 * Contributor(s): David Gilbert (for Object Refinery Limited);
33 *
34 * $Id: TableXYDatasetTests.java,v 1.3 2005/03/15 16:34:36 mungady Exp $
35 *
36 * Changes
37 * -------
38 * 11-Aug-2003 : Version 1 (RA);
39 * 18-Aug-2003 : Added tests for event notification when removing and updating
40 * series (RA);
41 * 22-Sep-2003 : Changed to recognise that empty values are now null rather
42 * than zero (RA);
43 * 16-Feb-2004 : Added some additional tests (DG);
44 * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
45 * getYValue() (DG);
46 *
47 */
48
49 package org.jfree.data.xy.junit;
50
51 import java.io.ByteArrayInputStream;
52 import java.io.ByteArrayOutputStream;
53 import java.io.ObjectInput;
54 import java.io.ObjectInputStream;
55 import java.io.ObjectOutput;
56 import java.io.ObjectOutputStream;
57
58 import junit.framework.Test;
59 import junit.framework.TestCase;
60 import junit.framework.TestSuite;
61
62 import org.jfree.data.xy.DefaultTableXYDataset;
63 import org.jfree.data.xy.XYSeries;
64
65 /**
66 * Tests for {@link DefaultTableXYDataset}.
67 *
68 * @author Richard Atkinson
69 */
70 public class TableXYDatasetTests extends TestCase {
71
72 /**
73 * Returns the tests as a test suite.
74 *
75 * @return The test suite.
76 */
77 public static Test suite() {
78 return new TestSuite(TableXYDatasetTests.class);
79 }
80
81 /**
82 * Constructs a new set of tests.
83 *
84 * @param name the name of the tests.
85 */
86 public TableXYDatasetTests(String name) {
87 super(name);
88 }
89
90 /**
91 * Confirm that the equals method can distinguish all the required fields.
92 */
93 public void testEquals() {
94
95 DefaultTableXYDataset d1 = new DefaultTableXYDataset();
96 DefaultTableXYDataset d2 = new DefaultTableXYDataset();
97 assertTrue(d1.equals(d2));
98 assertTrue(d2.equals(d1));
99
100 d1.addSeries(createSeries1());
101 assertFalse(d1.equals(d2));
102
103 d2.addSeries(createSeries1());
104 assertTrue(d1.equals(d2));
105
106 }
107
108 /**
109 * Confirm that cloning works.
110 */
111 public void testCloning() {
112 DefaultTableXYDataset d1 = new DefaultTableXYDataset();
113 d1.addSeries(createSeries1());
114 DefaultTableXYDataset d2 = null;
115 try {
116 d2 = (DefaultTableXYDataset) d1.clone();
117 }
118 catch (CloneNotSupportedException e) {
119 System.err.println("Failed to clone.");
120 }
121 assertTrue(d1 != d2);
122 assertTrue(d1.getClass() == d2.getClass());
123 assertTrue(d1.equals(d2));
124 }
125
126 /**
127 * Serialize an instance, restore it, and check for equality.
128 */
129 public void testSerialization() {
130
131 DefaultTableXYDataset d1 = new DefaultTableXYDataset();
132 d1.addSeries(createSeries2());
133 DefaultTableXYDataset d2 = null;
134
135 try {
136 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
137 ObjectOutput out = new ObjectOutputStream(buffer);
138 out.writeObject(d1);
139 out.close();
140
141 ObjectInput in = new ObjectInputStream(
142 new ByteArrayInputStream(buffer.toByteArray())
143 );
144 d2 = (DefaultTableXYDataset) in.readObject();
145 in.close();
146 }
147 catch (Exception e) {
148 System.out.println(e.toString());
149 }
150 assertEquals(d1, d2);
151
152 }
153
154 /**
155 * Assorted tests.
156 */
157 public void testTableXYDataset() {
158
159 XYSeries series1 = createSeries1();
160 XYSeries series2 = createSeries2();
161
162 DefaultTableXYDataset dataset = new DefaultTableXYDataset();
163 dataset.addSeries(series1);
164 dataset.addSeries(series2);
165
166 // Test that there are 6 X points and some specific values
167 assertEquals(6, dataset.getItemCount());
168 assertEquals(6, dataset.getX(0, 5).intValue());
169 assertEquals(null, dataset.getY(0, 5));
170 assertEquals(6, dataset.getX(1, 5).intValue());
171 assertEquals(2, dataset.getY(1, 5).intValue());
172
173 // after adding a point to a series, check that there are now 7
174 // items in each series
175 series2.add(7, 2);
176 assertEquals(7, dataset.getItemCount());
177 assertEquals(null, dataset.getY(0, 6));
178 assertEquals(2, dataset.getY(1, 6).intValue());
179
180 // Remove series 1
181 dataset.removeSeries(series1);
182 // Test that there are still 7 X points
183 assertEquals(7, dataset.getItemCount());
184
185 // Remove series 2 and add new series
186 dataset.removeSeries(series2);
187 series1 = createSeries1();
188 dataset.addSeries(series1);
189
190 // Test that there are now 4 X points
191 assertEquals(4, dataset.getItemCount());
192
193 }
194
195 /**
196 * A test for bug report 788597.
197 */
198 public void test788597() {
199 DefaultTableXYDataset dataset = new DefaultTableXYDataset();
200 dataset.addSeries(createSeries1());
201 assertEquals(4, dataset.getItemCount());
202 dataset.removeAllSeries();
203 assertEquals(0, dataset.getItemCount());
204 }
205
206 /**
207 * Test that removing all values for a given x works.
208 */
209 public void testRemoveAllValuesForX() {
210 DefaultTableXYDataset dataset = new DefaultTableXYDataset();
211 dataset.addSeries(createSeries1());
212 dataset.addSeries(createSeries2());
213 dataset.removeAllValuesForX(new Double(2.0));
214 assertEquals(5, dataset.getItemCount());
215 assertEquals(new Double(1.0), dataset.getX(0, 0));
216 assertEquals(new Double(3.0), dataset.getX(0, 1));
217 assertEquals(new Double(4.0), dataset.getX(0, 2));
218 assertEquals(new Double(5.0), dataset.getX(0, 3));
219 assertEquals(new Double(6.0), dataset.getX(0, 4));
220 }
221
222 /**
223 * Tests to see that pruning removes unwanted x values.
224 */
225 public void testPrune() {
226 DefaultTableXYDataset dataset = new DefaultTableXYDataset();
227 dataset.addSeries(createSeries1());
228 dataset.addSeries(createSeries2());
229 dataset.removeSeries(1);
230 dataset.prune();
231 assertEquals(4, dataset.getItemCount());
232 }
233
234 /**
235 * Tests the auto-pruning feature.
236 */
237 public void testAutoPrune() {
238
239 // WITH AUTOPRUNING
240 DefaultTableXYDataset dataset = new DefaultTableXYDataset(true);
241 dataset.addSeries(createSeriesA());
242 assertEquals(2, dataset.getItemCount()); // should be 2 items
243 dataset.addSeries(createSeriesB());
244 assertEquals(2, dataset.getItemCount()); // still 2
245 dataset.removeSeries(1);
246 assertEquals(1, dataset.getItemCount()); // 1 value pruned.
247
248 // WITHOUT AUTOPRUNING
249 DefaultTableXYDataset dataset2 = new DefaultTableXYDataset(true);
250 dataset2.addSeries(createSeriesA());
251 assertEquals(2, dataset2.getItemCount()); // should be 2 items
252 dataset2.addSeries(createSeriesB());
253 assertEquals(2, dataset2.getItemCount()); // still 2
254 dataset2.removeSeries(1);
255 assertEquals(1, dataset2.getItemCount()); // still 2.
256
257 }
258
259 /**
260 * Creates a series for testing.
261 *
262 * @return A series.
263 */
264 private XYSeries createSeriesA() {
265 XYSeries s = new XYSeries("A", true, false);
266 s.add(1.0, 1.1);
267 s.add(2.0, null);
268 return s;
269 }
270
271 /**
272 * Creates a series for testing.
273 *
274 * @return A series.
275 */
276 private XYSeries createSeriesB() {
277 XYSeries s = new XYSeries("B", true, false);
278 s.add(1.0, null);
279 s.add(2.0, 2.2);
280 return s;
281 }
282
283 /**
284 * Creates a series for testing.
285 *
286 * @return A series.
287 */
288 private XYSeries createSeries1() {
289 XYSeries series1 = new XYSeries("Series 1", true, false);
290 series1.add(1.0, 1.0);
291 series1.add(2.0, 1.0);
292 series1.add(4.0, 1.0);
293 series1.add(5.0, 1.0);
294 return series1;
295 }
296
297 /**
298 * Creates a series for testing.
299 *
300 * @return A series.
301 */
302 private XYSeries createSeries2() {
303 XYSeries series2 = new XYSeries("Series 2", true, false);
304 series2.add(2.0, 2.0);
305 series2.add(3.0, 2.0);
306 series2.add(4.0, 2.0);
307 series2.add(5.0, 2.0);
308 series2.add(6.0, 2.0);
309 return series2;
310 }
311
312 }