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. * StandardCategoryToolTipGeneratorTests.java
  28. * ------------------------------------------
  29. * (C) Copyright 2004, by Object Refinery Limited and Contributors.
  30. *
  31. * Original Author: David Gilbert (for Object Refinery Limited);
  32. * Contributor(s): -;
  33. *
  34. * $Id: StandardCategoryToolTipGeneratorTests.java,v 1.2 2005/02/09 14:23:48 mungady Exp $
  35. *
  36. * Changes
  37. * -------
  38. * 11-May-2004 : Version 1 (DG);
  39. *
  40. */
  41. package org.jfree.chart.labels.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 java.text.DateFormat;
  49. import java.text.DecimalFormat;
  50. import java.text.SimpleDateFormat;
  51. import junit.framework.Test;
  52. import junit.framework.TestCase;
  53. import junit.framework.TestSuite;
  54. import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
  55. /**
  56. * Tests for the {@link StandardCategoryToolTipGenerator} class.
  57. */
  58. public class StandardCategoryToolTipGeneratorTests extends TestCase {
  59. /**
  60. * Returns the tests as a test suite.
  61. *
  62. * @return The test suite.
  63. */
  64. public static Test suite() {
  65. return new TestSuite(StandardCategoryToolTipGeneratorTests.class);
  66. }
  67. /**
  68. * Constructs a new set of tests.
  69. *
  70. * @param name the name of the tests.
  71. */
  72. public StandardCategoryToolTipGeneratorTests(String name) {
  73. super(name);
  74. }
  75. /**
  76. * Tests the equals() method.
  77. */
  78. public void testEquals() {
  79. StandardCategoryToolTipGenerator g1
  80. = new StandardCategoryToolTipGenerator();
  81. StandardCategoryToolTipGenerator g2
  82. = new StandardCategoryToolTipGenerator();
  83. assertTrue(g1.equals(g2));
  84. assertTrue(g2.equals(g1));
  85. g1 = new StandardCategoryToolTipGenerator(
  86. "{0}", new DecimalFormat("0.000")
  87. );
  88. assertFalse(g1.equals(g2));
  89. g2 = new StandardCategoryToolTipGenerator(
  90. "{0}", new DecimalFormat("0.000")
  91. );
  92. assertTrue(g1.equals(g2));
  93. g1 = new StandardCategoryToolTipGenerator(
  94. "{1}", new DecimalFormat("0.000")
  95. );
  96. assertFalse(g1.equals(g2));
  97. g2 = new StandardCategoryToolTipGenerator(
  98. "{1}", new DecimalFormat("0.000")
  99. );
  100. assertTrue(g1.equals(g2));
  101. g1 = new StandardCategoryToolTipGenerator(
  102. "{2}", new SimpleDateFormat("d-MMM")
  103. );
  104. assertFalse(g1.equals(g2));
  105. g2 = new StandardCategoryToolTipGenerator(
  106. "{2}", new SimpleDateFormat("d-MMM")
  107. );
  108. assertTrue(g1.equals(g2));
  109. }
  110. /**
  111. * Confirm that cloning works.
  112. */
  113. public void testCloning() {
  114. StandardCategoryToolTipGenerator g1
  115. = new StandardCategoryToolTipGenerator();
  116. StandardCategoryToolTipGenerator g2 = null;
  117. try {
  118. g2 = (StandardCategoryToolTipGenerator) g1.clone();
  119. }
  120. catch (CloneNotSupportedException e) {
  121. System.err.println("Failed to clone.");
  122. }
  123. assertTrue(g1 != g2);
  124. assertTrue(g1.getClass() == g2.getClass());
  125. assertTrue(g1.equals(g2));
  126. }
  127. /**
  128. * Serialize an instance, restore it, and check for equality.
  129. */
  130. public void testSerialization() {
  131. StandardCategoryToolTipGenerator g1
  132. = new StandardCategoryToolTipGenerator(
  133. "{2}", DateFormat.getInstance()
  134. );
  135. StandardCategoryToolTipGenerator g2 = null;
  136. try {
  137. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  138. ObjectOutput out = new ObjectOutputStream(buffer);
  139. out.writeObject(g1);
  140. out.close();
  141. ObjectInput in = new ObjectInputStream(
  142. new ByteArrayInputStream(buffer.toByteArray())
  143. );
  144. g2 = (StandardCategoryToolTipGenerator) in.readObject();
  145. in.close();
  146. }
  147. catch (Exception e) {
  148. System.out.println(e.toString());
  149. }
  150. assertEquals(g1, g2);
  151. }
  152. }