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. * TextTitleTests.java
  28. * -------------------
  29. * (C) Copyright 2004, 2005, by Object Refinery Limited and Contributors.
  30. *
  31. * Original Author: David Gilbert (for Object Refinery Limited);
  32. * Contributor(s): -;
  33. *
  34. * $Id: TextTitleTests.java,v 1.4 2005/02/04 17:05:45 mungady Exp $
  35. *
  36. * Changes
  37. * -------
  38. * 17-Feb-2004 : Version 1 (DG);
  39. *
  40. */
  41. package org.jfree.chart.title.junit;
  42. import java.awt.Color;
  43. import java.awt.Font;
  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.title.TextTitle;
  54. /**
  55. * Tests for the {@link TextTitle} class.
  56. */
  57. public class TextTitleTests extends TestCase {
  58. /**
  59. * Returns the tests as a test suite.
  60. *
  61. * @return The test suite.
  62. */
  63. public static Test suite() {
  64. return new TestSuite(TextTitleTests.class);
  65. }
  66. /**
  67. * Constructs a new set of tests.
  68. *
  69. * @param name the name of the tests.
  70. */
  71. public TextTitleTests(String name) {
  72. super(name);
  73. }
  74. /**
  75. * Check that the equals() method distinguishes all fields.
  76. */
  77. public void testEquals() {
  78. TextTitle t1 = new TextTitle();
  79. TextTitle t2 = new TextTitle();
  80. assertEquals(t1, t2);
  81. t1.setText("Test 1");
  82. assertFalse(t1.equals(t2));
  83. t2.setText("Test 1");
  84. assertTrue(t1.equals(t2));
  85. Font f = new Font("SansSerif", Font.PLAIN, 15);
  86. t1.setFont(f);
  87. assertFalse(t1.equals(t2));
  88. t2.setFont(f);
  89. assertTrue(t1.equals(t2));
  90. t1.setPaint(Color.blue);
  91. assertFalse(t1.equals(t2));
  92. t2.setPaint(Color.blue);
  93. assertTrue(t1.equals(t2));
  94. t1.setBackgroundPaint(Color.blue);
  95. assertFalse(t1.equals(t2));
  96. t2.setBackgroundPaint(Color.blue);
  97. assertTrue(t1.equals(t2));
  98. }
  99. /**
  100. * Two objects that are equal are required to return the same hashCode.
  101. */
  102. public void testHashcode() {
  103. TextTitle t1 = new TextTitle();
  104. TextTitle t2 = new TextTitle();
  105. assertTrue(t1.equals(t2));
  106. int h1 = t1.hashCode();
  107. int h2 = t2.hashCode();
  108. assertEquals(h1, h2);
  109. }
  110. /**
  111. * Confirm that cloning works.
  112. */
  113. public void testCloning() {
  114. TextTitle t1 = new TextTitle();
  115. TextTitle t2 = null;
  116. try {
  117. t2 = (TextTitle) t1.clone();
  118. }
  119. catch (CloneNotSupportedException e) {
  120. System.err.println("TextTitleTests.testCloning: failed to clone.");
  121. }
  122. assertTrue(t1 != t2);
  123. assertTrue(t1.getClass() == t2.getClass());
  124. assertTrue(t1.equals(t2));
  125. }
  126. /**
  127. * Serialize an instance, restore it, and check for equality.
  128. */
  129. public void testSerialization() {
  130. TextTitle t1 = new TextTitle("Test");
  131. TextTitle t2 = null;
  132. try {
  133. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  134. ObjectOutput out = new ObjectOutputStream(buffer);
  135. out.writeObject(t1);
  136. out.close();
  137. ObjectInput in = new ObjectInputStream(
  138. new ByteArrayInputStream(buffer.toByteArray())
  139. );
  140. t2 = (TextTitle) in.readObject();
  141. in.close();
  142. }
  143. catch (Exception e) {
  144. System.out.println(e.toString());
  145. }
  146. assertEquals(t1, t2);
  147. }
  148. }