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. * StandardPieSectionLabelGeneratorTests.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: StandardPieSectionLabelGeneratorTests.java,v 1.2 2005/02/09 14:23:47 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.StandardPieSectionLabelGenerator;
  56. /**
  57. * Tests for the {@link StandardPieSectionLabelGenerator} class.
  58. */
  59. public class StandardPieSectionLabelGeneratorTests 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(StandardPieSectionLabelGeneratorTests.class);
  67. }
  68. /**
  69. * Constructs a new set of tests.
  70. *
  71. * @param name the name of the tests.
  72. */
  73. public StandardPieSectionLabelGeneratorTests(String name) {
  74. super(name);
  75. }
  76. /**
  77. * Test that the equals() method distinguishes all fields.
  78. */
  79. public void testEquals() {
  80. StandardPieSectionLabelGenerator g1
  81. = new StandardPieSectionLabelGenerator();
  82. StandardPieSectionLabelGenerator g2
  83. = new StandardPieSectionLabelGenerator();
  84. assertTrue(g1.equals(g2));
  85. assertTrue(g2.equals(g1));
  86. g1 = new StandardPieSectionLabelGenerator(
  87. "{0}", new DecimalFormat("#,##0.00"),
  88. NumberFormat.getPercentInstance()
  89. );
  90. assertFalse(g1.equals(g2));
  91. g2 = new StandardPieSectionLabelGenerator(
  92. "{0}", new DecimalFormat("#,##0.00"),
  93. NumberFormat.getPercentInstance()
  94. );
  95. assertTrue(g1.equals(g2));
  96. g1 = new StandardPieSectionLabelGenerator(
  97. "{0} {1}", new DecimalFormat("#,##0.00"),
  98. NumberFormat.getPercentInstance()
  99. );
  100. assertFalse(g1.equals(g2));
  101. g2 = new StandardPieSectionLabelGenerator(
  102. "{0} {1}", new DecimalFormat("#,##0.00"),
  103. NumberFormat.getPercentInstance()
  104. );
  105. assertTrue(g1.equals(g2));
  106. g1 = new StandardPieSectionLabelGenerator(
  107. "{0} {1}", new DecimalFormat("#,##0"),
  108. NumberFormat.getPercentInstance()
  109. );
  110. assertFalse(g1.equals(g2));
  111. g2 = new StandardPieSectionLabelGenerator(
  112. "{0} {1}", new DecimalFormat("#,##0"),
  113. NumberFormat.getPercentInstance()
  114. );
  115. assertTrue(g1.equals(g2));
  116. g1 = new StandardPieSectionLabelGenerator(
  117. "{0} {1}", new DecimalFormat("#,##0"), new DecimalFormat("0.000%")
  118. );
  119. assertFalse(g1.equals(g2));
  120. g2 = new StandardPieSectionLabelGenerator(
  121. "{0} {1}", new DecimalFormat("#,##0"), new DecimalFormat("0.000%")
  122. );
  123. assertTrue(g1.equals(g2));
  124. }
  125. /**
  126. * Confirm that cloning works.
  127. */
  128. public void testCloning() {
  129. StandardPieSectionLabelGenerator g1
  130. = new StandardPieSectionLabelGenerator();
  131. StandardPieSectionLabelGenerator g2 = null;
  132. try {
  133. g2 = (StandardPieSectionLabelGenerator) g1.clone();
  134. }
  135. catch (CloneNotSupportedException e) {
  136. System.err.println("Failed to clone.");
  137. }
  138. assertTrue(g1 != g2);
  139. assertTrue(g1.getClass() == g2.getClass());
  140. assertTrue(g1.equals(g2));
  141. }
  142. /**
  143. * Serialize an instance, restore it, and check for equality.
  144. */
  145. public void testSerialization() {
  146. StandardPieSectionLabelGenerator g1
  147. = new StandardPieSectionLabelGenerator();
  148. StandardPieSectionLabelGenerator g2 = null;
  149. try {
  150. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  151. ObjectOutput out = new ObjectOutputStream(buffer);
  152. out.writeObject(g1);
  153. out.close();
  154. ObjectInput in = new ObjectInputStream(
  155. new ByteArrayInputStream(buffer.toByteArray())
  156. );
  157. g2 = (StandardPieSectionLabelGenerator) in.readObject();
  158. in.close();
  159. }
  160. catch (Exception e) {
  161. e.printStackTrace();
  162. }
  163. assertEquals(g1, g2);
  164. }
  165. /**
  166. * Runs the test suite using JUnit's text-based runner.
  167. *
  168. * @param args ignored.
  169. */
  170. public static void main(String[] args) {
  171. junit.textui.TestRunner.run(suite());
  172. }
  173. }