DialPointerTest.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. * DialPointerTest.java
  29. * --------------------
  30. * (C) Copyright 2007-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. * 30-Apr-2007 : Version 1 (DG);
  38. * 23-Nov-2007 : Added testEqualsPointer() and testSerialization2() (DG);
  39. *
  40. */
  41. package org.jfree.chart.plot.dial;
  42. import static org.junit.Assert.assertEquals;
  43. import static org.junit.Assert.assertFalse;
  44. import static org.junit.Assert.assertTrue;
  45. import java.awt.BasicStroke;
  46. import java.awt.Color;
  47. import org.jfree.chart.TestUtilities;
  48. import org.junit.Test;
  49. /**
  50. * Tests for the {@link DialPointer} class.
  51. */
  52. public class DialPointerTest {
  53. /**
  54. * Confirm that the equals method can distinguish all the required fields.
  55. */
  56. @Test
  57. public void testEquals() {
  58. DialPointer i1 = new DialPointer.Pin(1);
  59. DialPointer i2 = new DialPointer.Pin(1);
  60. assertTrue(i1.equals(i2));
  61. // dataset index
  62. i1 = new DialPointer.Pin(2);
  63. assertFalse(i1.equals(i2));
  64. i2 = new DialPointer.Pin(2);
  65. assertTrue(i1.equals(i2));
  66. // check an inherited attribute
  67. i1.setVisible(false);
  68. assertFalse(i1.equals(i2));
  69. i2.setVisible(false);
  70. assertTrue(i1.equals(i2));
  71. }
  72. /**
  73. * Check the equals() method for the DialPointer.Pin class.
  74. */
  75. @Test
  76. public void testEqualsPin() {
  77. DialPointer.Pin p1 = new DialPointer.Pin();
  78. DialPointer.Pin p2 = new DialPointer.Pin();
  79. assertEquals(p1, p2);
  80. p1.setPaint(Color.green);
  81. assertFalse(p1.equals(p2));
  82. p2.setPaint(Color.green);
  83. assertTrue(p1.equals(p2));
  84. BasicStroke s = new BasicStroke(4.4f);
  85. p1.setStroke(s);
  86. assertFalse(p1.equals(p2));
  87. p2.setStroke(s);
  88. assertTrue(p1.equals(p2));
  89. }
  90. /**
  91. * Check the equals() method for the DialPointer.Pointer class.
  92. */
  93. @Test
  94. public void testEqualsPointer() {
  95. DialPointer.Pointer p1 = new DialPointer.Pointer();
  96. DialPointer.Pointer p2 = new DialPointer.Pointer();
  97. assertEquals(p1, p2);
  98. p1.setFillPaint(Color.green);
  99. assertFalse(p1.equals(p2));
  100. p2.setFillPaint(Color.green);
  101. assertTrue(p1.equals(p2));
  102. p1.setOutlinePaint(Color.green);
  103. assertFalse(p1.equals(p2));
  104. p2.setOutlinePaint(Color.green);
  105. assertTrue(p1.equals(p2));
  106. }
  107. /**
  108. * Two objects that are equal are required to return the same hashCode.
  109. */
  110. @Test
  111. public void testHashCode() {
  112. DialPointer i1 = new DialPointer.Pin(1);
  113. DialPointer i2 = new DialPointer.Pin(1);
  114. assertTrue(i1.equals(i2));
  115. int h1 = i1.hashCode();
  116. int h2 = i2.hashCode();
  117. assertEquals(h1, h2);
  118. }
  119. /**
  120. * Confirm that cloning works.
  121. */
  122. @Test
  123. public void testCloning() throws CloneNotSupportedException {
  124. DialPointer i1 = new DialPointer.Pin(1);
  125. DialPointer i2 = (DialPointer) i1.clone();
  126. assertTrue(i1 != i2);
  127. assertTrue(i1.getClass() == i2.getClass());
  128. assertTrue(i1.equals(i2));
  129. // check that the listener lists are independent
  130. MyDialLayerChangeListener l1 = new MyDialLayerChangeListener();
  131. i1.addChangeListener(l1);
  132. assertTrue(i1.hasListener(l1));
  133. assertFalse(i2.hasListener(l1));
  134. }
  135. /**
  136. * Serialize an instance, restore it, and check for equality.
  137. */
  138. @Test
  139. public void testSerialization() {
  140. // test a default instance
  141. DialPointer i1 = new DialPointer.Pin(1);
  142. DialPointer i2 = (DialPointer) TestUtilities.serialised(i1);
  143. assertEquals(i1, i2);
  144. }
  145. /**
  146. * Serialize an instance, restore it, and check for equality.
  147. */
  148. @Test
  149. public void testSerialization2() {
  150. DialPointer i1 = new DialPointer.Pointer(1);
  151. DialPointer i2 = (DialPointer) TestUtilities.serialised(i1);
  152. assertEquals(i1, i2);
  153. }
  154. }