1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2004, 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 under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * -----------------
  25. * PiePlotTests.java
  26. * -----------------
  27. * (C) Copyright 2003, 2004, by Object Refinery Limited and Contributors.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: PiePlotTests.java,v 1.4 2004/11/29 15:16:50 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 18-Mar-2003 : Version 1 (DG);
  37. *
  38. */
  39. package org.jfree.chart.plot.junit;
  40. import java.awt.BasicStroke;
  41. import java.awt.Color;
  42. import java.awt.Font;
  43. import java.awt.Stroke;
  44. import java.awt.geom.Rectangle2D;
  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 junit.framework.Test;
  52. import junit.framework.TestCase;
  53. import junit.framework.TestSuite;
  54. import org.jfree.chart.labels.StandardPieItemLabelGenerator;
  55. import org.jfree.chart.plot.PiePlot;
  56. import org.jfree.chart.urls.StandardPieURLGenerator;
  57. import org.jfree.util.Rotation;
  58. /**
  59. * Tests for the {@link PiePlot} class.
  60. */
  61. public class PiePlotTests extends TestCase {
  62. /**
  63. * Returns the tests as a test suite.
  64. *
  65. * @return The test suite.
  66. */
  67. public static Test suite() {
  68. return new TestSuite(PiePlotTests.class);
  69. }
  70. /**
  71. * Constructs a new set of tests.
  72. *
  73. * @param name the name of the tests.
  74. */
  75. public PiePlotTests(String name) {
  76. super(name);
  77. }
  78. /**
  79. * Test the equals() method.
  80. */
  81. public void testEquals() {
  82. PiePlot plot1 = new PiePlot();
  83. PiePlot plot2 = new PiePlot();
  84. assertTrue(plot1.equals(plot2));
  85. assertTrue(plot2.equals(plot1));
  86. // pieIndex...
  87. plot1.setPieIndex(99);
  88. assertFalse(plot1.equals(plot2));
  89. plot2.setPieIndex(99);
  90. assertTrue(plot1.equals(plot2));
  91. // interiorGap...
  92. plot1.setInteriorGap(0.15);
  93. assertFalse(plot1.equals(plot2));
  94. plot2.setInteriorGap(0.15);
  95. assertTrue(plot1.equals(plot2));
  96. // circular
  97. plot1.setCircular(!plot1.isCircular());
  98. assertFalse(plot1.equals(plot2));
  99. plot2.setCircular(false);
  100. assertTrue(plot1.equals(plot2));
  101. // startAngle
  102. plot1.setStartAngle(Math.PI);
  103. assertFalse(plot1.equals(plot2));
  104. plot2.setStartAngle(Math.PI);
  105. assertTrue(plot1.equals(plot2));
  106. // direction
  107. plot1.setDirection(Rotation.ANTICLOCKWISE);
  108. assertFalse(plot1.equals(plot2));
  109. plot2.setDirection(Rotation.ANTICLOCKWISE);
  110. assertTrue(plot1.equals(plot2));
  111. // sectionPaint
  112. plot1.setSectionPaint(Color.red);
  113. assertFalse(plot1.equals(plot2));
  114. plot2.setSectionPaint(Color.red);
  115. assertTrue(plot1.equals(plot2));
  116. // sectionPaintList
  117. plot1.setSectionPaint(2, Color.red);
  118. assertFalse(plot1.equals(plot2));
  119. plot2.setSectionPaint(2, Color.red);
  120. assertTrue(plot1.equals(plot2));
  121. // baseSectionPaint
  122. plot1.setBaseSectionPaint(Color.red);
  123. assertFalse(plot1.equals(plot2));
  124. plot2.setBaseSectionPaint(Color.red);
  125. assertTrue(plot1.equals(plot2));
  126. // sectionOutlinePaint
  127. plot1.setSectionOutlinePaint(Color.red);
  128. assertFalse(plot1.equals(plot2));
  129. plot2.setSectionOutlinePaint(Color.red);
  130. assertTrue(plot1.equals(plot2));
  131. // sectionOutlinePaintList
  132. plot1.setSectionOutlinePaint(2, Color.red);
  133. assertFalse(plot1.equals(plot2));
  134. plot2.setSectionOutlinePaint(2, Color.red);
  135. assertTrue(plot1.equals(plot2));
  136. // baseSectionOutlinePaint
  137. plot1.setBaseSectionOutlinePaint(Color.red);
  138. assertFalse(plot1.equals(plot2));
  139. plot2.setBaseSectionOutlinePaint(Color.red);
  140. assertTrue(plot1.equals(plot2));
  141. // sectionOutlineStroke
  142. plot1.setSectionOutlineStroke(new BasicStroke(1.0f));
  143. assertFalse(plot1.equals(plot2));
  144. plot2.setSectionOutlineStroke(new BasicStroke(1.0f));
  145. assertTrue(plot1.equals(plot2));
  146. // sectionOutlineStrokeList
  147. plot1.setSectionOutlineStroke(2, new BasicStroke(1.0f));
  148. assertFalse(plot1.equals(plot2));
  149. plot2.setSectionOutlineStroke(2, new BasicStroke(1.0f));
  150. assertTrue(plot1.equals(plot2));
  151. // baseSectionOutlineStroke
  152. plot1.setBaseSectionOutlineStroke(new BasicStroke(1.0f));
  153. assertFalse(plot1.equals(plot2));
  154. plot2.setBaseSectionOutlineStroke(new BasicStroke(1.0f));
  155. assertTrue(plot1.equals(plot2));
  156. // shadowPaint
  157. plot1.setShadowPaint(Color.red);
  158. assertFalse(plot1.equals(plot2));
  159. plot2.setShadowPaint(Color.red);
  160. assertTrue(plot1.equals(plot2));
  161. // shadowXOffset
  162. plot1.setShadowXOffset(4.4);
  163. assertFalse(plot1.equals(plot2));
  164. plot2.setShadowXOffset(4.4);
  165. assertTrue(plot1.equals(plot2));
  166. // shadowYOffset
  167. plot1.setShadowYOffset(4.4);
  168. assertFalse(plot1.equals(plot2));
  169. plot2.setShadowYOffset(4.4);
  170. assertTrue(plot1.equals(plot2));
  171. // labelFont
  172. plot1.setLabelFont(new Font("Serif", Font.PLAIN, 18));
  173. assertFalse(plot1.equals(plot2));
  174. plot2.setLabelFont(new Font("Serif", Font.PLAIN, 18));
  175. assertTrue(plot1.equals(plot2));
  176. // labelPaint
  177. plot1.setLabelPaint(Color.red);
  178. assertFalse(plot1.equals(plot2));
  179. plot2.setLabelPaint(Color.red);
  180. assertTrue(plot1.equals(plot2));
  181. // labelBackgroundPaint
  182. plot1.setLabelBackgroundPaint(Color.red);
  183. assertFalse(plot1.equals(plot2));
  184. plot2.setLabelBackgroundPaint(Color.red);
  185. assertTrue(plot1.equals(plot2));
  186. // labelOutlinePaint
  187. plot1.setLabelOutlinePaint(Color.red);
  188. assertFalse(plot1.equals(plot2));
  189. plot2.setLabelOutlinePaint(Color.red);
  190. assertTrue(plot1.equals(plot2));
  191. // labelOutlineStroke
  192. Stroke s = new BasicStroke(1.1f);
  193. plot1.setLabelOutlineStroke(s);
  194. assertFalse(plot1.equals(plot2));
  195. plot2.setLabelOutlineStroke(s);
  196. assertTrue(plot1.equals(plot2));
  197. // labelShadowPaint
  198. plot1.setLabelShadowPaint(Color.red);
  199. assertFalse(plot1.equals(plot2));
  200. plot2.setLabelShadowPaint(Color.red);
  201. assertTrue(plot1.equals(plot2));
  202. // explodePercentages
  203. plot1.setExplodePercent(3, 0.33);
  204. assertFalse(plot1.equals(plot2));
  205. plot2.setExplodePercent(3, 0.33);
  206. assertTrue(plot1.equals(plot2));
  207. // labelGenerator
  208. plot1.setLabelGenerator(new StandardPieItemLabelGenerator("{2}{1}{0}"));
  209. assertFalse(plot1.equals(plot2));
  210. plot2.setLabelGenerator(new StandardPieItemLabelGenerator("{2}{1}{0}"));
  211. assertTrue(plot1.equals(plot2));
  212. // labelFont
  213. Font f = new Font("SansSerif", Font.PLAIN, 20);
  214. plot1.setLabelFont(f);
  215. assertFalse(plot1.equals(plot2));
  216. plot2.setLabelFont(f);
  217. assertTrue(plot1.equals(plot2));
  218. // labelPaint
  219. plot1.setLabelPaint(Color.blue);
  220. assertFalse(plot1.equals(plot2));
  221. plot2.setLabelPaint(Color.blue);
  222. assertTrue(plot1.equals(plot2));
  223. // maximumLabelWidth
  224. plot1.setMaximumLabelWidth(0.33);
  225. assertFalse(plot1.equals(plot2));
  226. plot2.setMaximumLabelWidth(0.33);
  227. assertTrue(plot1.equals(plot2));
  228. // labelGap
  229. plot1.setLabelGap(0.11);
  230. assertFalse(plot1.equals(plot2));
  231. plot2.setLabelGap(0.11);
  232. assertTrue(plot1.equals(plot2));
  233. // linkMargin
  234. plot1.setLabelLinkMargin(0.11);
  235. assertFalse(plot1.equals(plot2));
  236. plot2.setLabelLinkMargin(0.11);
  237. assertTrue(plot1.equals(plot2));
  238. // labelLinkPaint
  239. plot1.setLabelLinkPaint(Color.red);
  240. assertFalse(plot1.equals(plot2));
  241. plot2.setLabelLinkPaint(Color.red);
  242. assertTrue(plot1.equals(plot2));
  243. // labelLinkStroke
  244. plot1.setLabelLinkStroke(new BasicStroke(1.0f));
  245. assertFalse(plot1.equals(plot2));
  246. plot2.setLabelLinkStroke(new BasicStroke(1.0f));
  247. assertTrue(plot1.equals(plot2));
  248. // toolTipGenerator
  249. plot1.setToolTipGenerator(new StandardPieItemLabelGenerator("{2}{1}{0}"));
  250. assertFalse(plot1.equals(plot2));
  251. plot2.setToolTipGenerator(new StandardPieItemLabelGenerator("{2}{1}{0}"));
  252. assertTrue(plot1.equals(plot2));
  253. // urlGenerator
  254. plot1.setURLGenerator(new StandardPieURLGenerator("xx"));
  255. assertFalse(plot1.equals(plot2));
  256. plot2.setURLGenerator(new StandardPieURLGenerator("xx"));
  257. assertTrue(plot1.equals(plot2));
  258. // minimumArcAngleToDraw
  259. plot1.setMinimumArcAngleToDraw(1.0);
  260. assertFalse(plot1.equals(plot2));
  261. plot2.setMinimumArcAngleToDraw(1.0);
  262. assertTrue(plot1.equals(plot2));
  263. // legendItemShape
  264. plot1.setLegendItemShape(new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0));
  265. assertFalse(plot1.equals(plot2));
  266. plot2.setLegendItemShape(new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0));
  267. assertTrue(plot1.equals(plot2));
  268. }
  269. /**
  270. * Some basic checks for the clone() method.
  271. */
  272. public void testCloning() {
  273. PiePlot p1 = new PiePlot();
  274. PiePlot p2 = null;
  275. try {
  276. p2 = (PiePlot) p1.clone();
  277. }
  278. catch (CloneNotSupportedException e) {
  279. e.printStackTrace();
  280. System.err.println("Failed to clone.");
  281. }
  282. assertTrue(p1 != p2);
  283. assertTrue(p1.getClass() == p2.getClass());
  284. assertTrue(p1.equals(p2));
  285. }
  286. /**
  287. * Serialize an instance, restore it, and check for equality.
  288. */
  289. public void testSerialization() {
  290. PiePlot p1 = new PiePlot(null);
  291. PiePlot p2 = null;
  292. try {
  293. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  294. ObjectOutput out = new ObjectOutputStream(buffer);
  295. out.writeObject(p1);
  296. out.close();
  297. ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
  298. p2 = (PiePlot) in.readObject();
  299. in.close();
  300. }
  301. catch (Exception e) {
  302. e.printStackTrace();
  303. }
  304. assertEquals(p1, p2);
  305. }
  306. }