DialCapTest.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. * DialCapTest.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.BasicStroke;
  45. import java.awt.Color;
  46. import java.awt.GradientPaint;
  47. import org.jfree.chart.TestUtilities;
  48. import org.junit.Test;
  49. /**
  50. * Tests for the {@link DialCap} class.
  51. */
  52. public class DialCapTest {
  53. /**
  54. * Confirm that the equals method can distinguish all the required fields.
  55. */
  56. @Test
  57. public void testEquals() {
  58. DialCap c1 = new DialCap();
  59. DialCap c2 = new DialCap();
  60. assertTrue(c1.equals(c2));
  61. // radius
  62. c1.setRadius(0.5);
  63. assertFalse(c1.equals(c2));
  64. c2.setRadius(0.5);
  65. assertTrue(c1.equals(c2));
  66. // fill paint
  67. c1.setFillPaint(new GradientPaint(1.0f, 2.0f, Color.blue,
  68. 3.0f, 4.0f, Color.green));
  69. assertFalse(c1.equals(c2));
  70. c2.setFillPaint(new GradientPaint(1.0f, 2.0f, Color.blue,
  71. 3.0f, 4.0f, Color.green));
  72. // outline paint
  73. c1.setOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.white,
  74. 3.0f, 4.0f, Color.gray));
  75. assertFalse(c1.equals(c2));
  76. c2.setOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.white,
  77. 3.0f, 4.0f, Color.gray));
  78. assertTrue(c1.equals(c2));
  79. // outline stroke
  80. c1.setOutlineStroke(new BasicStroke(1.1f));
  81. assertFalse(c1.equals(c2));
  82. c2.setOutlineStroke(new BasicStroke(1.1f));
  83. assertTrue(c1.equals(c2));
  84. // check an inherited attribute
  85. c1.setVisible(false);
  86. assertFalse(c1.equals(c2));
  87. c2.setVisible(false);
  88. assertTrue(c1.equals(c2));
  89. }
  90. /**
  91. * Two objects that are equal are required to return the same hashCode.
  92. */
  93. @Test
  94. public void testHashCode() {
  95. DialCap c1 = new DialCap();
  96. DialCap c2 = new DialCap();
  97. assertTrue(c1.equals(c2));
  98. int h1 = c1.hashCode();
  99. int h2 = c2.hashCode();
  100. assertEquals(h1, h2);
  101. }
  102. /**
  103. * Confirm that cloning works.
  104. */
  105. @Test
  106. public void testCloning() throws CloneNotSupportedException {
  107. // test a default instance
  108. DialCap c1 = new DialCap();
  109. DialCap c2 = (DialCap) c1.clone();
  110. assertTrue(c1 != c2);
  111. assertTrue(c1.getClass() == c2.getClass());
  112. assertTrue(c1.equals(c2));
  113. // test a customised instance
  114. c1 = new DialCap();
  115. c1.setFillPaint(new GradientPaint(1.0f, 2.0f, Color.blue,
  116. 3.0f, 4.0f, Color.green));
  117. c1.setOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.white,
  118. 3.0f, 4.0f, Color.gray));
  119. c1.setOutlineStroke(new BasicStroke(2.0f));
  120. c2 = (DialCap) c1.clone();
  121. assertTrue(c1 != c2);
  122. assertTrue(c1.getClass() == c2.getClass());
  123. assertTrue(c1.equals(c2));
  124. // check that the listener lists are independent
  125. MyDialLayerChangeListener l1 = new MyDialLayerChangeListener();
  126. c1.addChangeListener(l1);
  127. assertTrue(c1.hasListener(l1));
  128. assertFalse(c2.hasListener(l1));
  129. }
  130. /**
  131. * Serialize an instance, restore it, and check for equality.
  132. */
  133. @Test
  134. public void testSerialization() {
  135. // test a default instance
  136. DialCap c1 = new DialCap();
  137. DialCap c2 = (DialCap) TestUtilities.serialised(c1);
  138. assertEquals(c1, c2);
  139. // test a custom instance
  140. c1 = new DialCap();
  141. c1.setFillPaint(new GradientPaint(1.0f, 2.0f, Color.blue,
  142. 3.0f, 4.0f, Color.green));
  143. c1.setOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.white,
  144. 3.0f, 4.0f, Color.gray));
  145. c1.setOutlineStroke(new BasicStroke(2.0f));
  146. c2 = (DialCap) TestUtilities.serialised(c1);
  147. assertEquals(c1, c2);
  148. }
  149. }