FastScatterPlotTest.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. * FastScatterPlotTest.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. * 18-Mar-2003 : Version 1 (DG);
  38. * 29-Jan-2009 : Updated testEquals() (DG);
  39. * 26-Mar-2009 : Updated testEquals() for new panning fields (DG);
  40. *
  41. */
  42. package org.jfree.chart.plot;
  43. import static org.junit.Assert.assertEquals;
  44. import static org.junit.Assert.assertFalse;
  45. import static org.junit.Assert.assertTrue;
  46. import static org.junit.Assert.fail;
  47. import java.awt.BasicStroke;
  48. import java.awt.Color;
  49. import java.awt.GradientPaint;
  50. import java.awt.Stroke;
  51. import org.jfree.chart.JFreeChart;
  52. import org.jfree.chart.TestUtilities;
  53. import org.jfree.chart.axis.NumberAxis;
  54. import org.jfree.chart.axis.ValueAxis;
  55. import org.junit.Test;
  56. /**
  57. * Tests for the {@link FastScatterPlot} class.
  58. */
  59. public class FastScatterPlotTest {
  60. /**
  61. * Some checks for the equals() method.
  62. */
  63. @Test
  64. public void testEquals() {
  65. FastScatterPlot plot1 = new FastScatterPlot();
  66. FastScatterPlot plot2 = new FastScatterPlot();
  67. assertTrue(plot1.equals(plot2));
  68. assertTrue(plot2.equals(plot1));
  69. plot1.setPaint(new GradientPaint(1.0f, 2.0f, Color.red,
  70. 3.0f, 4.0f, Color.yellow));
  71. assertFalse(plot1.equals(plot2));
  72. plot2.setPaint(new GradientPaint(1.0f, 2.0f, Color.red,
  73. 3.0f, 4.0f, Color.yellow));
  74. assertTrue(plot1.equals(plot2));
  75. plot1.setDomainGridlinesVisible(false);
  76. assertFalse(plot1.equals(plot2));
  77. plot2.setDomainGridlinesVisible(false);
  78. assertTrue(plot1.equals(plot2));
  79. plot1.setDomainGridlinePaint(new GradientPaint(1.0f, 2.0f, Color.blue,
  80. 3.0f, 4.0f, Color.yellow));
  81. assertFalse(plot1.equals(plot2));
  82. plot2.setDomainGridlinePaint(new GradientPaint(1.0f, 2.0f, Color.blue,
  83. 3.0f, 4.0f, Color.yellow));
  84. assertTrue(plot1.equals(plot2));
  85. Stroke s = new BasicStroke(1.5f);
  86. plot1.setDomainGridlineStroke(s);
  87. assertFalse(plot1.equals(plot2));
  88. plot2.setDomainGridlineStroke(s);
  89. assertTrue(plot1.equals(plot2));
  90. plot1.setRangeGridlinesVisible(false);
  91. assertFalse(plot1.equals(plot2));
  92. plot2.setRangeGridlinesVisible(false);
  93. assertTrue(plot1.equals(plot2));
  94. plot1.setRangeGridlinePaint(new GradientPaint(1.0f, 2.0f, Color.green,
  95. 3.0f, 4.0f, Color.yellow));
  96. assertFalse(plot1.equals(plot2));
  97. plot2.setRangeGridlinePaint(new GradientPaint(1.0f, 2.0f, Color.green,
  98. 3.0f, 4.0f, Color.yellow));
  99. assertTrue(plot1.equals(plot2));
  100. Stroke s2 = new BasicStroke(1.5f);
  101. plot1.setRangeGridlineStroke(s2);
  102. assertFalse(plot1.equals(plot2));
  103. plot2.setRangeGridlineStroke(s2);
  104. assertTrue(plot1.equals(plot2));
  105. plot1.setDomainPannable(true);
  106. assertFalse(plot1.equals(plot2));
  107. plot2.setDomainPannable(true);
  108. assertTrue(plot1.equals(plot2));
  109. plot1.setRangePannable(true);
  110. assertFalse(plot1.equals(plot2));
  111. plot2.setRangePannable(true);
  112. assertTrue(plot1.equals(plot2));
  113. }
  114. /**
  115. * Some tests for the data array equality in the equals() method.
  116. */
  117. @Test
  118. public void testEquals2() {
  119. FastScatterPlot plot1 = new FastScatterPlot();
  120. FastScatterPlot plot2 = new FastScatterPlot();
  121. assertTrue(plot1.equals(plot2));
  122. assertTrue(plot2.equals(plot1));
  123. float[][] a = new float[2][];
  124. float[][] b = new float[2][];
  125. plot1.setData(a);
  126. assertFalse(plot1.equals(plot2));
  127. plot2.setData(b);
  128. assertTrue(plot1.equals(plot2));
  129. a[0] = new float[6];
  130. assertFalse(plot1.equals(plot2));
  131. b[0] = new float[6];
  132. assertTrue(plot1.equals(plot2));
  133. a[0][0] = 1.0f;
  134. assertFalse(plot1.equals(plot2));
  135. b[0][0] = 1.0f;
  136. assertTrue(plot1.equals(plot2));
  137. a[0][1] = Float.NaN;
  138. assertFalse(plot1.equals(plot2));
  139. b[0][1] = Float.NaN;
  140. assertTrue(plot1.equals(plot2));
  141. a[0][2] = Float.POSITIVE_INFINITY;
  142. assertFalse(plot1.equals(plot2));
  143. b[0][2] = Float.POSITIVE_INFINITY;
  144. assertTrue(plot1.equals(plot2));
  145. a[0][3] = Float.NEGATIVE_INFINITY;
  146. assertFalse(plot1.equals(plot2));
  147. b[0][3] = Float.NEGATIVE_INFINITY;
  148. assertTrue(plot1.equals(plot2));
  149. }
  150. /**
  151. * Confirm that cloning works.
  152. */
  153. @Test
  154. public void testCloning() throws CloneNotSupportedException {
  155. FastScatterPlot p1 = new FastScatterPlot();
  156. FastScatterPlot p2 = (FastScatterPlot) p1.clone();
  157. assertTrue(p1 != p2);
  158. assertTrue(p1.getClass() == p2.getClass());
  159. assertTrue(p1.equals(p2));
  160. }
  161. /**
  162. * Serialize an instance, restore it, and check for equality.
  163. */
  164. @Test
  165. public void testSerialization() {
  166. float[][] data = createData();
  167. ValueAxis domainAxis = new NumberAxis("X");
  168. ValueAxis rangeAxis = new NumberAxis("Y");
  169. FastScatterPlot p1 = new FastScatterPlot(data, domainAxis, rangeAxis);
  170. FastScatterPlot p2 = (FastScatterPlot) TestUtilities.serialised(p1);
  171. assertEquals(p1, p2);
  172. }
  173. /**
  174. * Draws the chart with a <code>null</code> info object to make sure that
  175. * no exceptions are thrown.
  176. */
  177. @Test
  178. public void testDrawWithNullInfo() {
  179. try {
  180. float[][] data = createData();
  181. ValueAxis domainAxis = new NumberAxis("X");
  182. ValueAxis rangeAxis = new NumberAxis("Y");
  183. FastScatterPlot plot = new FastScatterPlot(data, domainAxis,
  184. rangeAxis);
  185. JFreeChart chart = new JFreeChart(plot);
  186. /* BufferedImage image = */ chart.createBufferedImage(300, 200,
  187. null);
  188. }
  189. catch (NullPointerException e) {
  190. fail("No exception should be thrown.");
  191. }
  192. }
  193. /**
  194. * Populates the data array with random values.
  195. *
  196. * @return Random data.
  197. */
  198. private float[][] createData() {
  199. float[][] result = new float[2][1000];
  200. for (int i = 0; i < result[0].length; i++) {
  201. float x = (float) i + 100;
  202. result[0][i] = x;
  203. result[1][i] = 100 + (float) Math.random() * 1000;
  204. }
  205. return result;
  206. }
  207. }