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. * StandardXYItemRendererTests.java
  26. * --------------------------------
  27. * (C) Copyright 2003, 2004, by Object Refinery Limited and Contributors.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: StandardXYItemRendererTests.java,v 1.2 2004/10/08 09:13:29 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 25-Mar-2003 : Version 1 (DG);
  37. * 22-Oct-2003 : Added hashCode test (DG);
  38. * 08-Oct-2004 : Strengthened test for equals() method (DG);
  39. *
  40. */
  41. package org.jfree.chart.renderer.xy.junit;
  42. import java.io.ByteArrayInputStream;
  43. import java.io.ByteArrayOutputStream;
  44. import java.io.ObjectInput;
  45. import java.io.ObjectInputStream;
  46. import java.io.ObjectOutput;
  47. import java.io.ObjectOutputStream;
  48. import junit.framework.Test;
  49. import junit.framework.TestCase;
  50. import junit.framework.TestSuite;
  51. import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
  52. import org.jfree.util.UnitType;
  53. /**
  54. * Tests for the {@link StandardXYItemRenderer} class.
  55. */
  56. public class StandardXYItemRendererTests extends TestCase {
  57. /**
  58. * Returns the tests as a test suite.
  59. *
  60. * @return The test suite.
  61. */
  62. public static Test suite() {
  63. return new TestSuite(StandardXYItemRendererTests.class);
  64. }
  65. /**
  66. * Constructs a new set of tests.
  67. *
  68. * @param name the name of the tests.
  69. */
  70. public StandardXYItemRendererTests(String name) {
  71. super(name);
  72. }
  73. /**
  74. * Test that the equals() method distinguishes all fields.
  75. */
  76. public void testEquals() {
  77. StandardXYItemRenderer r1 = new StandardXYItemRenderer();
  78. StandardXYItemRenderer r2 = new StandardXYItemRenderer();
  79. assertEquals(r1, r2);
  80. r1.setPlotShapes(true);
  81. assertFalse(r1.equals(r2));
  82. r2.setPlotShapes(true);
  83. assertTrue(r1.equals(r2));
  84. r1.setPlotLines(false);
  85. assertFalse(r1.equals(r2));
  86. r2.setPlotLines(false);
  87. assertTrue(r1.equals(r2));
  88. r1.setPlotImages(true);
  89. assertFalse(r1.equals(r2));
  90. r2.setPlotImages(true);
  91. assertTrue(r1.equals(r2));
  92. r1.setShapesFilled(false);
  93. assertFalse(r1.equals(r2));
  94. r2.setShapesFilled(false);
  95. assertTrue(r1.equals(r2));
  96. r1.setGapThresholdType(UnitType.ABSOLUTE);
  97. assertFalse(r1.equals(r2));
  98. r2.setGapThresholdType(UnitType.ABSOLUTE);
  99. assertTrue(r1.equals(r2));
  100. r1.setGapThreshold(1.23);
  101. assertFalse(r1.equals(r2));
  102. r2.setGapThreshold(1.23);
  103. assertTrue(r1.equals(r2));
  104. }
  105. /**
  106. * Two objects that are equal are required to return the same hashCode.
  107. */
  108. public void testHashcode() {
  109. StandardXYItemRenderer r1 = new StandardXYItemRenderer();
  110. StandardXYItemRenderer r2 = new StandardXYItemRenderer();
  111. assertTrue(r1.equals(r2));
  112. int h1 = r1.hashCode();
  113. int h2 = r2.hashCode();
  114. assertEquals(h1, h2);
  115. }
  116. /**
  117. * Confirm that cloning works.
  118. */
  119. public void testCloning() {
  120. StandardXYItemRenderer r1 = new StandardXYItemRenderer();
  121. StandardXYItemRenderer r2 = null;
  122. try {
  123. r2 = (StandardXYItemRenderer) r1.clone();
  124. }
  125. catch (CloneNotSupportedException e) {
  126. System.err.println("Failed to clone.");
  127. }
  128. assertTrue(r1 != r2);
  129. assertTrue(r1.getClass() == r2.getClass());
  130. assertTrue(r1.equals(r2));
  131. }
  132. /**
  133. * Serialize an instance, restore it, and check for equality.
  134. */
  135. public void testSerialization() {
  136. StandardXYItemRenderer r1 = new StandardXYItemRenderer();
  137. StandardXYItemRenderer r2 = null;
  138. try {
  139. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  140. ObjectOutput out = new ObjectOutputStream(buffer);
  141. out.writeObject(r1);
  142. out.close();
  143. ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
  144. r2 = (StandardXYItemRenderer) in.readObject();
  145. in.close();
  146. }
  147. catch (Exception e) {
  148. System.out.println(e.toString());
  149. }
  150. assertEquals(r1, r2);
  151. }
  152. }