001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ------------------------------
028: * TimeSeriesCollectionTests.java
029: * ------------------------------
030: * (C) Copyright 2003-2007, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: TimeSeriesCollectionTests.java,v 1.1.2.2 2007/05/08 10:58:51 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 01-May-2003 : Version 1 (DG);
040: * 04-Dec-2003 : Added a test for the getSurroundingItems() method (DG);
041: * 08-May-2007 : Added testIndexOf() method (DG);
042: *
043: */
044:
045: package org.jfree.data.time.junit;
046:
047: import java.io.ByteArrayInputStream;
048: import java.io.ByteArrayOutputStream;
049: import java.io.ObjectInput;
050: import java.io.ObjectInputStream;
051: import java.io.ObjectOutput;
052: import java.io.ObjectOutputStream;
053:
054: import junit.framework.Test;
055: import junit.framework.TestCase;
056: import junit.framework.TestSuite;
057:
058: import org.jfree.data.time.Day;
059: import org.jfree.data.time.RegularTimePeriod;
060: import org.jfree.data.time.TimePeriodAnchor;
061: import org.jfree.data.time.TimeSeries;
062: import org.jfree.data.time.TimeSeriesCollection;
063:
064: /**
065: * A collection of test cases for the {@link TimeSeriesCollection} class.
066: */
067: public class TimeSeriesCollectionTests extends TestCase {
068:
069: /**
070: * Returns the tests as a test suite.
071: *
072: * @return The test suite.
073: */
074: public static Test suite() {
075: return new TestSuite(TimeSeriesCollectionTests.class);
076: }
077:
078: /**
079: * Constructs a new set of tests.
080: *
081: * @param name the name of the tests.
082: */
083: public TimeSeriesCollectionTests(String name) {
084: super (name);
085: }
086:
087: /**
088: * Some tests for the equals() method.
089: */
090: public void testEquals() {
091:
092: TimeSeriesCollection c1 = new TimeSeriesCollection();
093: TimeSeriesCollection c2 = new TimeSeriesCollection();
094:
095: TimeSeries s1 = new TimeSeries("Series 1");
096: TimeSeries s2 = new TimeSeries("Series 2");
097:
098: // newly created collections should be equal
099: boolean b1 = c1.equals(c2);
100: assertTrue("b1", b1);
101:
102: // add series to collection 1, should be not equal
103: c1.addSeries(s1);
104: c1.addSeries(s2);
105: boolean b2 = c1.equals(c2);
106: assertFalse("b2", b2);
107:
108: // now add the same series to collection 2 to make them equal again...
109: c2.addSeries(s1);
110: c2.addSeries(s2);
111: boolean b3 = c1.equals(c2);
112: assertTrue("b3", b3);
113:
114: // now remove series 2 from collection 2
115: c2.removeSeries(s2);
116: boolean b4 = c1.equals(c2);
117: assertFalse("b4", b4);
118:
119: // now remove series 2 from collection 1 to make them equal again
120: c1.removeSeries(s2);
121: boolean b5 = c1.equals(c2);
122: assertTrue("b5", b5);
123: }
124:
125: /**
126: * Tests the remove series method.
127: */
128: public void testRemoveSeries() {
129:
130: TimeSeriesCollection c1 = new TimeSeriesCollection();
131:
132: TimeSeries s1 = new TimeSeries("Series 1");
133: TimeSeries s2 = new TimeSeries("Series 2");
134: TimeSeries s3 = new TimeSeries("Series 3");
135: TimeSeries s4 = new TimeSeries("Series 4");
136:
137: c1.addSeries(s1);
138: c1.addSeries(s2);
139: c1.addSeries(s3);
140: c1.addSeries(s4);
141:
142: c1.removeSeries(s3);
143:
144: TimeSeries s = c1.getSeries(2);
145: boolean b1 = s.equals(s4);
146: assertTrue(b1);
147:
148: }
149:
150: /**
151: * Test the getSurroundingItems() method to ensure it is returning the
152: * values we expect.
153: */
154: public void testGetSurroundingItems() {
155:
156: TimeSeries series = new TimeSeries("Series 1", Day.class);
157: TimeSeriesCollection collection = new TimeSeriesCollection(
158: series);
159: collection.setXPosition(TimePeriodAnchor.MIDDLE);
160:
161: // for a series with no data, we expect {-1, -1}...
162: int[] result = collection.getSurroundingItems(0, 1000L);
163: assertTrue(result[0] == -1);
164: assertTrue(result[1] == -1);
165:
166: // now test with a single value in the series...
167: Day today = new Day();
168: long start1 = today.getFirstMillisecond();
169: long middle1 = today.getMiddleMillisecond();
170: long end1 = today.getLastMillisecond();
171:
172: series.add(today, 99.9);
173: result = collection.getSurroundingItems(0, start1);
174: assertTrue(result[0] == -1);
175: assertTrue(result[1] == 0);
176:
177: result = collection.getSurroundingItems(0, middle1);
178: assertTrue(result[0] == 0);
179: assertTrue(result[1] == 0);
180:
181: result = collection.getSurroundingItems(0, end1);
182: assertTrue(result[0] == 0);
183: assertTrue(result[1] == -1);
184:
185: // now add a second value to the series...
186: Day tomorrow = (Day) today.next();
187: long start2 = tomorrow.getFirstMillisecond();
188: long middle2 = tomorrow.getMiddleMillisecond();
189: long end2 = tomorrow.getLastMillisecond();
190:
191: series.add(tomorrow, 199.9);
192: result = collection.getSurroundingItems(0, start2);
193: assertTrue(result[0] == 0);
194: assertTrue(result[1] == 1);
195:
196: result = collection.getSurroundingItems(0, middle2);
197: assertTrue(result[0] == 1);
198: assertTrue(result[1] == 1);
199:
200: result = collection.getSurroundingItems(0, end2);
201: assertTrue(result[0] == 1);
202: assertTrue(result[1] == -1);
203:
204: // now add a third value to the series...
205: Day yesterday = (Day) today.previous();
206: long start3 = yesterday.getFirstMillisecond();
207: long middle3 = yesterday.getMiddleMillisecond();
208: long end3 = yesterday.getLastMillisecond();
209:
210: series.add(yesterday, 1.23);
211: result = collection.getSurroundingItems(0, start3);
212: assertTrue(result[0] == -1);
213: assertTrue(result[1] == 0);
214:
215: result = collection.getSurroundingItems(0, middle3);
216: assertTrue(result[0] == 0);
217: assertTrue(result[1] == 0);
218:
219: result = collection.getSurroundingItems(0, end3);
220: assertTrue(result[0] == 0);
221: assertTrue(result[1] == 1);
222:
223: }
224:
225: /**
226: * Serialize an instance, restore it, and check for equality.
227: */
228: public void testSerialization() {
229:
230: TimeSeriesCollection c1 = new TimeSeriesCollection(
231: createSeries());
232: TimeSeriesCollection c2 = null;
233:
234: try {
235: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
236: ObjectOutput out = new ObjectOutputStream(buffer);
237: out.writeObject(c1);
238: out.close();
239:
240: ObjectInput in = new ObjectInputStream(
241: new ByteArrayInputStream(buffer.toByteArray()));
242: c2 = (TimeSeriesCollection) in.readObject();
243: in.close();
244: } catch (Exception e) {
245: System.out.println(e.toString());
246: }
247: assertEquals(c1, c2);
248:
249: }
250:
251: /**
252: * Creates a time series for testing.
253: *
254: * @return A time series.
255: */
256: private TimeSeries createSeries() {
257: RegularTimePeriod t = new Day();
258: TimeSeries series = new TimeSeries("Test");
259: series.add(t, 1.0);
260: t = t.next();
261: series.add(t, 2.0);
262: t = t.next();
263: series.add(t, null);
264: t = t.next();
265: series.add(t, 4.0);
266: return series;
267: }
268:
269: /**
270: * A test for bug report 1170825.
271: */
272: public void test1170825() {
273: TimeSeries s1 = new TimeSeries("Series1");
274: TimeSeriesCollection dataset = new TimeSeriesCollection();
275: dataset.addSeries(s1);
276: try {
277: /* TimeSeries s = */dataset.getSeries(1);
278: } catch (IllegalArgumentException e) {
279: // correct outcome
280: } catch (IndexOutOfBoundsException e) {
281: assertTrue(false); // wrong outcome
282: }
283: }
284:
285: /**
286: * Some tests for the indexOf() method.
287: */
288: public void testIndexOf() {
289: TimeSeries s1 = new TimeSeries("S1");
290: TimeSeries s2 = new TimeSeries("S2");
291: TimeSeriesCollection dataset = new TimeSeriesCollection();
292: assertEquals(-1, dataset.indexOf(s1));
293: assertEquals(-1, dataset.indexOf(s2));
294:
295: dataset.addSeries(s1);
296: assertEquals(0, dataset.indexOf(s1));
297: assertEquals(-1, dataset.indexOf(s2));
298:
299: dataset.addSeries(s2);
300: assertEquals(0, dataset.indexOf(s1));
301: assertEquals(1, dataset.indexOf(s2));
302:
303: dataset.removeSeries(s1);
304: assertEquals(-1, dataset.indexOf(s1));
305: assertEquals(0, dataset.indexOf(s2));
306:
307: TimeSeries s2b = new TimeSeries("S2");
308: assertEquals(0, dataset.indexOf(s2b));
309: }
310:
311: }
|