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