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. * BoxAndWhiskerXYToolTipGeneratorTests.java
  28. * -----------------------------------------
  29. * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
  30. *
  31. * Original Author: David Gilbert (for Object Refinery Limited);
  32. * Contributor(s): -;
  33. *
  34. * $Id: BoxAndWhiskerXYToolTipGeneratorTests.java,v 1.3 2005/02/09 14:23:48 mungady Exp $
  35. *
  36. * Changes
  37. * -------
  38. * 13-Aug-2003 : Version 1 (DG);
  39. * 27-Feb-2004 : Renamed BoxAndWhiskerItemLabelGenerator
  40. * --> XYBoxAndWhiskerItemLabelGenerator (DG);
  41. *
  42. */
  43. package org.jfree.chart.labels.junit;
  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 java.text.DecimalFormat;
  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.BoxAndWhiskerXYToolTipGenerator;
  56. /**
  57. * Tests for the {@link BoxAndWhiskerXYToolTipGenerator} class.
  58. */
  59. public class BoxAndWhiskerXYToolTipGeneratorTests 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(BoxAndWhiskerXYToolTipGeneratorTests.class);
  67. }
  68. /**
  69. * Constructs a new set of tests.
  70. *
  71. * @param name the name of the tests.
  72. */
  73. public BoxAndWhiskerXYToolTipGeneratorTests(String name) {
  74. super(name);
  75. }
  76. /**
  77. * A series of tests for the equals() method.
  78. */
  79. public void testEquals() {
  80. // standard test
  81. BoxAndWhiskerXYToolTipGenerator g1
  82. = new BoxAndWhiskerXYToolTipGenerator();
  83. BoxAndWhiskerXYToolTipGenerator g2
  84. = new BoxAndWhiskerXYToolTipGenerator();
  85. assertTrue(g1.equals(g2));
  86. assertTrue(g2.equals(g1));
  87. // tooltip format
  88. g1 = new BoxAndWhiskerXYToolTipGenerator(
  89. "{0} --> {1} {2}",
  90. new SimpleDateFormat("yyyy"), new DecimalFormat("0.0")
  91. );
  92. g2 = new BoxAndWhiskerXYToolTipGenerator(
  93. "{1} {2}", new SimpleDateFormat("yyyy"), new DecimalFormat("0.0")
  94. );
  95. assertFalse(g1.equals(g2));
  96. g2 = new BoxAndWhiskerXYToolTipGenerator(
  97. "{0} --> {1} {2}",
  98. new SimpleDateFormat("yyyy"), new DecimalFormat("0.0")
  99. );
  100. assertTrue(g1.equals(g2));
  101. // date format
  102. g1 = new BoxAndWhiskerXYToolTipGenerator(
  103. "{0} --> {1} {2}",
  104. new SimpleDateFormat("yyyy"), new DecimalFormat("0.0")
  105. );
  106. g2 = new BoxAndWhiskerXYToolTipGenerator(
  107. "{0} --> {1} {2}",
  108. new SimpleDateFormat("MMM-yyyy"), new DecimalFormat("0.0")
  109. );
  110. assertFalse(g1.equals(g2));
  111. g2 = new BoxAndWhiskerXYToolTipGenerator(
  112. "{0} --> {1} {2}",
  113. new SimpleDateFormat("yyyy"), new DecimalFormat("0.0")
  114. );
  115. assertTrue(g1.equals(g2));
  116. // Y format
  117. g1 = new BoxAndWhiskerXYToolTipGenerator(
  118. "{0} --> {1} {2}",
  119. new SimpleDateFormat("yyyy"), new DecimalFormat("0.0")
  120. );
  121. g2 = new BoxAndWhiskerXYToolTipGenerator(
  122. "{0} --> {1} {2}",
  123. new SimpleDateFormat("yyyy"), new DecimalFormat("0.00")
  124. );
  125. assertFalse(g1.equals(g2));
  126. g2 = new BoxAndWhiskerXYToolTipGenerator(
  127. "{0} --> {1} {2}",
  128. new SimpleDateFormat("yyyy"), new DecimalFormat("0.0")
  129. );
  130. assertTrue(g1.equals(g2));
  131. }
  132. /**
  133. * Confirm that cloning works.
  134. */
  135. public void testCloning() {
  136. BoxAndWhiskerXYToolTipGenerator g1
  137. = new BoxAndWhiskerXYToolTipGenerator();
  138. BoxAndWhiskerXYToolTipGenerator g2 = null;
  139. try {
  140. g2 = (BoxAndWhiskerXYToolTipGenerator) g1.clone();
  141. }
  142. catch (CloneNotSupportedException e) {
  143. System.err.println("Failed to clone.");
  144. }
  145. assertTrue(g1 != g2);
  146. assertTrue(g1.getClass() == g2.getClass());
  147. assertTrue(g1.equals(g2));
  148. }
  149. /**
  150. * Serialize an instance, restore it, and check for equality.
  151. */
  152. public void testSerialization() {
  153. BoxAndWhiskerXYToolTipGenerator g1
  154. = new BoxAndWhiskerXYToolTipGenerator();
  155. BoxAndWhiskerXYToolTipGenerator g2 = null;
  156. try {
  157. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  158. ObjectOutput out = new ObjectOutputStream(buffer);
  159. out.writeObject(g1);
  160. out.close();
  161. ObjectInput in = new ObjectInputStream(
  162. new ByteArrayInputStream(buffer.toByteArray())
  163. );
  164. g2 = (BoxAndWhiskerXYToolTipGenerator) in.readObject();
  165. in.close();
  166. }
  167. catch (Exception e) {
  168. System.out.println(e.toString());
  169. }
  170. assertEquals(g1, g2);
  171. }
  172. }