KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DefaultTableXYDatasetTests.java
29  * -------------------------------
30  * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: DefaultTableXYDatasetTests.java,v 1.1.2.1 2006/10/03 15:41:37 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 23-Dec-2003 : Version 1 (DG);
40  * 06-Oct-2005 : Added test for new data updating interval width (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.DefaultTableXYDataset;
58 import org.jfree.data.xy.XYSeries;
59
60 /**
61  * Tests for the {@link DefaultTableXYDataset} class.
62  */

63 public class DefaultTableXYDatasetTests extends TestCase {
64
65     /**
66      * Returns the tests as a test suite.
67      *
68      * @return The test suite.
69      */

70     public static Test suite() {
71         return new TestSuite(DefaultTableXYDatasetTests.class);
72     }
73
74     /**
75      * Constructs a new set of tests.
76      *
77      * @param name the name of the tests.
78      */

79     public DefaultTableXYDatasetTests(String JavaDoc name) {
80         super(name);
81     }
82
83     /**
84      * Confirm that the equals method can distinguish all the required fields.
85      */

86     public void testEquals() {
87         
88         DefaultTableXYDataset d1 = new DefaultTableXYDataset();
89         XYSeries s1 = new XYSeries("Series 1", true, false);
90         s1.add(1.0, 1.1);
91         s1.add(2.0, 2.2);
92         d1.addSeries(s1);
93         
94         DefaultTableXYDataset d2 = new DefaultTableXYDataset();
95         XYSeries s2 = new XYSeries("Series 1", true, false);
96         s2.add(1.0, 1.1);
97         s2.add(2.0, 2.2);
98         d2.addSeries(s2);
99         
100         assertTrue(d1.equals(d2));
101         assertTrue(d2.equals(d1));
102
103         s1.add(3.0, 3.3);
104         assertFalse(d1.equals(d2));
105
106         s2.add(3.0, 3.3);
107         assertTrue(d1.equals(d2));
108
109     }
110
111     /**
112      * Confirm that cloning works.
113      */

114     public void testCloning() {
115         DefaultTableXYDataset d1 = new DefaultTableXYDataset();
116         XYSeries s1 = new XYSeries("Series 1", true, false);
117         s1.add(1.0, 1.1);
118         s1.add(2.0, 2.2);
119         d1.addSeries(s1);
120         
121         DefaultTableXYDataset d2 = null;
122         try {
123             d2 = (DefaultTableXYDataset) d1.clone();
124         }
125         catch (CloneNotSupportedException JavaDoc e) {
126             System.err.println("Failed to clone.");
127         }
128         assertTrue(d1 != d2);
129         assertTrue(d1.getClass() == d2.getClass());
130         assertTrue(d1.equals(d2));
131     }
132
133     /**
134      * Serialize an instance, restore it, and check for equality.
135      */

136     public void testSerialization() {
137
138         DefaultTableXYDataset d1 = new DefaultTableXYDataset();
139         XYSeries s1 = new XYSeries("Series 1", true, false);
140         s1.add(1.0, 1.1);
141         s1.add(2.0, 2.2);
142         d1.addSeries(s1);
143         
144         DefaultTableXYDataset d2 = null;
145
146         try {
147             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
148             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
149             out.writeObject(d1);
150             out.close();
151
152             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
153                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
154             );
155             d2 = (DefaultTableXYDataset) in.readObject();
156             in.close();
157         }
158         catch (Exception JavaDoc e) {
159             System.out.println(e.toString());
160         }
161         assertEquals(d1, d2);
162
163     }
164     
165     private static final double EPSILON = 0.0000000001;
166     
167     /**
168      * This is a test for bug 1312066 - adding a new series should trigger a
169      * recalculation of the interval width, if it is being automatically
170      * calculated.
171      */

172     public void testAddSeries() {
173         DefaultTableXYDataset d1 = new DefaultTableXYDataset();
174         d1.setAutoWidth(true);
175         XYSeries s1 = new XYSeries("Series 1", true, false);
176         s1.add(3.0, 1.1);
177         s1.add(7.0, 2.2);
178         d1.addSeries(s1);
179         assertEquals(3.0, d1.getXValue(0, 0), EPSILON);
180         assertEquals(7.0, d1.getXValue(0, 1), EPSILON);
181         assertEquals(1.0, d1.getStartXValue(0, 0), EPSILON);
182         assertEquals(5.0, d1.getStartXValue(0, 1), EPSILON);
183         assertEquals(5.0, d1.getEndXValue(0, 0), EPSILON);
184         assertEquals(9.0, d1.getEndXValue(0, 1), EPSILON);
185
186         // now add another series
187
XYSeries s2 = new XYSeries("Series 2", true, false);
188         s2.add(7.5, 1.1);
189         s2.add(9.0, 2.2);
190         d1.addSeries(s2);
191  
192         assertEquals(3.0, d1.getXValue(1, 0), EPSILON);
193         assertEquals(7.0, d1.getXValue(1, 1), EPSILON);
194         assertEquals(7.5, d1.getXValue(1, 2), EPSILON);
195         assertEquals(9.0, d1.getXValue(1, 3), EPSILON);
196         
197         assertEquals(7.25, d1.getStartXValue(1, 2), EPSILON);
198         assertEquals(8.75, d1.getStartXValue(1, 3), EPSILON);
199         assertEquals(7.75, d1.getEndXValue(1, 2), EPSILON);
200         assertEquals(9.25, d1.getEndXValue(1, 3), EPSILON);
201
202         // and check the first series too...
203
assertEquals(2.75, d1.getStartXValue(0, 0), EPSILON);
204         assertEquals(6.75, d1.getStartXValue(0, 1), EPSILON);
205         assertEquals(3.25, d1.getEndXValue(0, 0), EPSILON);
206         assertEquals(7.25, d1.getEndXValue(0, 1), EPSILON);
207     }
208 }
209
Popular Tags