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 under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * ------------------------
  25. * MarkerAxisBandTests.java
  26. * ------------------------
  27. * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: MarkerAxisBandTests.java,v 1.2 2005/01/07 16:37:58 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 26-Mar-2003 : Version 1 (DG);
  37. * 07-Jan-2005 : Added tests for equals() and hashCode() (DG);
  38. *
  39. */
  40. package org.jfree.chart.axis.junit;
  41. import java.awt.Font;
  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 junit.framework.Test;
  49. import junit.framework.TestCase;
  50. import junit.framework.TestSuite;
  51. import org.jfree.chart.axis.MarkerAxisBand;
  52. /**
  53. * Tests for the {@link MarkerAxisBand} class.
  54. */
  55. public class MarkerAxisBandTests extends TestCase {
  56. /**
  57. * Returns the tests as a test suite.
  58. *
  59. * @return The test suite.
  60. */
  61. public static Test suite() {
  62. return new TestSuite(MarkerAxisBandTests.class);
  63. }
  64. /**
  65. * Constructs a new set of tests.
  66. *
  67. * @param name the name of the tests.
  68. */
  69. public MarkerAxisBandTests(String name) {
  70. super(name);
  71. }
  72. /**
  73. * Test that the equals() method can distinguish all fields.
  74. */
  75. public void testEquals() {
  76. Font font1 = new Font("SansSerif", Font.PLAIN, 12);
  77. Font font2 = new Font("SansSerif", Font.PLAIN, 14);
  78. MarkerAxisBand a1 = new MarkerAxisBand(null, 1.0, 1.0, 1.0, 1.0, font1);
  79. MarkerAxisBand a2 = new MarkerAxisBand(null, 1.0, 1.0, 1.0, 1.0, font1);
  80. assertEquals(a1, a2);
  81. a1 = new MarkerAxisBand(null, 2.0, 1.0, 1.0, 1.0, font1);
  82. assertFalse(a1.equals(a2));
  83. a2 = new MarkerAxisBand(null, 2.0, 1.0, 1.0, 1.0, font1);
  84. assertTrue(a1.equals(a2));
  85. a1 = new MarkerAxisBand(null, 2.0, 3.0, 1.0, 1.0, font1);
  86. assertFalse(a1.equals(a2));
  87. a2 = new MarkerAxisBand(null, 2.0, 3.0, 1.0, 1.0, font1);
  88. assertTrue(a1.equals(a2));
  89. a1 = new MarkerAxisBand(null, 2.0, 3.0, 4.0, 1.0, font1);
  90. assertFalse(a1.equals(a2));
  91. a2 = new MarkerAxisBand(null, 2.0, 3.0, 4.0, 1.0, font1);
  92. assertTrue(a1.equals(a2));
  93. a1 = new MarkerAxisBand(null, 2.0, 3.0, 4.0, 5.0, font1);
  94. assertFalse(a1.equals(a2));
  95. a2 = new MarkerAxisBand(null, 2.0, 3.0, 4.0, 5.0, font1);
  96. assertTrue(a1.equals(a2));
  97. a1 = new MarkerAxisBand(null, 2.0, 3.0, 4.0, 5.0, font2);
  98. assertFalse(a1.equals(a2));
  99. a2 = new MarkerAxisBand(null, 2.0, 3.0, 4.0, 5.0, font2);
  100. assertTrue(a1.equals(a2));
  101. }
  102. /**
  103. * Two objects that are equal are required to return the same hashCode.
  104. */
  105. public void testHashCode() {
  106. Font font1 = new Font("SansSerif", Font.PLAIN, 12);
  107. MarkerAxisBand a1 = new MarkerAxisBand(null, 1.0, 1.0, 1.0, 1.0, font1);
  108. MarkerAxisBand a2 = new MarkerAxisBand(null, 1.0, 1.0, 1.0, 1.0, font1);
  109. assertTrue(a1.equals(a2));
  110. int h1 = a1.hashCode();
  111. int h2 = a2.hashCode();
  112. assertEquals(h1, h2);
  113. }
  114. /**
  115. * Serialize an instance, restore it, and check for equality.
  116. */
  117. public void testSerialization() {
  118. MarkerAxisBand a1 = new MarkerAxisBand(null, 1.0, 1.0, 1.0, 1.0, null);
  119. MarkerAxisBand a2 = null;
  120. try {
  121. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  122. ObjectOutput out = new ObjectOutputStream(buffer);
  123. out.writeObject(a1);
  124. out.close();
  125. ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
  126. a2 = (MarkerAxisBand) in.readObject();
  127. in.close();
  128. }
  129. catch (Exception e) {
  130. System.out.println(e.toString());
  131. }
  132. assertEquals(a1, a2);
  133. }
  134. }