XYAreaRenderer2Test.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. * XYAreaRenderer2Test.java
  29. * ------------------------
  30. * (C) Copyright 2005-2013, by Object Refinery Limited.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes
  36. * -------
  37. * 24-May-2005 : Version 1 (DG);
  38. * 30-Nov-2006 : Extended testEquals() and testCloning() (DG);
  39. * 17-May-2007 : Added testGetLegendItemSeriesIndex() (DG);
  40. * 22-Apr-2008 : Added testPublicCloneable (DG);
  41. *
  42. */
  43. package org.jfree.chart.renderer.xy;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertTrue;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.fail;
  48. import java.awt.Rectangle;
  49. import org.jfree.chart.JFreeChart;
  50. import org.jfree.chart.LegendItem;
  51. import org.jfree.chart.TestUtilities;
  52. import org.jfree.chart.axis.NumberAxis;
  53. import org.jfree.chart.plot.XYPlot;
  54. import org.jfree.data.xy.DefaultTableXYDataset;
  55. import org.jfree.data.xy.XYSeries;
  56. import org.jfree.data.xy.XYSeriesCollection;
  57. import org.jfree.util.PublicCloneable;
  58. import org.junit.Test;
  59. /**
  60. * Tests for the {@link XYAreaRenderer2} class.
  61. */
  62. public class XYAreaRenderer2Test {
  63. /**
  64. * Check that the equals() method distinguishes all fields.
  65. */
  66. @Test
  67. public void testEquals() {
  68. XYAreaRenderer2 r1 = new XYAreaRenderer2();
  69. XYAreaRenderer2 r2 = new XYAreaRenderer2();
  70. assertEquals(r1, r2);
  71. r1.setOutline(!r1.isOutline());
  72. assertFalse(r1.equals(r2));
  73. r2.setOutline(r1.isOutline());
  74. assertTrue(r1.equals(r2));
  75. r1.setLegendArea(new Rectangle(1, 2, 3, 4));
  76. assertFalse(r1.equals(r2));
  77. r2.setLegendArea(new Rectangle(1, 2, 3, 4));
  78. assertTrue(r1.equals(r2));
  79. }
  80. /**
  81. * Two objects that are equal are required to return the same hashCode.
  82. */
  83. @Test
  84. public void testHashcode() {
  85. XYAreaRenderer2 r1 = new XYAreaRenderer2();
  86. XYAreaRenderer2 r2 = new XYAreaRenderer2();
  87. assertTrue(r1.equals(r2));
  88. int h1 = r1.hashCode();
  89. int h2 = r2.hashCode();
  90. assertEquals(h1, h2);
  91. }
  92. /**
  93. * Confirm that cloning works.
  94. */
  95. @Test
  96. public void testCloning() throws CloneNotSupportedException {
  97. XYAreaRenderer2 r1 = new XYAreaRenderer2();
  98. Rectangle rect = new Rectangle(1, 2, 3, 4);
  99. r1.setLegendArea(rect);
  100. XYAreaRenderer2 r2 = (XYAreaRenderer2) r1.clone();
  101. assertTrue(r1 != r2);
  102. assertTrue(r1.getClass() == r2.getClass());
  103. assertTrue(r1.equals(r2));
  104. // check independence
  105. rect.setBounds(99, 99, 99, 99);
  106. assertFalse(r1.equals(r2));
  107. }
  108. /**
  109. * Verify that this class implements {@link PublicCloneable}.
  110. */
  111. @Test
  112. public void testPublicCloneable() {
  113. XYAreaRenderer2 r1 = new XYAreaRenderer2();
  114. assertTrue(r1 instanceof PublicCloneable);
  115. }
  116. /**
  117. * Serialize an instance, restore it, and check for equality.
  118. */
  119. @Test
  120. public void testSerialization() {
  121. XYAreaRenderer2 r1 = new XYAreaRenderer2();
  122. XYAreaRenderer2 r2 = (XYAreaRenderer2) TestUtilities.serialised(r1);
  123. assertEquals(r1, r2);
  124. }
  125. /**
  126. * Draws the chart with a <code>null</code> info object to make sure that
  127. * no exceptions are thrown (particularly by code in the renderer).
  128. */
  129. @Test
  130. public void testDrawWithNullInfo() {
  131. try {
  132. DefaultTableXYDataset dataset = new DefaultTableXYDataset();
  133. XYSeries s1 = new XYSeries("Series 1", true, false);
  134. s1.add(5.0, 5.0);
  135. s1.add(10.0, 15.5);
  136. s1.add(15.0, 9.5);
  137. s1.add(20.0, 7.5);
  138. dataset.addSeries(s1);
  139. XYSeries s2 = new XYSeries("Series 2", true, false);
  140. s2.add(5.0, 5.0);
  141. s2.add(10.0, 15.5);
  142. s2.add(15.0, 9.5);
  143. s2.add(20.0, 3.5);
  144. dataset.addSeries(s2);
  145. XYPlot plot = new XYPlot(dataset,
  146. new NumberAxis("X"), new NumberAxis("Y"),
  147. new XYAreaRenderer2());
  148. JFreeChart chart = new JFreeChart(plot);
  149. /* BufferedImage image = */ chart.createBufferedImage(300, 200,
  150. null);
  151. }
  152. catch (NullPointerException e) {
  153. fail("No exception should be thrown.");
  154. }
  155. }
  156. /**
  157. * A check for the datasetIndex and seriesIndex fields in the LegendItem
  158. * returned by the getLegendItem() method.
  159. */
  160. @Test
  161. public void testGetLegendItemSeriesIndex() {
  162. XYSeriesCollection d1 = new XYSeriesCollection();
  163. XYSeries s1 = new XYSeries("S1");
  164. s1.add(1.0, 1.1);
  165. XYSeries s2 = new XYSeries("S2");
  166. s2.add(1.0, 1.1);
  167. d1.addSeries(s1);
  168. d1.addSeries(s2);
  169. XYSeriesCollection d2 = new XYSeriesCollection();
  170. XYSeries s3 = new XYSeries("S3");
  171. s3.add(1.0, 1.1);
  172. XYSeries s4 = new XYSeries("S4");
  173. s4.add(1.0, 1.1);
  174. XYSeries s5 = new XYSeries("S5");
  175. s5.add(1.0, 1.1);
  176. d2.addSeries(s3);
  177. d2.addSeries(s4);
  178. d2.addSeries(s5);
  179. XYAreaRenderer2 r = new XYAreaRenderer2();
  180. XYPlot plot = new XYPlot(d1, new NumberAxis("x"),
  181. new NumberAxis("y"), r);
  182. plot.setDataset(1, d2);
  183. /*JFreeChart chart =*/ new JFreeChart(plot);
  184. LegendItem li = r.getLegendItem(1, 2);
  185. assertEquals("S5", li.getLabel());
  186. assertEquals(1, li.getDatasetIndex());
  187. assertEquals(2, li.getSeriesIndex());
  188. }
  189. }