KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > time > junit > TimeTableXYDatasetTests


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

42
43 package org.jfree.data.time.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 import java.util.TimeZone JavaDoc;
52
53 import junit.framework.Test;
54 import junit.framework.TestCase;
55 import junit.framework.TestSuite;
56
57 import org.jfree.data.time.TimeTableXYDataset;
58 import org.jfree.data.time.Year;
59
60 /**
61  * A collection of test cases for the {@link TimeTableXYDataset} class.
62  */

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

79     public TimeTableXYDatasetTests(String JavaDoc name) {
80         super(name);
81     }
82     
83     private static final double DELTA = 0.0000000001;
84     
85     /**
86      * Some checks for a simple dataset.
87      */

88     public void testStandard() {
89         TimeTableXYDataset d = new TimeTableXYDataset();
90         d.add(new Year(1999), 1.0, "Series 1");
91         assertEquals(d.getItemCount(), 1);
92         assertEquals(d.getSeriesCount(), 1);
93         d.add(new Year(2000), 2.0, "Series 2");
94         assertEquals(d.getItemCount(), 2);
95         assertEquals(d.getSeriesCount(), 2);
96         assertEquals(d.getYValue(0, 0), 1.0, DELTA);
97         assertTrue(Double.isNaN(d.getYValue(0, 1)));
98         assertTrue(Double.isNaN(d.getYValue(1, 0)));
99         assertEquals(d.getYValue(1, 1), 2.0, DELTA);
100     }
101     
102     /**
103      * Some checks for the getTimePeriod() method.
104      */

105     public void testGetTimePeriod() {
106         TimeTableXYDataset d = new TimeTableXYDataset();
107         d.add(new Year(1999), 1.0, "Series 1");
108         d.add(new Year(1998), 2.0, "Series 1");
109         d.add(new Year(1996), 3.0, "Series 1");
110         assertEquals(d.getTimePeriod(0), new Year(1996));
111         assertEquals(d.getTimePeriod(1), new Year(1998));
112         assertEquals(d.getTimePeriod(2), new Year(1999));
113     }
114     
115     /**
116      * Some checks for the equals() method.
117      */

118     public void testEquals() {
119         TimeTableXYDataset d1 = new TimeTableXYDataset();
120         TimeTableXYDataset d2 = new TimeTableXYDataset();
121         assertTrue(d1.equals(d2));
122         assertTrue(d2.equals(d1));
123
124         d1.add(new Year(1999), 123.4, "S1");
125         assertFalse(d1.equals(d2));
126         d2.add(new Year(1999), 123.4, "S1");
127         assertTrue(d1.equals(d2));
128         
129         d1.setDomainIsPointsInTime(!d1.getDomainIsPointsInTime());
130         assertFalse(d1.equals(d2));
131         d2.setDomainIsPointsInTime(!d2.getDomainIsPointsInTime());
132         assertTrue(d1.equals(d2));
133         
134         d1 = new TimeTableXYDataset(TimeZone.getTimeZone("GMT"));
135         d2 = new TimeTableXYDataset(
136             TimeZone.getTimeZone("America/Los_Angeles")
137         );
138         assertFalse(d1.equals(d2));
139     }
140     
141     /**
142      * Some checks for cloning.
143      */

144     public void testClone() {
145
146         TimeTableXYDataset d = new TimeTableXYDataset();
147         d.add(new Year(1999), 25.0, "Series");
148
149         TimeTableXYDataset clone = null;
150         try {
151             clone = (TimeTableXYDataset) d.clone();
152         }
153         catch (CloneNotSupportedException JavaDoc e) {
154             assertTrue(false);
155         }
156         assertTrue(clone.equals(d));
157
158         // now test that the clone is independent of the original
159
clone.add(new Year(2004), 1.2, "SS");
160         assertFalse(clone.equals(d));
161     }
162
163     /**
164      * Serialize an instance, restore it, and check for equality.
165      */

166     public void testSerialization() {
167
168         TimeTableXYDataset d1 = new TimeTableXYDataset();
169         d1.add(new Year(1999), 123.4, "S1");
170         TimeTableXYDataset d2 = null;
171
172         try {
173             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
174             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
175             out.writeObject(d1);
176             out.close();
177
178             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
179                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
180             );
181             d2 = (TimeTableXYDataset) in.readObject();
182             in.close();
183         }
184         catch (Exception JavaDoc e) {
185             System.out.println(e.toString());
186         }
187         assertTrue(d1.equals(d2));
188
189     }
190
191 }
192
Popular Tags