SpiderWebPlotTest.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. * SpiderWebPlotTest.java
  29. * ----------------------
  30. * (C) Copyright 2005-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. * 10-Jun-2005 : Version 1 (DG);
  38. * 01-Jun-2006 : Added testDrawWithNullInfo() method (DG);
  39. * 05-Feb-2007 : Added more checks to testCloning (DG);
  40. * 01-Jun-2009 : Added test for getLegendItems() bug, series key is not
  41. * set (DG);
  42. *
  43. */
  44. package org.jfree.chart.plot;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertTrue;
  48. import static org.junit.Assert.fail;
  49. import java.awt.BasicStroke;
  50. import java.awt.Color;
  51. import java.awt.Font;
  52. import java.awt.GradientPaint;
  53. import java.awt.Graphics2D;
  54. import java.awt.Rectangle;
  55. import java.awt.geom.Rectangle2D;
  56. import java.awt.image.BufferedImage;
  57. import java.text.DecimalFormat;
  58. import org.jfree.chart.JFreeChart;
  59. import org.jfree.chart.LegendItem;
  60. import org.jfree.chart.LegendItemCollection;
  61. import org.jfree.chart.TestUtilities;
  62. import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
  63. import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
  64. import org.jfree.chart.urls.StandardCategoryURLGenerator;
  65. import org.jfree.data.category.DefaultCategoryDataset;
  66. import org.jfree.util.Rotation;
  67. import org.jfree.util.TableOrder;
  68. import org.junit.Test;
  69. /**
  70. * Tests for the {@link SpiderWebPlot} class.
  71. */
  72. public class SpiderWebPlotTest {
  73. /**
  74. * Some checks for the equals() method.
  75. */
  76. @Test
  77. public void testEquals() {
  78. SpiderWebPlot p1 = new SpiderWebPlot(new DefaultCategoryDataset());
  79. SpiderWebPlot p2 = new SpiderWebPlot(new DefaultCategoryDataset());
  80. assertTrue(p1.equals(p2));
  81. assertTrue(p2.equals(p1));
  82. // dataExtractOrder
  83. p1.setDataExtractOrder(TableOrder.BY_COLUMN);
  84. assertFalse(p1.equals(p2));
  85. p2.setDataExtractOrder(TableOrder.BY_COLUMN);
  86. assertTrue(p1.equals(p2));
  87. // headPercent
  88. p1.setHeadPercent(0.321);
  89. assertFalse(p1.equals(p2));
  90. p2.setHeadPercent(0.321);
  91. assertTrue(p1.equals(p2));
  92. // interiorGap
  93. p1.setInteriorGap(0.123);
  94. assertFalse(p1.equals(p2));
  95. p2.setInteriorGap(0.123);
  96. assertTrue(p1.equals(p2));
  97. // startAngle
  98. p1.setStartAngle(0.456);
  99. assertFalse(p1.equals(p2));
  100. p2.setStartAngle(0.456);
  101. assertTrue(p1.equals(p2));
  102. // direction
  103. p1.setDirection(Rotation.ANTICLOCKWISE);
  104. assertFalse(p1.equals(p2));
  105. p2.setDirection(Rotation.ANTICLOCKWISE);
  106. assertTrue(p1.equals(p2));
  107. // maxValue
  108. p1.setMaxValue(123.4);
  109. assertFalse(p1.equals(p2));
  110. p2.setMaxValue(123.4);
  111. assertTrue(p1.equals(p2));
  112. // legendItemShape
  113. p1.setLegendItemShape(new Rectangle(1, 2, 3, 4));
  114. assertFalse(p1.equals(p2));
  115. p2.setLegendItemShape(new Rectangle(1, 2, 3, 4));
  116. assertTrue(p1.equals(p2));
  117. // seriesPaint
  118. p1.setSeriesPaint(new GradientPaint(1.0f, 2.0f, Color.red,
  119. 3.0f, 4.0f, Color.white));
  120. assertFalse(p1.equals(p2));
  121. p2.setSeriesPaint(new GradientPaint(1.0f, 2.0f, Color.red,
  122. 3.0f, 4.0f, Color.white));
  123. assertTrue(p1.equals(p2));
  124. // seriesPaintList
  125. p1.setSeriesPaint(1, new GradientPaint(1.0f, 2.0f, Color.yellow,
  126. 3.0f, 4.0f, Color.white));
  127. assertFalse(p1.equals(p2));
  128. p2.setSeriesPaint(1, new GradientPaint(1.0f, 2.0f, Color.yellow,
  129. 3.0f, 4.0f, Color.white));
  130. assertTrue(p1.equals(p2));
  131. // baseSeriesPaint
  132. p1.setBaseSeriesPaint(new GradientPaint(1.0f, 2.0f, Color.red,
  133. 3.0f, 4.0f, Color.black));
  134. assertFalse(p1.equals(p2));
  135. p2.setBaseSeriesPaint(new GradientPaint(1.0f, 2.0f, Color.red,
  136. 3.0f, 4.0f, Color.black));
  137. assertTrue(p1.equals(p2));
  138. // seriesOutlinePaint
  139. p1.setSeriesOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.blue,
  140. 3.0f, 4.0f, Color.black));
  141. assertFalse(p1.equals(p2));
  142. p2.setSeriesOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.blue,
  143. 3.0f, 4.0f, Color.black));
  144. assertTrue(p1.equals(p2));
  145. // seriesOutlinePaintList
  146. p1.setSeriesOutlinePaint(1, new GradientPaint(1.0f, 2.0f, Color.blue,
  147. 3.0f, 4.0f, Color.green));
  148. assertFalse(p1.equals(p2));
  149. p2.setSeriesOutlinePaint(1, new GradientPaint(1.0f, 2.0f, Color.blue,
  150. 3.0f, 4.0f, Color.green));
  151. assertTrue(p1.equals(p2));
  152. // baseSeriesOutlinePaint
  153. p1.setBaseSeriesOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.cyan,
  154. 3.0f, 4.0f, Color.green));
  155. assertFalse(p1.equals(p2));
  156. p2.setBaseSeriesOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.cyan,
  157. 3.0f, 4.0f, Color.green));
  158. assertTrue(p1.equals(p2));
  159. // seriesOutlineStroke
  160. BasicStroke s = new BasicStroke(1.23f);
  161. p1.setSeriesOutlineStroke(s);
  162. assertFalse(p1.equals(p2));
  163. p2.setSeriesOutlineStroke(s);
  164. assertTrue(p1.equals(p2));
  165. // seriesOutlineStrokeList
  166. p1.setSeriesOutlineStroke(1, s);
  167. assertFalse(p1.equals(p2));
  168. p2.setSeriesOutlineStroke(1, s);
  169. assertTrue(p1.equals(p2));
  170. // baseSeriesOutlineStroke
  171. p1.setBaseSeriesOutlineStroke(s);
  172. assertFalse(p1.equals(p2));
  173. p2.setBaseSeriesOutlineStroke(s);
  174. assertTrue(p1.equals(p2));
  175. // webFilled
  176. p1.setWebFilled(false);
  177. assertFalse(p1.equals(p2));
  178. p2.setWebFilled(false);
  179. assertTrue(p1.equals(p2));
  180. // axisLabelGap
  181. p1.setAxisLabelGap(0.11);
  182. assertFalse(p1.equals(p2));
  183. p2.setAxisLabelGap(0.11);
  184. assertTrue(p1.equals(p2));
  185. // labelFont
  186. p1.setLabelFont(new Font("Serif", Font.PLAIN, 9));
  187. assertFalse(p1.equals(p2));
  188. p2.setLabelFont(new Font("Serif", Font.PLAIN, 9));
  189. assertTrue(p1.equals(p2));
  190. // labelPaint
  191. p1.setLabelPaint(new GradientPaint(1.0f, 2.0f, Color.red,
  192. 3.0f, 4.0f, Color.blue));
  193. assertFalse(p1.equals(p2));
  194. p2.setLabelPaint(new GradientPaint(1.0f, 2.0f, Color.red,
  195. 3.0f, 4.0f, Color.blue));
  196. assertTrue(p1.equals(p2));
  197. // labelGenerator
  198. p1.setLabelGenerator(new StandardCategoryItemLabelGenerator("XYZ: {0}",
  199. new DecimalFormat("0.000")));
  200. assertFalse(p1.equals(p2));
  201. p2.setLabelGenerator(new StandardCategoryItemLabelGenerator("XYZ: {0}",
  202. new DecimalFormat("0.000")));
  203. assertTrue(p1.equals(p2));
  204. // toolTipGenerator
  205. p1.setToolTipGenerator(new StandardCategoryToolTipGenerator());
  206. assertFalse(p1.equals(p2));
  207. p2.setToolTipGenerator(new StandardCategoryToolTipGenerator());
  208. assertTrue(p1.equals(p2));
  209. // urlGenerator
  210. p1.setURLGenerator(new StandardCategoryURLGenerator());
  211. assertFalse(p1.equals(p2));
  212. p2.setURLGenerator(new StandardCategoryURLGenerator());
  213. assertTrue(p1.equals(p2));
  214. // axisLinePaint
  215. p1.setAxisLinePaint(Color.red);
  216. assertFalse(p1.equals(p2));
  217. p2.setAxisLinePaint(Color.red);
  218. assertTrue(p1.equals(p2));
  219. // axisLineStroke
  220. p1.setAxisLineStroke(new BasicStroke(1.1f));
  221. assertFalse(p1.equals(p2));
  222. p2.setAxisLineStroke(new BasicStroke(1.1f));
  223. assertTrue(p1.equals(p2));
  224. }
  225. /**
  226. * Confirm that cloning works.
  227. */
  228. @Test
  229. public void testCloning() throws CloneNotSupportedException {
  230. SpiderWebPlot p1 = new SpiderWebPlot(new DefaultCategoryDataset());
  231. Rectangle2D legendShape = new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0);
  232. p1.setLegendItemShape(legendShape);
  233. SpiderWebPlot p2 = (SpiderWebPlot) p1.clone();
  234. assertTrue(p1 != p2);
  235. assertTrue(p1.getClass() == p2.getClass());
  236. assertTrue(p1.equals(p2));
  237. // change the legendItemShape
  238. legendShape.setRect(4.0, 3.0, 2.0, 1.0);
  239. assertFalse(p1.equals(p2));
  240. p2.setLegendItemShape(legendShape);
  241. assertTrue(p1.equals(p2));
  242. // change a series paint
  243. p1.setSeriesPaint(1, Color.black);
  244. assertFalse(p1.equals(p2));
  245. p2.setSeriesPaint(1, Color.black);
  246. assertTrue(p1.equals(p2));
  247. // change a series outline paint
  248. p1.setSeriesOutlinePaint(0, Color.red);
  249. assertFalse(p1.equals(p2));
  250. p2.setSeriesOutlinePaint(0, Color.red);
  251. assertTrue(p1.equals(p2));
  252. // change a series outline stroke
  253. p1.setSeriesOutlineStroke(0, new BasicStroke(1.1f));
  254. assertFalse(p1.equals(p2));
  255. p2.setSeriesOutlineStroke(0, new BasicStroke(1.1f));
  256. assertTrue(p1.equals(p2));
  257. }
  258. /**
  259. * Serialize an instance, restore it, and check for equality.
  260. */
  261. @Test
  262. public void testSerialization() {
  263. SpiderWebPlot p1 = new SpiderWebPlot(new DefaultCategoryDataset());
  264. SpiderWebPlot p2 = (SpiderWebPlot) TestUtilities.serialised(p1);
  265. assertEquals(p1, p2);
  266. }
  267. /**
  268. * Draws the chart with a null info object to make sure that no exceptions
  269. * are thrown.
  270. */
  271. @Test
  272. public void testDrawWithNullInfo() {
  273. DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  274. dataset.addValue(35.0, "S1", "C1");
  275. dataset.addValue(45.0, "S1", "C2");
  276. dataset.addValue(55.0, "S1", "C3");
  277. dataset.addValue(15.0, "S1", "C4");
  278. dataset.addValue(25.0, "S1", "C5");
  279. SpiderWebPlot plot = new SpiderWebPlot(dataset);
  280. JFreeChart chart = new JFreeChart(plot);
  281. try {
  282. BufferedImage image = new BufferedImage(200 , 100,
  283. BufferedImage.TYPE_INT_RGB);
  284. Graphics2D g2 = image.createGraphics();
  285. chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, null);
  286. g2.dispose();
  287. }
  288. catch (Exception e) {
  289. fail("There should be no exception.");
  290. }
  291. }
  292. /**
  293. * Fetches the legend items and checks the values.
  294. */
  295. @Test
  296. public void testGetLegendItems() {
  297. DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  298. dataset.addValue(35.0, "S1", "C1");
  299. dataset.addValue(45.0, "S1", "C2");
  300. dataset.addValue(55.0, "S2", "C1");
  301. dataset.addValue(15.0, "S2", "C2");
  302. SpiderWebPlot plot = new SpiderWebPlot(dataset);
  303. JFreeChart chart = new JFreeChart(plot);
  304. LegendItemCollection legendItems = plot.getLegendItems();
  305. assertEquals(2, legendItems.getItemCount());
  306. LegendItem item1 = legendItems.get(0);
  307. assertEquals("S1", item1.getLabel());
  308. assertEquals("S1", item1.getSeriesKey());
  309. assertEquals(0, item1.getSeriesIndex());
  310. assertEquals(dataset, item1.getDataset());
  311. assertEquals(0, item1.getDatasetIndex());
  312. LegendItem item2 = legendItems.get(1);
  313. assertEquals("S2", item2.getLabel());
  314. assertEquals("S2", item2.getSeriesKey());
  315. assertEquals(1, item2.getSeriesIndex());
  316. assertEquals(dataset, item2.getDataset());
  317. assertEquals(0, item2.getDatasetIndex());
  318. }
  319. }