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