MeterPlotTest.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. * MeterPlotTest.java
  29. * ------------------
  30. * (C) Copyright 2003-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. * 27-Mar-2003 : Version 1 (DG);
  38. * 12-May-2004 : Updated testEquals() (DG);
  39. * 29-Nov-2007 : Updated testEquals() and testSerialization1() for
  40. * dialOutlinePaint (DG)
  41. *
  42. */
  43. package org.jfree.chart.plot;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertTrue;
  47. import java.awt.Color;
  48. import java.awt.Font;
  49. import java.awt.GradientPaint;
  50. import java.text.DecimalFormat;
  51. import org.jfree.chart.TestUtilities;
  52. import org.jfree.data.Range;
  53. import org.jfree.data.general.DefaultValueDataset;
  54. import org.junit.Test;
  55. /**
  56. * Tests for the {@link MeterPlot} class.
  57. */
  58. public class MeterPlotTest {
  59. /**
  60. * Test the equals method to ensure that it can distinguish the required
  61. * fields. Note that the dataset is NOT considered in the equals test.
  62. */
  63. @Test
  64. public void testEquals() {
  65. MeterPlot plot1 = new MeterPlot();
  66. MeterPlot plot2 = new MeterPlot();
  67. assertTrue(plot1.equals(plot2));
  68. // units
  69. plot1.setUnits("mph");
  70. assertFalse(plot1.equals(plot2));
  71. plot2.setUnits("mph");
  72. assertTrue(plot1.equals(plot2));
  73. // range
  74. plot1.setRange(new Range(50.0, 70.0));
  75. assertFalse(plot1.equals(plot2));
  76. plot2.setRange(new Range(50.0, 70.0));
  77. assertTrue(plot1.equals(plot2));
  78. // interval
  79. plot1.addInterval(new MeterInterval("Normal", new Range(55.0, 60.0)));
  80. assertFalse(plot1.equals(plot2));
  81. plot2.addInterval(new MeterInterval("Normal", new Range(55.0, 60.0)));
  82. assertTrue(plot1.equals(plot2));
  83. // dial outline paint
  84. plot1.setDialOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.red,
  85. 3.0f, 4.0f, Color.blue));
  86. assertFalse(plot1.equals(plot2));
  87. plot2.setDialOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.red,
  88. 3.0f, 4.0f, Color.blue));
  89. assertTrue(plot1.equals(plot2));
  90. // dial shape
  91. plot1.setDialShape(DialShape.CHORD);
  92. assertFalse(plot1.equals(plot2));
  93. plot2.setDialShape(DialShape.CHORD);
  94. assertTrue(plot1.equals(plot2));
  95. // dial background paint
  96. plot1.setDialBackgroundPaint(new GradientPaint(9.0f, 8.0f, Color.red,
  97. 7.0f, 6.0f, Color.blue));
  98. assertFalse(plot1.equals(plot2));
  99. plot2.setDialBackgroundPaint(new GradientPaint(9.0f, 8.0f, Color.red,
  100. 7.0f, 6.0f, Color.blue));
  101. assertTrue(plot1.equals(plot2));
  102. // dial outline paint
  103. plot1.setDialOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.green,
  104. 3.0f, 4.0f, Color.red));
  105. assertFalse(plot1.equals(plot2));
  106. plot2.setDialOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.green,
  107. 3.0f, 4.0f, Color.red));
  108. assertTrue(plot1.equals(plot2));
  109. // needle paint
  110. plot1.setNeedlePaint(new GradientPaint(9.0f, 8.0f, Color.red,
  111. 7.0f, 6.0f, Color.blue));
  112. assertFalse(plot1.equals(plot2));
  113. plot2.setNeedlePaint(new GradientPaint(9.0f, 8.0f, Color.red,
  114. 7.0f, 6.0f, Color.blue));
  115. assertTrue(plot1.equals(plot2));
  116. // value font
  117. plot1.setValueFont(new Font("Serif", Font.PLAIN, 6));
  118. assertFalse(plot1.equals(plot2));
  119. plot2.setValueFont(new Font("Serif", Font.PLAIN, 6));
  120. assertTrue(plot1.equals(plot2));
  121. // value paint
  122. plot1.setValuePaint(new GradientPaint(1.0f, 2.0f, Color.black,
  123. 3.0f, 4.0f, Color.white));
  124. assertFalse(plot1.equals(plot2));
  125. plot2.setValuePaint(new GradientPaint(1.0f, 2.0f, Color.black,
  126. 3.0f, 4.0f, Color.white));
  127. assertTrue(plot1.equals(plot2));
  128. // tick labels visible
  129. plot1.setTickLabelsVisible(false);
  130. assertFalse(plot1.equals(plot2));
  131. plot2.setTickLabelsVisible(false);
  132. assertTrue(plot1.equals(plot2));
  133. // tick label font
  134. plot1.setTickLabelFont(new Font("Serif", Font.PLAIN, 6));
  135. assertFalse(plot1.equals(plot2));
  136. plot2.setTickLabelFont(new Font("Serif", Font.PLAIN, 6));
  137. assertTrue(plot1.equals(plot2));
  138. // tick label paint
  139. plot1.setTickLabelPaint(Color.red);
  140. assertFalse(plot1.equals(plot2));
  141. plot2.setTickLabelPaint(Color.red);
  142. assertTrue(plot1.equals(plot2));
  143. // tick label format
  144. plot1.setTickLabelFormat(new DecimalFormat("0"));
  145. assertFalse(plot1.equals(plot2));
  146. plot2.setTickLabelFormat(new DecimalFormat("0"));
  147. assertTrue(plot1.equals(plot2));
  148. // tick paint
  149. plot1.setTickPaint(Color.green);
  150. assertFalse(plot1.equals(plot2));
  151. plot2.setTickPaint(Color.green);
  152. assertTrue(plot1.equals(plot2));
  153. // tick size
  154. plot1.setTickSize(1.23);
  155. assertFalse(plot1.equals(plot2));
  156. plot2.setTickSize(1.23);
  157. assertTrue(plot1.equals(plot2));
  158. // draw border
  159. plot1.setDrawBorder(!plot1.getDrawBorder());
  160. assertFalse(plot1.equals(plot2));
  161. plot2.setDrawBorder(plot1.getDrawBorder());
  162. assertTrue(plot1.equals(plot2));
  163. // meter angle
  164. plot1.setMeterAngle(22);
  165. assertFalse(plot1.equals(plot2));
  166. plot2.setMeterAngle(22);
  167. assertTrue(plot1.equals(plot2));
  168. }
  169. /**
  170. * Confirm that cloning works.
  171. */
  172. @Test
  173. public void testCloning() throws CloneNotSupportedException {
  174. MeterPlot p1 = new MeterPlot();
  175. MeterPlot p2 = (MeterPlot) p1.clone();
  176. assertTrue(p1 != p2);
  177. assertTrue(p1.getClass() == p2.getClass());
  178. assertTrue(p1.equals(p2));
  179. // the clone and the original share a reference to the SAME dataset
  180. assertTrue(p1.getDataset() == p2.getDataset());
  181. // try a few checks to ensure that the clone is independent of the
  182. // original
  183. p1.getTickLabelFormat().setMinimumIntegerDigits(99);
  184. assertFalse(p1.equals(p2));
  185. p2.getTickLabelFormat().setMinimumIntegerDigits(99);
  186. assertTrue(p1.equals(p2));
  187. p1.addInterval(new MeterInterval("Test", new Range(1.234, 5.678)));
  188. assertFalse(p1.equals(p2));
  189. p2.addInterval(new MeterInterval("Test", new Range(1.234, 5.678)));
  190. assertTrue(p1.equals(p2));
  191. }
  192. /**
  193. * Serialize an instance, restore it, and check for equality.
  194. */
  195. @Test
  196. public void testSerialization1() {
  197. MeterPlot p1 = new MeterPlot(null);
  198. p1.setDialBackgroundPaint(new GradientPaint(1.0f, 2.0f, Color.red,
  199. 3.0f, 4.0f, Color.blue));
  200. p1.setDialOutlinePaint(new GradientPaint(4.0f, 3.0f, Color.red,
  201. 2.0f, 1.0f, Color.blue));
  202. p1.setNeedlePaint(new GradientPaint(1.0f, 2.0f, Color.red,
  203. 3.0f, 4.0f, Color.blue));
  204. p1.setTickLabelPaint(new GradientPaint(1.0f, 2.0f, Color.red,
  205. 3.0f, 4.0f, Color.blue));
  206. p1.setTickPaint(new GradientPaint(1.0f, 2.0f, Color.red,
  207. 3.0f, 4.0f, Color.blue));
  208. MeterPlot p2 = (MeterPlot) TestUtilities.serialised(p1);
  209. assertEquals(p1, p2);
  210. }
  211. /**
  212. * Serialize an instance, restore it, and check for equality.
  213. */
  214. @Test
  215. public void testSerialization2() {
  216. MeterPlot p1 = new MeterPlot(new DefaultValueDataset(1.23));
  217. MeterPlot p2 = (MeterPlot) TestUtilities.serialised(p1);
  218. assertEquals(p1, p2);
  219. }
  220. }