1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2004, 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. * TimeTableXYDatasetTests.java
  26. * ----------------------------
  27. * (C) Copyright 2004, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: TimeTableXYDatasetTests.java,v 1.3 2005/01/12 14:59:05 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 15-Sep-2004 : Version 1 (DG);
  37. *
  38. */
  39. package org.jfree.data.time.junit;
  40. import java.io.ByteArrayInputStream;
  41. import java.io.ByteArrayOutputStream;
  42. import java.io.ObjectInput;
  43. import java.io.ObjectInputStream;
  44. import java.io.ObjectOutput;
  45. import java.io.ObjectOutputStream;
  46. import java.util.TimeZone;
  47. import junit.framework.Test;
  48. import junit.framework.TestCase;
  49. import junit.framework.TestSuite;
  50. import org.jfree.data.time.TimeTableXYDataset;
  51. import org.jfree.data.time.Year;
  52. /**
  53. * A collection of test cases for the {@link TimeTableXYDataset} class.
  54. */
  55. public class TimeTableXYDatasetTests extends TestCase {
  56. /**
  57. * Returns the tests as a test suite.
  58. *
  59. * @return The test suite.
  60. */
  61. public static Test suite() {
  62. return new TestSuite(TimeTableXYDatasetTests.class);
  63. }
  64. /**
  65. * Constructs a new set of tests.
  66. *
  67. * @param name the name of the tests.
  68. */
  69. public TimeTableXYDatasetTests(String name) {
  70. super(name);
  71. }
  72. private static final double DELTA = 0.0000000001;
  73. /**
  74. * Some checks for a simple dataset.
  75. */
  76. public void testStandard() {
  77. TimeTableXYDataset d = new TimeTableXYDataset();
  78. d.add(new Year(1999), 1.0, "Series 1");
  79. assertEquals(d.getItemCount(), 1);
  80. assertEquals(d.getSeriesCount(), 1);
  81. d.add(new Year(2000), 2.0, "Series 2");
  82. assertEquals(d.getItemCount(), 2);
  83. assertEquals(d.getSeriesCount(), 2);
  84. assertEquals(d.getYValue(0, 0), 1.0, DELTA);
  85. assertTrue(Double.isNaN(d.getYValue(0, 1)));
  86. assertTrue(Double.isNaN(d.getYValue(1, 0)));
  87. assertEquals(d.getYValue(1, 1), 2.0, DELTA);
  88. }
  89. /**
  90. * Some checks for the getTimePeriod() method.
  91. */
  92. public void testGetTimePeriod() {
  93. TimeTableXYDataset d = new TimeTableXYDataset();
  94. d.add(new Year(1999), 1.0, "Series 1");
  95. d.add(new Year(1998), 2.0, "Series 1");
  96. d.add(new Year(1996), 3.0, "Series 1");
  97. assertEquals(d.getTimePeriod(0), new Year(1996));
  98. assertEquals(d.getTimePeriod(1), new Year(1998));
  99. assertEquals(d.getTimePeriod(2), new Year(1999));
  100. }
  101. /**
  102. * Some checks for the equals() method.
  103. */
  104. public void testEquals() {
  105. TimeTableXYDataset d1 = new TimeTableXYDataset();
  106. TimeTableXYDataset d2 = new TimeTableXYDataset();
  107. assertTrue(d1.equals(d2));
  108. assertTrue(d2.equals(d1));
  109. d1.add(new Year(1999), 123.4, "S1");
  110. assertFalse(d1.equals(d2));
  111. d2.add(new Year(1999), 123.4, "S1");
  112. assertTrue(d1.equals(d2));
  113. d1.setDomainIsPointsInTime(!d1.getDomainIsPointsInTime());
  114. assertFalse(d1.equals(d2));
  115. d2.setDomainIsPointsInTime(!d2.getDomainIsPointsInTime());
  116. assertTrue(d1.equals(d2));
  117. d1 = new TimeTableXYDataset(TimeZone.getTimeZone("GMT"));
  118. d2 = new TimeTableXYDataset(TimeZone.getTimeZone("America/Los_Angeles"));
  119. assertFalse(d1.equals(d2));
  120. }
  121. /**
  122. * Some checks for cloning.
  123. */
  124. public void testClone() {
  125. TimeTableXYDataset d = new TimeTableXYDataset();
  126. d.add(new Year(1999), 25.0, "Series");
  127. TimeTableXYDataset clone = null;
  128. try {
  129. clone = (TimeTableXYDataset) d.clone();
  130. }
  131. catch (CloneNotSupportedException e) {
  132. assertTrue(false);
  133. }
  134. assertTrue(clone.equals(d));
  135. // now test that the clone is independent of the original
  136. clone.add(new Year(2004), 1.2, "SS");
  137. assertFalse(clone.equals(d));
  138. }
  139. /**
  140. * Serialize an instance, restore it, and check for equality.
  141. */
  142. public void testSerialization() {
  143. TimeTableXYDataset d1 = new TimeTableXYDataset();
  144. d1.add(new Year(1999), 123.4, "S1");
  145. TimeTableXYDataset d2 = null;
  146. try {
  147. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  148. ObjectOutput out = new ObjectOutputStream(buffer);
  149. out.writeObject(d1);
  150. out.close();
  151. ObjectInput in = new ObjectInputStream(
  152. new ByteArrayInputStream(buffer.toByteArray())
  153. );
  154. d2 = (TimeTableXYDataset) in.readObject();
  155. in.close();
  156. }
  157. catch (Exception e) {
  158. System.out.println(e.toString());
  159. }
  160. assertTrue(d1.equals(d2));
  161. }
  162. }