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. * XYBarRendererTests.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: XYBarRendererTests.java,v 1.3 2005/01/07 12:14:08 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 25-Mar-2003 : Version 1 (DG);
  37. * 22-Oct-2003 : Added hashCode test (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.XYBarRenderer;
  56. import org.jfree.data.Range;
  57. import org.jfree.data.xy.XYSeriesCollection;
  58. import org.jfree.ui.GradientPaintTransformType;
  59. import org.jfree.ui.StandardGradientPaintTransformer;
  60. /**
  61. * Tests for the {@link XYBarRenderer} class.
  62. */
  63. public class XYBarRendererTests extends TestCase {
  64. /**
  65. * Returns the tests as a test suite.
  66. *
  67. * @return The test suite.
  68. */
  69. public static Test suite() {
  70. return new TestSuite(XYBarRendererTests.class);
  71. }
  72. /**
  73. * Constructs a new set of tests.
  74. *
  75. * @param name the name of the tests.
  76. */
  77. public XYBarRendererTests(String name) {
  78. super(name);
  79. }
  80. /**
  81. * Test that the equals() method distinguishes all fields.
  82. */
  83. public void testEquals() {
  84. // default instances
  85. XYBarRenderer r1 = new XYBarRenderer();
  86. XYBarRenderer r2 = new XYBarRenderer();
  87. assertTrue(r1.equals(r2));
  88. assertTrue(r2.equals(r1));
  89. // setBase()
  90. r1.setBase(1.0);
  91. assertFalse(r1.equals(r2));
  92. r2.setBase(1.0);
  93. assertTrue(r1.equals(r2));
  94. // setUseYInterval
  95. r1.setUseYInterval(!r1.getUseYInterval());
  96. assertFalse(r1.equals(r2));
  97. r2.setUseYInterval(!r2.getUseYInterval());
  98. assertTrue(r1.equals(r2));
  99. // setMargin()
  100. r1.setMargin(0.10);
  101. assertFalse(r1.equals(r2));
  102. r2.setMargin(0.10);
  103. assertTrue(r1.equals(r2));
  104. // setDrawBarOutline()
  105. r1.setDrawBarOutline(!r1.isDrawBarOutline());
  106. assertFalse(r1.equals(r2));
  107. r2.setDrawBarOutline(!r2.isDrawBarOutline());
  108. assertTrue(r1.equals(r2));
  109. // setGradientPaintTransformer()
  110. r1.setGradientPaintTransformer(
  111. new StandardGradientPaintTransformer(
  112. GradientPaintTransformType.CENTER_HORIZONTAL
  113. )
  114. );
  115. assertFalse(r1.equals(r2));
  116. r2.setGradientPaintTransformer(
  117. new StandardGradientPaintTransformer(
  118. GradientPaintTransformType.CENTER_HORIZONTAL
  119. )
  120. );
  121. assertTrue(r1.equals(r2));
  122. }
  123. /**
  124. * Two objects that are equal are required to return the same hashCode.
  125. */
  126. public void testHashcode() {
  127. XYBarRenderer r1 = new XYBarRenderer();
  128. XYBarRenderer r2 = new XYBarRenderer();
  129. assertTrue(r1.equals(r2));
  130. int h1 = r1.hashCode();
  131. int h2 = r2.hashCode();
  132. assertEquals(h1, h2);
  133. }
  134. /**
  135. * Confirm that cloning works.
  136. */
  137. public void testCloning() {
  138. XYBarRenderer r1 = new XYBarRenderer();
  139. XYBarRenderer r2 = null;
  140. try {
  141. r2 = (XYBarRenderer) r1.clone();
  142. }
  143. catch (CloneNotSupportedException e) {
  144. System.err.println("XYBarRendererTests.testCloning: failed to clone.");
  145. }
  146. assertTrue(r1 != r2);
  147. assertTrue(r1.getClass() == r2.getClass());
  148. assertTrue(r1.equals(r2));
  149. }
  150. /**
  151. * Serialize an instance, restore it, and check for equality.
  152. */
  153. public void testSerialization() {
  154. XYBarRenderer r1 = new XYBarRenderer();
  155. XYBarRenderer r2 = null;
  156. try {
  157. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  158. ObjectOutput out = new ObjectOutputStream(buffer);
  159. out.writeObject(r1);
  160. out.close();
  161. ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
  162. r2 = (XYBarRenderer) in.readObject();
  163. in.close();
  164. }
  165. catch (Exception e) {
  166. System.out.println(e.toString());
  167. }
  168. assertEquals(r1, r2);
  169. }
  170. /**
  171. * Check that the renderer is calculating the domain bounds correctly.
  172. */
  173. public void testFindDomainBounds() {
  174. XYSeriesCollection dataset = RendererXYPackageTests.createTestXYSeriesCollection();
  175. JFreeChart chart = ChartFactory.createXYBarChart(
  176. "Test Chart", "X", false, "Y", dataset, PlotOrientation.VERTICAL, false, false, false
  177. );
  178. XYPlot plot = (XYPlot) chart.getPlot();
  179. NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
  180. domainAxis.setAutoRangeIncludesZero(false);
  181. Range bounds = domainAxis.getRange();
  182. System.out.println(bounds);
  183. assertFalse(bounds.contains(0.3));
  184. assertTrue(bounds.contains(0.5));
  185. assertTrue(bounds.contains(2.5));
  186. assertFalse(bounds.contains(2.8));
  187. }
  188. }