KickJava   Java API By Example, From Geeks To Geeks.

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


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  * TimePeriodValueTests.java
28  * -------------------------
29  * (C) Copyright 2003-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: TimePeriodValuesTests.java,v 1.6 2005/05/20 08:20:04 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 30-Jul-2003 : Version 1 (DG);
39  *
40  */

41
42 package org.jfree.data.time.junit;
43
44 import java.io.ByteArrayInputStream JavaDoc;
45 import java.io.ByteArrayOutputStream JavaDoc;
46 import java.io.ObjectInput JavaDoc;
47 import java.io.ObjectInputStream JavaDoc;
48 import java.io.ObjectOutput JavaDoc;
49 import java.io.ObjectOutputStream JavaDoc;
50
51 import junit.framework.Test;
52 import junit.framework.TestCase;
53 import junit.framework.TestSuite;
54
55 import org.jfree.data.general.SeriesException;
56 import org.jfree.data.time.Day;
57 import org.jfree.data.time.RegularTimePeriod;
58 import org.jfree.data.time.TimePeriodValues;
59 import org.jfree.data.time.Year;
60 import org.jfree.date.MonthConstants;
61
62 /**
63  * A collection of test cases for the {@link TimePeriodValues} class.
64  */

65 public class TimePeriodValuesTests extends TestCase {
66
67     /** Series A. */
68     private TimePeriodValues seriesA;
69
70     /** Series B. */
71     private TimePeriodValues seriesB;
72
73     /** Series C. */
74     private TimePeriodValues seriesC;
75
76     /**
77      * Returns the tests as a test suite.
78      *
79      * @return The test suite.
80      */

81     public static Test suite() {
82         return new TestSuite(TimePeriodValuesTests.class);
83     }
84
85     /**
86      * Constructs a new set of tests.
87      *
88      * @param name the name of the tests.
89      */

90     public TimePeriodValuesTests(String JavaDoc name) {
91         super(name);
92     }
93
94     /**
95      * Common test setup.
96      */

97     protected void setUp() {
98
99         this.seriesA = new TimePeriodValues("Series A");
100         try {
101             this.seriesA.add(new Year(2000), new Integer JavaDoc(102000));
102             this.seriesA.add(new Year(2001), new Integer JavaDoc(102001));
103             this.seriesA.add(new Year(2002), new Integer JavaDoc(102002));
104             this.seriesA.add(new Year(2003), new Integer JavaDoc(102003));
105             this.seriesA.add(new Year(2004), new Integer JavaDoc(102004));
106             this.seriesA.add(new Year(2005), new Integer JavaDoc(102005));
107         }
108         catch (SeriesException e) {
109             System.err.println("Problem creating series.");
110         }
111
112         this.seriesB = new TimePeriodValues("Series B");
113         try {
114             this.seriesB.add(new Year(2006), new Integer JavaDoc(202006));
115             this.seriesB.add(new Year(2007), new Integer JavaDoc(202007));
116             this.seriesB.add(new Year(2008), new Integer JavaDoc(202008));
117         }
118         catch (SeriesException e) {
119             System.err.println("Problem creating series.");
120         }
121
122         this.seriesC = new TimePeriodValues("Series C");
123         try {
124             this.seriesC.add(new Year(1999), new Integer JavaDoc(301999));
125             this.seriesC.add(new Year(2000), new Integer JavaDoc(302000));
126             this.seriesC.add(new Year(2002), new Integer JavaDoc(302002));
127         }
128         catch (SeriesException e) {
129             System.err.println("Problem creating series.");
130         }
131
132     }
133
134     /**
135      * Set up a quarter equal to Q1 1900. Request the previous quarter, it
136      * should be null.
137      */

138     public void testClone() {
139
140         TimePeriodValues series = new TimePeriodValues("Test Series");
141
142         RegularTimePeriod jan1st2002 = new Day(1, MonthConstants.JANUARY, 2002);
143         try {
144             series.add(jan1st2002, new Integer JavaDoc(42));
145         }
146         catch (SeriesException e) {
147             System.err.println("Problem adding to collection.");
148         }
149
150         TimePeriodValues clone = null;
151         try {
152             clone = (TimePeriodValues) series.clone();
153             clone.setKey("Clone Series");
154             try {
155                 clone.update(0, new Integer JavaDoc(10));
156             }
157             catch (SeriesException e) {
158                 System.err.println("Problem updating series.");
159             }
160         }
161         catch (CloneNotSupportedException JavaDoc e) {
162             assertTrue(false);
163         }
164
165         int seriesValue = series.getValue(0).intValue();
166         int cloneValue = clone.getValue(0).intValue();
167
168         assertEquals(42, seriesValue);
169         assertEquals(10, cloneValue);
170         assertEquals("Test Series", series.getKey());
171         assertEquals("Clone Series", clone.getKey());
172
173     }
174
175     /**
176      * Add a value to series A for 1999. It should be added at index 0.
177      */

178     public void testAddValue() {
179
180         TimePeriodValues tpvs = new TimePeriodValues("Test");
181         try {
182             tpvs.add(new Year(1999), new Integer JavaDoc(1));
183         }
184         catch (SeriesException e) {
185             System.err.println("Problem adding to series.");
186         }
187
188         int value = tpvs.getValue(0).intValue();
189         assertEquals(1, value);
190
191     }
192
193     /**
194      * Serialize an instance, restore it, and check for equality.
195      */

196     public void testSerialization() {
197
198         TimePeriodValues s1 = new TimePeriodValues("A test");
199         s1.add(new Year(2000), 13.75);
200         s1.add(new Year(2001), 11.90);
201         s1.add(new Year(2002), null);
202         s1.add(new Year(2005), 19.32);
203         s1.add(new Year(2007), 16.89);
204         TimePeriodValues s2 = null;
205
206         try {
207             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
208             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
209             out.writeObject(s1);
210             out.close();
211
212             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
213                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
214             );
215             s2 = (TimePeriodValues) in.readObject();
216             in.close();
217         }
218         catch (Exception JavaDoc e) {
219             System.out.println(e.toString());
220         }
221         assertTrue(s1.equals(s2));
222
223     }
224
225     /**
226      * Tests the equals method.
227      */

228     public void testEquals() {
229         TimePeriodValues s1 = new TimePeriodValues("Time Series 1");
230         TimePeriodValues s2 = new TimePeriodValues("Time Series 2");
231         boolean b1 = s1.equals(s2);
232         assertFalse("b1", b1);
233
234         s2.setKey("Time Series 1");
235         boolean b2 = s1.equals(s2);
236         assertTrue("b2", b2);
237
238         RegularTimePeriod p1 = new Day();
239         RegularTimePeriod p2 = p1.next();
240         s1.add(p1, 100.0);
241         s1.add(p2, 200.0);
242         boolean b3 = s1.equals(s2);
243         assertFalse("b3", b3);
244
245         s2.add(p1, 100.0);
246         s2.add(p2, 200.0);
247         boolean b4 = s1.equals(s2);
248         assertTrue("b4", b4);
249
250     }
251     
252     /**
253      * A test for bug report 1161329.
254      */

255     public void test1161329() {
256         TimePeriodValues tpv = new TimePeriodValues("Test");
257         RegularTimePeriod t = new Day();
258         tpv.add(t, 1.0);
259         t = t.next();
260         tpv.add(t, 2.0);
261         tpv.delete(0, 1);
262         assertEquals(0, tpv.getItemCount());
263         tpv.add(t, 2.0);
264         assertEquals(1, tpv.getItemCount());
265     }
266
267 }
268
Popular Tags