📄 timeseriestests.java
字号:
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* --------------------
* TimeSeriesTests.java
* --------------------
* (C) Copyright 2001-2005, by Object Refinery Limited.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): -;
*
* $Id: TimeSeriesTests.java,v 1.8.2.2 2005/11/17 12:07:45 mungady Exp $
*
* Changes
* -------
* 16-Nov-2001 : Version 1 (DG);
* 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
* 13-Mar-2003 : Added serialization test (DG);
* 15-Oct-2003 : Added test for setMaximumItemCount method (DG);
* 23-Aug-2004 : Added test that highlights a bug where the addOrUpdate()
* method can lead to more than maximumItemCount items in the
* dataset (DG);
*
*/
package org.jfree.data.time.junit;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.jfree.data.general.SeriesChangeEvent;
import org.jfree.data.general.SeriesChangeListener;
import org.jfree.data.general.SeriesException;
import org.jfree.data.time.Day;
import org.jfree.data.time.FixedMillisecond;
import org.jfree.data.time.Month;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesDataItem;
import org.jfree.data.time.Year;
import org.jfree.date.MonthConstants;
/**
* A collection of test cases for the {@link TimeSeries} class.
*/
public class TimeSeriesTests extends TestCase implements SeriesChangeListener {
/** A time series. */
private TimeSeries seriesA;
/** A time series. */
private TimeSeries seriesB;
/** A time series. */
private TimeSeries seriesC;
/** A flag that indicates whether or not a change event was fired. */
private boolean gotSeriesChangeEvent = false;
/**
* Returns the tests as a test suite.
*
* @return The test suite.
*/
public static Test suite() {
return new TestSuite(TimeSeriesTests.class);
}
/**
* Constructs a new set of tests.
*
* @param name the name of the tests.
*/
public TimeSeriesTests(String name) {
super(name);
}
/**
* Common test setup.
*/
protected void setUp() {
this.seriesA = new TimeSeries("Series A", Year.class);
try {
this.seriesA.add(new Year(2000), new Integer(102000));
this.seriesA.add(new Year(2001), new Integer(102001));
this.seriesA.add(new Year(2002), new Integer(102002));
this.seriesA.add(new Year(2003), new Integer(102003));
this.seriesA.add(new Year(2004), new Integer(102004));
this.seriesA.add(new Year(2005), new Integer(102005));
}
catch (SeriesException e) {
System.err.println("Problem creating series.");
}
this.seriesB = new TimeSeries("Series B", Year.class);
try {
this.seriesB.add(new Year(2006), new Integer(202006));
this.seriesB.add(new Year(2007), new Integer(202007));
this.seriesB.add(new Year(2008), new Integer(202008));
}
catch (SeriesException e) {
System.err.println("Problem creating series.");
}
this.seriesC = new TimeSeries("Series C", Year.class);
try {
this.seriesC.add(new Year(1999), new Integer(301999));
this.seriesC.add(new Year(2000), new Integer(302000));
this.seriesC.add(new Year(2002), new Integer(302002));
}
catch (SeriesException e) {
System.err.println("Problem creating series.");
}
}
/**
* Sets the flag to indicate that a {@link SeriesChangeEvent} has been
* received.
*
* @param event the event.
*/
public void seriesChanged(SeriesChangeEvent event) {
this.gotSeriesChangeEvent = true;
}
/**
* Check that cloning works.
*/
public void testClone() {
TimeSeries series = new TimeSeries("Test Series");
RegularTimePeriod jan1st2002 = new Day(1, MonthConstants.JANUARY, 2002);
try {
series.add(jan1st2002, new Integer(42));
}
catch (SeriesException e) {
System.err.println("Problem adding to series.");
}
TimeSeries clone = null;
try {
clone = (TimeSeries) series.clone();
clone.setKey("Clone Series");
try {
clone.update(jan1st2002, new Integer(10));
}
catch (SeriesException e) {
System.err.println("Problem updating series.");
}
}
catch (CloneNotSupportedException e) {
assertTrue(false);
}
int seriesValue = series.getValue(jan1st2002).intValue();
int cloneValue = clone.getValue(jan1st2002).intValue();
assertEquals(42, seriesValue);
assertEquals(10, cloneValue);
assertEquals("Test Series", series.getKey());
assertEquals("Clone Series", clone.getKey());
}
/**
* Add a value to series A for 1999. It should be added at index 0.
*/
public void testAddValue() {
try {
this.seriesA.add(new Year(1999), new Integer(1));
}
catch (SeriesException e) {
System.err.println("Problem adding to series.");
}
int value = this.seriesA.getValue(0).intValue();
assertEquals(1, value);
}
/**
* Tests the retrieval of values.
*/
public void testGetValue() {
Number value1 = this.seriesA.getValue(new Year(1999));
assertNull(value1);
int value2 = this.seriesA.getValue(new Year(2000)).intValue();
assertEquals(102000, value2);
}
/**
* Tests the deletion of values.
*/
public void testDelete() {
this.seriesA.delete(0, 0);
assertEquals(5, this.seriesA.getItemCount());
Number value = this.seriesA.getValue(new Year(2000));
assertNull(value);
}
/**
* Basic tests for the delete() method.
*/
public void testDelete2() {
TimeSeries s1 = new TimeSeries("Series", Year.class);
s1.add(new Year(2000), 13.75);
s1.add(new Year(2001), 11.90);
s1.add(new Year(2002), null);
s1.addChangeListener(this);
this.gotSeriesChangeEvent = false;
s1.delete(new Year(2001));
assertTrue(this.gotSeriesChangeEvent);
assertEquals(2, s1.getItemCount());
assertEquals(null, s1.getValue(new Year(2001)));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -