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. * CategoryPlotTests.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: CategoryPlotTests.java,v 1.2 2005/02/22 12:55:35 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 26-Mar-2003 : Version 1 (DG);
  37. * 15-Sep-2003 : Added a unit test to reproduce a bug in serialization (now fixed) (DG);
  38. *
  39. */
  40. package org.jfree.chart.plot.junit;
  41. import java.awt.BasicStroke;
  42. import java.awt.Color;
  43. import java.awt.Stroke;
  44. import java.io.ByteArrayInputStream;
  45. import java.io.ByteArrayOutputStream;
  46. import java.io.ObjectInput;
  47. import java.io.ObjectInputStream;
  48. import java.io.ObjectOutput;
  49. import java.io.ObjectOutputStream;
  50. import junit.framework.Test;
  51. import junit.framework.TestCase;
  52. import junit.framework.TestSuite;
  53. import org.jfree.chart.ChartFactory;
  54. import org.jfree.chart.JFreeChart;
  55. import org.jfree.chart.annotations.CategoryTextAnnotation;
  56. import org.jfree.chart.axis.AxisLocation;
  57. import org.jfree.chart.axis.AxisSpace;
  58. import org.jfree.chart.axis.CategoryAnchor;
  59. import org.jfree.chart.axis.CategoryAxis;
  60. import org.jfree.chart.axis.NumberAxis;
  61. import org.jfree.chart.plot.CategoryPlot;
  62. import org.jfree.chart.plot.DatasetRenderingOrder;
  63. import org.jfree.chart.plot.PlotOrientation;
  64. import org.jfree.chart.plot.ValueMarker;
  65. import org.jfree.chart.renderer.category.AreaRenderer;
  66. import org.jfree.chart.renderer.category.BarRenderer;
  67. import org.jfree.chart.renderer.category.LineAndShapeRenderer;
  68. import org.jfree.data.Range;
  69. import org.jfree.data.category.DefaultCategoryDataset;
  70. import org.jfree.ui.Layer;
  71. import org.jfree.ui.RectangleInsets;
  72. import org.jfree.util.SortOrder;
  73. /**
  74. * Tests for the {@link CategoryPlot} class.
  75. */
  76. public class CategoryPlotTests extends TestCase {
  77. /**
  78. * Returns the tests as a test suite.
  79. *
  80. * @return The test suite.
  81. */
  82. public static Test suite() {
  83. return new TestSuite(CategoryPlotTests.class);
  84. }
  85. /**
  86. * Constructs a new set of tests.
  87. *
  88. * @param name the name of the tests.
  89. */
  90. public CategoryPlotTests(String name) {
  91. super(name);
  92. }
  93. /**
  94. * A test for a bug reported in the forum.
  95. */
  96. public void testAxisRange() {
  97. DefaultCategoryDataset datasetA = new DefaultCategoryDataset();
  98. DefaultCategoryDataset datasetB = new DefaultCategoryDataset();
  99. datasetB.addValue(50.0, "R1", "C1");
  100. datasetB.addValue(80.0, "R1", "C1");
  101. CategoryPlot plot = new CategoryPlot(
  102. datasetA, new CategoryAxis(null), new NumberAxis(null), new LineAndShapeRenderer()
  103. );
  104. plot.setDataset(1, datasetB);
  105. plot.setRenderer(1, new LineAndShapeRenderer());
  106. Range r = plot.getRangeAxis().getRange();
  107. assertEquals(84.0, r.getUpperBound(), 0.00001);
  108. }
  109. /**
  110. * Test that the equals() method differentiates all the required fields.
  111. */
  112. public void testEquals() {
  113. CategoryPlot plot1 = new CategoryPlot();
  114. CategoryPlot plot2 = new CategoryPlot();
  115. assertTrue(plot1.equals(plot2));
  116. assertTrue(plot2.equals(plot1));
  117. // orientation...
  118. plot1.setOrientation(PlotOrientation.HORIZONTAL);
  119. assertFalse(plot1.equals(plot2));
  120. plot2.setOrientation(PlotOrientation.HORIZONTAL);
  121. assertTrue(plot1.equals(plot2));
  122. // axisOffset...
  123. plot1.setAxisOffset(new RectangleInsets(0.05, 0.05, 0.05, 0.05));
  124. assertFalse(plot1.equals(plot2));
  125. plot2.setAxisOffset(new RectangleInsets(0.05, 0.05, 0.05, 0.05));
  126. assertTrue(plot1.equals(plot2));
  127. // domainAxis - no longer a separate field but test anyway...
  128. plot1.setDomainAxis(new CategoryAxis("Category Axis"));
  129. assertFalse(plot1.equals(plot2));
  130. plot2.setDomainAxis(new CategoryAxis("Category Axis"));
  131. assertTrue(plot1.equals(plot2));
  132. // domainAxes...
  133. plot1.setDomainAxis(11, new CategoryAxis("Secondary Axis"));
  134. assertFalse(plot1.equals(plot2));
  135. plot2.setDomainAxis(11, new CategoryAxis("Secondary Axis"));
  136. assertTrue(plot1.equals(plot2));
  137. // domainAxisLocation - no longer a separate field but test anyway...
  138. plot1.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
  139. assertFalse(plot1.equals(plot2));
  140. plot2.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
  141. assertTrue(plot1.equals(plot2));
  142. // domainAxisLocations...
  143. plot1.setDomainAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
  144. assertFalse(plot1.equals(plot2));
  145. plot2.setDomainAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
  146. assertTrue(plot1.equals(plot2));
  147. // draw shared domain axis...
  148. plot1.setDrawSharedDomainAxis(!plot1.getDrawSharedDomainAxis());
  149. assertFalse(plot1.equals(plot2));
  150. plot2.setDrawSharedDomainAxis(!plot2.getDrawSharedDomainAxis());
  151. assertTrue(plot1.equals(plot2));
  152. // rangeAxis - no longer a separate field but test anyway...
  153. plot1.setRangeAxis(new NumberAxis("Range Axis"));
  154. assertFalse(plot1.equals(plot2));
  155. plot2.setRangeAxis(new NumberAxis("Range Axis"));
  156. assertTrue(plot1.equals(plot2));
  157. // rangeAxes...
  158. plot1.setRangeAxis(11, new NumberAxis("Secondary Range Axis"));
  159. assertFalse(plot1.equals(plot2));
  160. plot2.setRangeAxis(11, new NumberAxis("Secondary Range Axis"));
  161. assertTrue(plot1.equals(plot2));
  162. // rangeAxisLocation - no longer a separate field but test anyway...
  163. plot1.setRangeAxisLocation(AxisLocation.TOP_OR_RIGHT);
  164. assertFalse(plot1.equals(plot2));
  165. plot2.setRangeAxisLocation(AxisLocation.TOP_OR_RIGHT);
  166. assertTrue(plot1.equals(plot2));
  167. // rangeAxisLocations...
  168. plot1.setRangeAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
  169. assertFalse(plot1.equals(plot2));
  170. plot2.setRangeAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
  171. assertTrue(plot1.equals(plot2));
  172. // datasetToDomainAxisMap...
  173. plot1.mapDatasetToDomainAxis(11, 11);
  174. assertFalse(plot1.equals(plot2));
  175. plot2.mapDatasetToDomainAxis(11, 11);
  176. assertTrue(plot1.equals(plot2));
  177. // datasetToRangeAxisMap...
  178. plot1.mapDatasetToRangeAxis(11, 11);
  179. assertFalse(plot1.equals(plot2));
  180. plot2.mapDatasetToRangeAxis(11, 11);
  181. assertTrue(plot1.equals(plot2));
  182. // renderer - no longer a separate field but test anyway...
  183. plot1.setRenderer(new AreaRenderer());
  184. assertFalse(plot1.equals(plot2));
  185. plot2.setRenderer(new AreaRenderer());
  186. assertTrue(plot1.equals(plot2));
  187. // renderers...
  188. plot1.setRenderer(11, new AreaRenderer());
  189. assertFalse(plot1.equals(plot2));
  190. plot2.setRenderer(11, new AreaRenderer());
  191. assertTrue(plot1.equals(plot2));
  192. // rendering order...
  193. plot1.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
  194. assertFalse(plot1.equals(plot2));
  195. plot2.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
  196. assertTrue(plot1.equals(plot2));
  197. // columnRenderingOrder...
  198. plot1.setColumnRenderingOrder(SortOrder.DESCENDING);
  199. assertFalse(plot1.equals(plot2));
  200. plot2.setColumnRenderingOrder(SortOrder.DESCENDING);
  201. assertTrue(plot1.equals(plot2));
  202. // rowRenderingOrder...
  203. plot1.setRowRenderingOrder(SortOrder.DESCENDING);
  204. assertFalse(plot1.equals(plot2));
  205. plot2.setRowRenderingOrder(SortOrder.DESCENDING);
  206. assertTrue(plot1.equals(plot2));
  207. // domainGridlinesVisible
  208. plot1.setDomainGridlinesVisible(true);
  209. assertFalse(plot1.equals(plot2));
  210. plot2.setDomainGridlinesVisible(true);
  211. assertTrue(plot1.equals(plot2));
  212. // domainGridlinePosition
  213. plot1.setDomainGridlinePosition(CategoryAnchor.END);
  214. assertFalse(plot1.equals(plot2));
  215. plot2.setDomainGridlinePosition(CategoryAnchor.END);
  216. assertTrue(plot1.equals(plot2));
  217. // domainGridlineStroke
  218. Stroke stroke = new BasicStroke(2.0f);
  219. plot1.setDomainGridlineStroke(stroke);
  220. assertFalse(plot1.equals(plot2));
  221. plot2.setDomainGridlineStroke(stroke);
  222. assertTrue(plot1.equals(plot2));
  223. // domainGridlinePaint
  224. plot1.setDomainGridlinePaint(Color.blue);
  225. assertFalse(plot1.equals(plot2));
  226. plot2.setDomainGridlinePaint(Color.blue);
  227. assertTrue(plot1.equals(plot2));
  228. // rangeGridlinesVisible
  229. plot1.setRangeGridlinesVisible(false);
  230. assertFalse(plot1.equals(plot2));
  231. plot2.setRangeGridlinesVisible(false);
  232. assertTrue(plot1.equals(plot2));
  233. // rangeGridlineStroke
  234. plot1.setRangeGridlineStroke(stroke);
  235. assertFalse(plot1.equals(plot2));
  236. plot2.setRangeGridlineStroke(stroke);
  237. assertTrue(plot1.equals(plot2));
  238. // rangeGridlinePaint
  239. plot1.setRangeGridlinePaint(Color.blue);
  240. assertFalse(plot1.equals(plot2));
  241. plot2.setRangeGridlinePaint(Color.blue);
  242. assertTrue(plot1.equals(plot2));
  243. // anchorValue
  244. plot1.setAnchorValue(100.0);
  245. assertFalse(plot1.equals(plot2));
  246. plot2.setAnchorValue(100.0);
  247. assertTrue(plot1.equals(plot2));
  248. // rangeCrosshairVisible
  249. plot1.setRangeCrosshairVisible(true);
  250. assertFalse(plot1.equals(plot2));
  251. plot2.setRangeCrosshairVisible(true);
  252. assertTrue(plot1.equals(plot2));
  253. // rangeCrosshairValue
  254. plot1.setRangeCrosshairValue(100.0);
  255. assertFalse(plot1.equals(plot2));
  256. plot2.setRangeCrosshairValue(100.0);
  257. assertTrue(plot1.equals(plot2));
  258. // rangeCrosshairStroke
  259. plot1.setRangeCrosshairStroke(stroke);
  260. assertFalse(plot1.equals(plot2));
  261. plot2.setRangeCrosshairStroke(stroke);
  262. assertTrue(plot1.equals(plot2));
  263. // rangeCrosshairPaint
  264. plot1.setRangeCrosshairPaint(Color.blue);
  265. assertFalse(plot1.equals(plot2));
  266. plot2.setRangeCrosshairPaint(Color.blue);
  267. assertTrue(plot1.equals(plot2));
  268. // rangeCrosshairLockedOnData
  269. plot1.setRangeCrosshairLockedOnData(false);
  270. assertFalse(plot1.equals(plot2));
  271. plot2.setRangeCrosshairLockedOnData(false);
  272. assertTrue(plot1.equals(plot2));
  273. // range markers - no longer separate fields but test anyway...
  274. plot1.addRangeMarker(new ValueMarker(4.0), Layer.FOREGROUND);
  275. assertFalse(plot1.equals(plot2));
  276. plot2.addRangeMarker(new ValueMarker(4.0), Layer.FOREGROUND);
  277. assertTrue(plot1.equals(plot2));
  278. plot1.addRangeMarker(new ValueMarker(5.0), Layer.BACKGROUND);
  279. assertFalse(plot1.equals(plot2));
  280. plot2.addRangeMarker(new ValueMarker(5.0), Layer.BACKGROUND);
  281. assertTrue(plot1.equals(plot2));
  282. // foreground range markers...
  283. plot1.addRangeMarker(1, new ValueMarker(4.0), Layer.FOREGROUND);
  284. assertFalse(plot1.equals(plot2));
  285. plot2.addRangeMarker(1, new ValueMarker(4.0), Layer.FOREGROUND);
  286. assertTrue(plot1.equals(plot2));
  287. // background range markers...
  288. plot1.addRangeMarker(1, new ValueMarker(5.0), Layer.BACKGROUND);
  289. assertFalse(plot1.equals(plot2));
  290. plot2.addRangeMarker(1, new ValueMarker(5.0), Layer.BACKGROUND);
  291. assertTrue(plot1.equals(plot2));
  292. // annotations
  293. plot1.addAnnotation(new CategoryTextAnnotation("Text", "Category", 43.0));
  294. assertFalse(plot1.equals(plot2));
  295. plot2.addAnnotation(new CategoryTextAnnotation("Text", "Category", 43.0));
  296. assertTrue(plot1.equals(plot2));
  297. // weight
  298. plot1.setWeight(3);
  299. assertFalse(plot1.equals(plot2));
  300. plot2.setWeight(3);
  301. assertTrue(plot1.equals(plot2));
  302. // fixed domain axis space...
  303. plot1.setFixedDomainAxisSpace(new AxisSpace());
  304. assertFalse(plot1.equals(plot2));
  305. plot2.setFixedDomainAxisSpace(new AxisSpace());
  306. assertTrue(plot1.equals(plot2));
  307. // fixed range axis space...
  308. plot1.setFixedRangeAxisSpace(new AxisSpace());
  309. assertFalse(plot1.equals(plot2));
  310. plot2.setFixedRangeAxisSpace(new AxisSpace());
  311. assertTrue(plot1.equals(plot2));
  312. }
  313. /**
  314. * Confirm that cloning works.
  315. */
  316. public void testCloning() {
  317. CategoryPlot p1 = new CategoryPlot();
  318. CategoryPlot p2 = null;
  319. try {
  320. p2 = (CategoryPlot) p1.clone();
  321. }
  322. catch (CloneNotSupportedException e) {
  323. e.printStackTrace();
  324. System.err.println("CategoryPlotTests.testCloning: failed to clone.");
  325. }
  326. assertTrue(p1 != p2);
  327. assertTrue(p1.getClass() == p2.getClass());
  328. assertTrue(p1.equals(p2));
  329. }
  330. /**
  331. * Serialize an instance, restore it, and check for equality.
  332. */
  333. public void testSerialization() {
  334. DefaultCategoryDataset data = new DefaultCategoryDataset();
  335. CategoryAxis domainAxis = new CategoryAxis("Domain");
  336. NumberAxis rangeAxis = new NumberAxis("Range");
  337. BarRenderer renderer = new BarRenderer();
  338. CategoryPlot p1 = new CategoryPlot(data, domainAxis, rangeAxis, renderer);
  339. p1.setOrientation(PlotOrientation.HORIZONTAL);
  340. CategoryPlot p2 = null;
  341. try {
  342. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  343. ObjectOutput out = new ObjectOutputStream(buffer);
  344. out.writeObject(p1);
  345. out.close();
  346. ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
  347. p2 = (CategoryPlot) in.readObject();
  348. in.close();
  349. }
  350. catch (Exception e) {
  351. e.printStackTrace();
  352. }
  353. assertEquals(p1, p2);
  354. }
  355. /**
  356. * Serialize an instance, restore it, and check for equality.
  357. */
  358. public void testSerialization2() {
  359. DefaultCategoryDataset data = new DefaultCategoryDataset();
  360. CategoryAxis domainAxis = new CategoryAxis("Domain");
  361. NumberAxis rangeAxis = new NumberAxis("Range");
  362. BarRenderer renderer = new BarRenderer();
  363. CategoryPlot p1 = new CategoryPlot(data, domainAxis, rangeAxis, renderer);
  364. p1.setOrientation(PlotOrientation.VERTICAL);
  365. CategoryPlot p2 = null;
  366. try {
  367. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  368. ObjectOutput out = new ObjectOutputStream(buffer);
  369. out.writeObject(p1);
  370. out.close();
  371. ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
  372. p2 = (CategoryPlot) in.readObject();
  373. in.close();
  374. }
  375. catch (Exception e) {
  376. System.out.println(e.toString());
  377. }
  378. assertEquals(p1, p2);
  379. }
  380. /**
  381. * Serialize an instance, restore it, and check for equality.
  382. */
  383. public void testSerialization3() {
  384. DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  385. JFreeChart chart = ChartFactory.createBarChart(
  386. "Test Chart",
  387. "Category Axis",
  388. "Value Axis",
  389. dataset,
  390. PlotOrientation.VERTICAL,
  391. true,
  392. true,
  393. false
  394. );
  395. JFreeChart chart2 = null;
  396. // serialize and deserialize the chart....
  397. try {
  398. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  399. ObjectOutput out = new ObjectOutputStream(buffer);
  400. out.writeObject(chart);
  401. out.close();
  402. ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
  403. chart2 = (JFreeChart) in.readObject();
  404. in.close();
  405. }
  406. catch (Exception e) {
  407. System.out.println(e.toString());
  408. }
  409. // now check that the chart is usable...
  410. boolean passed = true;
  411. try {
  412. chart2.createBufferedImage(300, 200);
  413. }
  414. catch (Exception e) {
  415. passed = false;
  416. e.printStackTrace();
  417. }
  418. assertTrue(passed);
  419. }
  420. }