1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2005, 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 License
  20. * along with this library; if not, write to the Free Software Foundation,
  21. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  24. * in the United States and other countries.]
  25. *
  26. * ------------------------------------
  27. * CombinedRangeCategoryPlotTests.java
  28. * ------------------------------------
  29. * (C) Copyright 2003, 2004, by Object Refinery Limited and Contributors.
  30. *
  31. * Original Author: David Gilbert (for Object Refinery Limited);
  32. * Contributor(s): -;
  33. *
  34. * $Id: CombinedRangeCategoryPlotTests.java,v 1.2 2005/02/21 12:43:28 mungady Exp $
  35. *
  36. * Changes
  37. * -------
  38. * 21-Aug-2003 : Version 1 (DG);
  39. *
  40. */
  41. package org.jfree.chart.plot.junit;
  42. import java.io.ByteArrayInputStream;
  43. import java.io.ByteArrayOutputStream;
  44. import java.io.ObjectInput;
  45. import java.io.ObjectInputStream;
  46. import java.io.ObjectOutput;
  47. import java.io.ObjectOutputStream;
  48. import java.util.List;
  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.labels.StandardCategoryToolTipGenerator;
  54. import org.jfree.chart.plot.CategoryPlot;
  55. import org.jfree.chart.plot.CombinedRangeCategoryPlot;
  56. import org.jfree.chart.renderer.category.BarRenderer;
  57. import org.jfree.chart.renderer.category.LineAndShapeRenderer;
  58. import org.jfree.data.category.CategoryDataset;
  59. import org.jfree.data.category.DefaultCategoryDataset;
  60. /**
  61. * Tests for the {@link CombinedRangeCategoryPlot} class.
  62. */
  63. public class CombinedRangeCategoryPlotTests extends TestCase {
  64. /**
  65. * Returns the tests as a test suite.
  66. *
  67. * @return The test suite.
  68. */
  69. public static Test suite() {
  70. return new TestSuite(CombinedRangeCategoryPlotTests.class);
  71. }
  72. /**
  73. * Constructs a new set of tests.
  74. *
  75. * @param name the name of the tests.
  76. */
  77. public CombinedRangeCategoryPlotTests(String name) {
  78. super(name);
  79. }
  80. /**
  81. * Test the equals() method.
  82. */
  83. public void testEquals() {
  84. CombinedRangeCategoryPlot plot1 = createPlot();
  85. CombinedRangeCategoryPlot plot2 = createPlot();
  86. assertTrue(plot1.equals(plot2));
  87. }
  88. /**
  89. * Confirm that cloning works.
  90. */
  91. public void testCloning() {
  92. CombinedRangeCategoryPlot plot1 = createPlot();
  93. CombinedRangeCategoryPlot plot2 = null;
  94. try {
  95. plot2 = (CombinedRangeCategoryPlot) plot1.clone();
  96. }
  97. catch (CloneNotSupportedException e) {
  98. System.err.println("Failed to clone.");
  99. }
  100. assertTrue(plot1 != plot2);
  101. assertTrue(plot1.getClass() == plot2.getClass());
  102. assertTrue(plot1.equals(plot2));
  103. }
  104. /**
  105. * Serialize an instance, restore it, and check for equality.
  106. */
  107. public void testSerialization() {
  108. CombinedRangeCategoryPlot plot1 = createPlot();
  109. CombinedRangeCategoryPlot plot2 = null;
  110. try {
  111. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  112. ObjectOutput out = new ObjectOutputStream(buffer);
  113. out.writeObject(plot1);
  114. out.close();
  115. ObjectInput in = new ObjectInputStream(
  116. new ByteArrayInputStream(buffer.toByteArray())
  117. );
  118. plot2 = (CombinedRangeCategoryPlot) in.readObject();
  119. in.close();
  120. }
  121. catch (Exception e) {
  122. e.printStackTrace();
  123. }
  124. assertEquals(plot1, plot2);
  125. }
  126. /**
  127. * This is a test to replicate the bug report 1121172.
  128. */
  129. public void testRemoveSubplot() {
  130. CombinedRangeCategoryPlot plot = new CombinedRangeCategoryPlot();
  131. CategoryPlot plot1 = new CategoryPlot();
  132. CategoryPlot plot2 = new CategoryPlot();
  133. CategoryPlot plot3 = new CategoryPlot();
  134. plot.add(plot1);
  135. plot.add(plot2);
  136. plot.add(plot3);
  137. plot.remove(plot2);
  138. List plots = plot.getSubplots();
  139. assertEquals(2, plots.size());
  140. }
  141. /**
  142. * Creates a dataset.
  143. *
  144. * @return A dataset.
  145. */
  146. public CategoryDataset createDataset1() {
  147. DefaultCategoryDataset result = new DefaultCategoryDataset();
  148. // row keys...
  149. String series1 = "First";
  150. String series2 = "Second";
  151. // column keys...
  152. String type1 = "Type 1";
  153. String type2 = "Type 2";
  154. String type3 = "Type 3";
  155. String type4 = "Type 4";
  156. String type5 = "Type 5";
  157. String type6 = "Type 6";
  158. String type7 = "Type 7";
  159. String type8 = "Type 8";
  160. result.addValue(1.0, series1, type1);
  161. result.addValue(4.0, series1, type2);
  162. result.addValue(3.0, series1, type3);
  163. result.addValue(5.0, series1, type4);
  164. result.addValue(5.0, series1, type5);
  165. result.addValue(7.0, series1, type6);
  166. result.addValue(7.0, series1, type7);
  167. result.addValue(8.0, series1, type8);
  168. result.addValue(5.0, series2, type1);
  169. result.addValue(7.0, series2, type2);
  170. result.addValue(6.0, series2, type3);
  171. result.addValue(8.0, series2, type4);
  172. result.addValue(4.0, series2, type5);
  173. result.addValue(4.0, series2, type6);
  174. result.addValue(2.0, series2, type7);
  175. result.addValue(1.0, series2, type8);
  176. return result;
  177. }
  178. /**
  179. * Creates a dataset.
  180. *
  181. * @return A dataset.
  182. */
  183. public CategoryDataset createDataset2() {
  184. DefaultCategoryDataset result = new DefaultCategoryDataset();
  185. // row keys...
  186. String series1 = "Third";
  187. String series2 = "Fourth";
  188. // column keys...
  189. String type1 = "Type 1";
  190. String type2 = "Type 2";
  191. String type3 = "Type 3";
  192. String type4 = "Type 4";
  193. String type5 = "Type 5";
  194. String type6 = "Type 6";
  195. String type7 = "Type 7";
  196. String type8 = "Type 8";
  197. result.addValue(11.0, series1, type1);
  198. result.addValue(14.0, series1, type2);
  199. result.addValue(13.0, series1, type3);
  200. result.addValue(15.0, series1, type4);
  201. result.addValue(15.0, series1, type5);
  202. result.addValue(17.0, series1, type6);
  203. result.addValue(17.0, series1, type7);
  204. result.addValue(18.0, series1, type8);
  205. result.addValue(15.0, series2, type1);
  206. result.addValue(17.0, series2, type2);
  207. result.addValue(16.0, series2, type3);
  208. result.addValue(18.0, series2, type4);
  209. result.addValue(14.0, series2, type5);
  210. result.addValue(14.0, series2, type6);
  211. result.addValue(12.0, series2, type7);
  212. result.addValue(11.0, series2, type8);
  213. return result;
  214. }
  215. /**
  216. * Creates a sample plot.
  217. *
  218. * @return A plot.
  219. */
  220. private CombinedRangeCategoryPlot createPlot() {
  221. CategoryDataset dataset1 = createDataset1();
  222. NumberAxis rangeAxis1 = new NumberAxis("Value");
  223. rangeAxis1.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
  224. LineAndShapeRenderer renderer1 = new LineAndShapeRenderer();
  225. renderer1.setBaseToolTipGenerator(
  226. new StandardCategoryToolTipGenerator()
  227. );
  228. CategoryPlot subplot1 = new CategoryPlot(
  229. dataset1, null, rangeAxis1, renderer1
  230. );
  231. subplot1.setDomainGridlinesVisible(true);
  232. CategoryDataset dataset2 = createDataset2();
  233. NumberAxis rangeAxis2 = new NumberAxis("Value");
  234. rangeAxis2.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
  235. BarRenderer renderer2 = new BarRenderer();
  236. renderer2.setBaseToolTipGenerator(
  237. new StandardCategoryToolTipGenerator()
  238. );
  239. CategoryPlot subplot2 = new CategoryPlot(
  240. dataset2, null, rangeAxis2, renderer2
  241. );
  242. subplot2.setDomainGridlinesVisible(true);
  243. NumberAxis rangeAxis = new NumberAxis("Value");
  244. CombinedRangeCategoryPlot plot
  245. = new CombinedRangeCategoryPlot(rangeAxis);
  246. plot.add(subplot1, 2);
  247. plot.add(subplot2, 1);
  248. return plot;
  249. }
  250. }