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. * StandardPieItemLabelGeneratorTests.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: StandardPieItemLabelGeneratorTests.java,v 1.4 2005/02/09 14:23:48 mungady Exp $
  35. *
  36. * Changes
  37. * -------
  38. * 18-Mar-2003 : Version 1 (DG);
  39. * 13-Aug-2003 : Added clone tests (DG);
  40. * 04-Mar-2004 : Added test for equals() method (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.NumberFormat;
  52. import junit.framework.Test;
  53. import junit.framework.TestCase;
  54. import junit.framework.TestSuite;
  55. import org.jfree.chart.labels.StandardPieItemLabelGenerator;
  56. /**
  57. * Tests for the {@link StandardPieItemLabelGenerator} class.
  58. */
  59. public class StandardPieItemLabelGeneratorTests 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(StandardPieItemLabelGeneratorTests.class);
  67. }
  68. /**
  69. * Constructs a new set of tests.
  70. *
  71. * @param name the name of the tests.
  72. */
  73. public StandardPieItemLabelGeneratorTests(String name) {
  74. super(name);
  75. }
  76. /**
  77. * Test that the equals() method distinguishes all fields.
  78. */
  79. public void testEquals() {
  80. StandardPieItemLabelGenerator g1 = new StandardPieItemLabelGenerator();
  81. StandardPieItemLabelGenerator g2 = new StandardPieItemLabelGenerator();
  82. assertTrue(g1.equals(g2));
  83. assertTrue(g2.equals(g1));
  84. g1 = new StandardPieItemLabelGenerator(
  85. "{0}", new DecimalFormat("#,##0.00"),
  86. NumberFormat.getPercentInstance()
  87. );
  88. assertFalse(g1.equals(g2));
  89. g2 = new StandardPieItemLabelGenerator(
  90. "{0}", new DecimalFormat("#,##0.00"),
  91. NumberFormat.getPercentInstance()
  92. );
  93. assertTrue(g1.equals(g2));
  94. g1 = new StandardPieItemLabelGenerator(
  95. "{0} {1}", new DecimalFormat("#,##0.00"),
  96. NumberFormat.getPercentInstance()
  97. );
  98. assertFalse(g1.equals(g2));
  99. g2 = new StandardPieItemLabelGenerator(
  100. "{0} {1}", new DecimalFormat("#,##0.00"),
  101. NumberFormat.getPercentInstance()
  102. );
  103. assertTrue(g1.equals(g2));
  104. g1 = new StandardPieItemLabelGenerator(
  105. "{0} {1}", new DecimalFormat("#,##0"),
  106. NumberFormat.getPercentInstance()
  107. );
  108. assertFalse(g1.equals(g2));
  109. g2 = new StandardPieItemLabelGenerator(
  110. "{0} {1}", new DecimalFormat("#,##0"),
  111. NumberFormat.getPercentInstance()
  112. );
  113. assertTrue(g1.equals(g2));
  114. g1 = new StandardPieItemLabelGenerator(
  115. "{0} {1}", new DecimalFormat("#,##0"), new DecimalFormat("0.000%")
  116. );
  117. assertFalse(g1.equals(g2));
  118. g2 = new StandardPieItemLabelGenerator(
  119. "{0} {1}", new DecimalFormat("#,##0"), new DecimalFormat("0.000%")
  120. );
  121. assertTrue(g1.equals(g2));
  122. }
  123. /**
  124. * Confirm that cloning works.
  125. */
  126. public void testCloning() {
  127. StandardPieItemLabelGenerator g1 = new StandardPieItemLabelGenerator();
  128. StandardPieItemLabelGenerator g2 = null;
  129. try {
  130. g2 = (StandardPieItemLabelGenerator) g1.clone();
  131. }
  132. catch (CloneNotSupportedException e) {
  133. System.err.println("Failed to clone.");
  134. }
  135. assertTrue(g1 != g2);
  136. assertTrue(g1.getClass() == g2.getClass());
  137. assertTrue(g1.equals(g2));
  138. }
  139. /**
  140. * Serialize an instance, restore it, and check for equality.
  141. */
  142. public void testSerialization() {
  143. StandardPieItemLabelGenerator g1 = new StandardPieItemLabelGenerator();
  144. StandardPieItemLabelGenerator g2 = null;
  145. try {
  146. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  147. ObjectOutput out = new ObjectOutputStream(buffer);
  148. out.writeObject(g1);
  149. out.close();
  150. ObjectInput in = new ObjectInputStream(
  151. new ByteArrayInputStream(buffer.toByteArray())
  152. );
  153. g2 = (StandardPieItemLabelGenerator) in.readObject();
  154. in.close();
  155. }
  156. catch (Exception e) {
  157. e.printStackTrace();
  158. }
  159. assertEquals(g1, g2);
  160. }
  161. }