TimeTableXYDatasetTest.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2013, 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. * TimeTableXYDatasetTests.java
  29. * ----------------------------
  30. * (C) Copyright 2004-2008, by Object Refinery Limited.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): Rob Eden;
  34. *
  35. * Changes
  36. * -------
  37. * 15-Sep-2004 : Version 1 (DG);
  38. * 25-Jul-2007 : Added test for clear() method, by Rob Eden (DG);
  39. *
  40. */
  41. package org.jfree.data.time;
  42. import static org.junit.Assert.assertTrue;
  43. import static org.junit.Assert.assertFalse;
  44. import static org.junit.Assert.assertEquals;
  45. import java.util.TimeZone;
  46. import org.jfree.chart.TestUtilities;
  47. import org.junit.Test;
  48. /**
  49. * A collection of test cases for the {@link TimeTableXYDataset} class.
  50. */
  51. public class TimeTableXYDatasetTest {
  52. private static final double DELTA = 0.0000000001;
  53. /**
  54. * Some checks for a simple dataset.
  55. */
  56. @Test
  57. public void testStandard() {
  58. TimeTableXYDataset d = new TimeTableXYDataset();
  59. d.add(new Year(1999), 1.0, "Series 1");
  60. assertEquals(d.getItemCount(), 1);
  61. assertEquals(d.getSeriesCount(), 1);
  62. d.add(new Year(2000), 2.0, "Series 2");
  63. assertEquals(d.getItemCount(), 2);
  64. assertEquals(d.getSeriesCount(), 2);
  65. assertEquals(d.getYValue(0, 0), 1.0, DELTA);
  66. assertTrue(Double.isNaN(d.getYValue(0, 1)));
  67. assertTrue(Double.isNaN(d.getYValue(1, 0)));
  68. assertEquals(d.getYValue(1, 1), 2.0, DELTA);
  69. }
  70. /**
  71. * Some checks for the getTimePeriod() method.
  72. */
  73. @Test
  74. public void testGetTimePeriod() {
  75. TimeTableXYDataset d = new TimeTableXYDataset();
  76. d.add(new Year(1999), 1.0, "Series 1");
  77. d.add(new Year(1998), 2.0, "Series 1");
  78. d.add(new Year(1996), 3.0, "Series 1");
  79. assertEquals(d.getTimePeriod(0), new Year(1996));
  80. assertEquals(d.getTimePeriod(1), new Year(1998));
  81. assertEquals(d.getTimePeriod(2), new Year(1999));
  82. }
  83. /**
  84. * Some checks for the equals() method.
  85. */
  86. @Test
  87. public void testEquals() {
  88. TimeTableXYDataset d1 = new TimeTableXYDataset();
  89. TimeTableXYDataset d2 = new TimeTableXYDataset();
  90. assertTrue(d1.equals(d2));
  91. assertTrue(d2.equals(d1));
  92. d1.add(new Year(1999), 123.4, "S1");
  93. assertFalse(d1.equals(d2));
  94. d2.add(new Year(1999), 123.4, "S1");
  95. assertTrue(d1.equals(d2));
  96. d1.setDomainIsPointsInTime(!d1.getDomainIsPointsInTime());
  97. assertFalse(d1.equals(d2));
  98. d2.setDomainIsPointsInTime(!d2.getDomainIsPointsInTime());
  99. assertTrue(d1.equals(d2));
  100. d1 = new TimeTableXYDataset(TimeZone.getTimeZone("GMT"));
  101. d2 = new TimeTableXYDataset(TimeZone.getTimeZone(
  102. "America/Los_Angeles"));
  103. assertFalse(d1.equals(d2));
  104. }
  105. /**
  106. * Some checks for cloning.
  107. */
  108. @Test
  109. public void testClone() {
  110. TimeTableXYDataset d = new TimeTableXYDataset();
  111. d.add(new Year(1999), 25.0, "Series");
  112. TimeTableXYDataset clone = null;
  113. try {
  114. clone = (TimeTableXYDataset) d.clone();
  115. }
  116. catch (CloneNotSupportedException e) {
  117. assertTrue(false);
  118. }
  119. assertTrue(clone.equals(d));
  120. // now test that the clone is independent of the original
  121. clone.add(new Year(2004), 1.2, "SS");
  122. assertFalse(clone.equals(d));
  123. }
  124. /**
  125. * Serialize an instance, restore it, and check for equality.
  126. */
  127. @Test
  128. public void testSerialization() {
  129. TimeTableXYDataset d1 = new TimeTableXYDataset();
  130. d1.add(new Year(1999), 123.4, "S1");
  131. TimeTableXYDataset d2 = (TimeTableXYDataset)
  132. TestUtilities.serialised(d1);
  133. assertTrue(d1.equals(d2));
  134. }
  135. /**
  136. * Test clearing data.
  137. */
  138. @Test
  139. public void testClear() {
  140. TimeTableXYDataset d = new TimeTableXYDataset();
  141. d.add(new Year(1999), 1.0, "Series 1");
  142. assertEquals(d.getItemCount(), 1);
  143. assertEquals(d.getSeriesCount(), 1);
  144. d.add(new Year(2000), 2.0, "Series 2");
  145. d.clear();
  146. // Make sure there's nothing left
  147. assertEquals(0, d.getItemCount());
  148. assertEquals(0, d.getSeriesCount());
  149. }
  150. }