DialTextAnnotationTest.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. * DialTextAnnotationTest.java
  29. * ---------------------------
  30. * (C) Copyright 2006-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. * 03-Nov-2006 : Version 1 (DG);
  38. *
  39. */
  40. package org.jfree.chart.plot.dial;
  41. import static org.junit.Assert.assertEquals;
  42. import static org.junit.Assert.assertFalse;
  43. import static org.junit.Assert.assertTrue;
  44. import java.awt.Color;
  45. import java.awt.Font;
  46. import java.awt.GradientPaint;
  47. import org.jfree.chart.TestUtilities;
  48. import org.junit.Test;
  49. /**
  50. * Tests for the {@link DialTextAnnotation} class.
  51. */
  52. public class DialTextAnnotationTest {
  53. /**
  54. * Confirm that the equals method can distinguish all the required fields.
  55. */
  56. @Test
  57. public void testEquals() {
  58. DialTextAnnotation a1 = new DialTextAnnotation("A1");
  59. DialTextAnnotation a2 = new DialTextAnnotation("A1");
  60. assertTrue(a1.equals(a2));
  61. // angle
  62. a1.setAngle(1.1);
  63. assertFalse(a1.equals(a2));
  64. a2.setAngle(1.1);
  65. assertTrue(a1.equals(a2));
  66. // radius
  67. a1.setRadius(9.9);
  68. assertFalse(a1.equals(a2));
  69. a2.setRadius(9.9);
  70. assertTrue(a1.equals(a2));
  71. // font
  72. Font f = new Font("SansSerif", Font.PLAIN, 14);
  73. a1.setFont(f);
  74. assertFalse(a1.equals(a2));
  75. a2.setFont(f);
  76. assertTrue(a1.equals(a2));
  77. // paint
  78. a1.setPaint(Color.red);
  79. assertFalse(a1.equals(a2));
  80. a2.setPaint(Color.red);
  81. assertTrue(a1.equals(a2));
  82. // label
  83. a1.setLabel("ABC");
  84. assertFalse(a1.equals(a2));
  85. a2.setLabel("ABC");
  86. assertTrue(a1.equals(a2));
  87. // check an inherited attribute
  88. a1.setVisible(false);
  89. assertFalse(a1.equals(a2));
  90. a2.setVisible(false);
  91. assertTrue(a1.equals(a2));
  92. }
  93. /**
  94. * Two objects that are equal are required to return the same hashCode.
  95. */
  96. @Test
  97. public void testHashCode() {
  98. DialTextAnnotation a1 = new DialTextAnnotation("A1");
  99. DialTextAnnotation a2 = new DialTextAnnotation("A1");
  100. assertTrue(a1.equals(a2));
  101. int h1 = a1.hashCode();
  102. int h2 = a2.hashCode();
  103. assertEquals(h1, h2);
  104. }
  105. /**
  106. * Confirm that cloning works.
  107. */
  108. @Test
  109. public void testCloning() throws CloneNotSupportedException {
  110. // test a default instance
  111. DialTextAnnotation a1 = new DialTextAnnotation("A1");
  112. DialTextAnnotation a2 = (DialTextAnnotation) a1.clone();
  113. assertTrue(a1 != a2);
  114. assertTrue(a1.getClass() == a2.getClass());
  115. assertTrue(a1.equals(a2));
  116. // check that the listener lists are independent
  117. MyDialLayerChangeListener l1 = new MyDialLayerChangeListener();
  118. a1.addChangeListener(l1);
  119. assertTrue(a1.hasListener(l1));
  120. assertFalse(a2.hasListener(l1));
  121. }
  122. /**
  123. * Serialize an instance, restore it, and check for equality.
  124. */
  125. @Test
  126. public void testSerialization() {
  127. // test a default instance
  128. DialTextAnnotation a1 = new DialTextAnnotation("A1");
  129. DialTextAnnotation a2 = (DialTextAnnotation) TestUtilities.serialised(a1);
  130. assertEquals(a1, a2);
  131. // test a custom instance
  132. a1 = new DialTextAnnotation("A1");
  133. a1.setPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f, 4.0f,
  134. Color.blue));
  135. a2 = (DialTextAnnotation) TestUtilities.serialised(a1);
  136. assertEquals(a1, a2);
  137. }
  138. }