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