XYBubbleRendererTest.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. * XYBubbleRendererTest.java
  29. * -------------------------
  30. * (C) Copyright 2003-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. * 25-Mar-2003 : Version 1 (DG);
  38. * 24-Jan-2007 : Added more checks to testEquals() (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 org.jfree.chart.JFreeChart;
  48. import org.jfree.chart.LegendItem;
  49. import org.jfree.chart.TestUtilities;
  50. import org.jfree.chart.axis.NumberAxis;
  51. import org.jfree.chart.plot.XYPlot;
  52. import org.jfree.data.xy.DefaultXYZDataset;
  53. import org.jfree.util.PublicCloneable;
  54. import org.junit.Test;
  55. /**
  56. * Tests for the {@link XYBubbleRenderer} class.
  57. */
  58. public class XYBubbleRendererTest {
  59. /**
  60. * Check that the equals() method distinguishes all fields.
  61. */
  62. @Test
  63. public void testEquals() {
  64. XYBubbleRenderer r1 = new XYBubbleRenderer();
  65. XYBubbleRenderer r2 = new XYBubbleRenderer();
  66. assertEquals(r1, r2);
  67. r1 = new XYBubbleRenderer(XYBubbleRenderer.SCALE_ON_RANGE_AXIS);
  68. assertFalse(r1.equals(r2));
  69. r2 = new XYBubbleRenderer(XYBubbleRenderer.SCALE_ON_RANGE_AXIS);
  70. assertTrue(r1.equals(r2));
  71. }
  72. /**
  73. * Two objects that are equal are required to return the same hashCode.
  74. */
  75. @Test
  76. public void testHashcode() {
  77. XYBubbleRenderer r1 = new XYBubbleRenderer();
  78. XYBubbleRenderer r2 = new XYBubbleRenderer();
  79. assertTrue(r1.equals(r2));
  80. int h1 = r1.hashCode();
  81. int h2 = r2.hashCode();
  82. assertEquals(h1, h2);
  83. }
  84. /**
  85. * Confirm that cloning works.
  86. */
  87. @Test
  88. public void testCloning() throws CloneNotSupportedException {
  89. XYBubbleRenderer r1 = new XYBubbleRenderer();
  90. XYBubbleRenderer r2 = (XYBubbleRenderer) r1.clone();
  91. assertTrue(r1 != r2);
  92. assertTrue(r1.getClass() == r2.getClass());
  93. assertTrue(r1.equals(r2));
  94. }
  95. /**
  96. * Verify that this class implements {@link PublicCloneable}.
  97. */
  98. @Test
  99. public void testPublicCloneable() {
  100. XYBubbleRenderer r1 = new XYBubbleRenderer();
  101. assertTrue(r1 instanceof PublicCloneable);
  102. }
  103. /**
  104. * Serialize an instance, restore it, and check for equality.
  105. */
  106. @Test
  107. public void testSerialization() {
  108. XYBubbleRenderer r1 = new XYBubbleRenderer();
  109. XYBubbleRenderer r2 = (XYBubbleRenderer) TestUtilities.serialised(r1);
  110. assertEquals(r1, r2);
  111. }
  112. /**
  113. * A check for the datasetIndex and seriesIndex fields in the LegendItem
  114. * returned by the getLegendItem() method.
  115. */
  116. @Test
  117. public void testGetLegendItemSeriesIndex() {
  118. DefaultXYZDataset d1 = new DefaultXYZDataset();
  119. double[] x = {2.1, 2.3, 2.3, 2.2, 2.2, 1.8, 1.8, 1.9, 2.3, 3.8};
  120. double[] y = {14.1, 11.1, 10.0, 8.8, 8.7, 8.4, 5.4, 4.1, 4.1, 25};
  121. double[] z = {2.4, 2.7, 2.7, 2.2, 2.2, 2.2, 2.1, 2.2, 1.6, 4};
  122. double[][] s1 = new double[][] {x, y, z};
  123. d1.addSeries("S1", s1);
  124. x = new double[] {2.1};
  125. y = new double[] {14.1};
  126. z = new double[] {2.4};
  127. double[][] s2 = new double[][] {x, y, z};
  128. d1.addSeries("S2", s2);
  129. DefaultXYZDataset d2 = new DefaultXYZDataset();
  130. x = new double[] {2.1};
  131. y = new double[] {14.1};
  132. z = new double[] {2.4};
  133. double[][] s3 = new double[][] {x, y, z};
  134. d2.addSeries("S3", s3);
  135. x = new double[] {2.1};
  136. y = new double[] {14.1};
  137. z = new double[] {2.4};
  138. double[][] s4 = new double[][] {x, y, z};
  139. d2.addSeries("S4", s4);
  140. x = new double[] {2.1};
  141. y = new double[] {14.1};
  142. z = new double[] {2.4};
  143. double[][] s5 = new double[][] {x, y, z};
  144. d2.addSeries("S5", s5);
  145. XYBubbleRenderer r = new XYBubbleRenderer();
  146. XYPlot plot = new XYPlot(d1, new NumberAxis("x"),
  147. new NumberAxis("y"), r);
  148. plot.setDataset(1, d2);
  149. /*JFreeChart chart =*/ new JFreeChart(plot);
  150. LegendItem li = r.getLegendItem(1, 2);
  151. assertEquals("S5", li.getLabel());
  152. assertEquals(1, li.getDatasetIndex());
  153. assertEquals(2, li.getSeriesIndex());
  154. }
  155. }