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 * DefaultTableXYDatasetTests.java
28 * -------------------------------
29 * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
30 *
31 * Original Author: David Gilbert (for Object Refinery Limited);
32 * Contributor(s): -;
33 *
34 * $Id: DefaultTableXYDatasetTests.java,v 1.3 2005/02/03 09:48:53 mungady Exp $
35 *
36 * Changes
37 * -------
38 * 23-Dec-2003 : Version 1 (DG);
39 *
40 */
41
42 package org.jfree.data.xy.junit;
43
44 import java.io.ByteArrayInputStream;
45 import java.io.ByteArrayOutputStream;
46 import java.io.ObjectInput;
47 import java.io.ObjectInputStream;
48 import java.io.ObjectOutput;
49 import java.io.ObjectOutputStream;
50
51 import junit.framework.Test;
52 import junit.framework.TestCase;
53 import junit.framework.TestSuite;
54
55 import org.jfree.data.xy.DefaultTableXYDataset;
56 import org.jfree.data.xy.XYSeries;
57
58 /**
59 * Tests for the {@link DefaultTableXYDataset} class.
60 */
61 public class DefaultTableXYDatasetTests extends TestCase {
62
63 /**
64 * Returns the tests as a test suite.
65 *
66 * @return The test suite.
67 */
68 public static Test suite() {
69 return new TestSuite(DefaultTableXYDatasetTests.class);
70 }
71
72 /**
73 * Constructs a new set of tests.
74 *
75 * @param name the name of the tests.
76 */
77 public DefaultTableXYDatasetTests(String name) {
78 super(name);
79 }
80
81 /**
82 * Confirm that the equals method can distinguish all the required fields.
83 */
84 public void testEquals() {
85
86 DefaultTableXYDataset d1 = new DefaultTableXYDataset();
87 XYSeries s1 = new XYSeries("Series 1", true, false);
88 s1.add(1.0, 1.1);
89 s1.add(2.0, 2.2);
90 d1.addSeries(s1);
91
92 DefaultTableXYDataset d2 = new DefaultTableXYDataset();
93 XYSeries s2 = new XYSeries("Series 1", true, false);
94 s2.add(1.0, 1.1);
95 s2.add(2.0, 2.2);
96 d2.addSeries(s2);
97
98 assertTrue(d1.equals(d2));
99 assertTrue(d2.equals(d1));
100
101 s1.add(3.0, 3.3);
102 assertFalse(d1.equals(d2));
103
104 s2.add(3.0, 3.3);
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 XYSeries s1 = new XYSeries("Series 1", true, false);
115 s1.add(1.0, 1.1);
116 s1.add(2.0, 2.2);
117 d1.addSeries(s1);
118
119 DefaultTableXYDataset d2 = null;
120 try {
121 d2 = (DefaultTableXYDataset) d1.clone();
122 }
123 catch (CloneNotSupportedException e) {
124 System.err.println("Failed to clone.");
125 }
126 assertTrue(d1 != d2);
127 assertTrue(d1.getClass() == d2.getClass());
128 assertTrue(d1.equals(d2));
129 }
130
131 /**
132 * Serialize an instance, restore it, and check for equality.
133 */
134 public void testSerialization() {
135
136 DefaultTableXYDataset d1 = new DefaultTableXYDataset();
137 XYSeries s1 = new XYSeries("Series 1", true, false);
138 s1.add(1.0, 1.1);
139 s1.add(2.0, 2.2);
140 d1.addSeries(s1);
141
142 DefaultTableXYDataset d2 = null;
143
144 try {
145 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
146 ObjectOutput out = new ObjectOutputStream(buffer);
147 out.writeObject(d1);
148 out.close();
149
150 ObjectInput in = new ObjectInputStream(
151 new ByteArrayInputStream(buffer.toByteArray())
152 );
153 d2 = (DefaultTableXYDataset) in.readObject();
154 in.close();
155 }
156 catch (Exception e) {
157 System.out.println(e.toString());
158 }
159 assertEquals(d1, d2);
160
161 }
162
163 }