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