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. * StandardXYToolTipGeneratorTests.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: StandardXYToolTipGeneratorTests.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.NumberFormat;
  51. import java.text.SimpleDateFormat;
  52. import junit.framework.Test;
  53. import junit.framework.TestCase;
  54. import junit.framework.TestSuite;
  55. import org.jfree.chart.labels.StandardXYToolTipGenerator;
  56. /**
  57. * Tests for the {@link StandardXYToolTipGenerator} class.
  58. */
  59. public class StandardXYToolTipGeneratorTests extends TestCase {
  60. /**
  61. * Returns the tests as a test suite.
  62. *
  63. * @return The test suite.
  64. */
  65. public static Test suite() {
  66. return new TestSuite(StandardXYToolTipGeneratorTests.class);
  67. }
  68. /**
  69. * Constructs a new set of tests.
  70. *
  71. * @param name the name of the tests.
  72. */
  73. public StandardXYToolTipGeneratorTests(String name) {
  74. super(name);
  75. }
  76. /**
  77. * Tests the equals() method.
  78. */
  79. public void testEquals() {
  80. // some setup...
  81. String f1 = "{1}";
  82. String f2 = "{2}";
  83. NumberFormat xnf1 = new DecimalFormat("0.00");
  84. NumberFormat xnf2 = new DecimalFormat("0.000");
  85. NumberFormat ynf1 = new DecimalFormat("0.00");
  86. NumberFormat ynf2 = new DecimalFormat("0.000");
  87. StandardXYToolTipGenerator g1 = null;
  88. StandardXYToolTipGenerator g2 = null;
  89. g1 = new StandardXYToolTipGenerator(f1, xnf1, ynf1);
  90. g2 = new StandardXYToolTipGenerator(f1, xnf1, ynf1);
  91. assertTrue(g1.equals(g2));
  92. assertTrue(g2.equals(g1));
  93. g1 = new StandardXYToolTipGenerator(f2, xnf1, ynf1);
  94. assertFalse(g1.equals(g2));
  95. g2 = new StandardXYToolTipGenerator(f2, xnf1, ynf1);
  96. assertTrue(g1.equals(g2));
  97. g1 = new StandardXYToolTipGenerator(f2, xnf2, ynf1);
  98. assertFalse(g1.equals(g2));
  99. g2 = new StandardXYToolTipGenerator(f2, xnf2, ynf1);
  100. assertTrue(g1.equals(g2));
  101. g1 = new StandardXYToolTipGenerator(f2, xnf2, ynf2);
  102. assertFalse(g1.equals(g2));
  103. g2 = new StandardXYToolTipGenerator(f2, xnf2, ynf2);
  104. assertTrue(g1.equals(g2));
  105. DateFormat xdf1 = new SimpleDateFormat("d-MMM");
  106. DateFormat xdf2 = new SimpleDateFormat("d-MMM-yyyy");
  107. DateFormat ydf1 = new SimpleDateFormat("d-MMM");
  108. DateFormat ydf2 = new SimpleDateFormat("d-MMM-yyyy");
  109. g1 = new StandardXYToolTipGenerator(f1, xdf1, ydf1);
  110. g2 = new StandardXYToolTipGenerator(f1, xdf1, ydf1);
  111. assertTrue(g1.equals(g2));
  112. assertTrue(g2.equals(g1));
  113. g1 = new StandardXYToolTipGenerator(f1, xdf2, ydf1);
  114. assertFalse(g1.equals(g2));
  115. g2 = new StandardXYToolTipGenerator(f1, xdf2, ydf1);
  116. assertTrue(g1.equals(g2));
  117. g1 = new StandardXYToolTipGenerator(f1, xdf2, ydf2);
  118. assertFalse(g1.equals(g2));
  119. g2 = new StandardXYToolTipGenerator(f1, xdf2, ydf2);
  120. assertTrue(g1.equals(g2));
  121. }
  122. /**
  123. * Confirm that cloning works.
  124. */
  125. public void testCloning() {
  126. StandardXYToolTipGenerator g1 = new StandardXYToolTipGenerator();
  127. StandardXYToolTipGenerator g2 = null;
  128. try {
  129. g2 = (StandardXYToolTipGenerator) g1.clone();
  130. }
  131. catch (CloneNotSupportedException e) {
  132. System.err.println("Failed to clone.");
  133. }
  134. assertTrue(g1 != g2);
  135. assertTrue(g1.getClass() == g2.getClass());
  136. assertTrue(g1.equals(g2));
  137. }
  138. /**
  139. * Serialize an instance, restore it, and check for equality.
  140. */
  141. public void testSerialization() {
  142. StandardXYToolTipGenerator g1 = new StandardXYToolTipGenerator();
  143. StandardXYToolTipGenerator g2 = null;
  144. try {
  145. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  146. ObjectOutput out = new ObjectOutputStream(buffer);
  147. out.writeObject(g1);
  148. out.close();
  149. ObjectInput in = new ObjectInputStream(
  150. new ByteArrayInputStream(buffer.toByteArray())
  151. );
  152. g2 = (StandardXYToolTipGenerator) in.readObject();
  153. in.close();
  154. }
  155. catch (Exception e) {
  156. System.out.println(e.toString());
  157. }
  158. assertEquals(g1, g2);
  159. }
  160. }