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. * StandardXYLabelGeneratorTests.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: StandardXYLabelGeneratorTests.java,v 1.2 2005/02/09 14:23:48 mungady Exp $
  35. *
  36. * Changes
  37. * -------
  38. * 23-Mar-2003 : Version 1 (DG);
  39. * 26-Feb-2004 : Updates for new code (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.NumberFormat;
  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.StandardXYLabelGenerator;
  57. /**
  58. * Tests for the {@link StandardXYLabelGenerator} class.
  59. */
  60. public class StandardXYLabelGeneratorTests 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(StandardXYLabelGeneratorTests.class);
  68. }
  69. /**
  70. * Constructs a new set of tests.
  71. *
  72. * @param name the name of the tests.
  73. */
  74. public StandardXYLabelGeneratorTests(String name) {
  75. super(name);
  76. }
  77. /**
  78. * A series of tests for the equals() method.
  79. */
  80. public void testEquals() {
  81. // some setup...
  82. String f1 = "{1}";
  83. String f2 = "{2}";
  84. NumberFormat xnf1 = new DecimalFormat("0.00");
  85. NumberFormat xnf2 = new DecimalFormat("0.000");
  86. NumberFormat ynf1 = new DecimalFormat("0.00");
  87. NumberFormat ynf2 = new DecimalFormat("0.000");
  88. StandardXYLabelGenerator g1 = null;
  89. StandardXYLabelGenerator g2 = null;
  90. g1 = new StandardXYLabelGenerator(f1, xnf1, ynf1);
  91. g2 = new StandardXYLabelGenerator(f1, xnf1, ynf1);
  92. assertTrue(g1.equals(g2));
  93. assertTrue(g2.equals(g1));
  94. g1 = new StandardXYLabelGenerator(f2, xnf1, ynf1);
  95. assertFalse(g1.equals(g2));
  96. g2 = new StandardXYLabelGenerator(f2, xnf1, ynf1);
  97. assertTrue(g1.equals(g2));
  98. g1 = new StandardXYLabelGenerator(f2, xnf2, ynf1);
  99. assertFalse(g1.equals(g2));
  100. g2 = new StandardXYLabelGenerator(f2, xnf2, ynf1);
  101. assertTrue(g1.equals(g2));
  102. g1 = new StandardXYLabelGenerator(f2, xnf2, ynf2);
  103. assertFalse(g1.equals(g2));
  104. g2 = new StandardXYLabelGenerator(f2, xnf2, ynf2);
  105. assertTrue(g1.equals(g2));
  106. DateFormat xdf1 = new SimpleDateFormat("d-MMM");
  107. DateFormat xdf2 = new SimpleDateFormat("d-MMM-yyyy");
  108. DateFormat ydf1 = new SimpleDateFormat("d-MMM");
  109. DateFormat ydf2 = new SimpleDateFormat("d-MMM-yyyy");
  110. g1 = new StandardXYLabelGenerator(f1, xdf1, ydf1);
  111. g2 = new StandardXYLabelGenerator(f1, xdf1, ydf1);
  112. assertTrue(g1.equals(g2));
  113. assertTrue(g2.equals(g1));
  114. g1 = new StandardXYLabelGenerator(f1, xdf2, ydf1);
  115. assertFalse(g1.equals(g2));
  116. g2 = new StandardXYLabelGenerator(f1, xdf2, ydf1);
  117. assertTrue(g1.equals(g2));
  118. g1 = new StandardXYLabelGenerator(f1, xdf2, ydf2);
  119. assertFalse(g1.equals(g2));
  120. g2 = new StandardXYLabelGenerator(f1, xdf2, ydf2);
  121. assertTrue(g1.equals(g2));
  122. }
  123. /**
  124. * Confirm that cloning works.
  125. */
  126. public void testCloning() {
  127. StandardXYLabelGenerator g1 = new StandardXYLabelGenerator();
  128. StandardXYLabelGenerator g2 = null;
  129. try {
  130. g2 = (StandardXYLabelGenerator) g1.clone();
  131. }
  132. catch (CloneNotSupportedException e) {
  133. System.err.println("Clone failed.");
  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. StandardXYLabelGenerator g1 = new StandardXYLabelGenerator();
  144. StandardXYLabelGenerator 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 = (StandardXYLabelGenerator) in.readObject();
  154. in.close();
  155. }
  156. catch (Exception e) {
  157. System.out.println(e.toString());
  158. }
  159. assertEquals(g1, g2);
  160. }
  161. }