StandardDialScaleTest.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. * StandardDialScaleTest.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. * 24-Oct-2007 : Updated for API changes (DG);
  39. * 08-Jan-2012 : Added tests for valueToAngle() and angleToValue();
  40. *
  41. */
  42. package org.jfree.chart.plot.dial;
  43. import static org.junit.Assert.assertEquals;
  44. import static org.junit.Assert.assertFalse;
  45. import static org.junit.Assert.assertTrue;
  46. import java.awt.BasicStroke;
  47. import java.awt.Color;
  48. import java.awt.Font;
  49. import java.awt.GradientPaint;
  50. import org.jfree.chart.TestUtilities;
  51. import org.junit.Test;
  52. /**
  53. * Tests for the {@link StandardDialScale} class.
  54. */
  55. public class StandardDialScaleTest {
  56. /**
  57. * Confirm that the equals method can distinguish all the required fields.
  58. */
  59. @Test
  60. public void testEquals() {
  61. StandardDialScale s1 = new StandardDialScale();
  62. StandardDialScale s2 = new StandardDialScale();
  63. assertTrue(s1.equals(s2));
  64. // lowerBound
  65. s1 = new StandardDialScale(10.0, 100.0, 0.0, 270.0, 10.0, 4);
  66. assertFalse(s1.equals(s2));
  67. s2 = new StandardDialScale(10.0, 100.0, 0.0, 270.0, 10.0, 4);
  68. assertTrue(s1.equals(s2));
  69. // upperBound
  70. s1 = new StandardDialScale(10.0, 200.0, 0.0, 270.0, 10.0, 4);
  71. assertFalse(s1.equals(s2));
  72. s2 = new StandardDialScale(10.0, 200.0, 0.0, 270.0, 10.0, 4);
  73. assertTrue(s1.equals(s2));
  74. // startAngle
  75. s1 = new StandardDialScale(10.0, 200.0, 20.0, 270.0, 10.0, 4);
  76. assertFalse(s1.equals(s2));
  77. s2 = new StandardDialScale(10.0, 200.0, 20.0, 270.0, 10.0, 4);
  78. assertTrue(s1.equals(s2));
  79. // extent
  80. s1 = new StandardDialScale(10.0, 200.0, 20.0, 99.0, 10.0, 4);
  81. assertFalse(s1.equals(s2));
  82. s2 = new StandardDialScale(10.0, 200.0, 20.0, 99.0, 10.0, 4);
  83. assertTrue(s1.equals(s2));
  84. // tickRadius
  85. s1.setTickRadius(0.99);
  86. assertFalse(s1.equals(s2));
  87. s2.setTickRadius(0.99);
  88. assertTrue(s1.equals(s2));
  89. // majorTickIncrement
  90. s1.setMajorTickIncrement(11.1);
  91. assertFalse(s1.equals(s2));
  92. s2.setMajorTickIncrement(11.1);
  93. assertTrue(s1.equals(s2));
  94. // majorTickLength
  95. s1.setMajorTickLength(0.09);
  96. assertFalse(s1.equals(s2));
  97. s2.setMajorTickLength(0.09);
  98. assertTrue(s1.equals(s2));
  99. // majorTickPaint
  100. s1.setMajorTickPaint(new GradientPaint(1.0f, 2.0f, Color.red,
  101. 3.0f, 4.0f, Color.yellow));
  102. assertFalse(s1.equals(s2));
  103. s2.setMajorTickPaint(new GradientPaint(1.0f, 2.0f, Color.red,
  104. 3.0f, 4.0f, Color.yellow));
  105. assertTrue(s1.equals(s2));
  106. // majorTickStroke
  107. s1.setMajorTickStroke(new BasicStroke(1.1f));
  108. assertFalse(s1.equals(s2));
  109. s2.setMajorTickStroke(new BasicStroke(1.1f));
  110. assertTrue(s1.equals(s2));
  111. // minorTickCount
  112. s1.setMinorTickCount(7);
  113. assertFalse(s1.equals(s2));
  114. s2.setMinorTickCount(7);
  115. assertTrue(s1.equals(s2));
  116. // minorTickLength
  117. s1.setMinorTickLength(0.09);
  118. assertFalse(s1.equals(s2));
  119. s2.setMinorTickLength(0.09);
  120. assertTrue(s1.equals(s2));
  121. // tickLabelOffset
  122. s1.setTickLabelOffset(0.11);
  123. assertFalse(s1.equals(s2));
  124. s2.setTickLabelOffset(0.11);
  125. assertTrue(s1.equals(s2));
  126. // tickLabelFont
  127. s1.setTickLabelFont(new Font("Dialog", Font.PLAIN, 15));
  128. assertFalse(s1.equals(s2));
  129. s2.setTickLabelFont(new Font("Dialog", Font.PLAIN, 15));
  130. assertTrue(s1.equals(s2));
  131. // tickLabelPaint
  132. s1.setTickLabelPaint(new GradientPaint(1.0f, 2.0f, Color.white,
  133. 3.0f, 4.0f, Color.green));
  134. assertFalse(s1.equals(s2));
  135. s2.setTickLabelPaint(new GradientPaint(1.0f, 2.0f, Color.white,
  136. 3.0f, 4.0f, Color.green));
  137. assertTrue(s1.equals(s2));
  138. s1.setTickLabelsVisible(false);
  139. assertFalse(s1.equals(s2));
  140. s2.setTickLabelsVisible(false);
  141. assertTrue(s1.equals(s2));
  142. // check an inherited attribute
  143. s1.setVisible(false);
  144. assertFalse(s1.equals(s2));
  145. s2.setVisible(false);
  146. assertTrue(s1.equals(s2));
  147. }
  148. /**
  149. * Two objects that are equal are required to return the same hashCode.
  150. */
  151. @Test
  152. public void testHashCode() {
  153. StandardDialScale s1 = new StandardDialScale();
  154. StandardDialScale s2 = new StandardDialScale();
  155. assertTrue(s1.equals(s2));
  156. int h1 = s1.hashCode();
  157. int h2 = s2.hashCode();
  158. assertEquals(h1, h2);
  159. }
  160. /**
  161. * Confirm that cloning works.
  162. */
  163. @Test
  164. public void testCloning() throws CloneNotSupportedException {
  165. // try a default instance
  166. StandardDialScale s1 = new StandardDialScale();
  167. StandardDialScale s2 = (StandardDialScale) s1.clone();
  168. assertTrue(s1 != s2);
  169. assertTrue(s1.getClass() == s2.getClass());
  170. assertTrue(s1.equals(s2));
  171. // try a customised instance
  172. s1 = new StandardDialScale();
  173. s1.setExtent(123.4);
  174. s1.setMajorTickPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
  175. 4.0f, Color.white));
  176. s1.setMajorTickStroke(new BasicStroke(2.0f));
  177. s2 = (StandardDialScale) s1.clone();
  178. assertTrue(s1 != s2);
  179. assertTrue(s1.getClass() == s2.getClass());
  180. assertTrue(s1.equals(s2));
  181. // check that the listener lists are independent
  182. MyDialLayerChangeListener l1 = new MyDialLayerChangeListener();
  183. s1.addChangeListener(l1);
  184. assertTrue(s1.hasListener(l1));
  185. assertFalse(s2.hasListener(l1));
  186. }
  187. /**
  188. * Serialize an instance, restore it, and check for equality.
  189. */
  190. @Test
  191. public void testSerialization() {
  192. // try a default instance
  193. StandardDialScale s1 = new StandardDialScale();
  194. StandardDialScale s2 = (StandardDialScale) TestUtilities.serialised(s1);
  195. assertEquals(s1, s2);
  196. // try a customised instance
  197. s1 = new StandardDialScale();
  198. s1.setExtent(123.4);
  199. s1.setMajorTickPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
  200. 4.0f, Color.white));
  201. s1.setMajorTickStroke(new BasicStroke(2.0f));
  202. s2 = (StandardDialScale) TestUtilities.serialised(s1);
  203. assertEquals(s1, s2);
  204. }
  205. private static final double EPSILON = 0.0000000001;
  206. /**
  207. * Some checks for the valueToAngle() method.
  208. */
  209. @Test
  210. public void testValueToAngle() {
  211. StandardDialScale s = new StandardDialScale();
  212. assertEquals(175.0, s.valueToAngle(0.0), EPSILON);
  213. assertEquals(90.0, s.valueToAngle(50.0), EPSILON);
  214. assertEquals(5.0, s.valueToAngle(100.0), EPSILON);
  215. assertEquals(192.0, s.valueToAngle(-10.0), EPSILON);
  216. assertEquals(-12.0, s.valueToAngle(110.0), EPSILON);
  217. s = new StandardDialScale(0, 20, 180, -180.0, 10, 3);
  218. assertEquals(180.0, s.valueToAngle(0.0), EPSILON);
  219. assertEquals(90.0, s.valueToAngle(10.0), EPSILON);
  220. assertEquals(0.0, s.valueToAngle(20.0), EPSILON);
  221. }
  222. /**
  223. * Some checks for the angleToValue() method.
  224. */
  225. @Test
  226. public void testAngleToValue() {
  227. StandardDialScale s = new StandardDialScale();
  228. assertEquals(0.0, s.angleToValue(175.0), EPSILON);
  229. assertEquals(50.0, s.angleToValue(90.0), EPSILON);
  230. assertEquals(100.0, s.angleToValue(5.0), EPSILON);
  231. assertEquals(-10.0, s.angleToValue(192.0), EPSILON);
  232. assertEquals(110.0, s.angleToValue(-12.0), EPSILON);
  233. s = new StandardDialScale(0, 20, 180, -180.0, 10, 3);
  234. assertEquals(0.0, s.angleToValue(180.0), EPSILON);
  235. assertEquals(10.0, s.angleToValue(90.0), EPSILON);
  236. assertEquals(20.0, s.angleToValue(0.0), EPSILON);
  237. }
  238. }