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 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. * StackedXYBarRendererTests.java
  26. * ------------------------------
  27. * (C) Copyright 2004, 2005, by Object Refinery Limited and Contributors.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: StackedXYBarRendererTests.java,v 1.3 2005/01/07 12:14:08 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 10-Sep-2004 : Version 1 (DG);
  37. * 06-Jan-2005 : Added test for auto range calculation (DG);
  38. *
  39. */
  40. package org.jfree.chart.renderer.xy.junit;
  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 junit.framework.Test;
  48. import junit.framework.TestCase;
  49. import junit.framework.TestSuite;
  50. import org.jfree.chart.ChartFactory;
  51. import org.jfree.chart.JFreeChart;
  52. import org.jfree.chart.axis.NumberAxis;
  53. import org.jfree.chart.plot.PlotOrientation;
  54. import org.jfree.chart.plot.XYPlot;
  55. import org.jfree.chart.renderer.xy.StackedXYBarRenderer;
  56. import org.jfree.data.Range;
  57. import org.jfree.data.xy.TableXYDataset;
  58. /**
  59. * Tests for the {@link StackedXYBarRenderer} class.
  60. */
  61. public class StackedXYBarRendererTests extends TestCase {
  62. /**
  63. * Returns the tests as a test suite.
  64. *
  65. * @return The test suite.
  66. */
  67. public static Test suite() {
  68. return new TestSuite(StackedXYBarRendererTests.class);
  69. }
  70. /**
  71. * Constructs a new set of tests.
  72. *
  73. * @param name the name of the tests.
  74. */
  75. public StackedXYBarRendererTests(String name) {
  76. super(name);
  77. }
  78. /**
  79. * Test that the equals() method distinguishes all fields.
  80. */
  81. public void testEquals() {
  82. StackedXYBarRenderer r1 = new StackedXYBarRenderer();
  83. StackedXYBarRenderer r2 = new StackedXYBarRenderer();
  84. assertTrue(r1.equals(r2));
  85. assertTrue(r2.equals(r1));
  86. }
  87. /**
  88. * Two objects that are equal are required to return the same hashCode.
  89. */
  90. public void testHashcode() {
  91. StackedXYBarRenderer r1 = new StackedXYBarRenderer();
  92. StackedXYBarRenderer r2 = new StackedXYBarRenderer();
  93. assertTrue(r1.equals(r2));
  94. int h1 = r1.hashCode();
  95. int h2 = r2.hashCode();
  96. assertEquals(h1, h2);
  97. }
  98. /**
  99. * Confirm that cloning works.
  100. */
  101. public void testCloning() {
  102. StackedXYBarRenderer r1 = new StackedXYBarRenderer();
  103. StackedXYBarRenderer r2 = null;
  104. try {
  105. r2 = (StackedXYBarRenderer) r1.clone();
  106. }
  107. catch (CloneNotSupportedException e) {
  108. System.err.println("Failed to clone.");
  109. }
  110. assertTrue(r1 != r2);
  111. assertTrue(r1.getClass() == r2.getClass());
  112. assertTrue(r1.equals(r2));
  113. }
  114. /**
  115. * Serialize an instance, restore it, and check for equality.
  116. */
  117. public void testSerialization() {
  118. StackedXYBarRenderer r1 = new StackedXYBarRenderer();
  119. StackedXYBarRenderer r2 = null;
  120. try {
  121. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  122. ObjectOutput out = new ObjectOutputStream(buffer);
  123. out.writeObject(r1);
  124. out.close();
  125. ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
  126. r2 = (StackedXYBarRenderer) in.readObject();
  127. in.close();
  128. }
  129. catch (Exception e) {
  130. System.out.println(e.toString());
  131. }
  132. assertEquals(r1, r2);
  133. }
  134. /**
  135. * Check that the renderer is calculating the domain bounds correctly.
  136. */
  137. public void testFindDomainBounds() {
  138. TableXYDataset dataset = RendererXYPackageTests.createTestTableXYDataset();
  139. JFreeChart chart = ChartFactory.createStackedXYAreaChart(
  140. "Test Chart", "X", "Y", dataset, PlotOrientation.VERTICAL, false, false, false
  141. );
  142. XYPlot plot = (XYPlot) chart.getPlot();
  143. plot.setRenderer(new StackedXYBarRenderer());
  144. NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
  145. domainAxis.setAutoRangeIncludesZero(false);
  146. Range bounds = domainAxis.getRange();
  147. assertFalse(bounds.contains(0.3));
  148. assertTrue(bounds.contains(0.5));
  149. assertTrue(bounds.contains(2.5));
  150. assertFalse(bounds.contains(2.8));
  151. }
  152. /**
  153. * Check that the renderer is calculating the range bounds correctly.
  154. */
  155. public void testFindRangeBounds() {
  156. TableXYDataset dataset = RendererXYPackageTests.createTestTableXYDataset();
  157. JFreeChart chart = ChartFactory.createStackedXYAreaChart(
  158. "Test Chart", "X", "Y", dataset, PlotOrientation.VERTICAL, false, false, false
  159. );
  160. XYPlot plot = (XYPlot) chart.getPlot();
  161. plot.setRenderer(new StackedXYBarRenderer());
  162. NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
  163. Range bounds = rangeAxis.getRange();
  164. assertTrue(bounds.contains(6.0));
  165. assertTrue(bounds.contains(8.0));
  166. }
  167. }