1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2005, 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 License
  20. * along with this library; if not, write to the Free Software Foundation,
  21. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  24. * in the United States and other countries.]
  25. *
  26. * -------------------
  27. * MeterPlotTests.java
  28. * -------------------
  29. * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
  30. *
  31. * Original Author: David Gilbert (for Object Refinery Limited);
  32. * Contributor(s): -;
  33. *
  34. * $Id: MeterPlotTests.java,v 1.3 2005/02/02 17:47:55 mungady Exp $
  35. *
  36. * Changes
  37. * -------
  38. * 27-Mar-2003 : Version 1 (DG);
  39. * 12-May-2004 : Updated testEquals();
  40. *
  41. */
  42. package org.jfree.chart.plot.junit;
  43. import java.awt.Color;
  44. import java.awt.Font;
  45. import java.io.ByteArrayInputStream;
  46. import java.io.ByteArrayOutputStream;
  47. import java.io.ObjectInput;
  48. import java.io.ObjectInputStream;
  49. import java.io.ObjectOutput;
  50. import java.io.ObjectOutputStream;
  51. import java.text.DecimalFormat;
  52. import junit.framework.Test;
  53. import junit.framework.TestCase;
  54. import junit.framework.TestSuite;
  55. import org.jfree.chart.plot.DialShape;
  56. import org.jfree.chart.plot.MeterPlot;
  57. import org.jfree.data.Range;
  58. import org.jfree.data.general.DefaultValueDataset;
  59. /**
  60. * Tests for the {@link MeterPlot} class.
  61. */
  62. public class MeterPlotTests extends TestCase {
  63. /**
  64. * Returns the tests as a test suite.
  65. *
  66. * @return The test suite.
  67. */
  68. public static Test suite() {
  69. return new TestSuite(MeterPlotTests.class);
  70. }
  71. /**
  72. * Constructs a new set of tests.
  73. *
  74. * @param name the name of the tests.
  75. */
  76. public MeterPlotTests(String name) {
  77. super(name);
  78. }
  79. /**
  80. * Test the equals method to ensure that it can distinguish the required
  81. * fields. Note that the dataset is NOT considered in the equals test.
  82. */
  83. public void testEquals() {
  84. MeterPlot plot1 = new MeterPlot();
  85. MeterPlot plot2 = new MeterPlot();
  86. assertTrue(plot1.equals(plot2));
  87. // units
  88. plot1.setUnits("mph");
  89. assertFalse(plot1.equals(plot2));
  90. plot2.setUnits("mph");
  91. assertTrue(plot1.equals(plot2));
  92. // range
  93. plot1.setRange(new Range(50.0, 70.0));
  94. assertFalse(plot1.equals(plot2));
  95. plot2.setRange(new Range(50.0, 70.0));
  96. assertTrue(plot1.equals(plot2));
  97. // normal range
  98. plot1.setNormalRange(new Range(55.0, 60.0));
  99. assertFalse(plot1.equals(plot2));
  100. plot2.setNormalRange(new Range(55.0, 60.0));
  101. assertTrue(plot1.equals(plot2));
  102. // warning range
  103. plot1.setWarningRange(new Range(60.0, 65.0));
  104. assertFalse(plot1.equals(plot2));
  105. plot2.setWarningRange(new Range(60.0, 65.0));
  106. assertTrue(plot1.equals(plot2));
  107. // critical range
  108. plot1.setCriticalRange(new Range(65.0, 70.0));
  109. assertFalse(plot1.equals(plot2));
  110. plot2.setCriticalRange(new Range(65.0, 70.0));
  111. assertTrue(plot1.equals(plot2));
  112. // dial outline paint
  113. plot1.setDialOutlinePaint(Color.red);
  114. assertFalse(plot1.equals(plot2));
  115. plot2.setDialOutlinePaint(Color.red);
  116. assertTrue(plot1.equals(plot2));
  117. // normal paint
  118. plot1.setNormalPaint(Color.blue);
  119. assertFalse(plot1.equals(plot2));
  120. plot2.setNormalPaint(Color.blue);
  121. assertTrue(plot1.equals(plot2));
  122. // warning paint
  123. plot1.setWarningPaint(Color.blue);
  124. assertFalse(plot1.equals(plot2));
  125. plot2.setWarningPaint(Color.blue);
  126. assertTrue(plot1.equals(plot2));
  127. // critical paint
  128. plot1.setCriticalPaint(Color.blue);
  129. assertFalse(plot1.equals(plot2));
  130. plot2.setCriticalPaint(Color.blue);
  131. assertTrue(plot1.equals(plot2));
  132. // normal background paint
  133. plot1.setNormalBackgroundPaint(Color.blue);
  134. assertFalse(plot1.equals(plot2));
  135. plot2.setNormalBackgroundPaint(Color.blue);
  136. assertTrue(plot1.equals(plot2));
  137. // warning background paint
  138. plot1.setWarningBackgroundPaint(Color.blue);
  139. assertFalse(plot1.equals(plot2));
  140. plot2.setWarningBackgroundPaint(Color.blue);
  141. assertTrue(plot1.equals(plot2));
  142. // critical paint
  143. plot1.setCriticalBackgroundPaint(Color.blue);
  144. assertFalse(plot1.equals(plot2));
  145. plot2.setCriticalBackgroundPaint(Color.blue);
  146. assertTrue(plot1.equals(plot2));
  147. // dial shape
  148. plot1.setDialShape(DialShape.CHORD);
  149. assertFalse(plot1.equals(plot2));
  150. plot2.setDialShape(DialShape.CHORD);
  151. assertTrue(plot1.equals(plot2));
  152. // dial background paint
  153. plot1.setDialBackgroundPaint(Color.yellow);
  154. assertFalse(plot1.equals(plot2));
  155. plot2.setDialBackgroundPaint(Color.yellow);
  156. assertTrue(plot1.equals(plot2));
  157. // needle paint
  158. plot1.setNeedlePaint(Color.black);
  159. assertFalse(plot1.equals(plot2));
  160. plot2.setNeedlePaint(Color.black);
  161. assertTrue(plot1.equals(plot2));
  162. // value font
  163. plot1.setValueFont(new Font("Serif", Font.PLAIN, 6));
  164. assertFalse(plot1.equals(plot2));
  165. plot2.setValueFont(new Font("Serif", Font.PLAIN, 6));
  166. assertTrue(plot1.equals(plot2));
  167. // value paint
  168. plot1.setValuePaint(Color.black);
  169. assertFalse(plot1.equals(plot2));
  170. plot2.setValuePaint(Color.black);
  171. assertTrue(plot1.equals(plot2));
  172. // tick label type
  173. plot1.setTickLabelType(MeterPlot.NO_LABELS);
  174. assertFalse(plot1.equals(plot2));
  175. plot2.setTickLabelType(MeterPlot.NO_LABELS);
  176. assertTrue(plot1.equals(plot2));
  177. // tick label font
  178. plot1.setTickLabelFont(new Font("Serif", Font.PLAIN, 6));
  179. assertFalse(plot1.equals(plot2));
  180. plot2.setTickLabelFont(new Font("Serif", Font.PLAIN, 6));
  181. assertTrue(plot1.equals(plot2));
  182. // tick label format
  183. plot1.setTickLabelFormat(new DecimalFormat("0"));
  184. assertFalse(plot1.equals(plot2));
  185. plot2.setTickLabelFormat(new DecimalFormat("0"));
  186. assertTrue(plot1.equals(plot2));
  187. // draw border
  188. plot1.setDrawBorder(!plot1.getDrawBorder());
  189. assertFalse(plot1.equals(plot2));
  190. plot2.setDrawBorder(plot1.getDrawBorder());
  191. assertTrue(plot1.equals(plot2));
  192. // meter angle
  193. plot1.setMeterAngle(22);
  194. assertFalse(plot1.equals(plot2));
  195. plot2.setMeterAngle(22);
  196. assertTrue(plot1.equals(plot2));
  197. }
  198. /**
  199. * Confirm that cloning works.
  200. */
  201. public void testCloning() {
  202. MeterPlot p1 = new MeterPlot();
  203. MeterPlot p2 = null;
  204. try {
  205. p2 = (MeterPlot) p1.clone();
  206. }
  207. catch (CloneNotSupportedException e) {
  208. e.printStackTrace();
  209. System.err.println("Failed to clone.");
  210. }
  211. assertTrue(p1 != p2);
  212. assertTrue(p1.getClass() == p2.getClass());
  213. assertTrue(p1.equals(p2));
  214. }
  215. /**
  216. * Serialize an instance, restore it, and check for equality.
  217. */
  218. public void testSerialization1() {
  219. MeterPlot p1 = new MeterPlot(null);
  220. MeterPlot p2 = null;
  221. try {
  222. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  223. ObjectOutput out = new ObjectOutputStream(buffer);
  224. out.writeObject(p1);
  225. out.close();
  226. ObjectInput in = new ObjectInputStream(
  227. new ByteArrayInputStream(buffer.toByteArray())
  228. );
  229. p2 = (MeterPlot) in.readObject();
  230. in.close();
  231. }
  232. catch (Exception e) {
  233. System.out.println(e.toString());
  234. }
  235. assertEquals(p1, p2);
  236. }
  237. /**
  238. * Serialize an instance, restore it, and check for equality.
  239. */
  240. public void testSerialization2() {
  241. MeterPlot p1 = new MeterPlot(new DefaultValueDataset(1.23));
  242. MeterPlot p2 = null;
  243. try {
  244. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  245. ObjectOutput out = new ObjectOutputStream(buffer);
  246. out.writeObject(p1);
  247. out.close();
  248. ObjectInput in = new ObjectInputStream(
  249. new ByteArrayInputStream(buffer.toByteArray())
  250. );
  251. p2 = (MeterPlot) in.readObject();
  252. in.close();
  253. }
  254. catch (Exception e) {
  255. System.out.println(e.toString());
  256. }
  257. assertEquals(p1, p2);
  258. }
  259. }