KickJava   Java API By Example, From Geeks To Geeks.

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


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  * TimeSeriesTests.java
28  * --------------------
29  * (C) Copyright 2001-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: TimeSeriesTests.java,v 1.8 2005/05/20 08:20:04 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 16-Nov-2001 : Version 1 (DG);
39  * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
40  * 13-Mar-2003 : Added serialization test (DG);
41  * 15-Oct-2003 : Added test for setMaximumItemCount method (DG);
42  * 23-Aug-2004 : Added test that highlights a bug where the addOrUpdate()
43  * method can lead to more than maximumItemCount items in the
44  * dataset (DG);
45  *
46  */

47
48 package org.jfree.data.time.junit;
49
50 import java.io.ByteArrayInputStream JavaDoc;
51 import java.io.ByteArrayOutputStream JavaDoc;
52 import java.io.ObjectInput JavaDoc;
53 import java.io.ObjectInputStream JavaDoc;
54 import java.io.ObjectOutput JavaDoc;
55 import java.io.ObjectOutputStream JavaDoc;
56
57 import junit.framework.Test;
58 import junit.framework.TestCase;
59 import junit.framework.TestSuite;
60
61 import org.jfree.data.general.SeriesChangeEvent;
62 import org.jfree.data.general.SeriesChangeListener;
63 import org.jfree.data.general.SeriesException;
64 import org.jfree.data.time.Day;
65 import org.jfree.data.time.FixedMillisecond;
66 import org.jfree.data.time.Month;
67 import org.jfree.data.time.RegularTimePeriod;
68 import org.jfree.data.time.TimeSeries;
69 import org.jfree.data.time.TimeSeriesDataItem;
70 import org.jfree.data.time.Year;
71 import org.jfree.date.MonthConstants;
72
73 /**
74  * A collection of test cases for the {@link TimeSeries} class.
75  */

76 public class TimeSeriesTests extends TestCase implements SeriesChangeListener {
77
78     /** A time series. */
79     private TimeSeries seriesA;
80
81     /** A time series. */
82     private TimeSeries seriesB;
83
84     /** A time series. */
85     private TimeSeries seriesC;
86
87     /** A flag that indicates whether or not a change event was fired. */
88     private boolean gotSeriesChangeEvent = false;
89     
90     /**
91      * Returns the tests as a test suite.
92      *
93      * @return The test suite.
94      */

95     public static Test suite() {
96         return new TestSuite(TimeSeriesTests.class);
97     }
98
99     /**
100      * Constructs a new set of tests.
101      *
102      * @param name the name of the tests.
103      */

104     public TimeSeriesTests(String JavaDoc name) {
105         super(name);
106     }
107
108     /**
109      * Common test setup.
110      */

111     protected void setUp() {
112
113         this.seriesA = new TimeSeries("Series A", Year.class);
114         try {
115             this.seriesA.add(new Year(2000), new Integer JavaDoc(102000));
116             this.seriesA.add(new Year(2001), new Integer JavaDoc(102001));
117             this.seriesA.add(new Year(2002), new Integer JavaDoc(102002));
118             this.seriesA.add(new Year(2003), new Integer JavaDoc(102003));
119             this.seriesA.add(new Year(2004), new Integer JavaDoc(102004));
120             this.seriesA.add(new Year(2005), new Integer JavaDoc(102005));
121         }
122         catch (SeriesException e) {
123             System.err.println("Problem creating series.");
124         }
125
126         this.seriesB = new TimeSeries("Series B", Year.class);
127         try {
128             this.seriesB.add(new Year(2006), new Integer JavaDoc(202006));
129             this.seriesB.add(new Year(2007), new Integer JavaDoc(202007));
130             this.seriesB.add(new Year(2008), new Integer JavaDoc(202008));
131         }
132         catch (SeriesException e) {
133             System.err.println("Problem creating series.");
134         }
135
136         this.seriesC = new TimeSeries("Series C", Year.class);
137         try {
138             this.seriesC.add(new Year(1999), new Integer JavaDoc(301999));
139             this.seriesC.add(new Year(2000), new Integer JavaDoc(302000));
140             this.seriesC.add(new Year(2002), new Integer JavaDoc(302002));
141         }
142         catch (SeriesException e) {
143             System.err.println("Problem creating series.");
144         }
145
146     }
147     
148     /**
149      * Sets the flag to indicate that a {@link SeriesChangeEvent} has been
150      * received.
151      *
152      * @param event the event.
153      */

154     public void seriesChanged(SeriesChangeEvent event) {
155         this.gotSeriesChangeEvent = true;
156     }
157
158     /**
159      * Check that cloning works.
160      */

161     public void testClone() {
162
163         TimeSeries series = new TimeSeries("Test Series");
164
165         RegularTimePeriod jan1st2002 = new Day(1, MonthConstants.JANUARY, 2002);
166         try {
167             series.add(jan1st2002, new Integer JavaDoc(42));
168         }
169         catch (SeriesException e) {
170             System.err.println("Problem adding to series.");
171         }
172
173         TimeSeries clone = null;
174         try {
175             clone = (TimeSeries) series.clone();
176             clone.setKey("Clone Series");
177             try {
178                 clone.update(jan1st2002, new Integer JavaDoc(10));
179             }
180             catch (SeriesException e) {
181                 System.err.println("Problem updating series.");
182             }
183         }
184         catch (CloneNotSupportedException JavaDoc e) {
185             assertTrue(false);
186         }
187
188         int seriesValue = series.getValue(jan1st2002).intValue();
189         int cloneValue = clone.getValue(jan1st2002).intValue();
190
191         assertEquals(42, seriesValue);
192         assertEquals(10, cloneValue);
193         assertEquals("Test Series", series.getKey());
194         assertEquals("Clone Series", clone.getKey());
195
196     }
197
198     /**
199      * Add a value to series A for 1999. It should be added at index 0.
200      */

201     public void testAddValue() {
202
203         try {
204             this.seriesA.add(new Year(1999), new Integer JavaDoc(1));
205         }
206         catch (SeriesException e) {
207             System.err.println("Problem adding to series.");
208         }
209
210         int value = this.seriesA.getValue(0).intValue();
211         assertEquals(1, value);
212
213     }
214
215     /**
216      * Tests the retrieval of values.
217      */

218     public void testGetValue() {
219
220         Number JavaDoc value1 = this.seriesA.getValue(new Year(1999));
221         assertNull(value1);
222         int value2 = this.seriesA.getValue(new Year(2000)).intValue();
223         assertEquals(102000, value2);
224
225     }
226
227     /**
228      * Tests the deletion of values.
229      */

230     public void testDelete() {
231         this.seriesA.delete(0, 0);
232         assertEquals(5, this.seriesA.getItemCount());
233         Number JavaDoc value = this.seriesA.getValue(new Year(2000));
234         assertNull(value);
235     }
236
237     /**
238      * Basic tests for the delete() method.
239      */

240     public void testDelete2() {
241         TimeSeries s1 = new TimeSeries("Series", Year.class);
242         s1.add(new Year(2000), 13.75);
243         s1.add(new Year(2001), 11.90);
244         s1.add(new Year(2002), null);
245         s1.addChangeListener(this);
246         this.gotSeriesChangeEvent = false;
247         s1.delete(new Year(2001));
248         assertTrue(this.gotSeriesChangeEvent);
249         assertEquals(2, s1.getItemCount());
250         assertEquals(null, s1.getValue(new Year(2001)));
251     }
252     
253     /**
254      * Serialize an instance, restore it, and check for equality.
255      */

256     public void testSerialization() {
257
258         TimeSeries s1 = new TimeSeries("A test", Year.class);
259         s1.add(new Year(2000), 13.75);
260         s1.add(new Year(2001), 11.90);
261         s1.add(new Year(2002), null);
262         s1.add(new Year(2005), 19.32);
263         s1.add(new Year(2007), 16.89);
264         TimeSeries s2 = null;
265
266         try {
267             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
268             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
269             out.writeObject(s1);
270             out.close();
271
272             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
273                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
274             );
275             s2 = (TimeSeries) in.readObject();
276             in.close();
277         }
278         catch (Exception JavaDoc e) {
279             System.out.println(e.toString());
280         }
281         assertTrue(s1.equals(s2));
282
283     }
284
285     /**
286      * Tests the equals method.
287      */

288     public void testEquals() {
289         TimeSeries s1 = new TimeSeries("Time Series 1");
290         TimeSeries s2 = new TimeSeries("Time Series 2");
291         boolean b1 = s1.equals(s2);
292         assertFalse("b1", b1);
293
294         s2.setKey("Time Series 1");
295         boolean b2 = s1.equals(s2);
296         assertTrue("b2", b2);
297
298         RegularTimePeriod p1 = new Day();
299         RegularTimePeriod p2 = p1.next();
300         s1.add(p1, 100.0);
301         s1.add(p2, 200.0);
302         boolean b3 = s1.equals(s2);
303         assertFalse("b3", b3);
304
305         s2.add(p1, 100.0);
306         s2.add(p2, 200.0);
307         boolean b4 = s1.equals(s2);
308         assertTrue("b4", b4);
309
310         s1.setMaximumItemCount(100);
311         boolean b5 = s1.equals(s2);
312         assertFalse("b5", b5);
313
314         s2.setMaximumItemCount(100);
315         boolean b6 = s1.equals(s2);
316         assertTrue("b6", b6);
317
318         s1.setHistoryCount(100);
319         boolean b7 = s1.equals(s2);
320         assertFalse("b7", b7);
321
322         s2.setHistoryCount(100);
323         boolean b8 = s1.equals(s2);
324         assertTrue("b8", b8);
325     }
326     
327     /**
328      * Tests a specific bug report where null arguments in the constructor
329      * cause the equals() method to fail. Fixed for 0.9.21.
330      */

331     public void testEquals2() {
332         TimeSeries s1 = new TimeSeries("Series", null, null, Day.class);
333         TimeSeries s2 = new TimeSeries("Series", null, null, Day.class);
334         assertTrue(s1.equals(s2));
335     }
336     
337     /**
338      * Some tests to ensure that the createCopy(RegularTimePeriod,
339      * RegularTimePeriod) method is functioning correctly.
340      */

341     public void testCreateCopy1() {
342         
343         TimeSeries series = new TimeSeries("Series", Month.class);
344         series.add(new Month(MonthConstants.JANUARY, 2003), 45.0);
345         series.add(new Month(MonthConstants.FEBRUARY, 2003), 55.0);
346         series.add(new Month(MonthConstants.JUNE, 2003), 35.0);
347         series.add(new Month(MonthConstants.NOVEMBER, 2003), 85.0);
348         series.add(new Month(MonthConstants.DECEMBER, 2003), 75.0);
349         
350         try {
351             // copy a range before the start of the series data...
352
TimeSeries result1 = series.createCopy(
353                 new Month(MonthConstants.NOVEMBER, 2002),
354                 new Month(MonthConstants.DECEMBER, 2002)
355             );
356             assertEquals(0, result1.getItemCount());
357         
358             // copy a range that includes only the first item in the series...
359
TimeSeries result2 = series.createCopy(
360                 new Month(MonthConstants.NOVEMBER, 2002),
361                 new Month(MonthConstants.JANUARY, 2003)
362             );
363             assertEquals(1, result2.getItemCount());
364         
365             // copy a range that begins before and ends in the middle of the
366
// series...
367
TimeSeries result3 = series.createCopy(
368                 new Month(MonthConstants.NOVEMBER, 2002),
369                 new Month(MonthConstants.APRIL, 2003)
370             );
371             assertEquals(2, result3.getItemCount());
372         
373             TimeSeries result4 = series.createCopy(
374                 new Month(MonthConstants.NOVEMBER, 2002),
375                 new Month(MonthConstants.DECEMBER, 2003)
376             );
377             assertEquals(5, result4.getItemCount());
378
379             TimeSeries result5 = series.createCopy(
380                 new Month(MonthConstants.NOVEMBER, 2002),
381                 new Month(MonthConstants.MARCH, 2004)
382             );
383             assertEquals(5, result5.getItemCount());
384         
385             TimeSeries result6 = series.createCopy(
386                 new Month(MonthConstants.JANUARY, 2003),
387                 new Month(MonthConstants.JANUARY, 2003)
388             );
389             assertEquals(1, result6.getItemCount());
390
391             TimeSeries result7 = series.createCopy(
392                 new Month(MonthConstants.JANUARY, 2003),
393                 new Month(MonthConstants.APRIL, 2003)
394             );
395             assertEquals(2, result7.getItemCount());
396
397             TimeSeries result8 = series.createCopy(
398                 new Month(MonthConstants.JANUARY, 2003),
399                 new Month(MonthConstants.DECEMBER, 2003)
400             );
401             assertEquals(5, result8.getItemCount());
402
403             TimeSeries result9 = series.createCopy(
404                 new Month(MonthConstants.JANUARY, 2003),
405                 new Month(MonthConstants.MARCH, 2004)
406             );
407             assertEquals(5, result9.getItemCount());
408         
409             TimeSeries result10 = series.createCopy(
410                 new Month(MonthConstants.MAY, 2003),
411                 new Month(MonthConstants.DECEMBER, 2003)
412             );
413             assertEquals(3, result10.getItemCount());
414
415             TimeSeries result11 = series.createCopy(
416                 new Month(MonthConstants.MAY, 2003),
417                 new Month(MonthConstants.MARCH, 2004)
418             );
419             assertEquals(3, result11.getItemCount());
420
421             TimeSeries result12 = series.createCopy(
422                 new Month(MonthConstants.DECEMBER, 2003),
423                 new Month(MonthConstants.DECEMBER, 2003)
424             );
425             assertEquals(1, result12.getItemCount());
426     
427             TimeSeries result13 = series.createCopy(
428                 new Month(MonthConstants.DECEMBER, 2003),
429                 new Month(MonthConstants.MARCH, 2004)
430             );
431             assertEquals(1, result13.getItemCount());
432
433             TimeSeries result14 = series.createCopy(
434                 new Month(MonthConstants.JANUARY, 2004),
435                 new Month(MonthConstants.MARCH, 2004)
436             );
437             assertEquals(0, result14.getItemCount());
438         }
439         catch (CloneNotSupportedException JavaDoc e) {
440             assertTrue(false);
441         }
442
443     }
444     
445     /**
446      * Test the setMaximumItemCount() method to ensure that it removes items
447      * from the series if necessary.
448      */

449     public void testSetMaximumItemCount() {
450
451         TimeSeries s1 = new TimeSeries("S1", Year.class);
452         s1.add(new Year(2000), 13.75);
453         s1.add(new Year(2001), 11.90);
454         s1.add(new Year(2002), null);
455         s1.add(new Year(2005), 19.32);
456         s1.add(new Year(2007), 16.89);
457
458         assertTrue(s1.getItemCount() == 5);
459         s1.setMaximumItemCount(3);
460         assertTrue(s1.getItemCount() == 3);
461         TimeSeriesDataItem item = s1.getDataItem(0);
462         assertTrue(item.getPeriod().equals(new Year(2002)));
463
464     }
465
466     /**
467      * Some checks for the addOrUpdate() method.
468      */

469     public void testAddOrUpdate() {
470         TimeSeries s1 = new TimeSeries("S1", Year.class);
471         s1.setMaximumItemCount(2);
472         s1.addOrUpdate(new Year(2000), 100.0);
473         assertEquals(1, s1.getItemCount());
474         s1.addOrUpdate(new Year(2001), 101.0);
475         assertEquals(2, s1.getItemCount());
476         s1.addOrUpdate(new Year(2001), 102.0);
477         assertEquals(2, s1.getItemCount());
478         s1.addOrUpdate(new Year(2002), 103.0);
479         assertEquals(2, s1.getItemCount());
480     }
481
482     /**
483      * A test for the bug report 1075255.
484      */

485     public void testBug1075255() {
486         TimeSeries ts = new TimeSeries("dummy", FixedMillisecond.class);
487         ts.add(new FixedMillisecond(0L), 0.0);
488         TimeSeries ts2 = new TimeSeries("dummy2", FixedMillisecond.class);
489         ts2.add(new FixedMillisecond(0L), 1.0);
490         try {
491             ts.addAndOrUpdate(ts2);
492         }
493         catch (Exception JavaDoc e) {
494             e.printStackTrace();
495             assertTrue(false);
496         }
497         assertEquals(1, ts.getItemCount());
498     }
499 }
500
Popular Tags