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. * PeriodAxisLabelInfoTests.java
  28. * -----------------------------
  29. * (C) Copyright 2004, 2005, by Object Refinery Limited and Contributors.
  30. *
  31. * Original Author: David Gilbert (for Object Refinery Limited);
  32. * Contributor(s): -;
  33. *
  34. * $Id: PeriodAxisLabelInfoTests.java,v 1.4 2005/03/04 11:55:01 mungady Exp $
  35. *
  36. * Changes
  37. * -------
  38. * 10-Jun-2003 : Version 1 (DG);
  39. * 07-Jan-2005 : Added test for hashCode() (DG);
  40. *
  41. */
  42. package org.jfree.chart.axis.junit;
  43. import java.awt.BasicStroke;
  44. import java.awt.Color;
  45. import java.awt.Font;
  46. import java.awt.Paint;
  47. import java.awt.Stroke;
  48. import java.io.ByteArrayInputStream;
  49. import java.io.ByteArrayOutputStream;
  50. import java.io.ObjectInput;
  51. import java.io.ObjectInputStream;
  52. import java.io.ObjectOutput;
  53. import java.io.ObjectOutputStream;
  54. import java.text.DateFormat;
  55. import java.text.SimpleDateFormat;
  56. import junit.framework.Test;
  57. import junit.framework.TestCase;
  58. import junit.framework.TestSuite;
  59. import org.jfree.chart.axis.PeriodAxisLabelInfo;
  60. import org.jfree.data.time.Day;
  61. import org.jfree.data.time.Month;
  62. import org.jfree.ui.RectangleInsets;
  63. /**
  64. * Tests for the {@link PeriodAxisLabelInfo} class.
  65. */
  66. public class PeriodAxisLabelInfoTests extends TestCase {
  67. /**
  68. * Returns the tests as a test suite.
  69. *
  70. * @return The test suite.
  71. */
  72. public static Test suite() {
  73. return new TestSuite(PeriodAxisLabelInfoTests.class);
  74. }
  75. /**
  76. * Constructs a new set of tests.
  77. *
  78. * @param name the name of the tests.
  79. */
  80. public PeriodAxisLabelInfoTests(String name) {
  81. super(name);
  82. }
  83. /**
  84. * Confirm that the equals method can distinguish all the required fields.
  85. */
  86. public void testEquals() {
  87. PeriodAxisLabelInfo info1 = new PeriodAxisLabelInfo(
  88. Day.class, new SimpleDateFormat("d")
  89. );
  90. PeriodAxisLabelInfo info2 = new PeriodAxisLabelInfo(
  91. Day.class, new SimpleDateFormat("d")
  92. );
  93. assertTrue(info1.equals(info2));
  94. assertTrue(info2.equals(info1));
  95. Class c1 = Day.class;
  96. Class c2 = Month.class;
  97. DateFormat df1 = new SimpleDateFormat("d");
  98. DateFormat df2 = new SimpleDateFormat("MMM");
  99. RectangleInsets sp1 = new RectangleInsets(1, 1, 1, 1);
  100. RectangleInsets sp2 = new RectangleInsets(2, 2, 2, 2);
  101. Font lf1 = new Font("SansSerif", Font.PLAIN, 10);
  102. Font lf2 = new Font("SansSerif", Font.BOLD, 9);
  103. Paint lp1 = Color.black;
  104. Paint lp2 = Color.blue;
  105. boolean b1 = true;
  106. boolean b2 = false;
  107. Stroke s1 = new BasicStroke(0.5f);
  108. Stroke s2 = new BasicStroke(0.25f);
  109. Paint dp1 = Color.red;
  110. Paint dp2 = Color.green;
  111. info1 = new PeriodAxisLabelInfo(c2, df1, sp1, lf1, lp1, b1, s1, dp1);
  112. info2 = new PeriodAxisLabelInfo(c1, df1, sp1, lf1, lp1, b1, s1, dp1);
  113. assertFalse(info1.equals(info2));
  114. info2 = new PeriodAxisLabelInfo(c2, df1, sp1, lf1, lp1, b1, s1, dp1);
  115. assertTrue(info1.equals(info2));
  116. info1 = new PeriodAxisLabelInfo(c2, df2, sp1, lf1, lp1, b1, s1, dp1);
  117. assertFalse(info1.equals(info2));
  118. info2 = new PeriodAxisLabelInfo(c2, df2, sp1, lf1, lp1, b1, s1, dp1);
  119. assertTrue(info1.equals(info2));
  120. info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf1, lp1, b1, s1, dp1);
  121. assertFalse(info1.equals(info2));
  122. info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf1, lp1, b1, s1, dp1);
  123. assertTrue(info1.equals(info2));
  124. info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp1, b1, s1, dp1);
  125. assertFalse(info1.equals(info2));
  126. info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp1, b1, s1, dp1);
  127. assertTrue(info1.equals(info2));
  128. info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b1, s1, dp1);
  129. assertFalse(info1.equals(info2));
  130. info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b1, s1, dp1);
  131. assertTrue(info1.equals(info2));
  132. info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s1, dp1);
  133. assertFalse(info1.equals(info2));
  134. info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s1, dp1);
  135. assertTrue(info1.equals(info2));
  136. info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s2, dp1);
  137. assertFalse(info1.equals(info2));
  138. info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s2, dp1);
  139. assertTrue(info1.equals(info2));
  140. info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s2, dp2);
  141. assertFalse(info1.equals(info2));
  142. info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s2, dp2);
  143. assertTrue(info1.equals(info2));
  144. }
  145. /**
  146. * Two objects that are equal are required to return the same hashCode.
  147. */
  148. public void testHashCode() {
  149. PeriodAxisLabelInfo info1 = new PeriodAxisLabelInfo(
  150. Day.class, new SimpleDateFormat("d")
  151. );
  152. PeriodAxisLabelInfo info2 = new PeriodAxisLabelInfo(
  153. Day.class, new SimpleDateFormat("d")
  154. );
  155. assertTrue(info1.equals(info2));
  156. int h1 = info1.hashCode();
  157. int h2 = info2.hashCode();
  158. assertEquals(h1, h2);
  159. }
  160. /**
  161. * Confirm that cloning works.
  162. */
  163. public void testCloning() {
  164. PeriodAxisLabelInfo info1 = new PeriodAxisLabelInfo(
  165. Day.class, new SimpleDateFormat("d")
  166. );
  167. PeriodAxisLabelInfo info2 = null;
  168. try {
  169. info2 = (PeriodAxisLabelInfo) info1.clone();
  170. }
  171. catch (CloneNotSupportedException e) {
  172. System.err.println("Failed to clone.");
  173. }
  174. assertTrue(info1 != info2);
  175. assertTrue(info1.getClass() == info2.getClass());
  176. assertTrue(info1.equals(info2));
  177. }
  178. /**
  179. * Serialize an instance, restore it, and check for equality.
  180. */
  181. public void testSerialization() {
  182. PeriodAxisLabelInfo info1 = new PeriodAxisLabelInfo(
  183. Day.class, new SimpleDateFormat("d")
  184. );
  185. PeriodAxisLabelInfo info2 = null;
  186. try {
  187. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  188. ObjectOutput out = new ObjectOutputStream(buffer);
  189. out.writeObject(info1);
  190. out.close();
  191. ObjectInput in = new ObjectInputStream(
  192. new ByteArrayInputStream(buffer.toByteArray())
  193. );
  194. info2 = (PeriodAxisLabelInfo) in.readObject();
  195. in.close();
  196. }
  197. catch (Exception e) {
  198. System.out.println(e.toString());
  199. }
  200. boolean b = info1.equals(info2);
  201. assertTrue(b);
  202. }
  203. }