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. * DefaultKeyedValueDatasetTests.java
  26. * ----------------------------------
  27. * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: DefaultKeyedValueDatasetTests.java,v 1.2 2005/01/11 17:50:15 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 18-Aug-2003 : Version 1 (DG);
  37. *
  38. */
  39. package org.jfree.data.general.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.general.DefaultKeyedValueDataset;
  50. /**
  51. * Tests for the {@link DefaultKeyedValueDataset} class.
  52. */
  53. public class DefaultKeyedValueDatasetTests extends TestCase {
  54. /**
  55. * Returns the tests as a test suite.
  56. *
  57. * @return The test suite.
  58. */
  59. public static Test suite() {
  60. return new TestSuite(DefaultKeyedValueDatasetTests.class);
  61. }
  62. /**
  63. * Constructs a new set of tests.
  64. *
  65. * @param name the name of the tests.
  66. */
  67. public DefaultKeyedValueDatasetTests(String name) {
  68. super(name);
  69. }
  70. /**
  71. * Confirm that the equals method can distinguish all the required fields.
  72. */
  73. public void testEquals() {
  74. DefaultKeyedValueDataset d1 = new DefaultKeyedValueDataset("Test", new Double(45.5));
  75. DefaultKeyedValueDataset d2 = new DefaultKeyedValueDataset("Test", new Double(45.5));
  76. assertTrue(d1.equals(d2));
  77. assertTrue(d2.equals(d1));
  78. d1 = new DefaultKeyedValueDataset("Test 1", new Double(45.5));
  79. d2 = new DefaultKeyedValueDataset("Test 2", new Double(45.5));
  80. assertFalse(d1.equals(d2));
  81. d1 = new DefaultKeyedValueDataset("Test", new Double(45.5));
  82. d2 = new DefaultKeyedValueDataset("Test", new Double(45.6));
  83. assertFalse(d1.equals(d2));
  84. }
  85. /**
  86. * Confirm that cloning works.
  87. */
  88. public void testCloning() {
  89. DefaultKeyedValueDataset d1 = new DefaultKeyedValueDataset("Test", new Double(45.5));
  90. DefaultKeyedValueDataset d2 = null;
  91. try {
  92. d2 = (DefaultKeyedValueDataset) d1.clone();
  93. }
  94. catch (CloneNotSupportedException e) {
  95. System.err.println("DefaultKeyedValueDatasetTests.testCloning: failed to clone.");
  96. }
  97. assertTrue(d1 != d2);
  98. assertTrue(d1.getClass() == d2.getClass());
  99. assertTrue(d1.equals(d2));
  100. }
  101. /**
  102. * Confirm that the clone is independent of the original.
  103. */
  104. public void testCloneIndependence() {
  105. DefaultKeyedValueDataset d1 = new DefaultKeyedValueDataset("Key", new Double(10.0));
  106. DefaultKeyedValueDataset d2 = null;
  107. try {
  108. d2 = (DefaultKeyedValueDataset) d1.clone();
  109. }
  110. catch (CloneNotSupportedException e) {
  111. System.err.println("Failed to clone.");
  112. }
  113. assertTrue(d1.equals(d2));
  114. d2.updateValue(new Double(99.9));
  115. assertFalse(d1.equals(d2));
  116. d2.updateValue(new Double(10.0));
  117. assertTrue(d1.equals(d2));
  118. }
  119. /**
  120. * Serialize an instance, restore it, and check for equality.
  121. */
  122. public void testSerialization() {
  123. DefaultKeyedValueDataset d1 = new DefaultKeyedValueDataset("Test", new Double(25.3));
  124. DefaultKeyedValueDataset d2 = null;
  125. try {
  126. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  127. ObjectOutput out = new ObjectOutputStream(buffer);
  128. out.writeObject(d1);
  129. out.close();
  130. ObjectInput in = new ObjectInputStream(
  131. new ByteArrayInputStream(buffer.toByteArray())
  132. );
  133. d2 = (DefaultKeyedValueDataset) in.readObject();
  134. in.close();
  135. }
  136. catch (Exception e) {
  137. System.out.println(e.toString());
  138. }
  139. assertEquals(d1, d2);
  140. }
  141. }