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