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. * BoxAndWhiskerToolTipGeneratorTests.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: BoxAndWhiskerToolTipGeneratorTests.java,v 1.2 2005/02/09 14:23:48 mungady Exp $
  35. *
  36. * Changes
  37. * -------
  38. * 02-Jun-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.DecimalFormat;
  49. import junit.framework.Test;
  50. import junit.framework.TestCase;
  51. import junit.framework.TestSuite;
  52. import org.jfree.chart.labels.BoxAndWhiskerToolTipGenerator;
  53. /**
  54. * Tests for the {@link BoxAndWhiskerToolTipGenerator} class.
  55. */
  56. public class BoxAndWhiskerToolTipGeneratorTests extends TestCase {
  57. /**
  58. * Returns the tests as a test suite.
  59. *
  60. * @return The test suite.
  61. */
  62. public static Test suite() {
  63. return new TestSuite(BoxAndWhiskerToolTipGeneratorTests.class);
  64. }
  65. /**
  66. * Constructs a new set of tests.
  67. *
  68. * @param name the name of the tests.
  69. */
  70. public BoxAndWhiskerToolTipGeneratorTests(String name) {
  71. super(name);
  72. }
  73. /**
  74. * A series of tests for the equals() method.
  75. */
  76. public void testEquals() {
  77. // standard test
  78. BoxAndWhiskerToolTipGenerator g1 = new BoxAndWhiskerToolTipGenerator();
  79. BoxAndWhiskerToolTipGenerator g2 = new BoxAndWhiskerToolTipGenerator();
  80. assertTrue(g1.equals(g2));
  81. assertTrue(g2.equals(g1));
  82. // tooltip format
  83. g1 = new BoxAndWhiskerToolTipGenerator(
  84. "{0} --> {1} {2}", new DecimalFormat("0.0")
  85. );
  86. g2 = new BoxAndWhiskerToolTipGenerator(
  87. "{1} {2}", new DecimalFormat("0.0")
  88. );
  89. assertFalse(g1.equals(g2));
  90. g2 = new BoxAndWhiskerToolTipGenerator(
  91. "{0} --> {1} {2}", new DecimalFormat("0.0")
  92. );
  93. assertTrue(g1.equals(g2));
  94. // Y format
  95. g1 = new BoxAndWhiskerToolTipGenerator(
  96. "{0} --> {1} {2}", new DecimalFormat("0.0")
  97. );
  98. g2 = new BoxAndWhiskerToolTipGenerator(
  99. "{0} --> {1} {2}", new DecimalFormat("0.00")
  100. );
  101. assertFalse(g1.equals(g2));
  102. g2 = new BoxAndWhiskerToolTipGenerator(
  103. "{0} --> {1} {2}", new DecimalFormat("0.0")
  104. );
  105. assertTrue(g1.equals(g2));
  106. }
  107. /**
  108. * Confirm that cloning works.
  109. */
  110. public void testCloning() {
  111. BoxAndWhiskerToolTipGenerator g1 = new BoxAndWhiskerToolTipGenerator();
  112. BoxAndWhiskerToolTipGenerator g2 = null;
  113. try {
  114. g2 = (BoxAndWhiskerToolTipGenerator) g1.clone();
  115. }
  116. catch (CloneNotSupportedException e) {
  117. System.err.println("Failed to clone.");
  118. }
  119. assertTrue(g1 != g2);
  120. assertTrue(g1.getClass() == g2.getClass());
  121. assertTrue(g1.equals(g2));
  122. }
  123. /**
  124. * Serialize an instance, restore it, and check for equality.
  125. */
  126. public void testSerialization() {
  127. BoxAndWhiskerToolTipGenerator g1 = new BoxAndWhiskerToolTipGenerator();
  128. BoxAndWhiskerToolTipGenerator g2 = null;
  129. try {
  130. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  131. ObjectOutput out = new ObjectOutputStream(buffer);
  132. out.writeObject(g1);
  133. out.close();
  134. ObjectInput in = new ObjectInputStream(
  135. new ByteArrayInputStream(buffer.toByteArray())
  136. );
  137. g2 = (BoxAndWhiskerToolTipGenerator) in.readObject();
  138. in.close();
  139. }
  140. catch (Exception e) {
  141. System.out.println(e.toString());
  142. }
  143. assertEquals(g1, g2);
  144. }
  145. }