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. * CombinedRangeXYPlotTests.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: CombinedRangeXYPlotTests.java,v 1.1 2004/08/31 14:42:21 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 21-Aug-2003 : Version 1 (DG);
  37. *
  38. */
  39. package org.jfree.chart.plot.junit;
  40. import java.awt.Font;
  41. import java.io.ByteArrayInputStream;
  42. import java.io.ByteArrayOutputStream;
  43. import java.io.ObjectInput;
  44. import java.io.ObjectInputStream;
  45. import java.io.ObjectOutput;
  46. import java.io.ObjectOutputStream;
  47. import java.util.List;
  48. import junit.framework.Test;
  49. import junit.framework.TestCase;
  50. import junit.framework.TestSuite;
  51. import org.jfree.chart.annotations.XYTextAnnotation;
  52. import org.jfree.chart.axis.AxisLocation;
  53. import org.jfree.chart.axis.NumberAxis;
  54. import org.jfree.chart.plot.CombinedRangeXYPlot;
  55. import org.jfree.chart.plot.PlotOrientation;
  56. import org.jfree.chart.plot.XYPlot;
  57. import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
  58. import org.jfree.chart.renderer.xy.XYItemRenderer;
  59. import org.jfree.data.xy.XYDataset;
  60. import org.jfree.data.xy.XYSeries;
  61. import org.jfree.data.xy.XYSeriesCollection;
  62. /**
  63. * Tests for the {@link CombinedRangeXYPlot} class.
  64. */
  65. public class CombinedRangeXYPlotTests extends TestCase {
  66. /**
  67. * Returns the tests as a test suite.
  68. *
  69. * @return The test suite.
  70. */
  71. public static Test suite() {
  72. return new TestSuite(CombinedRangeXYPlotTests.class);
  73. }
  74. /**
  75. * Constructs a new set of tests.
  76. *
  77. * @param name the name of the tests.
  78. */
  79. public CombinedRangeXYPlotTests(String name) {
  80. super(name);
  81. }
  82. /**
  83. * Test the equals method.
  84. */
  85. public void testEquals() {
  86. CombinedRangeXYPlot plot1 = createPlot();
  87. CombinedRangeXYPlot plot2 = createPlot();
  88. assertTrue(plot1.equals(plot2));
  89. assertTrue(plot2.equals(plot1));
  90. }
  91. /**
  92. * This is a test to replicate the bug report 987080.
  93. */
  94. public void testRemoveSubplot() {
  95. CombinedRangeXYPlot plot = new CombinedRangeXYPlot();
  96. XYPlot plot1 = new XYPlot();
  97. XYPlot plot2 = new XYPlot();
  98. plot.add(plot1);
  99. plot.add(plot2);
  100. // remove plot2, but plot1 is removed instead
  101. plot.remove(plot2);
  102. List plots = plot.getSubplots();
  103. assertTrue(plots.get(0) == plot1);
  104. }
  105. /**
  106. * Confirm that cloning works.
  107. */
  108. public void testCloning() {
  109. CombinedRangeXYPlot plot1 = createPlot();
  110. CombinedRangeXYPlot plot2 = null;
  111. try {
  112. plot2 = (CombinedRangeXYPlot) plot1.clone();
  113. }
  114. catch (CloneNotSupportedException e) {
  115. System.err.println("CombinedRangeXYPlotTests.testCloning: failed to clone.");
  116. }
  117. assertTrue(plot1 != plot2);
  118. assertTrue(plot1.getClass() == plot2.getClass());
  119. assertTrue(plot1.equals(plot2));
  120. }
  121. /**
  122. * Serialize an instance, restore it, and check for equality.
  123. */
  124. public void testSerialization() {
  125. CombinedRangeXYPlot plot1 = createPlot();
  126. CombinedRangeXYPlot plot2 = null;
  127. try {
  128. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  129. ObjectOutput out = new ObjectOutputStream(buffer);
  130. out.writeObject(plot1);
  131. out.close();
  132. ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
  133. plot2 = (CombinedRangeXYPlot) in.readObject();
  134. in.close();
  135. }
  136. catch (Exception e) {
  137. e.printStackTrace();
  138. }
  139. assertEquals(plot1, plot2);
  140. }
  141. /**
  142. * Creates a sample dataset.
  143. *
  144. * @return Series 1.
  145. */
  146. private XYDataset createDataset1() {
  147. // create dataset 1...
  148. XYSeries series1 = new XYSeries("Series 1");
  149. series1.add(10.0, 12353.3);
  150. series1.add(20.0, 13734.4);
  151. series1.add(30.0, 14525.3);
  152. series1.add(40.0, 13984.3);
  153. series1.add(50.0, 12999.4);
  154. series1.add(60.0, 14274.3);
  155. series1.add(70.0, 15943.5);
  156. series1.add(80.0, 14845.3);
  157. series1.add(90.0, 14645.4);
  158. series1.add(100.0, 16234.6);
  159. series1.add(110.0, 17232.3);
  160. series1.add(120.0, 14232.2);
  161. series1.add(130.0, 13102.2);
  162. series1.add(140.0, 14230.2);
  163. series1.add(150.0, 11235.2);
  164. XYSeries series2 = new XYSeries("Series 2");
  165. series2.add(10.0, 15000.3);
  166. series2.add(20.0, 11000.4);
  167. series2.add(30.0, 17000.3);
  168. series2.add(40.0, 15000.3);
  169. series2.add(50.0, 14000.4);
  170. series2.add(60.0, 12000.3);
  171. series2.add(70.0, 11000.5);
  172. series2.add(80.0, 12000.3);
  173. series2.add(90.0, 13000.4);
  174. series2.add(100.0, 12000.6);
  175. series2.add(110.0, 13000.3);
  176. series2.add(120.0, 17000.2);
  177. series2.add(130.0, 18000.2);
  178. series2.add(140.0, 16000.2);
  179. series2.add(150.0, 17000.2);
  180. XYSeriesCollection collection = new XYSeriesCollection();
  181. collection.addSeries(series1);
  182. collection.addSeries(series2);
  183. return collection;
  184. }
  185. /**
  186. * Creates a sample dataset.
  187. *
  188. * @return Series 2.
  189. */
  190. private XYDataset createDataset2() {
  191. // create dataset 2...
  192. XYSeries series2 = new XYSeries("Series 3");
  193. series2.add(10.0, 16853.2);
  194. series2.add(20.0, 19642.3);
  195. series2.add(30.0, 18253.5);
  196. series2.add(40.0, 15352.3);
  197. series2.add(50.0, 13532.0);
  198. series2.add(100.0, 12635.3);
  199. series2.add(110.0, 13998.2);
  200. series2.add(120.0, 11943.2);
  201. series2.add(130.0, 16943.9);
  202. series2.add(140.0, 17843.2);
  203. series2.add(150.0, 16495.3);
  204. series2.add(160.0, 17943.6);
  205. series2.add(170.0, 18500.7);
  206. series2.add(180.0, 19595.9);
  207. return new XYSeriesCollection(series2);
  208. }
  209. /**
  210. * Creates a sample plot.
  211. *
  212. * @return A sample plot.
  213. */
  214. private CombinedRangeXYPlot createPlot() {
  215. // create subplot 1...
  216. XYDataset data1 = createDataset1();
  217. XYItemRenderer renderer1 = new StandardXYItemRenderer();
  218. NumberAxis rangeAxis1 = new NumberAxis("Range 1");
  219. XYPlot subplot1 = new XYPlot(data1, null, rangeAxis1, renderer1);
  220. subplot1.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
  221. XYTextAnnotation annotation = new XYTextAnnotation("Hello!", 50.0, 10000.0);
  222. annotation.setFont(new Font("SansSerif", Font.PLAIN, 9));
  223. annotation.setRotationAngle(Math.PI / 4.0);
  224. subplot1.addAnnotation(annotation);
  225. // create subplot 2...
  226. XYDataset data2 = createDataset2();
  227. XYItemRenderer renderer2 = new StandardXYItemRenderer();
  228. NumberAxis rangeAxis2 = new NumberAxis("Range 2");
  229. rangeAxis2.setAutoRangeIncludesZero(false);
  230. XYPlot subplot2 = new XYPlot(data2, null, rangeAxis2, renderer2);
  231. subplot2.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
  232. // parent plot...
  233. CombinedRangeXYPlot plot = new CombinedRangeXYPlot(new NumberAxis("Range"));
  234. plot.setGap(10.0);
  235. // add the subplots...
  236. plot.add(subplot1, 1);
  237. plot.add(subplot2, 1);
  238. plot.setOrientation(PlotOrientation.VERTICAL);
  239. return plot;
  240. }
  241. }