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 under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * --------------------
  25. * TimeSeriesTests.java
  26. * --------------------
  27. * (C) Copyright 2001-2005, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: TimeSeriesTests.java,v 1.5 2005/02/10 10:05:07 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 16-Nov-2001 : Version 1 (DG);
  37. * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  38. * 13-Mar-2003 : Added serialization test (DG);
  39. * 15-Oct-2003 : Added test for setMaximumItemCount method (DG);
  40. * 23-Aug-2004 : Added test that highlights a bug where the addOrUpdate() method can
  41. * lead to more than maximumItemCount items in the dataset (DG);
  42. *
  43. */
  44. package org.jfree.data.time.junit;
  45. import java.io.ByteArrayInputStream;
  46. import java.io.ByteArrayOutputStream;
  47. import java.io.ObjectInput;
  48. import java.io.ObjectInputStream;
  49. import java.io.ObjectOutput;
  50. import java.io.ObjectOutputStream;
  51. import junit.framework.Test;
  52. import junit.framework.TestCase;
  53. import junit.framework.TestSuite;
  54. import org.jfree.data.general.SeriesChangeEvent;
  55. import org.jfree.data.general.SeriesChangeListener;
  56. import org.jfree.data.general.SeriesException;
  57. import org.jfree.data.time.Day;
  58. import org.jfree.data.time.FixedMillisecond;
  59. import org.jfree.data.time.Month;
  60. import org.jfree.data.time.RegularTimePeriod;
  61. import org.jfree.data.time.TimeSeries;
  62. import org.jfree.data.time.TimeSeriesDataItem;
  63. import org.jfree.data.time.Year;
  64. import org.jfree.date.MonthConstants;
  65. /**
  66. * A collection of test cases for the {@link TimeSeries} class.
  67. */
  68. public class TimeSeriesTests extends TestCase implements SeriesChangeListener {
  69. /** A time series. */
  70. private TimeSeries seriesA;
  71. /** A time series. */
  72. private TimeSeries seriesB;
  73. /** A time series. */
  74. private TimeSeries seriesC;
  75. /** A flag that indicates whether or not a change event was fired. */
  76. private boolean gotSeriesChangeEvent = false;
  77. /**
  78. * Returns the tests as a test suite.
  79. *
  80. * @return The test suite.
  81. */
  82. public static Test suite() {
  83. return new TestSuite(TimeSeriesTests.class);
  84. }
  85. /**
  86. * Constructs a new set of tests.
  87. *
  88. * @param name the name of the tests.
  89. */
  90. public TimeSeriesTests(String name) {
  91. super(name);
  92. }
  93. /**
  94. * Common test setup.
  95. */
  96. protected void setUp() {
  97. this.seriesA = new TimeSeries("Series A", Year.class);
  98. try {
  99. this.seriesA.add(new Year(2000), new Integer(102000));
  100. this.seriesA.add(new Year(2001), new Integer(102001));
  101. this.seriesA.add(new Year(2002), new Integer(102002));
  102. this.seriesA.add(new Year(2003), new Integer(102003));
  103. this.seriesA.add(new Year(2004), new Integer(102004));
  104. this.seriesA.add(new Year(2005), new Integer(102005));
  105. }
  106. catch (SeriesException e) {
  107. System.err.println("TimeSeriesTests.setUp(): problem creating series.");
  108. }
  109. this.seriesB = new TimeSeries("Series B", Year.class);
  110. try {
  111. this.seriesB.add(new Year(2006), new Integer(202006));
  112. this.seriesB.add(new Year(2007), new Integer(202007));
  113. this.seriesB.add(new Year(2008), new Integer(202008));
  114. }
  115. catch (SeriesException e) {
  116. System.err.println("TimeSeriesTests.setUp(): problem creating series.");
  117. }
  118. this.seriesC = new TimeSeries("Series C", Year.class);
  119. try {
  120. this.seriesC.add(new Year(1999), new Integer(301999));
  121. this.seriesC.add(new Year(2000), new Integer(302000));
  122. this.seriesC.add(new Year(2002), new Integer(302002));
  123. }
  124. catch (SeriesException e) {
  125. System.err.println("TimeSeriesTests.setUp(): problem creating series.");
  126. }
  127. }
  128. /**
  129. * Sets the flag to indicate that a {@link SeriesChangeEvent} has been received.
  130. *
  131. * @param event the event.
  132. */
  133. public void seriesChanged(SeriesChangeEvent event) {
  134. this.gotSeriesChangeEvent = true;
  135. }
  136. /**
  137. * Check that cloning works.
  138. */
  139. public void testClone() {
  140. TimeSeries series = new TimeSeries("Test Series");
  141. RegularTimePeriod jan1st2002 = new Day(1, MonthConstants.JANUARY, 2002);
  142. try {
  143. series.add(jan1st2002, new Integer(42));
  144. }
  145. catch (SeriesException e) {
  146. System.err.println("TimeSeriesTests.testClone: problem adding to series.");
  147. }
  148. TimeSeries clone = null;
  149. try {
  150. clone = (TimeSeries) series.clone();
  151. clone.setName("Clone Series");
  152. try {
  153. clone.update(jan1st2002, new Integer(10));
  154. }
  155. catch (SeriesException e) {
  156. System.err.println("TimeSeriesTests.testClone: problem updating series.");
  157. }
  158. }
  159. catch (CloneNotSupportedException e) {
  160. assertTrue(false);
  161. }
  162. int seriesValue = series.getValue(jan1st2002).intValue();
  163. int cloneValue = clone.getValue(jan1st2002).intValue();
  164. assertEquals(42, seriesValue);
  165. assertEquals(10, cloneValue);
  166. assertEquals("Test Series", series.getName());
  167. assertEquals("Clone Series", clone.getName());
  168. }
  169. /**
  170. * Add a value to series A for 1999. It should be added at index 0.
  171. */
  172. public void testAddValue() {
  173. try {
  174. this.seriesA.add(new Year(1999), new Integer(1));
  175. }
  176. catch (SeriesException e) {
  177. System.err.println("TimeSeriesTests.testAddValue: problem adding to series.");
  178. }
  179. int value = this.seriesA.getValue(0).intValue();
  180. assertEquals(1, value);
  181. }
  182. /**
  183. * Tests the retrieval of values.
  184. */
  185. public void testGetValue() {
  186. Number value1 = this.seriesA.getValue(new Year(1999));
  187. assertNull(value1);
  188. int value2 = this.seriesA.getValue(new Year(2000)).intValue();
  189. assertEquals(102000, value2);
  190. }
  191. /**
  192. * Tests the deletion of values.
  193. */
  194. public void testDelete() {
  195. this.seriesA.delete(0, 0);
  196. assertEquals(5, this.seriesA.getItemCount());
  197. Number value = this.seriesA.getValue(new Year(2000));
  198. assertNull(value);
  199. }
  200. /**
  201. * Basic tests for the delete() method.
  202. */
  203. public void testDelete2() {
  204. TimeSeries s1 = new TimeSeries("Series", Year.class);
  205. s1.add(new Year(2000), 13.75);
  206. s1.add(new Year(2001), 11.90);
  207. s1.add(new Year(2002), null);
  208. s1.addChangeListener(this);
  209. this.gotSeriesChangeEvent = false;
  210. s1.delete(new Year(2001));
  211. assertTrue(this.gotSeriesChangeEvent);
  212. assertEquals(2, s1.getItemCount());
  213. assertEquals(null, s1.getValue(new Year(2001)));
  214. }
  215. /**
  216. * Serialize an instance, restore it, and check for equality.
  217. */
  218. public void testSerialization() {
  219. TimeSeries s1 = new TimeSeries("A test", Year.class);
  220. s1.add(new Year(2000), 13.75);
  221. s1.add(new Year(2001), 11.90);
  222. s1.add(new Year(2002), null);
  223. s1.add(new Year(2005), 19.32);
  224. s1.add(new Year(2007), 16.89);
  225. TimeSeries s2 = null;
  226. try {
  227. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  228. ObjectOutput out = new ObjectOutputStream(buffer);
  229. out.writeObject(s1);
  230. out.close();
  231. ObjectInput in = new ObjectInputStream(
  232. new ByteArrayInputStream(buffer.toByteArray())
  233. );
  234. s2 = (TimeSeries) in.readObject();
  235. in.close();
  236. }
  237. catch (Exception e) {
  238. System.out.println(e.toString());
  239. }
  240. assertTrue(s1.equals(s2));
  241. }
  242. /**
  243. * Tests the equals method.
  244. */
  245. public void testEquals() {
  246. TimeSeries s1 = new TimeSeries("Time Series 1");
  247. TimeSeries s2 = new TimeSeries("Time Series 2");
  248. boolean b1 = s1.equals(s2);
  249. assertFalse("b1", b1);
  250. s2.setName("Time Series 1");
  251. boolean b2 = s1.equals(s2);
  252. assertTrue("b2", b2);
  253. RegularTimePeriod p1 = new Day();
  254. RegularTimePeriod p2 = p1.next();
  255. s1.add(p1, 100.0);
  256. s1.add(p2, 200.0);
  257. boolean b3 = s1.equals(s2);
  258. assertFalse("b3", b3);
  259. s2.add(p1, 100.0);
  260. s2.add(p2, 200.0);
  261. boolean b4 = s1.equals(s2);
  262. assertTrue("b4", b4);
  263. s1.setMaximumItemCount(100);
  264. boolean b5 = s1.equals(s2);
  265. assertFalse("b5", b5);
  266. s2.setMaximumItemCount(100);
  267. boolean b6 = s1.equals(s2);
  268. assertTrue("b6", b6);
  269. s1.setHistoryCount(100);
  270. boolean b7 = s1.equals(s2);
  271. assertFalse("b7", b7);
  272. s2.setHistoryCount(100);
  273. boolean b8 = s1.equals(s2);
  274. assertTrue("b8", b8);
  275. }
  276. /**
  277. * Tests a specific bug report where null arguments in the constructor cause the
  278. * equals() method to fail. Fixed for 0.9.21.
  279. */
  280. public void testEquals2() {
  281. TimeSeries s1 = new TimeSeries("Series", null, null, Day.class);
  282. TimeSeries s2 = new TimeSeries("Series", null, null, Day.class);
  283. assertTrue(s1.equals(s2));
  284. }
  285. /**
  286. * Some tests to ensure that the createCopy(RegularTimePeriod, RegularTimePeriod) method
  287. * is functioning correctly.
  288. */
  289. public void testCreateCopy1() {
  290. TimeSeries series = new TimeSeries("Series", Month.class);
  291. series.add(new Month(MonthConstants.JANUARY, 2003), 45.0);
  292. series.add(new Month(MonthConstants.FEBRUARY, 2003), 55.0);
  293. series.add(new Month(MonthConstants.JUNE, 2003), 35.0);
  294. series.add(new Month(MonthConstants.NOVEMBER, 2003), 85.0);
  295. series.add(new Month(MonthConstants.DECEMBER, 2003), 75.0);
  296. try {
  297. // copy a range before the start of the series data...
  298. TimeSeries result1 = series.createCopy(new Month(MonthConstants.NOVEMBER, 2002),
  299. new Month(MonthConstants.DECEMBER, 2002));
  300. assertEquals(0, result1.getItemCount());
  301. // copy a range that includes only the first item in the series...
  302. TimeSeries result2 = series.createCopy(new Month(MonthConstants.NOVEMBER, 2002),
  303. new Month(MonthConstants.JANUARY, 2003));
  304. assertEquals(1, result2.getItemCount());
  305. // copy a range that begins before and ends in the middle of the series...
  306. TimeSeries result3 = series.createCopy(new Month(MonthConstants.NOVEMBER, 2002),
  307. new Month(MonthConstants.APRIL, 2003));
  308. assertEquals(2, result3.getItemCount());
  309. TimeSeries result4 = series.createCopy(new Month(MonthConstants.NOVEMBER, 2002),
  310. new Month(MonthConstants.DECEMBER, 2003));
  311. assertEquals(5, result4.getItemCount());
  312. TimeSeries result5 = series.createCopy(new Month(MonthConstants.NOVEMBER, 2002),
  313. new Month(MonthConstants.MARCH, 2004));
  314. assertEquals(5, result5.getItemCount());
  315. TimeSeries result6 = series.createCopy(new Month(MonthConstants.JANUARY, 2003),
  316. new Month(MonthConstants.JANUARY, 2003));
  317. assertEquals(1, result6.getItemCount());
  318. TimeSeries result7 = series.createCopy(new Month(MonthConstants.JANUARY, 2003),
  319. new Month(MonthConstants.APRIL, 2003));
  320. assertEquals(2, result7.getItemCount());
  321. TimeSeries result8 = series.createCopy(new Month(MonthConstants.JANUARY, 2003),
  322. new Month(MonthConstants.DECEMBER, 2003));
  323. assertEquals(5, result8.getItemCount());
  324. TimeSeries result9 = series.createCopy(new Month(MonthConstants.JANUARY, 2003),
  325. new Month(MonthConstants.MARCH, 2004));
  326. assertEquals(5, result9.getItemCount());
  327. TimeSeries result10 = series.createCopy(new Month(MonthConstants.MAY, 2003),
  328. new Month(MonthConstants.DECEMBER, 2003));
  329. assertEquals(3, result10.getItemCount());
  330. TimeSeries result11 = series.createCopy(new Month(MonthConstants.MAY, 2003),
  331. new Month(MonthConstants.MARCH, 2004));
  332. assertEquals(3, result11.getItemCount());
  333. TimeSeries result12 = series.createCopy(new Month(MonthConstants.DECEMBER, 2003),
  334. new Month(MonthConstants.DECEMBER, 2003));
  335. assertEquals(1, result12.getItemCount());
  336. TimeSeries result13 = series.createCopy(new Month(MonthConstants.DECEMBER, 2003),
  337. new Month(MonthConstants.MARCH, 2004));
  338. assertEquals(1, result13.getItemCount());
  339. TimeSeries result14 = series.createCopy(new Month(MonthConstants.JANUARY, 2004),
  340. new Month(MonthConstants.MARCH, 2004));
  341. assertEquals(0, result14.getItemCount());
  342. }
  343. catch (CloneNotSupportedException e) {
  344. assertTrue(false);
  345. }
  346. }
  347. /**
  348. * Test the setMaximumItemCount() method to ensure that it removes items from the series
  349. * if necessary.
  350. */
  351. public void testSetMaximumItemCount() {
  352. TimeSeries s1 = new TimeSeries("S1", Year.class);
  353. s1.add(new Year(2000), 13.75);
  354. s1.add(new Year(2001), 11.90);
  355. s1.add(new Year(2002), null);
  356. s1.add(new Year(2005), 19.32);
  357. s1.add(new Year(2007), 16.89);
  358. assertTrue(s1.getItemCount() == 5);
  359. s1.setMaximumItemCount(3);
  360. assertTrue(s1.getItemCount() == 3);
  361. TimeSeriesDataItem item = s1.getDataItem(0);
  362. assertTrue(item.getPeriod().equals(new Year(2002)));
  363. }
  364. /**
  365. * Some checks for the addOrUpdate() method.
  366. */
  367. public void testAddOrUpdate() {
  368. TimeSeries s1 = new TimeSeries("S1", Year.class);
  369. s1.setMaximumItemCount(2);
  370. s1.addOrUpdate(new Year(2000), 100.0);
  371. assertEquals(1, s1.getItemCount());
  372. s1.addOrUpdate(new Year(2001), 101.0);
  373. assertEquals(2, s1.getItemCount());
  374. s1.addOrUpdate(new Year(2001), 102.0);
  375. assertEquals(2, s1.getItemCount());
  376. s1.addOrUpdate(new Year(2002), 103.0);
  377. assertEquals(2, s1.getItemCount());
  378. }
  379. /**
  380. * A test for the bug report 1075255.
  381. */
  382. public void testBug1075255() {
  383. TimeSeries ts = new TimeSeries("dummy", FixedMillisecond.class);
  384. ts.add(new FixedMillisecond(0L), 0.0);
  385. TimeSeries ts2 = new TimeSeries("dummy2", FixedMillisecond.class);
  386. ts2.add(new FixedMillisecond(0L), 1.0);
  387. try {
  388. ts.addAndOrUpdate(ts2);
  389. }
  390. catch (Exception e) {
  391. e.printStackTrace();
  392. assertTrue(false);
  393. }
  394. assertEquals(1, ts.getItemCount());
  395. }
  396. }