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. * TimeSeriesDataItemTests.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: TimeSeriesDataItemTests.java,v 1.2 2005/01/14 17:28:36 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 13-Mar-2003 : 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 junit.framework.Test;
  47. import junit.framework.TestCase;
  48. import junit.framework.TestSuite;
  49. import org.jfree.data.time.Day;
  50. import org.jfree.data.time.TimeSeriesDataItem;
  51. /**
  52. * Tests for the {@link TimeSeriesDataItem} class.
  53. */
  54. public class TimeSeriesDataItemTests extends TestCase {
  55. /**
  56. * Returns the tests as a test suite.
  57. *
  58. * @return The test suite.
  59. */
  60. public static Test suite() {
  61. return new TestSuite(TimeSeriesDataItemTests.class);
  62. }
  63. /**
  64. * Constructs a new set of tests.
  65. *
  66. * @param name the name of the tests.
  67. */
  68. public TimeSeriesDataItemTests(String name) {
  69. super(name);
  70. }
  71. /**
  72. * Common test setup.
  73. */
  74. protected void setUp() {
  75. // no setup
  76. }
  77. /**
  78. * Test that an instance is equal to itself.
  79. *
  80. * SourceForge Bug ID: 558850.
  81. */
  82. public void testEqualsSelf() {
  83. TimeSeriesDataItem item = new TimeSeriesDataItem(new Day(23, 9, 2001), 99.7);
  84. assertTrue(item.equals(item));
  85. }
  86. /**
  87. * Test the equals() method.
  88. */
  89. public void testEquals() {
  90. TimeSeriesDataItem item1 = new TimeSeriesDataItem(new Day(23, 9, 2001), 99.7);
  91. TimeSeriesDataItem item2 = new TimeSeriesDataItem(new Day(23, 9, 2001), 99.7);
  92. assertTrue(item1.equals(item2));
  93. assertTrue(item2.equals(item1));
  94. item1.setValue(new Integer(5));
  95. assertFalse(item1.equals(item2));
  96. item2.setValue(new Integer(5));
  97. assertTrue(item1.equals(item2));
  98. }
  99. /**
  100. * Serialize an instance, restore it, and check for equality.
  101. */
  102. public void testSerialization() {
  103. TimeSeriesDataItem item1 = new TimeSeriesDataItem(new Day(23, 9, 2001), 99.7);
  104. TimeSeriesDataItem item2 = null;
  105. try {
  106. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  107. ObjectOutput out = new ObjectOutputStream(buffer);
  108. out.writeObject(item1);
  109. out.close();
  110. ObjectInput in = new ObjectInputStream(
  111. new ByteArrayInputStream(buffer.toByteArray())
  112. );
  113. item2 = (TimeSeriesDataItem) in.readObject();
  114. in.close();
  115. }
  116. catch (Exception e) {
  117. System.out.println(e.toString());
  118. }
  119. assertEquals(item1, item2);
  120. }
  121. }