KickJava   Java API By Example, From Geeks To Geeks.

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


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  * CategoryTableXYDatasetTests.java
29  * --------------------------------
30  * (C) Copyright 2005, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: CategoryTableXYDatasetTests.java,v 1.1.2.1 2006/10/03 15:41:38 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 06-Oct-2005 : Version 1 (DG);
40  *
41  */

42
43 package org.jfree.data.xy.junit;
44
45 import java.io.ByteArrayInputStream JavaDoc;
46 import java.io.ByteArrayOutputStream JavaDoc;
47 import java.io.ObjectInput JavaDoc;
48 import java.io.ObjectInputStream JavaDoc;
49 import java.io.ObjectOutput JavaDoc;
50 import java.io.ObjectOutputStream JavaDoc;
51
52 import junit.framework.Test;
53 import junit.framework.TestCase;
54 import junit.framework.TestSuite;
55
56 import org.jfree.data.xy.CategoryTableXYDataset;
57
58 /**
59  * Tests for the {@link CategoryTableXYDataset} class.
60  */

61 public class CategoryTableXYDatasetTests 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(CategoryTableXYDatasetTests.class);
70     }
71
72     /**
73      * Constructs a new set of tests.
74      *
75      * @param name the name of the tests.
76      */

77     public CategoryTableXYDatasetTests(String JavaDoc 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         CategoryTableXYDataset d1 = new CategoryTableXYDataset();
87         d1.add(1.0, 1.1, "Series 1");
88         d1.add(2.0, 2.2, "Series 1");
89         
90         CategoryTableXYDataset d2 = new CategoryTableXYDataset();
91         d2.add(1.0, 1.1, "Series 1");
92         d2.add(2.0, 2.2, "Series 1");
93         
94         assertTrue(d1.equals(d2));
95         assertTrue(d2.equals(d1));
96
97         d1.add(3.0, 3.3, "Series 1");
98         assertFalse(d1.equals(d2));
99
100         d2.add(3.0, 3.3, "Series 1");
101         assertTrue(d1.equals(d2));
102
103     }
104
105     /**
106      * Confirm that cloning works.
107      */

108     public void testCloning() {
109         
110         CategoryTableXYDataset d1 = new CategoryTableXYDataset();
111         d1.add(1.0, 1.1, "Series 1");
112         d1.add(2.0, 2.2, "Series 1");
113         
114         CategoryTableXYDataset d2 = null;
115         try {
116             d2 = (CategoryTableXYDataset) d1.clone();
117         }
118         catch (CloneNotSupportedException JavaDoc e) {
119             e.printStackTrace();
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         CategoryTableXYDataset d1 = new CategoryTableXYDataset();
132         d1.add(1.0, 1.1, "Series 1");
133         d1.add(2.0, 2.2, "Series 1");
134         
135         CategoryTableXYDataset d2 = null;
136
137         try {
138             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
139             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
140             out.writeObject(d1);
141             out.close();
142
143             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
144                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
145             );
146             d2 = (CategoryTableXYDataset) in.readObject();
147             in.close();
148         }
149         catch (Exception JavaDoc e) {
150             e.printStackTrace();
151         }
152         assertEquals(d1, d2);
153
154     }
155     
156     private static final double EPSILON = 0.0000000001;
157     
158     /**
159      * This is a test for bug 1312066 - adding a new series should trigger a
160      * recalculation of the interval width, if it is being automatically
161      * calculated.
162      */

163     public void testAddSeries() {
164         CategoryTableXYDataset d1 = new CategoryTableXYDataset();
165         d1.setAutoWidth(true);
166         d1.add(3.0, 1.1, "Series 1");
167         d1.add(7.0, 2.2, "Series 1");
168         assertEquals(3.0, d1.getXValue(0, 0), EPSILON);
169         assertEquals(7.0, d1.getXValue(0, 1), EPSILON);
170         assertEquals(1.0, d1.getStartXValue(0, 0), EPSILON);
171         assertEquals(5.0, d1.getStartXValue(0, 1), EPSILON);
172         assertEquals(5.0, d1.getEndXValue(0, 0), EPSILON);
173         assertEquals(9.0, d1.getEndXValue(0, 1), EPSILON);
174
175         // now add some more data
176
d1.add(7.5, 1.1, "Series 2");
177         d1.add(9.0, 2.2, "Series 2");
178  
179         assertEquals(3.0, d1.getXValue(1, 0), EPSILON);
180         assertEquals(7.0, d1.getXValue(1, 1), EPSILON);
181         assertEquals(7.5, d1.getXValue(1, 2), EPSILON);
182         assertEquals(9.0, d1.getXValue(1, 3), EPSILON);
183         
184         assertEquals(7.25, d1.getStartXValue(1, 2), EPSILON);
185         assertEquals(8.75, d1.getStartXValue(1, 3), EPSILON);
186         assertEquals(7.75, d1.getEndXValue(1, 2), EPSILON);
187         assertEquals(9.25, d1.getEndXValue(1, 3), EPSILON);
188
189         // and check the first series too...
190
assertEquals(2.75, d1.getStartXValue(0, 0), EPSILON);
191         assertEquals(6.75, d1.getStartXValue(0, 1), EPSILON);
192         assertEquals(3.25, d1.getEndXValue(0, 0), EPSILON);
193         assertEquals(7.25, d1.getEndXValue(0, 1), EPSILON);
194     }
195 }
196
Popular Tags