TimeSeriesCollectionTest.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2014, 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
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  25. * Other names may be trademarks of their respective owners.]
  26. *
  27. * -----------------------------
  28. * TimeSeriesCollectionTest.java
  29. * -----------------------------
  30. * (C) Copyright 2003-2014, by Object Refinery Limited.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes
  36. * -------
  37. * 01-May-2003 : Version 1 (DG);
  38. * 04-Dec-2003 : Added a test for the getSurroundingItems() method (DG);
  39. * 08-May-2007 : Added testIndexOf() method (DG);
  40. * 18-May-2009 : Added testFindDomainBounds() (DG);
  41. * 08-Jan-2012 : Added testBug3445507() (DG);
  42. *
  43. */
  44. package org.jfree.data.time;
  45. import static org.junit.Assert.assertTrue;
  46. import static org.junit.Assert.assertEquals;
  47. import static org.junit.Assert.assertFalse;
  48. import static org.junit.Assert.assertNull;
  49. import java.util.ArrayList;
  50. import java.util.Arrays;
  51. import java.util.Calendar;
  52. import java.util.Collections;
  53. import java.util.GregorianCalendar;
  54. import java.util.List;
  55. import java.util.Locale;
  56. import java.util.TimeZone;
  57. import org.jfree.chart.TestUtilities;
  58. import org.jfree.data.Range;
  59. import org.jfree.data.general.DatasetUtilities;
  60. import org.junit.Test;
  61. /**
  62. * A collection of test cases for the {@link TimeSeriesCollection} class.
  63. */
  64. public class TimeSeriesCollectionTest {
  65. /**
  66. * Some tests for the equals() method.
  67. */
  68. @Test
  69. public void testEquals() {
  70. TimeSeriesCollection c1 = new TimeSeriesCollection();
  71. TimeSeriesCollection c2 = new TimeSeriesCollection();
  72. TimeSeries s1 = new TimeSeries("Series 1");
  73. TimeSeries s2 = new TimeSeries("Series 2");
  74. // newly created collections should be equal
  75. boolean b1 = c1.equals(c2);
  76. assertTrue("b1", b1);
  77. // add series to collection 1, should be not equal
  78. c1.addSeries(s1);
  79. c1.addSeries(s2);
  80. boolean b2 = c1.equals(c2);
  81. assertFalse("b2", b2);
  82. // now add the same series to collection 2 to make them equal again...
  83. c2.addSeries(s1);
  84. c2.addSeries(s2);
  85. boolean b3 = c1.equals(c2);
  86. assertTrue("b3", b3);
  87. // now remove series 2 from collection 2
  88. c2.removeSeries(s2);
  89. boolean b4 = c1.equals(c2);
  90. assertFalse("b4", b4);
  91. // now remove series 2 from collection 1 to make them equal again
  92. c1.removeSeries(s2);
  93. boolean b5 = c1.equals(c2);
  94. assertTrue("b5", b5);
  95. }
  96. /**
  97. * Tests the remove series method.
  98. */
  99. @Test
  100. public void testRemoveSeries() {
  101. TimeSeriesCollection c1 = new TimeSeriesCollection();
  102. TimeSeries s1 = new TimeSeries("Series 1");
  103. TimeSeries s2 = new TimeSeries("Series 2");
  104. TimeSeries s3 = new TimeSeries("Series 3");
  105. TimeSeries s4 = new TimeSeries("Series 4");
  106. c1.addSeries(s1);
  107. c1.addSeries(s2);
  108. c1.addSeries(s3);
  109. c1.addSeries(s4);
  110. c1.removeSeries(s3);
  111. TimeSeries s = c1.getSeries(2);
  112. boolean b1 = s.equals(s4);
  113. assertTrue(b1);
  114. }
  115. /**
  116. * Some checks for the {@link TimeSeriesCollection#removeSeries(int)}
  117. * method.
  118. */
  119. @Test
  120. public void testRemoveSeries_int() {
  121. TimeSeriesCollection c1 = new TimeSeriesCollection();
  122. TimeSeries s1 = new TimeSeries("Series 1");
  123. TimeSeries s2 = new TimeSeries("Series 2");
  124. TimeSeries s3 = new TimeSeries("Series 3");
  125. TimeSeries s4 = new TimeSeries("Series 4");
  126. c1.addSeries(s1);
  127. c1.addSeries(s2);
  128. c1.addSeries(s3);
  129. c1.addSeries(s4);
  130. c1.removeSeries(2);
  131. assertTrue(c1.getSeries(2).equals(s4));
  132. c1.removeSeries(0);
  133. assertTrue(c1.getSeries(0).equals(s2));
  134. assertEquals(2, c1.getSeriesCount());
  135. }
  136. /**
  137. * Test the getSurroundingItems() method to ensure it is returning the
  138. * values we expect.
  139. */
  140. @Test
  141. public void testGetSurroundingItems() {
  142. TimeSeries series = new TimeSeries("Series 1");
  143. TimeSeriesCollection collection = new TimeSeriesCollection(series);
  144. collection.setXPosition(TimePeriodAnchor.MIDDLE);
  145. // for a series with no data, we expect {-1, -1}...
  146. int[] result = collection.getSurroundingItems(0, 1000L);
  147. assertTrue(result[0] == -1);
  148. assertTrue(result[1] == -1);
  149. // now test with a single value in the series...
  150. Day today = new Day();
  151. long start1 = today.getFirstMillisecond();
  152. long middle1 = today.getMiddleMillisecond();
  153. long end1 = today.getLastMillisecond();
  154. series.add(today, 99.9);
  155. result = collection.getSurroundingItems(0, start1);
  156. assertTrue(result[0] == -1);
  157. assertTrue(result[1] == 0);
  158. result = collection.getSurroundingItems(0, middle1);
  159. assertTrue(result[0] == 0);
  160. assertTrue(result[1] == 0);
  161. result = collection.getSurroundingItems(0, end1);
  162. assertTrue(result[0] == 0);
  163. assertTrue(result[1] == -1);
  164. // now add a second value to the series...
  165. Day tomorrow = (Day) today.next();
  166. long start2 = tomorrow.getFirstMillisecond();
  167. long middle2 = tomorrow.getMiddleMillisecond();
  168. long end2 = tomorrow.getLastMillisecond();
  169. series.add(tomorrow, 199.9);
  170. result = collection.getSurroundingItems(0, start2);
  171. assertTrue(result[0] == 0);
  172. assertTrue(result[1] == 1);
  173. result = collection.getSurroundingItems(0, middle2);
  174. assertTrue(result[0] == 1);
  175. assertTrue(result[1] == 1);
  176. result = collection.getSurroundingItems(0, end2);
  177. assertTrue(result[0] == 1);
  178. assertTrue(result[1] == -1);
  179. // now add a third value to the series...
  180. Day yesterday = (Day) today.previous();
  181. long start3 = yesterday.getFirstMillisecond();
  182. long middle3 = yesterday.getMiddleMillisecond();
  183. long end3 = yesterday.getLastMillisecond();
  184. series.add(yesterday, 1.23);
  185. result = collection.getSurroundingItems(0, start3);
  186. assertTrue(result[0] == -1);
  187. assertTrue(result[1] == 0);
  188. result = collection.getSurroundingItems(0, middle3);
  189. assertTrue(result[0] == 0);
  190. assertTrue(result[1] == 0);
  191. result = collection.getSurroundingItems(0, end3);
  192. assertTrue(result[0] == 0);
  193. assertTrue(result[1] == 1);
  194. }
  195. /**
  196. * Serialize an instance, restore it, and check for equality.
  197. */
  198. @Test
  199. public void testSerialization() {
  200. TimeSeriesCollection c1 = new TimeSeriesCollection(createSeries());
  201. TimeSeriesCollection c2 = (TimeSeriesCollection)
  202. TestUtilities.serialised(c1);
  203. assertEquals(c1, c2);
  204. }
  205. /**
  206. * Creates a time series for testing.
  207. *
  208. * @return A time series.
  209. */
  210. private TimeSeries createSeries() {
  211. RegularTimePeriod t = new Day();
  212. TimeSeries series = new TimeSeries("Test");
  213. series.add(t, 1.0);
  214. t = t.next();
  215. series.add(t, 2.0);
  216. t = t.next();
  217. series.add(t, null);
  218. t = t.next();
  219. series.add(t, 4.0);
  220. return series;
  221. }
  222. /**
  223. * A test for bug report 1170825.
  224. */
  225. @Test
  226. public void test1170825() {
  227. TimeSeries s1 = new TimeSeries("Series1");
  228. TimeSeriesCollection dataset = new TimeSeriesCollection();
  229. dataset.addSeries(s1);
  230. try {
  231. /* TimeSeries s = */ dataset.getSeries(1);
  232. }
  233. catch (IllegalArgumentException e) {
  234. // correct outcome
  235. }
  236. catch (IndexOutOfBoundsException e) {
  237. assertTrue(false); // wrong outcome
  238. }
  239. }
  240. /**
  241. * Some tests for the indexOf() method.
  242. */
  243. @Test
  244. public void testIndexOf() {
  245. TimeSeries s1 = new TimeSeries("S1");
  246. TimeSeries s2 = new TimeSeries("S2");
  247. TimeSeriesCollection dataset = new TimeSeriesCollection();
  248. assertEquals(-1, dataset.indexOf(s1));
  249. assertEquals(-1, dataset.indexOf(s2));
  250. dataset.addSeries(s1);
  251. assertEquals(0, dataset.indexOf(s1));
  252. assertEquals(-1, dataset.indexOf(s2));
  253. dataset.addSeries(s2);
  254. assertEquals(0, dataset.indexOf(s1));
  255. assertEquals(1, dataset.indexOf(s2));
  256. dataset.removeSeries(s1);
  257. assertEquals(-1, dataset.indexOf(s1));
  258. assertEquals(0, dataset.indexOf(s2));
  259. TimeSeries s2b = new TimeSeries("S2");
  260. assertEquals(0, dataset.indexOf(s2b));
  261. }
  262. private static final double EPSILON = 0.0000000001;
  263. /**
  264. * This method provides a check for the bounds calculated using the
  265. * {@link DatasetUtilities#findDomainBounds(org.jfree.data.xy.XYDataset,
  266. * java.util.List, boolean)} method.
  267. */
  268. @Test
  269. public void testFindDomainBounds() {
  270. TimeSeriesCollection dataset = new TimeSeriesCollection();
  271. List visibleSeriesKeys = new java.util.ArrayList();
  272. Range r = DatasetUtilities.findDomainBounds(dataset, visibleSeriesKeys,
  273. true);
  274. assertNull(r);
  275. TimeSeries s1 = new TimeSeries("S1");
  276. dataset.addSeries(s1);
  277. visibleSeriesKeys.add("S1");
  278. r = DatasetUtilities.findDomainBounds(dataset, visibleSeriesKeys, true);
  279. assertNull(r);
  280. // store the current time zone
  281. TimeZone saved = TimeZone.getDefault();
  282. TimeZone.setDefault(TimeZone.getTimeZone("Europe/Paris"));
  283. s1.add(new Year(2008), 8.0);
  284. r = DatasetUtilities.findDomainBounds(dataset, visibleSeriesKeys, true);
  285. assertEquals(1199142000000.0, r.getLowerBound(), EPSILON);
  286. assertEquals(1230764399999.0, r.getUpperBound(), EPSILON);
  287. TimeSeries s2 = new TimeSeries("S2");
  288. dataset.addSeries(s2);
  289. s2.add(new Year(2009), 9.0);
  290. s2.add(new Year(2010), 10.0);
  291. r = DatasetUtilities.findDomainBounds(dataset, visibleSeriesKeys, true);
  292. assertEquals(1199142000000.0, r.getLowerBound(), EPSILON);
  293. assertEquals(1230764399999.0, r.getUpperBound(), EPSILON);
  294. visibleSeriesKeys.add("S2");
  295. r = DatasetUtilities.findDomainBounds(dataset, visibleSeriesKeys, true);
  296. assertEquals(1199142000000.0, r.getLowerBound(), EPSILON);
  297. assertEquals(1293836399999.0, r.getUpperBound(), EPSILON);
  298. // restore the default time zone
  299. TimeZone.setDefault(saved);
  300. }
  301. /**
  302. * Basic checks for cloning.
  303. */
  304. @Test
  305. public void testCloning() throws CloneNotSupportedException {
  306. TimeSeries s1 = new TimeSeries("Series");
  307. s1.add(new Year(2009), 1.1);
  308. TimeSeriesCollection c1 = new TimeSeriesCollection();
  309. c1.addSeries(s1);
  310. TimeSeriesCollection c2 = (TimeSeriesCollection) c1.clone();
  311. assertTrue(c1 != c2);
  312. assertTrue(c1.getClass() == c2.getClass());
  313. assertTrue(c1.equals(c2));
  314. // check independence
  315. s1.setDescription("XYZ");
  316. assertFalse(c1.equals(c2));
  317. c2.getSeries(0).setDescription("XYZ");
  318. assertTrue(c1.equals(c2));
  319. }
  320. /**
  321. * A test to cover bug 3445507.
  322. */
  323. @Test
  324. public void testBug3445507() {
  325. TimeSeries s1 = new TimeSeries("S1");
  326. s1.add(new Year(2011), null);
  327. s1.add(new Year(2012), null);
  328. TimeSeries s2 = new TimeSeries("S2");
  329. s2.add(new Year(2011), 5.0);
  330. s2.add(new Year(2012), 6.0);
  331. TimeSeriesCollection dataset = new TimeSeriesCollection();
  332. dataset.addSeries(s1);
  333. dataset.addSeries(s2);
  334. List keys = new ArrayList();
  335. keys.add("S1");
  336. keys.add("S2");
  337. Range r = dataset.getRangeBounds(keys, new Range(
  338. Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY), false);
  339. assertEquals(5.0, r.getLowerBound(), EPSILON);
  340. assertEquals(6.0, r.getUpperBound(), EPSILON);
  341. }
  342. /**
  343. * Some checks for the getRangeBounds() method.
  344. */
  345. @Test
  346. public void testGetRangeBounds() {
  347. TimeSeriesCollection dataset = new TimeSeriesCollection();
  348. // when the dataset contains no series, we expect the range to be null
  349. assertNull(dataset.getRangeBounds(false));
  350. assertNull(dataset.getRangeBounds(true));
  351. // when the dataset contains one or more series, but those series
  352. // contain no items, we still expect the range to be null
  353. TimeSeries s1 = new TimeSeries("S1");
  354. dataset.addSeries(s1);
  355. assertNull(dataset.getRangeBounds(false));
  356. assertNull(dataset.getRangeBounds(true));
  357. // tests with values
  358. s1.add(new Year(2012), 1.0);
  359. assertEquals(new Range(1.0, 1.0), dataset.getRangeBounds(false));
  360. assertEquals(new Range(1.0, 1.0), dataset.getRangeBounds(true));
  361. s1.add(new Year(2013), -1.0);
  362. assertEquals(new Range(-1.0, 1.0), dataset.getRangeBounds(false));
  363. assertEquals(new Range(-1.0, 1.0), dataset.getRangeBounds(true));
  364. s1.add(new Year(2014), null);
  365. assertEquals(new Range(-1.0, 1.0), dataset.getRangeBounds(false));
  366. assertEquals(new Range(-1.0, 1.0), dataset.getRangeBounds(true));
  367. // adding a second series
  368. TimeSeries s2 = new TimeSeries("S2");
  369. dataset.addSeries(s2);
  370. assertEquals(new Range(-1.0, 1.0), dataset.getRangeBounds(false));
  371. assertEquals(new Range(-1.0, 1.0), dataset.getRangeBounds(true));
  372. s2.add(new Year(2014), 5.0);
  373. assertEquals(new Range(-1.0, 5.0), dataset.getRangeBounds(false));
  374. assertEquals(new Range(-1.0, 5.0), dataset.getRangeBounds(true));
  375. dataset.removeAllSeries();
  376. assertNull(dataset.getRangeBounds(false));
  377. assertNull(dataset.getRangeBounds(true));
  378. s1 = new TimeSeries("s1");
  379. s2 = new TimeSeries("s2");
  380. dataset.addSeries(s1);
  381. dataset.addSeries(s2);
  382. assertNull(dataset.getRangeBounds(false));
  383. assertNull(dataset.getRangeBounds(true));
  384. s2.add(new Year(2014), 100.0);
  385. assertEquals(new Range(100.0, 100.0), dataset.getRangeBounds(false));
  386. assertEquals(new Range(100.0, 100.0), dataset.getRangeBounds(true));
  387. }
  388. @Test
  389. public void testGetRangeBounds2() {
  390. TimeZone tzone = TimeZone.getTimeZone("Europe/London");
  391. Calendar calendar = new GregorianCalendar(tzone, Locale.UK);
  392. calendar.clear();
  393. calendar.set(2014, Calendar.FEBRUARY, 23, 6, 0);
  394. long start = calendar.getTimeInMillis();
  395. calendar.clear();
  396. calendar.set(2014, Calendar.FEBRUARY, 24, 18, 0);
  397. long end = calendar.getTimeInMillis();
  398. Range range = new Range(start, end);
  399. TimeSeriesCollection collection = new TimeSeriesCollection(tzone);
  400. assertNull(collection.getRangeBounds(Collections.EMPTY_LIST, range,
  401. true));
  402. TimeSeries s1 = new TimeSeries("S1");
  403. s1.add(new Day(24, 2, 2014), 10.0);
  404. collection.addSeries(s1);
  405. assertEquals(new Range(10.0, 10.0), collection.getRangeBounds(
  406. Arrays.asList("S1"), range, true));
  407. collection.setXPosition(TimePeriodAnchor.MIDDLE);
  408. assertEquals(new Range(10.0, 10.0), collection.getRangeBounds(
  409. Arrays.asList("S1"), range, true));
  410. collection.setXPosition(TimePeriodAnchor.END);
  411. assertNull(collection.getRangeBounds(Arrays.asList("S1"), range, true));
  412. }
  413. }