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