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: * TimePeriodValueTests.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: TimePeriodValuesTests.java,v 1.1.2.2 2007/03/08 11:15:17 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 30-Jul-2003 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.data.time.junit;
044:
045: import java.io.ByteArrayInputStream;
046: import java.io.ByteArrayOutputStream;
047: import java.io.ObjectInput;
048: import java.io.ObjectInputStream;
049: import java.io.ObjectOutput;
050: import java.io.ObjectOutputStream;
051: import java.util.Date;
052:
053: import junit.framework.Test;
054: import junit.framework.TestCase;
055: import junit.framework.TestSuite;
056:
057: import org.jfree.data.general.SeriesChangeEvent;
058: import org.jfree.data.general.SeriesChangeListener;
059: import org.jfree.data.general.SeriesException;
060: import org.jfree.data.time.Day;
061: import org.jfree.data.time.RegularTimePeriod;
062: import org.jfree.data.time.SimpleTimePeriod;
063: import org.jfree.data.time.TimePeriodValue;
064: import org.jfree.data.time.TimePeriodValues;
065: import org.jfree.data.time.Year;
066: import org.jfree.date.MonthConstants;
067:
068: /**
069: * A collection of test cases for the {@link TimePeriodValues} class.
070: */
071: public class TimePeriodValuesTests extends TestCase {
072:
073: /** Series A. */
074: private TimePeriodValues seriesA;
075:
076: /** Series B. */
077: private TimePeriodValues seriesB;
078:
079: /** Series C. */
080: private TimePeriodValues seriesC;
081:
082: /**
083: * Returns the tests as a test suite.
084: *
085: * @return The test suite.
086: */
087: public static Test suite() {
088: return new TestSuite(TimePeriodValuesTests.class);
089: }
090:
091: /**
092: * Constructs a new set of tests.
093: *
094: * @param name the name of the tests.
095: */
096: public TimePeriodValuesTests(String name) {
097: super (name);
098: }
099:
100: /**
101: * Common test setup.
102: */
103: protected void setUp() {
104:
105: this .seriesA = new TimePeriodValues("Series A");
106: try {
107: this .seriesA.add(new Year(2000), new Integer(102000));
108: this .seriesA.add(new Year(2001), new Integer(102001));
109: this .seriesA.add(new Year(2002), new Integer(102002));
110: this .seriesA.add(new Year(2003), new Integer(102003));
111: this .seriesA.add(new Year(2004), new Integer(102004));
112: this .seriesA.add(new Year(2005), new Integer(102005));
113: } catch (SeriesException e) {
114: System.err.println("Problem creating series.");
115: }
116:
117: this .seriesB = new TimePeriodValues("Series B");
118: try {
119: this .seriesB.add(new Year(2006), new Integer(202006));
120: this .seriesB.add(new Year(2007), new Integer(202007));
121: this .seriesB.add(new Year(2008), new Integer(202008));
122: } catch (SeriesException e) {
123: System.err.println("Problem creating series.");
124: }
125:
126: this .seriesC = new TimePeriodValues("Series C");
127: try {
128: this .seriesC.add(new Year(1999), new Integer(301999));
129: this .seriesC.add(new Year(2000), new Integer(302000));
130: this .seriesC.add(new Year(2002), new Integer(302002));
131: } catch (SeriesException e) {
132: System.err.println("Problem creating series.");
133: }
134:
135: }
136:
137: /**
138: * Set up a quarter equal to Q1 1900. Request the previous quarter, it
139: * should be null.
140: */
141: public void testClone() {
142:
143: TimePeriodValues series = new TimePeriodValues("Test Series");
144:
145: RegularTimePeriod jan1st2002 = new Day(1,
146: MonthConstants.JANUARY, 2002);
147: try {
148: series.add(jan1st2002, new Integer(42));
149: } catch (SeriesException e) {
150: System.err.println("Problem adding to collection.");
151: }
152:
153: TimePeriodValues clone = null;
154: try {
155: clone = (TimePeriodValues) series.clone();
156: clone.setKey("Clone Series");
157: try {
158: clone.update(0, new Integer(10));
159: } catch (SeriesException e) {
160: System.err.println("Problem updating series.");
161: }
162: } catch (CloneNotSupportedException e) {
163: assertTrue(false);
164: }
165:
166: int seriesValue = series.getValue(0).intValue();
167: int cloneValue = clone.getValue(0).intValue();
168:
169: assertEquals(42, seriesValue);
170: assertEquals(10, cloneValue);
171: assertEquals("Test Series", series.getKey());
172: assertEquals("Clone Series", clone.getKey());
173:
174: }
175:
176: /**
177: * Add a value to series A for 1999. It should be added at index 0.
178: */
179: public void testAddValue() {
180:
181: TimePeriodValues tpvs = new TimePeriodValues("Test");
182: try {
183: tpvs.add(new Year(1999), new Integer(1));
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 buffer = new ByteArrayOutputStream();
208: ObjectOutput out = new ObjectOutputStream(buffer);
209: out.writeObject(s1);
210: out.close();
211:
212: ObjectInput in = new ObjectInputStream(
213: new ByteArrayInputStream(buffer.toByteArray()));
214: s2 = (TimePeriodValues) in.readObject();
215: in.close();
216: } catch (Exception e) {
217: e.printStackTrace();
218: }
219: assertTrue(s1.equals(s2));
220:
221: }
222:
223: /**
224: * Tests the equals method.
225: */
226: public void testEquals() {
227: TimePeriodValues s1 = new TimePeriodValues("Time Series 1");
228: TimePeriodValues s2 = new TimePeriodValues("Time Series 2");
229: boolean b1 = s1.equals(s2);
230: assertFalse("b1", b1);
231:
232: s2.setKey("Time Series 1");
233: boolean b2 = s1.equals(s2);
234: assertTrue("b2", b2);
235:
236: // domain description
237: s1.setDomainDescription("XYZ");
238: assertFalse(s1.equals(s2));
239: s2.setDomainDescription("XYZ");
240: assertTrue(s1.equals(s2));
241:
242: // domain description - null
243: s1.setDomainDescription(null);
244: assertFalse(s1.equals(s2));
245: s2.setDomainDescription(null);
246: assertTrue(s1.equals(s2));
247:
248: // range description
249: s1.setRangeDescription("XYZ");
250: assertFalse(s1.equals(s2));
251: s2.setRangeDescription("XYZ");
252: assertTrue(s1.equals(s2));
253:
254: // range description - null
255: s1.setRangeDescription(null);
256: assertFalse(s1.equals(s2));
257: s2.setRangeDescription(null);
258: assertTrue(s1.equals(s2));
259:
260: RegularTimePeriod p1 = new Day();
261: RegularTimePeriod p2 = p1.next();
262: s1.add(p1, 100.0);
263: s1.add(p2, 200.0);
264: boolean b3 = s1.equals(s2);
265: assertFalse("b3", b3);
266:
267: s2.add(p1, 100.0);
268: s2.add(p2, 200.0);
269: boolean b4 = s1.equals(s2);
270: assertTrue("b4", b4);
271:
272: }
273:
274: /**
275: * A test for bug report 1161329.
276: */
277: public void test1161329() {
278: TimePeriodValues tpv = new TimePeriodValues("Test");
279: RegularTimePeriod t = new Day();
280: tpv.add(t, 1.0);
281: t = t.next();
282: tpv.add(t, 2.0);
283: tpv.delete(0, 1);
284: assertEquals(0, tpv.getItemCount());
285: tpv.add(t, 2.0);
286: assertEquals(1, tpv.getItemCount());
287: }
288:
289: static final double EPSILON = 0.0000000001;
290:
291: /**
292: * Some checks for the add() methods.
293: */
294: public void testAdd() {
295: TimePeriodValues tpv = new TimePeriodValues("Test");
296: MySeriesChangeListener listener = new MySeriesChangeListener();
297: tpv.addChangeListener(listener);
298: tpv.add(new TimePeriodValue(new SimpleTimePeriod(new Date(1L),
299: new Date(3L)), 99.0));
300: assertEquals(99.0, tpv.getValue(0).doubleValue(), EPSILON);
301: assertEquals(tpv, listener.getLastEvent().getSource());
302:
303: // a null item should throw an IllegalArgumentException
304: boolean pass = false;
305: try {
306: tpv.add((TimePeriodValue) null);
307: } catch (IllegalArgumentException e) {
308: pass = true;
309: }
310: assertTrue(pass);
311: }
312:
313: /**
314: * A listener used for detecting series change events.
315: */
316: static class MySeriesChangeListener implements SeriesChangeListener {
317:
318: SeriesChangeEvent lastEvent;
319:
320: /**
321: * Creates a new listener.
322: */
323: public MySeriesChangeListener() {
324: this .lastEvent = null;
325: }
326:
327: /**
328: * Returns the last event.
329: *
330: * @return The last event (possibly <code>null</code>).
331: */
332: public SeriesChangeEvent getLastEvent() {
333: return this .lastEvent;
334: }
335:
336: /**
337: * Clears the last event (sets it to <code>null</code>).
338: */
339: public void clearLastEvent() {
340: this .lastEvent = null;
341: }
342:
343: /**
344: * Callback method for series change events.
345: *
346: * @param event the event.
347: */
348: public void seriesChanged(SeriesChangeEvent event) {
349: this.lastEvent = event;
350: }
351: }
352:
353: }
|