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. * JFreeChartTests.java
  26. * --------------------
  27. * (C) Copyright 2002-2004, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: JFreeChartTests.java,v 1.1 2004/08/31 14:35:34 mungady Exp $
  33. *
  34. * Changes:
  35. * --------
  36. * 11-Jun-2002 : Version 1 (DG);
  37. * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  38. * 23-Sep-2003 : Removed null title test, since TM has added code to ensure null titles
  39. * cannot be created (DG);
  40. *
  41. */
  42. package org.jfree.chart.junit;
  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.ChartFactory;
  53. import org.jfree.chart.JFreeChart;
  54. import org.jfree.chart.plot.PlotOrientation;
  55. import org.jfree.data.category.DefaultCategoryDataset;
  56. import org.jfree.data.general.DefaultPieDataset;
  57. import org.jfree.data.time.Day;
  58. import org.jfree.data.time.RegularTimePeriod;
  59. import org.jfree.data.time.TimeSeries;
  60. import org.jfree.data.time.TimeSeriesCollection;
  61. /**
  62. * Tests for the {@link JFreeChart} class.
  63. *
  64. */
  65. public class JFreeChartTests extends TestCase {
  66. /** A pie chart. */
  67. private JFreeChart pieChart;
  68. /**
  69. * Returns the tests as a test suite.
  70. *
  71. * @return the test suite.
  72. */
  73. public static Test suite() {
  74. return new TestSuite(JFreeChartTests.class);
  75. }
  76. /**
  77. * Constructs a new set of tests.
  78. *
  79. * @param name the name of the tests.
  80. */
  81. public JFreeChartTests(String name) {
  82. super(name);
  83. }
  84. /**
  85. * Common test setup.
  86. */
  87. protected void setUp() {
  88. // create a dataset...
  89. DefaultPieDataset data = new DefaultPieDataset();
  90. data.setValue("Java", new Double(43.2));
  91. data.setValue("Visual Basic", new Double(0.0));
  92. data.setValue("C/C++", new Double(17.5));
  93. // create the chart...
  94. this.pieChart = ChartFactory.createPieChart(
  95. "Pie Chart", // chart title
  96. data, // data
  97. true, // include legend
  98. true,
  99. false
  100. );
  101. }
  102. /**
  103. * Checks the subtitle count - should be 0.
  104. */
  105. public void testSubtitleCount() {
  106. int count = this.pieChart.getSubtitleCount();
  107. assertEquals(count, 0);
  108. }
  109. /**
  110. * Serialize a pie chart, restore it, and check for equality.
  111. */
  112. public void testSerialization1() {
  113. DefaultPieDataset data = new DefaultPieDataset();
  114. data.setValue("Type 1", 54.5);
  115. data.setValue("Type 2", 23.9);
  116. data.setValue("Type 3", 45.8);
  117. JFreeChart c1 = ChartFactory.createPieChart("Test", data, true, true, true);
  118. JFreeChart c2 = null;
  119. try {
  120. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  121. ObjectOutput out = new ObjectOutputStream(buffer);
  122. out.writeObject(c1);
  123. out.close();
  124. ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
  125. c2 = (JFreeChart) in.readObject();
  126. in.close();
  127. }
  128. catch (Exception e) {
  129. e.printStackTrace();
  130. }
  131. assertEquals(c1, c2);
  132. }
  133. /**
  134. * Serialize a 3D pie chart, restore it, and check for equality.
  135. */
  136. public void testSerialization2() {
  137. DefaultPieDataset data = new DefaultPieDataset();
  138. data.setValue("Type 1", 54.5);
  139. data.setValue("Type 2", 23.9);
  140. data.setValue("Type 3", 45.8);
  141. JFreeChart c1 = ChartFactory.createPieChart3D("Test", data, true, true, true);
  142. JFreeChart c2 = null;
  143. try {
  144. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  145. ObjectOutput out = new ObjectOutputStream(buffer);
  146. out.writeObject(c1);
  147. out.close();
  148. ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
  149. c2 = (JFreeChart) in.readObject();
  150. in.close();
  151. }
  152. catch (Exception e) {
  153. e.printStackTrace();
  154. }
  155. assertEquals(c1, c2);
  156. }
  157. /**
  158. * Serialize a bar chart, restore it, and check for equality.
  159. */
  160. public void testSerialization3() {
  161. // row keys...
  162. String series1 = "First";
  163. String series2 = "Second";
  164. String series3 = "Third";
  165. // column keys...
  166. String category1 = "Category 1";
  167. String category2 = "Category 2";
  168. String category3 = "Category 3";
  169. String category4 = "Category 4";
  170. String category5 = "Category 5";
  171. String category6 = "Category 6";
  172. String category7 = "Category 7";
  173. String category8 = "Category 8";
  174. // create the dataset...
  175. DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  176. dataset.addValue(1.0, series1, category1);
  177. dataset.addValue(4.0, series1, category2);
  178. dataset.addValue(3.0, series1, category3);
  179. dataset.addValue(5.0, series1, category4);
  180. dataset.addValue(5.0, series1, category5);
  181. dataset.addValue(7.0, series1, category6);
  182. dataset.addValue(7.0, series1, category7);
  183. dataset.addValue(8.0, series1, category8);
  184. dataset.addValue(5.0, series2, category1);
  185. dataset.addValue(7.0, series2, category2);
  186. dataset.addValue(6.0, series2, category3);
  187. dataset.addValue(8.0, series2, category4);
  188. dataset.addValue(4.0, series2, category5);
  189. dataset.addValue(4.0, series2, category6);
  190. dataset.addValue(2.0, series2, category7);
  191. dataset.addValue(1.0, series2, category8);
  192. dataset.addValue(4.0, series3, category1);
  193. dataset.addValue(3.0, series3, category2);
  194. dataset.addValue(2.0, series3, category3);
  195. dataset.addValue(3.0, series3, category4);
  196. dataset.addValue(6.0, series3, category5);
  197. dataset.addValue(3.0, series3, category6);
  198. dataset.addValue(4.0, series3, category7);
  199. dataset.addValue(3.0, series3, category8);
  200. // create the chart...
  201. JFreeChart c1 = ChartFactory.createBarChart(
  202. "Vertical Bar Chart", // chart title
  203. "Category", // domain axis label
  204. "Value", // range axis label
  205. dataset, // data
  206. PlotOrientation.VERTICAL, // orientation
  207. true, // include legend
  208. true,
  209. false
  210. );
  211. JFreeChart c2 = null;
  212. try {
  213. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  214. ObjectOutput out = new ObjectOutputStream(buffer);
  215. out.writeObject(c1);
  216. out.close();
  217. ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
  218. c2 = (JFreeChart) in.readObject();
  219. in.close();
  220. }
  221. catch (Exception e) {
  222. e.printStackTrace();
  223. }
  224. assertEquals(c1, c2);
  225. }
  226. /**
  227. * Serialize a time seroes chart, restore it, and check for equality.
  228. */
  229. public void testSerialization4() {
  230. RegularTimePeriod t = new Day();
  231. TimeSeries series = new TimeSeries("Series 1");
  232. series.add(t, 36.4);
  233. t = t.next();
  234. series.add(t, 63.5);
  235. TimeSeriesCollection dataset = new TimeSeriesCollection();
  236. dataset.addSeries(series);
  237. JFreeChart c1 = ChartFactory.createTimeSeriesChart(
  238. "Test", "Date", "Value", dataset, true, true, true
  239. );
  240. JFreeChart c2 = null;
  241. try {
  242. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  243. ObjectOutput out = new ObjectOutputStream(buffer);
  244. out.writeObject(c1);
  245. out.close();
  246. ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
  247. c2 = (JFreeChart) in.readObject();
  248. in.close();
  249. }
  250. catch (Exception e) {
  251. e.printStackTrace();
  252. }
  253. assertEquals(c1, c2);
  254. }
  255. }