001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ------------------------
028: * TableXYDatasetTests.java
029: * ------------------------
030: * (C) Copyright 2003-2007, by Richard Atkinson and Contributors.
031: *
032: * Original Author: Richard Atkinson;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: TableXYDatasetTests.java,v 1.1.2.3 2007/02/02 15:10:21 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 11-Aug-2003 : Version 1 (RA);
040: * 18-Aug-2003 : Added tests for event notification when removing and updating
041: * series (RA);
042: * 22-Sep-2003 : Changed to recognise that empty values are now null rather
043: * than zero (RA);
044: * 16-Feb-2004 : Added some additional tests (DG);
045: * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
046: * getYValue() (DG);
047: * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
048: *
049: */
050:
051: package org.jfree.data.xy.junit;
052:
053: import java.io.ByteArrayInputStream;
054: import java.io.ByteArrayOutputStream;
055: import java.io.ObjectInput;
056: import java.io.ObjectInputStream;
057: import java.io.ObjectOutput;
058: import java.io.ObjectOutputStream;
059:
060: import junit.framework.Test;
061: import junit.framework.TestCase;
062: import junit.framework.TestSuite;
063:
064: import org.jfree.data.xy.DefaultTableXYDataset;
065: import org.jfree.data.xy.XYSeries;
066:
067: /**
068: * Tests for {@link DefaultTableXYDataset}.
069: */
070: public class TableXYDatasetTests extends TestCase {
071:
072: /**
073: * Returns the tests as a test suite.
074: *
075: * @return The test suite.
076: */
077: public static Test suite() {
078: return new TestSuite(TableXYDatasetTests.class);
079: }
080:
081: /**
082: * Constructs a new set of tests.
083: *
084: * @param name the name of the tests.
085: */
086: public TableXYDatasetTests(String name) {
087: super (name);
088: }
089:
090: /**
091: * Confirm that the equals method can distinguish all the required fields.
092: */
093: public void testEquals() {
094:
095: DefaultTableXYDataset d1 = new DefaultTableXYDataset();
096: DefaultTableXYDataset d2 = new DefaultTableXYDataset();
097: assertTrue(d1.equals(d2));
098: assertTrue(d2.equals(d1));
099:
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: } catch (CloneNotSupportedException e) {
118: System.err.println("Failed to clone.");
119: }
120: assertTrue(d1 != d2);
121: assertTrue(d1.getClass() == d2.getClass());
122: assertTrue(d1.equals(d2));
123: }
124:
125: /**
126: * Serialize an instance, restore it, and check for equality.
127: */
128: public void testSerialization() {
129:
130: DefaultTableXYDataset d1 = new DefaultTableXYDataset();
131: d1.addSeries(createSeries2());
132: DefaultTableXYDataset d2 = null;
133:
134: try {
135: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
136: ObjectOutput out = new ObjectOutputStream(buffer);
137: out.writeObject(d1);
138: out.close();
139:
140: ObjectInput in = new ObjectInputStream(
141: new ByteArrayInputStream(buffer.toByteArray()));
142: d2 = (DefaultTableXYDataset) in.readObject();
143: in.close();
144: } catch (Exception e) {
145: System.out.println(e.toString());
146: }
147: assertEquals(d1, d2);
148:
149: }
150:
151: /**
152: * Assorted tests.
153: */
154: public void testTableXYDataset() {
155:
156: XYSeries series1 = createSeries1();
157: XYSeries series2 = createSeries2();
158:
159: DefaultTableXYDataset dataset = new DefaultTableXYDataset();
160: dataset.addSeries(series1);
161: dataset.addSeries(series2);
162:
163: // Test that there are 6 X points and some specific values
164: assertEquals(6, dataset.getItemCount());
165: assertEquals(6, dataset.getX(0, 5).intValue());
166: assertEquals(null, dataset.getY(0, 5));
167: assertEquals(6, dataset.getX(1, 5).intValue());
168: assertEquals(2, dataset.getY(1, 5).intValue());
169:
170: // after adding a point to a series, check that there are now 7
171: // items in each series
172: series2.add(7, 2);
173: assertEquals(7, dataset.getItemCount());
174: assertEquals(null, dataset.getY(0, 6));
175: assertEquals(2, dataset.getY(1, 6).intValue());
176:
177: // Remove series 1
178: dataset.removeSeries(series1);
179: // Test that there are still 7 X points
180: assertEquals(7, dataset.getItemCount());
181:
182: // Remove series 2 and add new series
183: dataset.removeSeries(series2);
184: series1 = createSeries1();
185: dataset.addSeries(series1);
186:
187: // Test that there are now 4 X points
188: assertEquals(4, dataset.getItemCount());
189:
190: }
191:
192: /**
193: * A test for bug report 788597.
194: */
195: public void test788597() {
196: DefaultTableXYDataset dataset = new DefaultTableXYDataset();
197: dataset.addSeries(createSeries1());
198: assertEquals(4, dataset.getItemCount());
199: dataset.removeAllSeries();
200: assertEquals(0, dataset.getItemCount());
201: }
202:
203: /**
204: * Test that removing all values for a given x works.
205: */
206: public void testRemoveAllValuesForX() {
207: DefaultTableXYDataset dataset = new DefaultTableXYDataset();
208: dataset.addSeries(createSeries1());
209: dataset.addSeries(createSeries2());
210: dataset.removeAllValuesForX(new Double(2.0));
211: assertEquals(5, dataset.getItemCount());
212: assertEquals(new Double(1.0), dataset.getX(0, 0));
213: assertEquals(new Double(3.0), dataset.getX(0, 1));
214: assertEquals(new Double(4.0), dataset.getX(0, 2));
215: assertEquals(new Double(5.0), dataset.getX(0, 3));
216: assertEquals(new Double(6.0), dataset.getX(0, 4));
217: }
218:
219: /**
220: * Tests to see that pruning removes unwanted x values.
221: */
222: public void testPrune() {
223: DefaultTableXYDataset dataset = new DefaultTableXYDataset();
224: dataset.addSeries(createSeries1());
225: dataset.addSeries(createSeries2());
226: dataset.removeSeries(1);
227: dataset.prune();
228: assertEquals(4, dataset.getItemCount());
229: }
230:
231: /**
232: * Tests the auto-pruning feature.
233: */
234: public void testAutoPrune() {
235:
236: // WITH AUTOPRUNING
237: DefaultTableXYDataset dataset = new DefaultTableXYDataset(true);
238: dataset.addSeries(createSeriesA());
239: assertEquals(2, dataset.getItemCount()); // should be 2 items
240: dataset.addSeries(createSeriesB());
241: assertEquals(2, dataset.getItemCount()); // still 2
242: dataset.removeSeries(1);
243: assertEquals(1, dataset.getItemCount()); // 1 value pruned.
244:
245: // WITHOUT AUTOPRUNING
246: DefaultTableXYDataset dataset2 = new DefaultTableXYDataset(true);
247: dataset2.addSeries(createSeriesA());
248: assertEquals(2, dataset2.getItemCount()); // should be 2 items
249: dataset2.addSeries(createSeriesB());
250: assertEquals(2, dataset2.getItemCount()); // still 2
251: dataset2.removeSeries(1);
252: assertEquals(1, dataset2.getItemCount()); // still 2.
253:
254: }
255:
256: /**
257: * Creates a series for testing.
258: *
259: * @return A series.
260: */
261: private XYSeries createSeriesA() {
262: XYSeries s = new XYSeries("A", true, false);
263: s.add(1.0, 1.1);
264: s.add(2.0, null);
265: return s;
266: }
267:
268: /**
269: * Creates a series for testing.
270: *
271: * @return A series.
272: */
273: private XYSeries createSeriesB() {
274: XYSeries s = new XYSeries("B", true, false);
275: s.add(1.0, null);
276: s.add(2.0, 2.2);
277: return s;
278: }
279:
280: /**
281: * Creates a series for testing.
282: *
283: * @return A series.
284: */
285: private XYSeries createSeries1() {
286: XYSeries series1 = new XYSeries("Series 1", true, false);
287: series1.add(1.0, 1.0);
288: series1.add(2.0, 1.0);
289: series1.add(4.0, 1.0);
290: series1.add(5.0, 1.0);
291: return series1;
292: }
293:
294: /**
295: * Creates a series for testing.
296: *
297: * @return A series.
298: */
299: private XYSeries createSeries2() {
300: XYSeries series2 = new XYSeries("Series 2", true, false);
301: series2.add(2.0, 2.0);
302: series2.add(3.0, 2.0);
303: series2.add(4.0, 2.0);
304: series2.add(5.0, 2.0);
305: series2.add(6.0, 2.0);
306: return series2;
307: }
308:
309: }
|