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
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this library; if not, write to the Free Software Foundation,
  21. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  24. * in the United States and other countries.]
  25. *
  26. * ---------------------
  27. * LegendTitleTests.java
  28. * ---------------------
  29. * (C) Copyright 2005, by Object Refinery Limited and Contributors.
  30. *
  31. * Original Author: David Gilbert (for Object Refinery Limited);
  32. * Contributor(s): -;
  33. *
  34. * $Id: LegendTitleTests.java,v 1.1 2005/02/25 13:21:30 mungady Exp $
  35. *
  36. * Changes
  37. * -------
  38. * 25-Feb-2005 : Version 1 (DG);
  39. *
  40. */
  41. package org.jfree.chart.title.junit;
  42. import java.awt.Color;
  43. import java.awt.GradientPaint;
  44. import java.io.ByteArrayInputStream;
  45. import java.io.ByteArrayOutputStream;
  46. import java.io.ObjectInput;
  47. import java.io.ObjectInputStream;
  48. import java.io.ObjectOutput;
  49. import java.io.ObjectOutputStream;
  50. import junit.framework.Test;
  51. import junit.framework.TestCase;
  52. import junit.framework.TestSuite;
  53. import org.jfree.chart.LegendItemSource;
  54. import org.jfree.chart.plot.XYPlot;
  55. import org.jfree.chart.title.LegendTitle;
  56. import org.jfree.ui.RectangleAnchor;
  57. import org.jfree.ui.RectangleEdge;
  58. /**
  59. * Some tests for the {@link LegendTitle} class.
  60. */
  61. public class LegendTitleTests extends TestCase {
  62. /**
  63. * Returns the tests as a test suite.
  64. *
  65. * @return The test suite.
  66. */
  67. public static Test suite() {
  68. return new TestSuite(LegendTitleTests.class);
  69. }
  70. /**
  71. * Constructs a new set of tests.
  72. *
  73. * @param name the name of the tests.
  74. */
  75. public LegendTitleTests(String name) {
  76. super(name);
  77. }
  78. /**
  79. * Check that the equals() method distinguishes all fields.
  80. */
  81. public void testEquals() {
  82. XYPlot plot1 = new XYPlot();
  83. XYPlot plot2 = new XYPlot();
  84. LegendTitle t1 = new LegendTitle(plot1);
  85. LegendTitle t2 = new LegendTitle(plot1);
  86. assertEquals(t1, t2);
  87. t1.setSources(new LegendItemSource[] {plot1, plot2});
  88. assertFalse(t1.equals(t2));
  89. t2.setSources(new LegendItemSource[] {plot1, plot2});
  90. assertTrue(t1.equals(t2));
  91. t1.setBackgroundPaint(
  92. new GradientPaint(1.0f, 2.0f, Color.red, 3.0f, 4.0f, Color.yellow)
  93. );
  94. assertFalse(t1.equals(t2));
  95. t2.setBackgroundPaint(
  96. new GradientPaint(1.0f, 2.0f, Color.red, 3.0f, 4.0f, Color.yellow)
  97. );
  98. assertTrue(t1.equals(t2));
  99. t1.setLegendItemGraphicEdge(RectangleEdge.BOTTOM);
  100. assertFalse(t1.equals(t2));
  101. t2.setLegendItemGraphicEdge(RectangleEdge.BOTTOM);
  102. assertTrue(t1.equals(t2));
  103. t1.setLegendItemGraphicAnchor(RectangleAnchor.BOTTOM_LEFT);
  104. assertFalse(t1.equals(t2));
  105. t2.setLegendItemGraphicAnchor(RectangleAnchor.BOTTOM_LEFT);
  106. assertTrue(t1.equals(t2));
  107. t1.setLegendItemGraphicLocation(RectangleAnchor.TOP_LEFT);
  108. assertFalse(t1.equals(t2));
  109. t2.setLegendItemGraphicLocation(RectangleAnchor.TOP_LEFT);
  110. assertTrue(t1.equals(t2));
  111. }
  112. /**
  113. * Two objects that are equal are required to return the same hashCode.
  114. */
  115. public void testHashcode() {
  116. XYPlot plot1 = new XYPlot();
  117. LegendTitle t1 = new LegendTitle(plot1);
  118. LegendTitle t2 = new LegendTitle(plot1);
  119. assertTrue(t1.equals(t2));
  120. int h1 = t1.hashCode();
  121. int h2 = t2.hashCode();
  122. assertEquals(h1, h2);
  123. }
  124. /**
  125. * Confirm that cloning works.
  126. */
  127. public void testCloning() {
  128. XYPlot plot = new XYPlot();
  129. LegendTitle t1 = new LegendTitle(plot);
  130. t1.setBackgroundPaint(
  131. new GradientPaint(1.0f, 2.0f, Color.red, 3.0f, 4.0f, Color.yellow)
  132. );
  133. LegendTitle t2 = null;
  134. try {
  135. t2 = (LegendTitle) t1.clone();
  136. }
  137. catch (CloneNotSupportedException e) {
  138. System.err.println("Failed to clone.");
  139. }
  140. assertTrue(t1 != t2);
  141. assertTrue(t1.getClass() == t2.getClass());
  142. assertTrue(t1.equals(t2));
  143. }
  144. /**
  145. * Serialize an instance, restore it, and check for equality.
  146. */
  147. public void testSerialization() {
  148. XYPlot plot = new XYPlot();
  149. LegendTitle t1 = new LegendTitle(plot);
  150. LegendTitle t2 = null;
  151. try {
  152. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  153. ObjectOutput out = new ObjectOutputStream(buffer);
  154. out.writeObject(t1);
  155. out.close();
  156. ObjectInput in = new ObjectInputStream(
  157. new ByteArrayInputStream(buffer.toByteArray())
  158. );
  159. t2 = (LegendTitle) in.readObject();
  160. in.close();
  161. }
  162. catch (Exception e) {
  163. System.out.println(e.toString());
  164. }
  165. assertEquals(t1, t2);
  166. }
  167. }