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