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. * StandardXYZToolTipGeneratorTests.java
  28. * -------------------------------------
  29. * (C) Copyright 2003, 2004, by Object Refinery Limited and Contributors.
  30. *
  31. * Original Author: David Gilbert (for Object Refinery Limited);
  32. * Contributor(s): -;
  33. *
  34. * $Id: StandardXYZToolTipGeneratorTests.java,v 1.2 2005/02/09 14:23:48 mungady Exp $
  35. *
  36. * Changes
  37. * -------
  38. * 23-Mar-2003 : 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.StandardXYZToolTipGenerator;
  56. /**
  57. * Tests for the {@link StandardXYZToolTipGenerator} class.
  58. */
  59. public class StandardXYZToolTipGeneratorTests 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(StandardXYZToolTipGeneratorTests.class);
  67. }
  68. /**
  69. * Constructs a new set of tests.
  70. *
  71. * @param name the name of the tests.
  72. */
  73. public StandardXYZToolTipGeneratorTests(String name) {
  74. super(name);
  75. }
  76. /**
  77. * Tests that the equals() method can distinguish all fields.
  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. NumberFormat znf1 = new DecimalFormat("0.00");
  88. NumberFormat znf2 = new DecimalFormat("0.000");
  89. DateFormat xdf1 = new SimpleDateFormat("d-MMM");
  90. DateFormat xdf2 = new SimpleDateFormat("d-MMM-yyyy");
  91. DateFormat ydf1 = new SimpleDateFormat("d-MMM");
  92. DateFormat ydf2 = new SimpleDateFormat("d-MMM-yyyy");
  93. DateFormat zdf1 = new SimpleDateFormat("d-MMM");
  94. DateFormat zdf2 = new SimpleDateFormat("d-MMM-yyyy");
  95. StandardXYZToolTipGenerator g1 = null;
  96. StandardXYZToolTipGenerator g2 = null;
  97. g1 = new StandardXYZToolTipGenerator(f1, xnf1, ynf1, znf1);
  98. g2 = new StandardXYZToolTipGenerator(f1, xnf1, ynf1, znf1);
  99. assertTrue(g1.equals(g2));
  100. // format string...
  101. g1 = new StandardXYZToolTipGenerator(f2, xnf1, ynf1, znf1);
  102. assertFalse(g1.equals(g2));
  103. g2 = new StandardXYZToolTipGenerator(f2, xnf1, ynf1, znf1);
  104. assertTrue(g1.equals(g2));
  105. // x number format
  106. g1 = new StandardXYZToolTipGenerator(f2, xnf2, ynf1, znf1);
  107. assertFalse(g1.equals(g2));
  108. g2 = new StandardXYZToolTipGenerator(f2, xnf2, ynf1, znf1);
  109. assertTrue(g1.equals(g2));
  110. // y number format
  111. g1 = new StandardXYZToolTipGenerator(f2, xnf2, ynf2, znf1);
  112. assertFalse(g1.equals(g2));
  113. g2 = new StandardXYZToolTipGenerator(f2, xnf2, ynf2, znf1);
  114. assertTrue(g1.equals(g2));
  115. // z number format
  116. g1 = new StandardXYZToolTipGenerator(f2, xnf2, ynf2, znf2);
  117. assertFalse(g1.equals(g2));
  118. g2 = new StandardXYZToolTipGenerator(f2, xnf2, ynf2, znf2);
  119. assertTrue(g1.equals(g2));
  120. g1 = new StandardXYZToolTipGenerator(f2, xdf1, ydf1, zdf1);
  121. g2 = new StandardXYZToolTipGenerator(f2, xdf1, ydf1, zdf1);
  122. assertTrue(g1.equals(g2));
  123. // x date format
  124. g1 = new StandardXYZToolTipGenerator(f2, xdf2, ydf1, zdf1);
  125. assertFalse(g1.equals(g2));
  126. g2 = new StandardXYZToolTipGenerator(f2, xdf2, ydf1, zdf1);
  127. assertTrue(g1.equals(g2));
  128. // y date format
  129. g1 = new StandardXYZToolTipGenerator(f2, xdf2, ydf2, zdf1);
  130. assertFalse(g1.equals(g2));
  131. g2 = new StandardXYZToolTipGenerator(f2, xdf2, ydf2, zdf1);
  132. assertTrue(g1.equals(g2));
  133. // z date format
  134. g1 = new StandardXYZToolTipGenerator(f2, xdf2, ydf2, zdf2);
  135. assertFalse(g1.equals(g2));
  136. g2 = new StandardXYZToolTipGenerator(f2, xdf2, ydf2, zdf2);
  137. assertTrue(g1.equals(g2));
  138. }
  139. /**
  140. * Confirm that cloning works.
  141. */
  142. public void testCloning() {
  143. StandardXYZToolTipGenerator g1 = new StandardXYZToolTipGenerator();
  144. StandardXYZToolTipGenerator g2 = null;
  145. try {
  146. g2 = (StandardXYZToolTipGenerator) g1.clone();
  147. }
  148. catch (CloneNotSupportedException e) {
  149. System.err.println("Failed to clone.");
  150. }
  151. assertTrue(g1 != g2);
  152. assertTrue(g1.getClass() == g2.getClass());
  153. assertTrue(g1.equals(g2));
  154. }
  155. /**
  156. * Serialize an instance, restore it, and check for equality.
  157. */
  158. public void testSerialization() {
  159. StandardXYZToolTipGenerator g1 = new StandardXYZToolTipGenerator();
  160. StandardXYZToolTipGenerator g2 = null;
  161. try {
  162. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  163. ObjectOutput out = new ObjectOutputStream(buffer);
  164. out.writeObject(g1);
  165. out.close();
  166. ObjectInput in = new ObjectInputStream(
  167. new ByteArrayInputStream(buffer.toByteArray())
  168. );
  169. g2 = (StandardXYZToolTipGenerator) in.readObject();
  170. in.close();
  171. }
  172. catch (Exception e) {
  173. System.out.println(e.toString());
  174. }
  175. assertEquals(g1, g2);
  176. }
  177. }