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
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this library; if not, write to the Free Software Foundation,
  21. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  24. * in the United States and other countries.]
  25. *
  26. * ---------------------
  27. * BarRendererTests.java
  28. * ---------------------
  29. * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
  30. *
  31. * Original Author: David Gilbert (for Object Refinery Limited);
  32. * Contributor(s): -;
  33. *
  34. * $Id: BarRendererTests.java,v 1.3 2005/03/08 10:35:26 mungady Exp $
  35. *
  36. * Changes
  37. * -------
  38. * 25-Mar-2003 : Version 1 (DG);
  39. * 19-Aug-2003 : Renamed HorizontalBarRendererTests --> BarRendererTests (DG);
  40. * 22-Oct-2003 : Added hashCode test (DG);
  41. *
  42. */
  43. package org.jfree.chart.renderer.category.junit;
  44. import java.awt.Color;
  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.labels.ItemLabelAnchor;
  55. import org.jfree.chart.labels.ItemLabelPosition;
  56. import org.jfree.chart.labels.StandardCategoryLabelGenerator;
  57. import org.jfree.chart.renderer.category.BarRenderer;
  58. import org.jfree.chart.renderer.junit.RendererChangeDetector;
  59. import org.jfree.ui.GradientPaintTransformType;
  60. import org.jfree.ui.StandardGradientPaintTransformer;
  61. import org.jfree.ui.TextAnchor;
  62. /**
  63. * Tests for the {@link BarRenderer} class.
  64. */
  65. public class BarRendererTests 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(BarRendererTests.class);
  73. }
  74. /**
  75. * Constructs a new set of tests.
  76. *
  77. * @param name the name of the tests.
  78. */
  79. public BarRendererTests(String name) {
  80. super(name);
  81. }
  82. /**
  83. * Test that the equals() method distinguishes all fields.
  84. */
  85. public void testEquals() {
  86. BarRenderer r1 = new BarRenderer();
  87. BarRenderer r2 = new BarRenderer();
  88. assertTrue(r1.equals(r2));
  89. assertTrue(r2.equals(r1));
  90. // itemMargin
  91. r1.setItemMargin(0.22);
  92. assertFalse(r1.equals(r2));
  93. r2.setItemMargin(0.22);
  94. assertTrue(r1.equals(r2));
  95. // drawBarOutline
  96. r1.setDrawBarOutline(!r1.isDrawBarOutline());
  97. assertFalse(r1.equals(r2));
  98. r2.setDrawBarOutline(!r2.isDrawBarOutline());
  99. assertTrue(r1.equals(r2));
  100. // maxBarWidth
  101. r1.setMaxBarWidth(0.11);
  102. assertFalse(r1.equals(r2));
  103. r2.setMaxBarWidth(0.11);
  104. assertTrue(r1.equals(r2));
  105. // minimumBarLength
  106. r1.setMinimumBarLength(0.04);
  107. assertFalse(r1.equals(r2));
  108. r2.setMinimumBarLength(0.04);
  109. assertTrue(r1.equals(r2));
  110. // gradientPaintTransformer
  111. r1.setGradientPaintTransformer(
  112. new StandardGradientPaintTransformer(
  113. GradientPaintTransformType.CENTER_VERTICAL
  114. )
  115. );
  116. assertFalse(r1.equals(r2));
  117. r2.setGradientPaintTransformer(
  118. new StandardGradientPaintTransformer(
  119. GradientPaintTransformType.CENTER_VERTICAL
  120. )
  121. );
  122. assertTrue(r1.equals(r2));
  123. // positiveItemLabelPositionFallback
  124. r1.setPositiveItemLabelPositionFallback(
  125. new ItemLabelPosition(ItemLabelAnchor.INSIDE1, TextAnchor.CENTER)
  126. );
  127. assertFalse(r1.equals(r2));
  128. r2.setPositiveItemLabelPositionFallback(
  129. new ItemLabelPosition(ItemLabelAnchor.INSIDE1, TextAnchor.CENTER)
  130. );
  131. assertTrue(r1.equals(r2));
  132. // negativeItemLabelPositionFallback
  133. r1.setNegativeItemLabelPositionFallback(
  134. new ItemLabelPosition(ItemLabelAnchor.INSIDE1, TextAnchor.CENTER)
  135. );
  136. assertFalse(r1.equals(r2));
  137. r2.setNegativeItemLabelPositionFallback(
  138. new ItemLabelPosition(ItemLabelAnchor.INSIDE1, TextAnchor.CENTER)
  139. );
  140. assertTrue(r1.equals(r2));
  141. }
  142. /**
  143. * Two objects that are equal are required to return the same hashCode.
  144. */
  145. public void testHashcode() {
  146. BarRenderer r1 = new BarRenderer();
  147. BarRenderer r2 = new BarRenderer();
  148. assertTrue(r1.equals(r2));
  149. int h1 = r1.hashCode();
  150. int h2 = r2.hashCode();
  151. assertEquals(h1, h2);
  152. }
  153. /**
  154. * Confirm that cloning works.
  155. */
  156. public void testCloning() {
  157. BarRenderer r1 = new BarRenderer();
  158. r1.setLabelGenerator(new StandardCategoryLabelGenerator());
  159. BarRenderer r2 = null;
  160. try {
  161. r2 = (BarRenderer) r1.clone();
  162. }
  163. catch (CloneNotSupportedException e) {
  164. System.err.println("Failed to clone.");
  165. }
  166. assertTrue(r1 != r2);
  167. assertTrue(r1.getClass() == r2.getClass());
  168. assertTrue(r1.equals(r2));
  169. }
  170. /**
  171. * Serialize an instance, restore it, and check for equality.
  172. */
  173. public void testSerialization() {
  174. BarRenderer r1 = new BarRenderer();
  175. BarRenderer r2 = null;
  176. try {
  177. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  178. ObjectOutput out = new ObjectOutputStream(buffer);
  179. out.writeObject(r1);
  180. out.close();
  181. ObjectInput in = new ObjectInputStream(
  182. new ByteArrayInputStream(buffer.toByteArray())
  183. );
  184. r2 = (BarRenderer) in.readObject();
  185. in.close();
  186. }
  187. catch (Exception e) {
  188. System.out.println(e.toString());
  189. }
  190. assertEquals(r1, r2);
  191. }
  192. /**
  193. * Tests each setter method to ensure that it sends an event notification.
  194. */
  195. public void testEventNotification() {
  196. RendererChangeDetector detector = new RendererChangeDetector();
  197. BarRenderer r1 = new BarRenderer();
  198. r1.addChangeListener(detector);
  199. detector.setNotified(false);
  200. r1.setPaint(Color.red);
  201. assertTrue(detector.getNotified());
  202. }
  203. }