DialPlotTest.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. * DialPlotTest.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 static org.junit.Assert.assertNull;
  45. import static org.junit.Assert.assertNotNull;
  46. import java.awt.Color;
  47. import java.awt.GradientPaint;
  48. import org.jfree.chart.TestUtilities;
  49. import org.jfree.chart.event.PlotChangeEvent;
  50. import org.jfree.chart.event.PlotChangeListener;
  51. import org.junit.Test;
  52. /**
  53. * Tests for the {@link DialPlot} class.
  54. */
  55. public class DialPlotTest implements PlotChangeListener {
  56. /** The last plot change event received. */
  57. private PlotChangeEvent lastEvent;
  58. /**
  59. * Records the last plot change event received.
  60. *
  61. * @param event the event.
  62. */
  63. @Override
  64. public void plotChanged(PlotChangeEvent event) {
  65. this.lastEvent = event;
  66. }
  67. /**
  68. * Confirm that the equals method can distinguish all the required fields.
  69. */
  70. @Test
  71. public void testEquals() {
  72. DialPlot p1 = new DialPlot();
  73. DialPlot p2 = new DialPlot();
  74. assertTrue(p1.equals(p2));
  75. // background
  76. p1.setBackground(new DialBackground(Color.green));
  77. assertFalse(p1.equals(p2));
  78. p2.setBackground(new DialBackground(Color.green));
  79. assertTrue(p1.equals(p2));
  80. p1.setBackground(null);
  81. assertFalse(p1.equals(p2));
  82. p2.setBackground(null);
  83. assertTrue(p1.equals(p2));
  84. // dial cap
  85. DialCap cap1 = new DialCap();
  86. cap1.setFillPaint(Color.red);
  87. p1.setCap(cap1);
  88. assertFalse(p1.equals(p2));
  89. DialCap cap2 = new DialCap();
  90. cap2.setFillPaint(Color.red);
  91. p2.setCap(cap2);
  92. assertTrue(p1.equals(p2));
  93. p1.setCap(null);
  94. assertFalse(p1.equals(p2));
  95. p2.setCap(null);
  96. assertTrue(p1.equals(p2));
  97. // frame
  98. StandardDialFrame f1 = new StandardDialFrame();
  99. f1.setBackgroundPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
  100. 4.0f, Color.white));
  101. p1.setDialFrame(f1);
  102. assertFalse(p1.equals(p2));
  103. StandardDialFrame f2 = new StandardDialFrame();
  104. f2.setBackgroundPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
  105. 4.0f, Color.white));
  106. p2.setDialFrame(f2);
  107. assertTrue(p1.equals(p2));
  108. // view
  109. p1.setView(0.2, 0.0, 0.8, 1.0);
  110. assertFalse(p1.equals(p2));
  111. p2.setView(0.2, 0.0, 0.8, 1.0);
  112. assertTrue(p1.equals(p2));
  113. // layer
  114. p1.addLayer(new StandardDialScale());
  115. assertFalse(p1.equals(p2));
  116. p2.addLayer(new StandardDialScale());
  117. assertTrue(p1.equals(p2));
  118. }
  119. /**
  120. * Two objects that are equal are required to return the same hashCode.
  121. */
  122. @Test
  123. public void testHashCode() {
  124. DialPlot p1 = new DialPlot();
  125. DialPlot p2 = new DialPlot();
  126. assertTrue(p1.equals(p2));
  127. int h1 = p1.hashCode();
  128. int h2 = p2.hashCode();
  129. assertEquals(h1, h2);
  130. }
  131. /**
  132. * Confirm that cloning works.
  133. */
  134. @Test
  135. public void testCloning() throws CloneNotSupportedException {
  136. DialPlot p1 = new DialPlot();
  137. DialPlot p2 = (DialPlot) p1.clone();
  138. assertTrue(p1 != p2);
  139. assertTrue(p1.getClass() == p2.getClass());
  140. assertTrue(p1.equals(p2));
  141. }
  142. /**
  143. * Serialize an instance, restore it, and check for equality.
  144. */
  145. @Test
  146. public void testSerialization() {
  147. DialPlot p1 = new DialPlot();
  148. DialPlot p2 = (DialPlot) TestUtilities.serialised(p1);
  149. assertEquals(p1, p2);
  150. }
  151. /**
  152. * Check the notification event mechanism for the dial background.
  153. */
  154. @Test
  155. public void testBackgroundListener() {
  156. DialPlot p = new DialPlot();
  157. DialBackground b1 = new DialBackground(Color.red);
  158. p.setBackground(b1);
  159. p.addChangeListener(this);
  160. this.lastEvent = null;
  161. b1.setPaint(Color.blue);
  162. assertNotNull(this.lastEvent);
  163. DialBackground b2 = new DialBackground(Color.green);
  164. p.setBackground(b2);
  165. this.lastEvent = null;
  166. b1.setPaint(Color.red);
  167. assertNull(this.lastEvent);
  168. b2.setPaint(Color.red);
  169. assertNotNull(this.lastEvent);
  170. }
  171. /**
  172. * Check the notification event mechanism for the dial cap.
  173. */
  174. @Test
  175. public void testCapListener() {
  176. DialPlot p = new DialPlot();
  177. DialCap c1 = new DialCap();
  178. p.setCap(c1);
  179. p.addChangeListener(this);
  180. this.lastEvent = null;
  181. c1.setFillPaint(Color.red);
  182. assertNotNull(this.lastEvent);
  183. DialCap c2 = new DialCap();
  184. p.setCap(c2);
  185. this.lastEvent = null;
  186. c1.setFillPaint(Color.blue);
  187. assertNull(this.lastEvent);
  188. c2.setFillPaint(Color.green);
  189. assertNotNull(this.lastEvent);
  190. }
  191. /**
  192. * Check the notification event mechanism for the dial frame.
  193. */
  194. @Test
  195. public void testFrameListener() {
  196. DialPlot p = new DialPlot();
  197. ArcDialFrame f1 = new ArcDialFrame();
  198. p.setDialFrame(f1);
  199. p.addChangeListener(this);
  200. this.lastEvent = null;
  201. f1.setBackgroundPaint(Color.gray);
  202. assertNotNull(this.lastEvent);
  203. ArcDialFrame f2 = new ArcDialFrame();
  204. p.setDialFrame(f2);
  205. this.lastEvent = null;
  206. f1.setBackgroundPaint(Color.blue);
  207. assertNull(this.lastEvent);
  208. f2.setBackgroundPaint(Color.green);
  209. assertNotNull(this.lastEvent);
  210. }
  211. /**
  212. * Check the notification event mechanism for the dial scales.
  213. */
  214. @Test
  215. public void testScaleListener() {
  216. DialPlot p = new DialPlot();
  217. StandardDialScale s1 = new StandardDialScale();
  218. p.addScale(0, s1);
  219. p.addChangeListener(this);
  220. this.lastEvent = null;
  221. s1.setStartAngle(22.0);
  222. assertNotNull(this.lastEvent);
  223. StandardDialScale s2 = new StandardDialScale();
  224. p.addScale(0, s2);
  225. this.lastEvent = null;
  226. s1.setStartAngle(33.0);
  227. assertNull(this.lastEvent);
  228. s2.setStartAngle(33.0);
  229. assertNotNull(this.lastEvent);
  230. }
  231. /**
  232. * Check the notification event mechanism for a layer.
  233. */
  234. @Test
  235. public void testLayerListener() {
  236. DialPlot p = new DialPlot();
  237. DialBackground b1 = new DialBackground(Color.red);
  238. p.addLayer(b1);
  239. p.addChangeListener(this);
  240. this.lastEvent = null;
  241. b1.setPaint(Color.blue);
  242. assertNotNull(this.lastEvent);
  243. DialBackground b2 = new DialBackground(Color.green);
  244. p.addLayer(b2);
  245. this.lastEvent = null;
  246. b1.setPaint(Color.red);
  247. assertNotNull(this.lastEvent);
  248. b2.setPaint(Color.green);
  249. assertNotNull(this.lastEvent);
  250. p.removeLayer(b2);
  251. this.lastEvent = null;
  252. b2.setPaint(Color.red);
  253. assertNull(this.lastEvent);
  254. }
  255. }