1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2004, 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 under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * -------------------------
  25. * FastScatterPlotTests.java
  26. * -------------------------
  27. * (C) Copyright 2003, 2004, by Object Refinery Limited and Contributors.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: FastScatterPlotTests.java,v 1.1 2004/08/31 14:42:21 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 18-Mar-2003 : Version 1 (DG);
  37. *
  38. */
  39. package org.jfree.chart.plot.junit;
  40. import java.awt.BasicStroke;
  41. import java.awt.Color;
  42. import java.awt.Stroke;
  43. import java.io.ByteArrayInputStream;
  44. import java.io.ByteArrayOutputStream;
  45. import java.io.ObjectInput;
  46. import java.io.ObjectInputStream;
  47. import java.io.ObjectOutput;
  48. import java.io.ObjectOutputStream;
  49. import junit.framework.Test;
  50. import junit.framework.TestCase;
  51. import junit.framework.TestSuite;
  52. import org.jfree.chart.axis.NumberAxis;
  53. import org.jfree.chart.axis.ValueAxis;
  54. import org.jfree.chart.plot.FastScatterPlot;
  55. /**
  56. * Tests for the {@link FastScatterPlot} class.
  57. */
  58. public class FastScatterPlotTests extends TestCase {
  59. /**
  60. * Returns the tests as a test suite.
  61. *
  62. * @return The test suite.
  63. */
  64. public static Test suite() {
  65. return new TestSuite(FastScatterPlotTests.class);
  66. }
  67. /**
  68. * Constructs a new set of tests.
  69. *
  70. * @param name the name of the tests.
  71. */
  72. public FastScatterPlotTests(String name) {
  73. super(name);
  74. }
  75. /**
  76. * Test the equals() method.
  77. */
  78. public void testEquals() {
  79. FastScatterPlot plot1 = new FastScatterPlot();
  80. FastScatterPlot plot2 = new FastScatterPlot();
  81. assertTrue(plot1.equals(plot2));
  82. assertTrue(plot2.equals(plot1));
  83. plot1.setPaint(Color.yellow);
  84. assertFalse(plot1.equals(plot2));
  85. plot2.setPaint(Color.yellow);
  86. assertTrue(plot1.equals(plot2));
  87. plot1.setDomainGridlinesVisible(false);
  88. assertFalse(plot1.equals(plot2));
  89. plot2.setDomainGridlinesVisible(false);
  90. assertTrue(plot1.equals(plot2));
  91. plot1.setDomainGridlinePaint(Color.red);
  92. assertFalse(plot1.equals(plot2));
  93. plot2.setDomainGridlinePaint(Color.red);
  94. assertTrue(plot1.equals(plot2));
  95. Stroke s = new BasicStroke(1.5f);
  96. plot1.setDomainGridlineStroke(s);
  97. assertFalse(plot1.equals(plot2));
  98. plot2.setDomainGridlineStroke(s);
  99. assertTrue(plot1.equals(plot2));
  100. plot1.setRangeGridlinesVisible(false);
  101. assertFalse(plot1.equals(plot2));
  102. plot2.setRangeGridlinesVisible(false);
  103. assertTrue(plot1.equals(plot2));
  104. plot1.setRangeGridlinePaint(Color.red);
  105. assertFalse(plot1.equals(plot2));
  106. plot2.setRangeGridlinePaint(Color.red);
  107. assertTrue(plot1.equals(plot2));
  108. Stroke s2 = new BasicStroke(1.5f);
  109. plot1.setRangeGridlineStroke(s2);
  110. assertFalse(plot1.equals(plot2));
  111. plot2.setRangeGridlineStroke(s2);
  112. assertTrue(plot1.equals(plot2));
  113. }
  114. /**
  115. * Confirm that cloning works.
  116. */
  117. public void testCloning() {
  118. FastScatterPlot p1 = new FastScatterPlot();
  119. FastScatterPlot p2 = null;
  120. try {
  121. p2 = (FastScatterPlot) p1.clone();
  122. }
  123. catch (CloneNotSupportedException e) {
  124. e.printStackTrace();
  125. System.err.println("FastScatterPlotTests.testCloning: failed to clone.");
  126. }
  127. assertTrue(p1 != p2);
  128. assertTrue(p1.getClass() == p2.getClass());
  129. assertTrue(p1.equals(p2));
  130. }
  131. /**
  132. * Serialize an instance, restore it, and check for equality.
  133. */
  134. public void testSerialization() {
  135. float[][] data = createData();
  136. ValueAxis domainAxis = new NumberAxis("X");
  137. ValueAxis rangeAxis = new NumberAxis("Y");
  138. FastScatterPlot p1 = new FastScatterPlot(data, domainAxis, rangeAxis);
  139. FastScatterPlot p2 = null;
  140. try {
  141. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  142. ObjectOutput out = new ObjectOutputStream(buffer);
  143. out.writeObject(p1);
  144. out.close();
  145. ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
  146. p2 = (FastScatterPlot) in.readObject();
  147. in.close();
  148. }
  149. catch (Exception e) {
  150. System.out.println(e.toString());
  151. }
  152. assertEquals(p1, p2);
  153. }
  154. /**
  155. * Populates the data array with random values.
  156. *
  157. * @return Random data.
  158. */
  159. private float[][] createData() {
  160. float[][] result = new float[2][1000];
  161. for (int i = 0; i < result[0].length; i++) {
  162. float x = (float) i + 100;
  163. result[0][i] = x;
  164. result[1][i] = 100 + (float) Math.random() * 1000;
  165. }
  166. return result;
  167. }
  168. }