XYImageAnnotationTest.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2013, 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
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  25. * Other names may be trademarks of their respective owners.]
  26. *
  27. * --------------------------
  28. * XYImageAnnotationTest.java
  29. * --------------------------
  30. * (C) Copyright 2004-2013, by Object Refinery Limited and Contributors.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes
  36. * -------
  37. * 17-May-2004 : Version 1 (DG);
  38. * 01-Dec-2006 : Updated testEquals() for new field (DG);
  39. * 09-Jan-2007 : Comment out failing test (DG);
  40. * 23-Apr-2008 : Added testPublicCloneable() (DG);
  41. *
  42. */
  43. package org.jfree.chart.annotations;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertTrue;
  46. import static org.junit.Assert.assertFalse;
  47. import java.awt.Image;
  48. import org.jfree.chart.JFreeChart;
  49. import org.jfree.ui.RectangleAnchor;
  50. import org.jfree.util.PublicCloneable;
  51. import org.junit.Test;
  52. /**
  53. * Tests for the {@link XYImageAnnotation} class.
  54. */
  55. public class XYImageAnnotationTest {
  56. /**
  57. * Confirm that the equals method can distinguish all the required fields.
  58. */
  59. @Test
  60. public void testEquals() {
  61. Image image = JFreeChart.INFO.getLogo();
  62. XYImageAnnotation a1 = new XYImageAnnotation(10.0, 20.0, image);
  63. XYImageAnnotation a2 = new XYImageAnnotation(10.0, 20.0, image);
  64. assertTrue(a1.equals(a2));
  65. a1 = new XYImageAnnotation(10.0, 20.0, image, RectangleAnchor.LEFT);
  66. assertFalse(a1.equals(a2));
  67. a2 = new XYImageAnnotation(10.0, 20.0, image, RectangleAnchor.LEFT);
  68. assertTrue(a1.equals(a2));
  69. }
  70. /**
  71. * Two objects that are equal are required to return the same hashCode.
  72. */
  73. @Test
  74. public void testHashCode() {
  75. Image image = JFreeChart.INFO.getLogo();
  76. XYImageAnnotation a1 = new XYImageAnnotation(10.0, 20.0, image);
  77. XYImageAnnotation a2 = new XYImageAnnotation(10.0, 20.0, image);
  78. assertTrue(a1.equals(a2));
  79. int h1 = a1.hashCode();
  80. int h2 = a2.hashCode();
  81. assertEquals(h1, h2);
  82. }
  83. /**
  84. * Confirm that cloning works.
  85. */
  86. @Test
  87. public void testCloning() throws CloneNotSupportedException {
  88. XYImageAnnotation a1 = new XYImageAnnotation(10.0, 20.0,
  89. JFreeChart.INFO.getLogo());
  90. XYImageAnnotation a2 = (XYImageAnnotation) a1.clone();
  91. assertTrue(a1 != a2);
  92. assertTrue(a1.getClass() == a2.getClass());
  93. assertTrue(a1.equals(a2));
  94. }
  95. /**
  96. * Checks that this class implements PublicCloneable.
  97. */
  98. @Test
  99. public void testPublicCloneable() {
  100. XYImageAnnotation a1 = new XYImageAnnotation(10.0, 20.0,
  101. JFreeChart.INFO.getLogo());
  102. assertTrue(a1 instanceof PublicCloneable);
  103. }
  104. // FIXME: Make this test pass
  105. // /**
  106. // * Serialize an instance, restore it, and check for equality.
  107. // */
  108. // public void testSerialization() {
  109. // XYImageAnnotation a1 = new XYImageAnnotation(10.0, 20.0,
  110. // JFreeChart.INFO.getLogo());
  111. // XYImageAnnotation a2 = null;
  112. // try {
  113. // ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  114. // ObjectOutput out = new ObjectOutputStream(buffer);
  115. // out.writeObject(a1);
  116. // out.close();
  117. //
  118. // ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(
  119. // buffer.toByteArray()));
  120. // a2 = (XYImageAnnotation) in.readObject();
  121. // in.close();
  122. // }
  123. // catch (Exception e) {
  124. // e.printStackTrace();
  125. // }
  126. // assertEquals(a1, a2);
  127. // }
  128. }