TextAnnotationTest.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. * TextAnnotationTest.java
  29. * -----------------------
  30. * (C) Copyright 2003-2013, by Object Refinery Limited and Contributors.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): Martin Hoeller;
  34. *
  35. * Changes
  36. * -------
  37. * 19-Aug-2003 : Version 1 (DG);
  38. * 07-Jan-2005 : Added testHashCode() method (DG);
  39. * 28-Oct-2011 : Added testSetRotationAnchor() method for bug #3428870 (MH);
  40. * 01-Jul-2013 : Added testChangeEvents() (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 static org.junit.Assert.fail;
  48. import static org.junit.Assert.assertNotNull;
  49. import java.awt.Color;
  50. import java.awt.Font;
  51. import java.awt.GradientPaint;
  52. import org.jfree.chart.event.AnnotationChangeEvent;
  53. import org.jfree.chart.event.AnnotationChangeListener;
  54. import org.jfree.ui.TextAnchor;
  55. import org.junit.Test;
  56. /**
  57. * Tests for the {@link TextAnnotation} class.
  58. */
  59. public class TextAnnotationTest implements AnnotationChangeListener {
  60. /**
  61. * Confirm that the equals method can distinguish all the required fields.
  62. */
  63. @Test
  64. public void testEquals() {
  65. TextAnnotation a1 = new CategoryTextAnnotation("Test", "Category", 1.0);
  66. TextAnnotation a2 = new CategoryTextAnnotation("Test", "Category", 1.0);
  67. assertTrue(a1.equals(a2));
  68. // text
  69. a1.setText("Text");
  70. assertFalse(a1.equals(a2));
  71. a2.setText("Text");
  72. assertTrue(a1.equals(a2));
  73. // font
  74. a1.setFont(new Font("Serif", Font.BOLD, 18));
  75. assertFalse(a1.equals(a2));
  76. a2.setFont(new Font("Serif", Font.BOLD, 18));
  77. assertTrue(a1.equals(a2));
  78. // paint
  79. a1.setPaint(new GradientPaint(1.0f, 2.0f, Color.red,
  80. 3.0f, 4.0f, Color.pink));
  81. assertFalse(a1.equals(a2));
  82. a2.setPaint(new GradientPaint(1.0f, 2.0f, Color.red,
  83. 3.0f, 4.0f, Color.pink));
  84. assertTrue(a1.equals(a2));
  85. // textAnchor
  86. a1.setTextAnchor(TextAnchor.BOTTOM_LEFT);
  87. assertFalse(a1.equals(a2));
  88. a2.setTextAnchor(TextAnchor.BOTTOM_LEFT);
  89. assertTrue(a1.equals(a2));
  90. // rotationAnchor
  91. a1.setRotationAnchor(TextAnchor.BOTTOM_LEFT);
  92. assertFalse(a1.equals(a2));
  93. a2.setRotationAnchor(TextAnchor.BOTTOM_LEFT);
  94. assertTrue(a1.equals(a2));
  95. // rotationAngle
  96. a1.setRotationAngle(Math.PI);
  97. assertFalse(a1.equals(a2));
  98. a2.setRotationAngle(Math.PI);
  99. assertTrue(a1.equals(a2));
  100. }
  101. /**
  102. * Two objects that are equal are required to return the same hashCode.
  103. */
  104. @Test
  105. public void testHashCode() {
  106. TextAnnotation a1 = new CategoryTextAnnotation("Test", "Category", 1.0);
  107. TextAnnotation a2 = new CategoryTextAnnotation("Test", "Category", 1.0);
  108. assertTrue(a1.equals(a2));
  109. int h1 = a1.hashCode();
  110. int h2 = a2.hashCode();
  111. assertEquals(h1, h2);
  112. }
  113. /**
  114. * Test null-argument (Bug #3428870).
  115. */
  116. @Test
  117. public void testSetRotationAnchor() {
  118. TextAnnotation a = new CategoryTextAnnotation("Test", "Category", 1.0);
  119. try {
  120. a.setRotationAnchor(null);
  121. fail("Should have thrown Exception.");
  122. } catch (IllegalArgumentException e) {
  123. // ok, exception is expected
  124. }
  125. }
  126. /**
  127. * Some tests to ensure that change events are generated as expected.
  128. */
  129. @Test
  130. public void testChangeEvents() {
  131. TextAnnotation a = new CategoryTextAnnotation("Test", "A", 1.0);
  132. a.addChangeListener(this);
  133. this.lastEvent = null;
  134. a.setText("B");
  135. assertNotNull(this.lastEvent);
  136. this.lastEvent = null;
  137. a.setText("B");
  138. assertNotNull(this.lastEvent);
  139. this.lastEvent = null;
  140. a.setFont(new Font("SansSerif", Font.PLAIN, 12));
  141. assertNotNull(this.lastEvent);
  142. this.lastEvent = null;
  143. a.setPaint(Color.BLUE);
  144. assertNotNull(this.lastEvent);
  145. this.lastEvent = null;
  146. a.setTextAnchor(TextAnchor.CENTER_LEFT);
  147. assertNotNull(this.lastEvent);
  148. this.lastEvent = null;
  149. a.setRotationAnchor(TextAnchor.CENTER_LEFT);
  150. assertNotNull(this.lastEvent);
  151. this.lastEvent = null;
  152. a.setRotationAngle(123.4);
  153. assertNotNull(this.lastEvent);
  154. }
  155. private AnnotationChangeEvent lastEvent;
  156. /**
  157. * Receives notification of a change to an annotation.
  158. *
  159. * @param event the event.
  160. */
  161. @Override
  162. public void annotationChanged(AnnotationChangeEvent event) {
  163. this.lastEvent = event;
  164. }
  165. }