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. * TimeSeriesCollectionTests.java
  26. * ------------------------------
  27. * (C) Copyright 2003-2005, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: TimeSeriesCollectionTests.java,v 1.2 2005/01/14 17:28:37 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 01-May-2003 : Version 1 (DG);
  37. * 04-Dec-2003 : Added a test for the getSurroundingItems() method (DG);
  38. *
  39. */
  40. package org.jfree.data.time.junit;
  41. import java.io.ByteArrayInputStream;
  42. import java.io.ByteArrayOutputStream;
  43. import java.io.ObjectInput;
  44. import java.io.ObjectInputStream;
  45. import java.io.ObjectOutput;
  46. import java.io.ObjectOutputStream;
  47. import junit.framework.Test;
  48. import junit.framework.TestCase;
  49. import junit.framework.TestSuite;
  50. import org.jfree.data.time.Day;
  51. import org.jfree.data.time.RegularTimePeriod;
  52. import org.jfree.data.time.TimePeriodAnchor;
  53. import org.jfree.data.time.TimeSeries;
  54. import org.jfree.data.time.TimeSeriesCollection;
  55. /**
  56. * A collection of test cases for the {@link TimeSeriesCollection} class.
  57. */
  58. public class TimeSeriesCollectionTests extends TestCase {
  59. /**
  60. * Returns the tests as a test suite.
  61. *
  62. * @return The test suite.
  63. */
  64. public static Test suite() {
  65. return new TestSuite(TimeSeriesCollectionTests.class);
  66. }
  67. /**
  68. * Constructs a new set of tests.
  69. *
  70. * @param name the name of the tests.
  71. */
  72. public TimeSeriesCollectionTests(String name) {
  73. super(name);
  74. }
  75. /**
  76. * Some tests for the equals() method.
  77. */
  78. public void testEquals() {
  79. TimeSeriesCollection c1 = new TimeSeriesCollection();
  80. TimeSeriesCollection c2 = new TimeSeriesCollection();
  81. TimeSeries s1 = new TimeSeries("Series 1");
  82. TimeSeries s2 = new TimeSeries("Series 2");
  83. // newly created collections should be equal
  84. boolean b1 = c1.equals(c2);
  85. assertTrue("b1", b1);
  86. // add series to collection 1, should be not equal
  87. c1.addSeries(s1);
  88. c1.addSeries(s2);
  89. boolean b2 = c1.equals(c2);
  90. assertFalse("b2", b2);
  91. // now add the same series to collection 2 to make them equal again...
  92. c2.addSeries(s1);
  93. c2.addSeries(s2);
  94. boolean b3 = c1.equals(c2);
  95. assertTrue("b3", b3);
  96. // now remove series 2 from collection 2
  97. c2.removeSeries(s2);
  98. boolean b4 = c1.equals(c2);
  99. assertFalse("b4", b4);
  100. // now remove series 2 from collection 1 to make them equal again
  101. c1.removeSeries(s2);
  102. boolean b5 = c1.equals(c2);
  103. assertTrue("b5", b5);
  104. }
  105. /**
  106. * Tests the remove series method.
  107. */
  108. public void testRemoveSeries() {
  109. TimeSeriesCollection c1 = new TimeSeriesCollection();
  110. TimeSeries s1 = new TimeSeries("Series 1");
  111. TimeSeries s2 = new TimeSeries("Series 2");
  112. TimeSeries s3 = new TimeSeries("Series 3");
  113. TimeSeries s4 = new TimeSeries("Series 4");
  114. c1.addSeries(s1);
  115. c1.addSeries(s2);
  116. c1.addSeries(s3);
  117. c1.addSeries(s4);
  118. c1.removeSeries(s3);
  119. TimeSeries s = c1.getSeries(2);
  120. boolean b1 = s.equals(s4);
  121. assertTrue(b1);
  122. }
  123. /**
  124. * Test the getSurroundingItems() method to ensure it is returning the values we expect.
  125. */
  126. public void testGetSurroundingItems() {
  127. TimeSeries series = new TimeSeries("Series 1", Day.class);
  128. TimeSeriesCollection collection = new TimeSeriesCollection(series);
  129. collection.setXPosition(TimePeriodAnchor.MIDDLE);
  130. // for a series with no data, we expect {-1, -1}...
  131. int[] result = collection.getSurroundingItems(0, 1000L);
  132. assertTrue(result[0] == -1);
  133. assertTrue(result[1] == -1);
  134. // now test with a single value in the series...
  135. Day today = new Day();
  136. long start1 = today.getFirstMillisecond();
  137. long middle1 = today.getMiddleMillisecond();
  138. long end1 = today.getLastMillisecond();
  139. series.add(today, 99.9);
  140. result = collection.getSurroundingItems(0, start1);
  141. assertTrue(result[0] == -1);
  142. assertTrue(result[1] == 0);
  143. result = collection.getSurroundingItems(0, middle1);
  144. assertTrue(result[0] == 0);
  145. assertTrue(result[1] == 0);
  146. result = collection.getSurroundingItems(0, end1);
  147. assertTrue(result[0] == 0);
  148. assertTrue(result[1] == -1);
  149. // now add a second value to the series...
  150. Day tomorrow = (Day) today.next();
  151. long start2 = tomorrow.getFirstMillisecond();
  152. long middle2 = tomorrow.getMiddleMillisecond();
  153. long end2 = tomorrow.getLastMillisecond();
  154. series.add(tomorrow, 199.9);
  155. result = collection.getSurroundingItems(0, start2);
  156. assertTrue(result[0] == 0);
  157. assertTrue(result[1] == 1);
  158. result = collection.getSurroundingItems(0, middle2);
  159. assertTrue(result[0] == 1);
  160. assertTrue(result[1] == 1);
  161. result = collection.getSurroundingItems(0, end2);
  162. assertTrue(result[0] == 1);
  163. assertTrue(result[1] == -1);
  164. // now add a third value to the series...
  165. Day yesterday = (Day) today.previous();
  166. long start3 = yesterday.getFirstMillisecond();
  167. long middle3 = yesterday.getMiddleMillisecond();
  168. long end3 = yesterday.getLastMillisecond();
  169. series.add(yesterday, 1.23);
  170. result = collection.getSurroundingItems(0, start3);
  171. assertTrue(result[0] == -1);
  172. assertTrue(result[1] == 0);
  173. result = collection.getSurroundingItems(0, middle3);
  174. assertTrue(result[0] == 0);
  175. assertTrue(result[1] == 0);
  176. result = collection.getSurroundingItems(0, end3);
  177. assertTrue(result[0] == 0);
  178. assertTrue(result[1] == 1);
  179. }
  180. /**
  181. * Serialize an instance, restore it, and check for equality.
  182. */
  183. public void testSerialization() {
  184. TimeSeriesCollection c1 = new TimeSeriesCollection(createSeries());
  185. TimeSeriesCollection c2 = null;
  186. try {
  187. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  188. ObjectOutput out = new ObjectOutputStream(buffer);
  189. out.writeObject(c1);
  190. out.close();
  191. ObjectInput in = new ObjectInputStream(
  192. new ByteArrayInputStream(buffer.toByteArray())
  193. );
  194. c2 = (TimeSeriesCollection) in.readObject();
  195. in.close();
  196. }
  197. catch (Exception e) {
  198. System.out.println(e.toString());
  199. }
  200. assertEquals(c1, c2);
  201. }
  202. /**
  203. * Creates a time series for testing.
  204. *
  205. * @return A time series.
  206. */
  207. private TimeSeries createSeries() {
  208. RegularTimePeriod t = new Day();
  209. TimeSeries series = new TimeSeries("Test");
  210. series.add(t, 1.0);
  211. t = t.next();
  212. series.add(t, 2.0);
  213. t = t.next();
  214. series.add(t, null);
  215. t = t.next();
  216. series.add(t, 4.0);
  217. return series;
  218. }
  219. }