KickJava   Java API By Example, From Geeks To Geeks.

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


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  * TimeSeriesCollectionTests.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: TimeSeriesCollectionTests.java,v 1.4 2005/03/28 07:11:34 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 01-May-2003 : Version 1 (DG);
39  * 04-Dec-2003 : Added a test for the getSurroundingItems() method (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
52 import junit.framework.Test;
53 import junit.framework.TestCase;
54 import junit.framework.TestSuite;
55
56 import org.jfree.data.time.Day;
57 import org.jfree.data.time.RegularTimePeriod;
58 import org.jfree.data.time.TimePeriodAnchor;
59 import org.jfree.data.time.TimeSeries;
60 import org.jfree.data.time.TimeSeriesCollection;
61
62 /**
63  * A collection of test cases for the {@link TimeSeriesCollection} class.
64  */

65 public class TimeSeriesCollectionTests extends TestCase {
66
67     /**
68      * Returns the tests as a test suite.
69      *
70      * @return The test suite.
71      */

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

81     public TimeSeriesCollectionTests(String JavaDoc name) {
82         super(name);
83     }
84
85     /**
86      * Some tests for the equals() method.
87      */

88     public void testEquals() {
89
90         TimeSeriesCollection c1 = new TimeSeriesCollection();
91         TimeSeriesCollection c2 = new TimeSeriesCollection();
92
93         TimeSeries s1 = new TimeSeries("Series 1");
94         TimeSeries s2 = new TimeSeries("Series 2");
95
96         // newly created collections should be equal
97
boolean b1 = c1.equals(c2);
98         assertTrue("b1", b1);
99
100         // add series to collection 1, should be not equal
101
c1.addSeries(s1);
102         c1.addSeries(s2);
103         boolean b2 = c1.equals(c2);
104         assertFalse("b2", b2);
105
106         // now add the same series to collection 2 to make them equal again...
107
c2.addSeries(s1);
108         c2.addSeries(s2);
109         boolean b3 = c1.equals(c2);
110         assertTrue("b3", b3);
111
112         // now remove series 2 from collection 2
113
c2.removeSeries(s2);
114         boolean b4 = c1.equals(c2);
115         assertFalse("b4", b4);
116
117         // now remove series 2 from collection 1 to make them equal again
118
c1.removeSeries(s2);
119         boolean b5 = c1.equals(c2);
120         assertTrue("b5", b5);
121     }
122
123     /**
124      * Tests the remove series method.
125      */

126     public void testRemoveSeries() {
127
128         TimeSeriesCollection c1 = new TimeSeriesCollection();
129
130         TimeSeries s1 = new TimeSeries("Series 1");
131         TimeSeries s2 = new TimeSeries("Series 2");
132         TimeSeries s3 = new TimeSeries("Series 3");
133         TimeSeries s4 = new TimeSeries("Series 4");
134
135         c1.addSeries(s1);
136         c1.addSeries(s2);
137         c1.addSeries(s3);
138         c1.addSeries(s4);
139
140         c1.removeSeries(s3);
141
142         TimeSeries s = c1.getSeries(2);
143         boolean b1 = s.equals(s4);
144         assertTrue(b1);
145
146     }
147     
148     /**
149      * Test the getSurroundingItems() method to ensure it is returning the
150      * values we expect.
151      */

152     public void testGetSurroundingItems() {
153         
154         TimeSeries series = new TimeSeries("Series 1", Day.class);
155         TimeSeriesCollection collection = new TimeSeriesCollection(series);
156         collection.setXPosition(TimePeriodAnchor.MIDDLE);
157         
158         // for a series with no data, we expect {-1, -1}...
159
int[] result = collection.getSurroundingItems(0, 1000L);
160         assertTrue(result[0] == -1);
161         assertTrue(result[1] == -1);
162         
163         // now test with a single value in the series...
164
Day today = new Day();
165         long start1 = today.getFirstMillisecond();
166         long middle1 = today.getMiddleMillisecond();
167         long end1 = today.getLastMillisecond();
168         
169         series.add(today, 99.9);
170         result = collection.getSurroundingItems(0, start1);
171         assertTrue(result[0] == -1);
172         assertTrue(result[1] == 0);
173         
174         result = collection.getSurroundingItems(0, middle1);
175         assertTrue(result[0] == 0);
176         assertTrue(result[1] == 0);
177         
178         result = collection.getSurroundingItems(0, end1);
179         assertTrue(result[0] == 0);
180         assertTrue(result[1] == -1);
181         
182         // now add a second value to the series...
183
Day tomorrow = (Day) today.next();
184         long start2 = tomorrow.getFirstMillisecond();
185         long middle2 = tomorrow.getMiddleMillisecond();
186         long end2 = tomorrow.getLastMillisecond();
187         
188         series.add(tomorrow, 199.9);
189         result = collection.getSurroundingItems(0, start2);
190         assertTrue(result[0] == 0);
191         assertTrue(result[1] == 1);
192         
193         result = collection.getSurroundingItems(0, middle2);
194         assertTrue(result[0] == 1);
195         assertTrue(result[1] == 1);
196         
197         result = collection.getSurroundingItems(0, end2);
198         assertTrue(result[0] == 1);
199         assertTrue(result[1] == -1);
200         
201         // now add a third value to the series...
202
Day yesterday = (Day) today.previous();
203         long start3 = yesterday.getFirstMillisecond();
204         long middle3 = yesterday.getMiddleMillisecond();
205         long end3 = yesterday.getLastMillisecond();
206         
207         series.add(yesterday, 1.23);
208         result = collection.getSurroundingItems(0, start3);
209         assertTrue(result[0] == -1);
210         assertTrue(result[1] == 0);
211         
212         result = collection.getSurroundingItems(0, middle3);
213         assertTrue(result[0] == 0);
214         assertTrue(result[1] == 0);
215         
216         result = collection.getSurroundingItems(0, end3);
217         assertTrue(result[0] == 0);
218         assertTrue(result[1] == 1);
219         
220     }
221     
222     /**
223      * Serialize an instance, restore it, and check for equality.
224      */

225     public void testSerialization() {
226
227         TimeSeriesCollection c1 = new TimeSeriesCollection(createSeries());
228         TimeSeriesCollection c2 = null;
229
230         try {
231             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
232             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
233             out.writeObject(c1);
234             out.close();
235
236             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
237                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
238             );
239             c2 = (TimeSeriesCollection) in.readObject();
240             in.close();
241         }
242         catch (Exception JavaDoc e) {
243             System.out.println(e.toString());
244         }
245         assertEquals(c1, c2);
246
247     }
248
249     /**
250      * Creates a time series for testing.
251      *
252      * @return A time series.
253      */

254     private TimeSeries createSeries() {
255         RegularTimePeriod t = new Day();
256         TimeSeries series = new TimeSeries("Test");
257         series.add(t, 1.0);
258         t = t.next();
259         series.add(t, 2.0);
260         t = t.next();
261         series.add(t, null);
262         t = t.next();
263         series.add(t, 4.0);
264         return series;
265     }
266     
267     /**
268      * A test for bug report 1170825.
269      */

270     public void test1170825() {
271         TimeSeries s1 = new TimeSeries("Series1");
272         TimeSeriesCollection dataset = new TimeSeriesCollection();
273         dataset.addSeries(s1);
274         try {
275             /* TimeSeries s = */ dataset.getSeries(1);
276         }
277         catch (IllegalArgumentException JavaDoc e) {
278             // correct outcome
279
}
280         catch (IndexOutOfBoundsException JavaDoc e) {
281             assertTrue(false); // wrong outcome
282
}
283     }
284
285     
286 }
287
Popular Tags