PiePlotTest.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2014, 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. * PiePlotTest.java
  29. * ----------------
  30. * (C) Copyright 2003-2014, by Object Refinery Limited and Contributors.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes
  36. * -------
  37. * 18-Mar-2003 : Version 1 (DG);
  38. * 10-May-2005 : Strengthened equals() test (DG);
  39. * 27-Sep-2006 : Added tests for the getBaseSectionPaint() method (DG);
  40. * 23-Nov-2006 : Additional equals() and clone() tests (DG);
  41. * 17-Apr-2007 : Added check for label generator that returns a null label (DG);
  42. * 31-Mar-2008 : Updated testEquals() (DG);
  43. * 10-Jul-2009 : Updated testEquals() (DG);
  44. * 07-Apr-2014 : Add cloning tests (DG);
  45. *
  46. */
  47. package org.jfree.chart.plot;
  48. import static org.junit.Assert.assertEquals;
  49. import static org.junit.Assert.assertNotNull;
  50. import static org.junit.Assert.assertFalse;
  51. import static org.junit.Assert.assertTrue;
  52. import java.awt.BasicStroke;
  53. import java.awt.Color;
  54. import java.awt.Font;
  55. import java.awt.GradientPaint;
  56. import java.awt.Graphics2D;
  57. import java.awt.Rectangle;
  58. import java.awt.Stroke;
  59. import java.awt.geom.Rectangle2D;
  60. import java.awt.image.BufferedImage;
  61. import java.text.AttributedString;
  62. import org.jfree.chart.ChartFactory;
  63. import org.jfree.chart.JFreeChart;
  64. import org.jfree.chart.LegendItemCollection;
  65. import org.jfree.chart.TestUtilities;
  66. import org.jfree.chart.labels.PieSectionLabelGenerator;
  67. import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
  68. import org.jfree.chart.labels.StandardPieToolTipGenerator;
  69. import org.jfree.chart.urls.CustomPieURLGenerator;
  70. import org.jfree.chart.urls.StandardPieURLGenerator;
  71. import org.jfree.chart.util.DefaultShadowGenerator;
  72. import org.jfree.data.general.DefaultPieDataset;
  73. import org.jfree.data.general.PieDataset;
  74. import org.jfree.util.Rotation;
  75. import static org.junit.Assert.assertEquals;
  76. import static org.junit.Assert.assertNotEquals;
  77. import org.junit.Test;
  78. /**
  79. * Some tests for the {@link PiePlot} class.
  80. */
  81. public class PiePlotTest {
  82. /**
  83. * Test the equals() method.
  84. */
  85. @Test
  86. public void testEquals() {
  87. PiePlot plot1 = new PiePlot();
  88. PiePlot plot2 = new PiePlot();
  89. assertTrue(plot1.equals(plot2));
  90. assertTrue(plot2.equals(plot1));
  91. // pieIndex...
  92. plot1.setPieIndex(99);
  93. assertFalse(plot1.equals(plot2));
  94. plot2.setPieIndex(99);
  95. assertTrue(plot1.equals(plot2));
  96. // interiorGap...
  97. plot1.setInteriorGap(0.15);
  98. assertFalse(plot1.equals(plot2));
  99. plot2.setInteriorGap(0.15);
  100. assertTrue(plot1.equals(plot2));
  101. // circular
  102. plot1.setCircular(!plot1.isCircular());
  103. assertFalse(plot1.equals(plot2));
  104. plot2.setCircular(false);
  105. assertTrue(plot1.equals(plot2));
  106. // startAngle
  107. plot1.setStartAngle(Math.PI);
  108. assertFalse(plot1.equals(plot2));
  109. plot2.setStartAngle(Math.PI);
  110. assertTrue(plot1.equals(plot2));
  111. // direction
  112. plot1.setDirection(Rotation.ANTICLOCKWISE);
  113. assertFalse(plot1.equals(plot2));
  114. plot2.setDirection(Rotation.ANTICLOCKWISE);
  115. assertTrue(plot1.equals(plot2));
  116. // ignoreZeroValues
  117. plot1.setIgnoreZeroValues(true);
  118. plot2.setIgnoreZeroValues(false);
  119. assertFalse(plot1.equals(plot2));
  120. plot2.setIgnoreZeroValues(true);
  121. assertTrue(plot1.equals(plot2));
  122. // ignoreNullValues
  123. plot1.setIgnoreNullValues(true);
  124. plot2.setIgnoreNullValues(false);
  125. assertFalse(plot1.equals(plot2));
  126. plot2.setIgnoreNullValues(true);
  127. assertTrue(plot1.equals(plot2));
  128. // sectionPaint
  129. plot1.setSectionPaint(new GradientPaint(1.0f, 2.0f, Color.red,
  130. 3.0f, 4.0f, Color.white));
  131. assertFalse(plot1.equals(plot2));
  132. plot2.setSectionPaint(new GradientPaint(1.0f, 2.0f, Color.red,
  133. 3.0f, 4.0f, Color.white));
  134. assertTrue(plot1.equals(plot2));
  135. // sectionPaintMap
  136. plot1.setSectionPaint("A", new GradientPaint(1.0f, 2.0f, Color.blue,
  137. 3.0f, 4.0f, Color.white));
  138. assertFalse(plot1.equals(plot2));
  139. plot2.setSectionPaint("A", new GradientPaint(1.0f, 2.0f, Color.blue,
  140. 3.0f, 4.0f, Color.white));
  141. assertTrue(plot1.equals(plot2));
  142. // baseSectionPaint
  143. plot1.setBaseSectionPaint(new GradientPaint(1.0f, 2.0f, Color.black,
  144. 3.0f, 4.0f, Color.white));
  145. assertFalse(plot1.equals(plot2));
  146. plot2.setBaseSectionPaint(new GradientPaint(1.0f, 2.0f, Color.black,
  147. 3.0f, 4.0f, Color.white));
  148. assertTrue(plot1.equals(plot2));
  149. // sectionOutlinesVisible
  150. plot1.setSectionOutlinesVisible(false);
  151. assertFalse(plot1.equals(plot2));
  152. plot2.setSectionOutlinesVisible(false);
  153. assertTrue(plot1.equals(plot2));
  154. // sectionOutlinePaint
  155. plot1.setSectionOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.cyan,
  156. 3.0f, 4.0f, Color.white));
  157. assertFalse(plot1.equals(plot2));
  158. plot2.setSectionOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.cyan,
  159. 3.0f, 4.0f, Color.white));
  160. assertTrue(plot1.equals(plot2));
  161. // sectionOutlinePaintList
  162. plot1.setSectionOutlinePaint("A", new GradientPaint(1.0f, 2.0f,
  163. Color.green, 3.0f, 4.0f, Color.white));
  164. assertFalse(plot1.equals(plot2));
  165. plot2.setSectionOutlinePaint("A", new GradientPaint(1.0f, 2.0f,
  166. Color.green, 3.0f, 4.0f, Color.white));
  167. assertTrue(plot1.equals(plot2));
  168. // baseSectionOutlinePaint
  169. plot1.setBaseSectionOutlinePaint(new GradientPaint(1.0f, 2.0f,
  170. Color.gray, 3.0f, 4.0f, Color.white));
  171. assertFalse(plot1.equals(plot2));
  172. plot2.setBaseSectionOutlinePaint(new GradientPaint(1.0f, 2.0f,
  173. Color.gray, 3.0f, 4.0f, Color.white));
  174. assertTrue(plot1.equals(plot2));
  175. // sectionOutlineStroke
  176. plot1.setSectionOutlineStroke(new BasicStroke(1.0f));
  177. assertFalse(plot1.equals(plot2));
  178. plot2.setSectionOutlineStroke(new BasicStroke(1.0f));
  179. assertTrue(plot1.equals(plot2));
  180. // sectionOutlineStrokeList
  181. plot1.setSectionOutlineStroke("A", new BasicStroke(1.0f));
  182. assertFalse(plot1.equals(plot2));
  183. plot2.setSectionOutlineStroke("A", new BasicStroke(1.0f));
  184. assertTrue(plot1.equals(plot2));
  185. // baseSectionOutlineStroke
  186. plot1.setBaseSectionOutlineStroke(new BasicStroke(1.0f));
  187. assertFalse(plot1.equals(plot2));
  188. plot2.setBaseSectionOutlineStroke(new BasicStroke(1.0f));
  189. assertTrue(plot1.equals(plot2));
  190. // shadowPaint
  191. plot1.setShadowPaint(new GradientPaint(1.0f, 2.0f, Color.orange,
  192. 3.0f, 4.0f, Color.white));
  193. assertFalse(plot1.equals(plot2));
  194. plot2.setShadowPaint(new GradientPaint(1.0f, 2.0f, Color.orange,
  195. 3.0f, 4.0f, Color.white));
  196. assertTrue(plot1.equals(plot2));
  197. // shadowXOffset
  198. plot1.setShadowXOffset(4.4);
  199. assertFalse(plot1.equals(plot2));
  200. plot2.setShadowXOffset(4.4);
  201. assertTrue(plot1.equals(plot2));
  202. // shadowYOffset
  203. plot1.setShadowYOffset(4.4);
  204. assertFalse(plot1.equals(plot2));
  205. plot2.setShadowYOffset(4.4);
  206. assertTrue(plot1.equals(plot2));
  207. // labelFont
  208. plot1.setLabelFont(new Font("Serif", Font.PLAIN, 18));
  209. assertFalse(plot1.equals(plot2));
  210. plot2.setLabelFont(new Font("Serif", Font.PLAIN, 18));
  211. assertTrue(plot1.equals(plot2));
  212. // labelPaint
  213. plot1.setLabelPaint(new GradientPaint(1.0f, 2.0f, Color.darkGray,
  214. 3.0f, 4.0f, Color.white));
  215. assertFalse(plot1.equals(plot2));
  216. plot2.setLabelPaint(new GradientPaint(1.0f, 2.0f, Color.darkGray,
  217. 3.0f, 4.0f, Color.white));
  218. assertTrue(plot1.equals(plot2));
  219. // labelBackgroundPaint
  220. plot1.setLabelBackgroundPaint(new GradientPaint(1.0f, 2.0f, Color.red,
  221. 3.0f, 4.0f, Color.white));
  222. assertFalse(plot1.equals(plot2));
  223. plot2.setLabelBackgroundPaint(new GradientPaint(1.0f, 2.0f, Color.red,
  224. 3.0f, 4.0f, Color.white));
  225. assertTrue(plot1.equals(plot2));
  226. // labelOutlinePaint
  227. plot1.setLabelOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.blue,
  228. 3.0f, 4.0f, Color.white));
  229. assertFalse(plot1.equals(plot2));
  230. plot2.setLabelOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.blue,
  231. 3.0f, 4.0f, Color.white));
  232. assertTrue(plot1.equals(plot2));
  233. // labelOutlineStroke
  234. Stroke s = new BasicStroke(1.1f);
  235. plot1.setLabelOutlineStroke(s);
  236. assertFalse(plot1.equals(plot2));
  237. plot2.setLabelOutlineStroke(s);
  238. assertTrue(plot1.equals(plot2));
  239. // labelShadowPaint
  240. plot1.setLabelShadowPaint(new GradientPaint(1.0f, 2.0f, Color.yellow,
  241. 3.0f, 4.0f, Color.white));
  242. assertFalse(plot1.equals(plot2));
  243. plot2.setLabelShadowPaint(new GradientPaint(1.0f, 2.0f, Color.yellow,
  244. 3.0f, 4.0f, Color.white));
  245. assertTrue(plot1.equals(plot2));
  246. // explodePercentages
  247. plot1.setExplodePercent("A", 0.33);
  248. assertFalse(plot1.equals(plot2));
  249. plot2.setExplodePercent("A", 0.33);
  250. assertTrue(plot1.equals(plot2));
  251. // labelGenerator
  252. plot1.setLabelGenerator(new StandardPieSectionLabelGenerator(
  253. "{2}{1}{0}"));
  254. assertFalse(plot1.equals(plot2));
  255. plot2.setLabelGenerator(new StandardPieSectionLabelGenerator(
  256. "{2}{1}{0}"));
  257. assertTrue(plot1.equals(plot2));
  258. // labelFont
  259. Font f = new Font("SansSerif", Font.PLAIN, 20);
  260. plot1.setLabelFont(f);
  261. assertFalse(plot1.equals(plot2));
  262. plot2.setLabelFont(f);
  263. assertTrue(plot1.equals(plot2));
  264. // labelPaint
  265. plot1.setLabelPaint(new GradientPaint(1.0f, 2.0f, Color.magenta,
  266. 3.0f, 4.0f, Color.white));
  267. assertFalse(plot1.equals(plot2));
  268. plot2.setLabelPaint(new GradientPaint(1.0f, 2.0f, Color.magenta,
  269. 3.0f, 4.0f, Color.white));
  270. assertTrue(plot1.equals(plot2));
  271. // maximumLabelWidth
  272. plot1.setMaximumLabelWidth(0.33);
  273. assertFalse(plot1.equals(plot2));
  274. plot2.setMaximumLabelWidth(0.33);
  275. assertTrue(plot1.equals(plot2));
  276. // labelGap
  277. plot1.setLabelGap(0.11);
  278. assertFalse(plot1.equals(plot2));
  279. plot2.setLabelGap(0.11);
  280. assertTrue(plot1.equals(plot2));
  281. // links visible
  282. plot1.setLabelLinksVisible(false);
  283. assertFalse(plot1.equals(plot2));
  284. plot2.setLabelLinksVisible(false);
  285. assertTrue(plot1.equals(plot2));
  286. plot1.setLabelLinkStyle(PieLabelLinkStyle.QUAD_CURVE);
  287. assertFalse(plot1.equals(plot2));
  288. plot2.setLabelLinkStyle(PieLabelLinkStyle.QUAD_CURVE);
  289. assertTrue(plot1.equals(plot2));
  290. // linkMargin
  291. plot1.setLabelLinkMargin(0.11);
  292. assertFalse(plot1.equals(plot2));
  293. plot2.setLabelLinkMargin(0.11);
  294. assertTrue(plot1.equals(plot2));
  295. // labelLinkPaint
  296. plot1.setLabelLinkPaint(new GradientPaint(1.0f, 2.0f, Color.magenta,
  297. 3.0f, 4.0f, Color.white));
  298. assertFalse(plot1.equals(plot2));
  299. plot2.setLabelLinkPaint(new GradientPaint(1.0f, 2.0f, Color.magenta,
  300. 3.0f, 4.0f, Color.white));
  301. assertTrue(plot1.equals(plot2));
  302. // labelLinkStroke
  303. plot1.setLabelLinkStroke(new BasicStroke(1.0f));
  304. assertFalse(plot1.equals(plot2));
  305. plot2.setLabelLinkStroke(new BasicStroke(1.0f));
  306. assertTrue(plot1.equals(plot2));
  307. // toolTipGenerator
  308. plot1.setToolTipGenerator(
  309. new StandardPieToolTipGenerator("{2}{1}{0}")
  310. );
  311. assertFalse(plot1.equals(plot2));
  312. plot2.setToolTipGenerator(
  313. new StandardPieToolTipGenerator("{2}{1}{0}")
  314. );
  315. assertTrue(plot1.equals(plot2));
  316. // urlGenerator
  317. plot1.setURLGenerator(new StandardPieURLGenerator("xx"));
  318. assertFalse(plot1.equals(plot2));
  319. plot2.setURLGenerator(new StandardPieURLGenerator("xx"));
  320. assertTrue(plot1.equals(plot2));
  321. // minimumArcAngleToDraw
  322. plot1.setMinimumArcAngleToDraw(1.0);
  323. assertFalse(plot1.equals(plot2));
  324. plot2.setMinimumArcAngleToDraw(1.0);
  325. assertTrue(plot1.equals(plot2));
  326. // legendItemShape
  327. plot1.setLegendItemShape(new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0));
  328. assertFalse(plot1.equals(plot2));
  329. plot2.setLegendItemShape(new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0));
  330. assertTrue(plot1.equals(plot2));
  331. // legendLabelGenerator
  332. plot1.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(
  333. "{0} --> {1}"));
  334. assertFalse(plot1.equals(plot2));
  335. plot2.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(
  336. "{0} --> {1}"));
  337. assertTrue(plot1.equals(plot2));
  338. // legendLabelToolTipGenerator
  339. plot1.setLegendLabelToolTipGenerator(
  340. new StandardPieSectionLabelGenerator("{0} is {1}"));
  341. assertFalse(plot1.equals(plot2));
  342. plot2.setLegendLabelToolTipGenerator(
  343. new StandardPieSectionLabelGenerator("{0} is {1}"));
  344. assertTrue(plot1.equals(plot2));
  345. // legendLabelURLGenerator
  346. plot1.setLegendLabelURLGenerator(new StandardPieURLGenerator(
  347. "index.html"));
  348. assertFalse(plot1.equals(plot2));
  349. plot2.setLegendLabelURLGenerator(new StandardPieURLGenerator(
  350. "index.html"));
  351. assertTrue(plot1.equals(plot2));
  352. // autoPopulateSectionPaint
  353. plot1.setAutoPopulateSectionPaint(false);
  354. assertFalse(plot1.equals(plot2));
  355. plot2.setAutoPopulateSectionPaint(false);
  356. assertTrue(plot1.equals(plot2));
  357. // autoPopulateSectionOutlinePaint
  358. plot1.setAutoPopulateSectionOutlinePaint(true);
  359. assertFalse(plot1.equals(plot2));
  360. plot2.setAutoPopulateSectionOutlinePaint(true);
  361. assertTrue(plot1.equals(plot2));
  362. // autoPopulateSectionOutlineStroke
  363. plot1.setAutoPopulateSectionOutlineStroke(true);
  364. assertFalse(plot1.equals(plot2));
  365. plot2.setAutoPopulateSectionOutlineStroke(true);
  366. assertTrue(plot1.equals(plot2));
  367. // shadowGenerator
  368. plot1.setShadowGenerator(new DefaultShadowGenerator(5, Color.gray,
  369. 0.6f, 4, -Math.PI / 4));
  370. assertFalse(plot1.equals(plot2));
  371. plot2.setShadowGenerator(new DefaultShadowGenerator(5, Color.gray,
  372. 0.6f, 4, -Math.PI / 4));
  373. assertTrue(plot1.equals(plot2));
  374. plot1.setShadowGenerator(null);
  375. assertFalse(plot1.equals(plot2));
  376. plot2.setShadowGenerator(null);
  377. assertTrue(plot1.equals(plot2));
  378. }
  379. /**
  380. * Some basic checks for the clone() method.
  381. */
  382. @Test
  383. public void testCloning() throws CloneNotSupportedException {
  384. PiePlot p1 = new PiePlot();
  385. PiePlot p2 = (PiePlot) p1.clone();
  386. assertTrue(p1 != p2);
  387. assertTrue(p1.getClass() == p2.getClass());
  388. assertTrue(p1.equals(p2));
  389. }
  390. /**
  391. * Check cloning of the urlGenerator field.
  392. */
  393. @Test
  394. public void testCloning_URLGenerator() throws CloneNotSupportedException {
  395. CustomPieURLGenerator generator = new CustomPieURLGenerator();
  396. PiePlot p1 = new PiePlot();
  397. p1.setURLGenerator(generator);
  398. PiePlot p2 = (PiePlot) p1.clone();
  399. assertTrue(p1 != p2);
  400. assertTrue(p1.getClass() == p2.getClass());
  401. assertTrue(p1.equals(p2));
  402. // check that the URL generator has been cloned
  403. assertTrue(p1.getURLGenerator() != p2.getURLGenerator());
  404. }
  405. /**
  406. * Check cloning of the legendItemShape field.
  407. */
  408. @Test
  409. public void testCloning_LegendItemShape() throws CloneNotSupportedException {
  410. Rectangle shape = new Rectangle(-4, -4, 8, 8);
  411. PiePlot p1 = new PiePlot();
  412. p1.setLegendItemShape(shape);
  413. PiePlot p2 = (PiePlot) p1.clone();
  414. assertTrue(p1 != p2);
  415. assertTrue(p1.getClass() == p2.getClass());
  416. assertTrue(p1.equals(p2));
  417. // change the shape and make sure it only affects p1
  418. shape.setRect(1.0, 2.0, 3.0, 4.0);
  419. assertFalse(p1.equals(p2));
  420. }
  421. /**
  422. * Check cloning of the legendLabelGenerator field.
  423. */
  424. @Test
  425. public void testCloning_LegendLabelGenerator() throws CloneNotSupportedException {
  426. StandardPieSectionLabelGenerator generator
  427. = new StandardPieSectionLabelGenerator();
  428. PiePlot p1 = new PiePlot();
  429. p1.setLegendLabelGenerator(generator);
  430. PiePlot p2 = (PiePlot) p1.clone();
  431. assertTrue(p1 != p2);
  432. assertTrue(p1.getClass() == p2.getClass());
  433. assertTrue(p1.equals(p2));
  434. // change the generator and make sure it only affects p1
  435. generator.getNumberFormat().setMinimumFractionDigits(2);
  436. assertFalse(p1.equals(p2));
  437. }
  438. /**
  439. * Check cloning of the legendLabelToolTipGenerator field.
  440. */
  441. @Test
  442. public void testCloning_LegendLabelToolTipGenerator() throws CloneNotSupportedException {
  443. StandardPieSectionLabelGenerator generator
  444. = new StandardPieSectionLabelGenerator();
  445. PiePlot p1 = new PiePlot();
  446. p1.setLegendLabelToolTipGenerator(generator);
  447. PiePlot p2 = (PiePlot) p1.clone();
  448. assertTrue(p1 != p2);
  449. assertTrue(p1.getClass() == p2.getClass());
  450. assertTrue(p1.equals(p2));
  451. // change the generator and make sure it only affects p1
  452. generator.getNumberFormat().setMinimumFractionDigits(2);
  453. assertFalse(p1.equals(p2));
  454. }
  455. /**
  456. * Check cloning of the legendLabelURLGenerator field.
  457. */
  458. @Test
  459. public void testCloning_LegendLabelURLGenerator() throws CloneNotSupportedException {
  460. CustomPieURLGenerator generator = new CustomPieURLGenerator();
  461. PiePlot p1 = new PiePlot();
  462. p1.setLegendLabelURLGenerator(generator);
  463. PiePlot p2 = (PiePlot) p1.clone();
  464. assertTrue(p1 != p2);
  465. assertTrue(p1.getClass() == p2.getClass());
  466. assertTrue(p1.equals(p2));
  467. // check that the URL generator has been cloned
  468. assertTrue(p1.getLegendLabelURLGenerator()
  469. != p2.getLegendLabelURLGenerator());
  470. }
  471. /**
  472. * Serialize an instance, restore it, and check for equality.
  473. */
  474. @Test
  475. public void testSerialization() {
  476. PiePlot p1 = new PiePlot(null);
  477. PiePlot p2 = (PiePlot) TestUtilities.serialised(p1);
  478. assertEquals(p1, p2);
  479. }
  480. /**
  481. * Some checks for the getLegendItems() method.
  482. */
  483. @Test
  484. public void testGetLegendItems() {
  485. DefaultPieDataset dataset = new DefaultPieDataset();
  486. dataset.setValue("Item 1", 1.0);
  487. dataset.setValue("Item 2", 2.0);
  488. dataset.setValue("Item 3", 0.0);
  489. dataset.setValue("Item 4", null);
  490. PiePlot plot = new PiePlot(dataset);
  491. plot.setIgnoreNullValues(false);
  492. plot.setIgnoreZeroValues(false);
  493. LegendItemCollection items = plot.getLegendItems();
  494. assertEquals(4, items.getItemCount());
  495. // check that null items are ignored if requested
  496. plot.setIgnoreNullValues(true);
  497. items = plot.getLegendItems();
  498. assertEquals(3, items.getItemCount());
  499. // check that zero items are ignored if requested
  500. plot.setIgnoreZeroValues(true);
  501. items = plot.getLegendItems();
  502. assertEquals(2, items.getItemCount());
  503. // check that negative items are always ignored
  504. dataset.setValue("Item 5", -1.0);
  505. items = plot.getLegendItems();
  506. assertEquals(2, items.getItemCount());
  507. }
  508. /**
  509. * Check that the default base section paint is not null, and that you
  510. * can never set it to null.
  511. */
  512. @Test
  513. public void testGetBaseSectionPaint() {
  514. PiePlot plot = new PiePlot();
  515. assertNotNull(plot.getBaseSectionPaint());
  516. boolean pass = false;
  517. try {
  518. plot.setBaseSectionPaint(null);
  519. }
  520. catch (IllegalArgumentException e) {
  521. pass = true;
  522. }
  523. assertTrue(pass);
  524. }
  525. static class NullLegendLabelGenerator implements PieSectionLabelGenerator {
  526. @Override
  527. public AttributedString generateAttributedSectionLabel(
  528. PieDataset dataset, Comparable key) {
  529. return null;
  530. }
  531. @Override
  532. public String generateSectionLabel(PieDataset dataset, Comparable key) {
  533. return null;
  534. }
  535. }
  536. /**
  537. * Draws a pie chart where the label generator returns null.
  538. */
  539. @Test
  540. public void testDrawWithNullLegendLabels() {
  541. DefaultPieDataset dataset = new DefaultPieDataset();
  542. dataset.setValue("L1", 12.0);
  543. dataset.setValue("L2", 11.0);
  544. JFreeChart chart = ChartFactory.createPieChart("Test", dataset, true,
  545. false, false);
  546. PiePlot plot = (PiePlot) chart.getPlot();
  547. plot.setLegendLabelGenerator(new NullLegendLabelGenerator());
  548. boolean success = false;
  549. try {
  550. BufferedImage image = new BufferedImage(200 , 100,
  551. BufferedImage.TYPE_INT_RGB);
  552. Graphics2D g2 = image.createGraphics();
  553. chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, null);
  554. g2.dispose();
  555. success = true;
  556. }
  557. catch (Exception e) {
  558. success = false;
  559. }
  560. assertTrue(success);
  561. }
  562. @Test
  563. public void testBug1126() throws CloneNotSupportedException {
  564. DefaultPieDataset dataset1 = new DefaultPieDataset();
  565. PiePlot plot1 = new PiePlot(dataset1);
  566. plot1.setSectionPaint("A", Color.RED);
  567. plot1.setSectionPaint("B", Color.GREEN);
  568. PiePlot plot2 = (PiePlot) plot1.clone();
  569. plot2.setSectionPaint("A", Color.BLUE);
  570. plot2.setSectionPaint("B", Color.YELLOW);
  571. assertEquals(Color.RED, plot1.getSectionPaint("A"));
  572. assertEquals(Color.GREEN, plot1.getSectionPaint("B"));
  573. assertEquals(Color.BLUE, plot2.getSectionPaint("A"));
  574. assertEquals(Color.YELLOW, plot2.getSectionPaint("B"));
  575. }
  576. @Test
  577. public void testBug1126_b() throws CloneNotSupportedException {
  578. DefaultPieDataset dataset1 = new DefaultPieDataset();
  579. PiePlot plot1 = new PiePlot(dataset1);
  580. plot1.setSectionOutlinePaint("A", Color.RED);
  581. plot1.setSectionOutlinePaint("B", Color.GREEN);
  582. PiePlot plot2 = (PiePlot) plot1.clone();
  583. plot2.setSectionOutlinePaint("A", Color.BLUE);
  584. plot2.setSectionOutlinePaint("B", Color.YELLOW);
  585. assertEquals(Color.RED, plot1.getSectionOutlinePaint("A"));
  586. assertEquals(Color.GREEN, plot1.getSectionOutlinePaint("B"));
  587. assertEquals(Color.BLUE, plot2.getSectionOutlinePaint("A"));
  588. assertEquals(Color.YELLOW, plot2.getSectionOutlinePaint("B"));
  589. }
  590. @Test
  591. public void testBug1126_c() throws CloneNotSupportedException {
  592. DefaultPieDataset dataset1 = new DefaultPieDataset();
  593. PiePlot plot1 = new PiePlot(dataset1);
  594. plot1.setSectionOutlineStroke("A", new BasicStroke(5.0f));
  595. plot1.setSectionOutlineStroke("B", new BasicStroke(6.0f));
  596. PiePlot plot2 = (PiePlot) plot1.clone();
  597. plot2.setSectionOutlineStroke("A", new BasicStroke(7.0f));
  598. plot2.setSectionOutlineStroke("B", new BasicStroke(8.0f));
  599. assertEquals(new BasicStroke(5.0f), plot1.getSectionOutlineStroke("A"));
  600. assertEquals(new BasicStroke(6.0f), plot1.getSectionOutlineStroke("B"));
  601. assertEquals(new BasicStroke(7.0f), plot2.getSectionOutlineStroke("A"));
  602. assertEquals(new BasicStroke(8.0f), plot2.getSectionOutlineStroke("B"));
  603. }
  604. @Test
  605. public void testBug1126_d() throws CloneNotSupportedException {
  606. DefaultPieDataset dataset1 = new DefaultPieDataset();
  607. PiePlot plot1 = new PiePlot(dataset1);
  608. plot1.setExplodePercent("A", 0.1);
  609. plot1.setExplodePercent("B", 0.2);
  610. PiePlot plot2 = (PiePlot) plot1.clone();
  611. plot2.setExplodePercent("A", 0.3);
  612. plot2.setExplodePercent("B", 0.4);
  613. assertEquals(0.1, plot1.getExplodePercent("A"), EPSILON);
  614. assertEquals(0.2, plot1.getExplodePercent("B"), EPSILON);
  615. assertEquals(0.3, plot2.getExplodePercent("A"), EPSILON);
  616. assertEquals(0.4, plot2.getExplodePercent("B"), EPSILON);
  617. }
  618. private static final double EPSILON = 0.000000001;
  619. @Test
  620. public void testBug1126_e() throws CloneNotSupportedException {
  621. DefaultPieDataset dataset1 = new DefaultPieDataset();
  622. PiePlot plot1 = new PiePlot(dataset1);
  623. plot1.setLabelGenerator(new StandardPieSectionLabelGenerator());
  624. PiePlot plot2 = (PiePlot) plot1.clone();
  625. StandardPieSectionLabelGenerator g2
  626. = (StandardPieSectionLabelGenerator) plot2.getLabelGenerator();
  627. g2.setAttributedLabel(1, new AttributedString("TESTING"));
  628. assertNotEquals(plot1, plot2);
  629. }
  630. }