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. * IntervalCategoryLabelGeneratorTests.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: IntervalCategoryLabelGeneratorTests.java,v 1.3 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, and renamed class (DG);
  40. *
  41. */
  42. package org.jfree.chart.labels.junit;
  43. import java.io.ByteArrayInputStream;
  44. import java.io.ByteArrayOutputStream;
  45. import java.io.ObjectInput;
  46. import java.io.ObjectInputStream;
  47. import java.io.ObjectOutput;
  48. import java.io.ObjectOutputStream;
  49. import java.text.DateFormat;
  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.IntervalCategoryLabelGenerator;
  56. /**
  57. * Tests for the {@link IntervalCategoryLabelGenerator} class.
  58. */
  59. public class IntervalCategoryLabelGeneratorTests 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(IntervalCategoryLabelGeneratorTests.class);
  67. }
  68. /**
  69. * Constructs a new set of tests.
  70. *
  71. * @param name the name of the tests.
  72. */
  73. public IntervalCategoryLabelGeneratorTests(String name) {
  74. super(name);
  75. }
  76. /**
  77. * Tests the equals() method.
  78. */
  79. public void testEquals() {
  80. IntervalCategoryLabelGenerator g1 = new IntervalCategoryLabelGenerator();
  81. IntervalCategoryLabelGenerator g2 = new IntervalCategoryLabelGenerator();
  82. assertTrue(g1.equals(g2));
  83. assertTrue(g2.equals(g1));
  84. g1 = new IntervalCategoryLabelGenerator(
  85. "{3} - {4}", new DecimalFormat("0.000")
  86. );
  87. assertFalse(g1.equals(g2));
  88. g2 = new IntervalCategoryLabelGenerator(
  89. "{3} - {4}", new DecimalFormat("0.000")
  90. );
  91. assertTrue(g1.equals(g2));
  92. g1 = new IntervalCategoryLabelGenerator(
  93. "{3} - {4}", new SimpleDateFormat("d-MMM")
  94. );
  95. assertFalse(g1.equals(g2));
  96. g2 = new IntervalCategoryLabelGenerator(
  97. "{3} - {4}", new SimpleDateFormat("d-MMM")
  98. );
  99. assertTrue(g1.equals(g2));
  100. }
  101. /**
  102. * Confirm that cloning works.
  103. */
  104. public void testCloning() {
  105. IntervalCategoryLabelGenerator g1
  106. = new IntervalCategoryLabelGenerator();
  107. IntervalCategoryLabelGenerator g2 = null;
  108. try {
  109. g2 = (IntervalCategoryLabelGenerator) g1.clone();
  110. }
  111. catch (CloneNotSupportedException e) {
  112. System.err.println("Failed to clone.");
  113. }
  114. assertTrue(g1 != g2);
  115. assertTrue(g1.getClass() == g2.getClass());
  116. assertTrue(g1.equals(g2));
  117. }
  118. /**
  119. * Serialize an instance, restore it, and check for equality.
  120. */
  121. public void testSerialization() {
  122. IntervalCategoryLabelGenerator g1 = new IntervalCategoryLabelGenerator(
  123. "{3} - {4}", DateFormat.getInstance()
  124. );
  125. IntervalCategoryLabelGenerator g2 = null;
  126. try {
  127. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  128. ObjectOutput out = new ObjectOutputStream(buffer);
  129. out.writeObject(g1);
  130. out.close();
  131. ObjectInput in = new ObjectInputStream(
  132. new ByteArrayInputStream(buffer.toByteArray())
  133. );
  134. g2 = (IntervalCategoryLabelGenerator) in.readObject();
  135. in.close();
  136. }
  137. catch (Exception e) {
  138. System.out.println(e.toString());
  139. }
  140. assertEquals(g1, g2);
  141. }
  142. }