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. * XYBoxAndWhiskerRendererTests.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: XYBoxAndWhiskerRendererTests.java,v 1.2 2005/01/11 23:08:51 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 22-Oct-2003 : Version 1 (DG);
  37. * 23-Apr-2004 : Extended testEquals() method (DG);
  38. *
  39. */
  40. package org.jfree.chart.renderer.xy.junit;
  41. import java.awt.Color;
  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.XYBoxAndWhiskerRenderer;
  52. /**
  53. * Tests for the {@link XYBoxAndWhiskerRenderer} class.
  54. */
  55. public class XYBoxAndWhiskerRendererTests 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(XYBoxAndWhiskerRendererTests.class);
  63. }
  64. /**
  65. * Constructs a new set of tests.
  66. *
  67. * @param name the name of the tests.
  68. */
  69. public XYBoxAndWhiskerRendererTests(String name) {
  70. super(name);
  71. }
  72. /**
  73. * Check that the equals() method distinguishes all fields.
  74. */
  75. public void testEquals() {
  76. XYBoxAndWhiskerRenderer r1 = new XYBoxAndWhiskerRenderer();
  77. XYBoxAndWhiskerRenderer r2 = new XYBoxAndWhiskerRenderer();
  78. assertEquals(r1, r2);
  79. r1.setPaint(Color.yellow);
  80. assertFalse(r1.equals(r2));
  81. r2.setPaint(Color.yellow);
  82. assertEquals(r1, r2);
  83. r1.setArtifactPaint(Color.yellow);
  84. assertFalse(r1.equals(r2));
  85. r2.setArtifactPaint(Color.yellow);
  86. assertEquals(r1, r2);
  87. r1.setBoxWidth(0.55);
  88. assertFalse(r1.equals(r2));
  89. r2.setBoxWidth(0.55);
  90. assertEquals(r1, r2);
  91. r1.setFillBox(!r1.getFillBox());
  92. assertFalse(r1.equals(r2));
  93. r2.setFillBox(!r2.getFillBox());
  94. assertEquals(r1, r2);
  95. }
  96. /**
  97. * Two objects that are equal are required to return the same hashCode.
  98. */
  99. public void testHashcode() {
  100. XYBoxAndWhiskerRenderer r1 = new XYBoxAndWhiskerRenderer();
  101. XYBoxAndWhiskerRenderer r2 = new XYBoxAndWhiskerRenderer();
  102. assertTrue(r1.equals(r2));
  103. int h1 = r1.hashCode();
  104. int h2 = r2.hashCode();
  105. assertEquals(h1, h2);
  106. }
  107. /**
  108. * Confirm that cloning works.
  109. */
  110. public void testCloning() {
  111. XYBoxAndWhiskerRenderer r1 = new XYBoxAndWhiskerRenderer();
  112. XYBoxAndWhiskerRenderer r2 = null;
  113. try {
  114. r2 = (XYBoxAndWhiskerRenderer) r1.clone();
  115. }
  116. catch (CloneNotSupportedException e) {
  117. System.err.println("XYBoxAndWhiskerRendererTests.testCloning: failed to clone.");
  118. }
  119. assertTrue(r1 != r2);
  120. assertTrue(r1.getClass() == r2.getClass());
  121. assertTrue(r1.equals(r2));
  122. }
  123. /**
  124. * Serialize an instance, restore it, and check for equality.
  125. */
  126. public void testSerialization() {
  127. XYBoxAndWhiskerRenderer r1 = new XYBoxAndWhiskerRenderer();
  128. XYBoxAndWhiskerRenderer r2 = null;
  129. try {
  130. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  131. ObjectOutput out = new ObjectOutputStream(buffer);
  132. out.writeObject(r1);
  133. out.close();
  134. ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
  135. r2 = (XYBoxAndWhiskerRenderer) in.readObject();
  136. in.close();
  137. }
  138. catch (Exception e) {
  139. System.out.println(e.toString());
  140. }
  141. assertEquals(r1, r2);
  142. }
  143. }