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. * SimpleHistogramDatasetTests.java
  26. * --------------------------------
  27. * (C) Copyright 2005 by Object Refinery Limited and Contributors.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: SimpleHistogramDatasetTests.java,v 1.1 2005/01/10 14:35:58 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 10-Jan-2005 : Version 1 (DG);
  37. *
  38. */
  39. package org.jfree.data.statistics.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.statistics.SimpleHistogramBin;
  50. import org.jfree.data.statistics.SimpleHistogramDataset;
  51. /**
  52. * Tests for the {@link SimpleHistogramDataset} class.
  53. */
  54. public class SimpleHistogramDatasetTests 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(SimpleHistogramDatasetTests.class);
  62. }
  63. /**
  64. * Constructs a new set of tests.
  65. *
  66. * @param name the name of the tests.
  67. */
  68. public SimpleHistogramDatasetTests(String name) {
  69. super(name);
  70. }
  71. /**
  72. * Ensure that the equals() method can distinguish all fields.
  73. */
  74. public void testEquals() {
  75. SimpleHistogramDataset d1 = new SimpleHistogramDataset("Dataset 1");
  76. SimpleHistogramDataset d2 = new SimpleHistogramDataset("Dataset 1");
  77. assertTrue(d1.equals(d2));
  78. d1.addBin(new SimpleHistogramBin(1.0, 2.0));
  79. assertFalse(d1.equals(d2));
  80. d2.addBin(new SimpleHistogramBin(1.0, 2.0));
  81. assertTrue(d1.equals(d2));
  82. }
  83. /**
  84. * Some checks for the clone() method.
  85. */
  86. public void testCloning() {
  87. SimpleHistogramDataset d1 = new SimpleHistogramDataset("Dataset 1");
  88. SimpleHistogramDataset d2 = null;
  89. try {
  90. d2 = (SimpleHistogramDataset) d1.clone();
  91. }
  92. catch (CloneNotSupportedException e) {
  93. System.err.println("Failed to clone.");
  94. }
  95. assertTrue(d1 != d2);
  96. assertTrue(d1.getClass() == d2.getClass());
  97. assertTrue(d1.equals(d2));
  98. // check that clone is independent of the original
  99. d2.addBin(new SimpleHistogramBin(2.0, 3.0));
  100. d2.addObservation(2.3);
  101. assertFalse(d1.equals(d2));
  102. }
  103. /**
  104. * Serialize an instance, restore it, and check for equality.
  105. */
  106. public void testSerialization() {
  107. SimpleHistogramDataset d1 = new SimpleHistogramDataset("D1");
  108. SimpleHistogramDataset d2 = null;
  109. try {
  110. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  111. ObjectOutput out = new ObjectOutputStream(buffer);
  112. out.writeObject(d1);
  113. out.close();
  114. ObjectInput in = new ObjectInputStream(
  115. new ByteArrayInputStream(buffer.toByteArray())
  116. );
  117. d2 = (SimpleHistogramDataset) in.readObject();
  118. in.close();
  119. }
  120. catch (Exception e) {
  121. System.out.println(e.toString());
  122. }
  123. assertEquals(d1, d2);
  124. }
  125. }