001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * -----------------------------
028: * PeriodAxisLabelInfoTests.java
029: * -----------------------------
030: * (C) Copyright 2004, 2005, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: PeriodAxisLabelInfoTests.java,v 1.1.2.1 2006/10/03 15:41:23 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 10-Jun-2003 : Version 1 (DG);
040: * 07-Jan-2005 : Added test for hashCode() (DG);
041: *
042: */
043:
044: package org.jfree.chart.axis.junit;
045:
046: import java.awt.BasicStroke;
047: import java.awt.Color;
048: import java.awt.Font;
049: import java.awt.Paint;
050: import java.awt.Stroke;
051: import java.io.ByteArrayInputStream;
052: import java.io.ByteArrayOutputStream;
053: import java.io.ObjectInput;
054: import java.io.ObjectInputStream;
055: import java.io.ObjectOutput;
056: import java.io.ObjectOutputStream;
057: import java.text.DateFormat;
058: import java.text.SimpleDateFormat;
059:
060: import junit.framework.Test;
061: import junit.framework.TestCase;
062: import junit.framework.TestSuite;
063:
064: import org.jfree.chart.axis.PeriodAxisLabelInfo;
065: import org.jfree.data.time.Day;
066: import org.jfree.data.time.Month;
067: import org.jfree.ui.RectangleInsets;
068:
069: /**
070: * Tests for the {@link PeriodAxisLabelInfo} class.
071: */
072: public class PeriodAxisLabelInfoTests extends TestCase {
073:
074: /**
075: * Returns the tests as a test suite.
076: *
077: * @return The test suite.
078: */
079: public static Test suite() {
080: return new TestSuite(PeriodAxisLabelInfoTests.class);
081: }
082:
083: /**
084: * Constructs a new set of tests.
085: *
086: * @param name the name of the tests.
087: */
088: public PeriodAxisLabelInfoTests(String name) {
089: super (name);
090: }
091:
092: /**
093: * Confirm that the equals method can distinguish all the required fields.
094: */
095: public void testEquals() {
096: PeriodAxisLabelInfo info1 = new PeriodAxisLabelInfo(Day.class,
097: new SimpleDateFormat("d"));
098: PeriodAxisLabelInfo info2 = new PeriodAxisLabelInfo(Day.class,
099: new SimpleDateFormat("d"));
100: assertTrue(info1.equals(info2));
101: assertTrue(info2.equals(info1));
102:
103: Class c1 = Day.class;
104: Class c2 = Month.class;
105: DateFormat df1 = new SimpleDateFormat("d");
106: DateFormat df2 = new SimpleDateFormat("MMM");
107: RectangleInsets sp1 = new RectangleInsets(1, 1, 1, 1);
108: RectangleInsets sp2 = new RectangleInsets(2, 2, 2, 2);
109: Font lf1 = new Font("SansSerif", Font.PLAIN, 10);
110: Font lf2 = new Font("SansSerif", Font.BOLD, 9);
111: Paint lp1 = Color.black;
112: Paint lp2 = Color.blue;
113: boolean b1 = true;
114: boolean b2 = false;
115: Stroke s1 = new BasicStroke(0.5f);
116: Stroke s2 = new BasicStroke(0.25f);
117: Paint dp1 = Color.red;
118: Paint dp2 = Color.green;
119:
120: info1 = new PeriodAxisLabelInfo(c2, df1, sp1, lf1, lp1, b1, s1,
121: dp1);
122: info2 = new PeriodAxisLabelInfo(c1, df1, sp1, lf1, lp1, b1, s1,
123: dp1);
124: assertFalse(info1.equals(info2));
125: info2 = new PeriodAxisLabelInfo(c2, df1, sp1, lf1, lp1, b1, s1,
126: dp1);
127: assertTrue(info1.equals(info2));
128:
129: info1 = new PeriodAxisLabelInfo(c2, df2, sp1, lf1, lp1, b1, s1,
130: dp1);
131: assertFalse(info1.equals(info2));
132: info2 = new PeriodAxisLabelInfo(c2, df2, sp1, lf1, lp1, b1, s1,
133: dp1);
134: assertTrue(info1.equals(info2));
135:
136: info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf1, lp1, b1, s1,
137: dp1);
138: assertFalse(info1.equals(info2));
139: info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf1, lp1, b1, s1,
140: dp1);
141: assertTrue(info1.equals(info2));
142:
143: info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp1, b1, s1,
144: dp1);
145: assertFalse(info1.equals(info2));
146: info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp1, b1, s1,
147: dp1);
148: assertTrue(info1.equals(info2));
149:
150: info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b1, s1,
151: dp1);
152: assertFalse(info1.equals(info2));
153: info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b1, s1,
154: dp1);
155: assertTrue(info1.equals(info2));
156:
157: info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s1,
158: dp1);
159: assertFalse(info1.equals(info2));
160: info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s1,
161: dp1);
162: assertTrue(info1.equals(info2));
163:
164: info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s2,
165: dp1);
166: assertFalse(info1.equals(info2));
167: info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s2,
168: dp1);
169: assertTrue(info1.equals(info2));
170:
171: info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s2,
172: dp2);
173: assertFalse(info1.equals(info2));
174: info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s2,
175: dp2);
176: assertTrue(info1.equals(info2));
177:
178: }
179:
180: /**
181: * Two objects that are equal are required to return the same hashCode.
182: */
183: public void testHashCode() {
184: PeriodAxisLabelInfo info1 = new PeriodAxisLabelInfo(Day.class,
185: new SimpleDateFormat("d"));
186: PeriodAxisLabelInfo info2 = new PeriodAxisLabelInfo(Day.class,
187: new SimpleDateFormat("d"));
188: assertTrue(info1.equals(info2));
189: int h1 = info1.hashCode();
190: int h2 = info2.hashCode();
191: assertEquals(h1, h2);
192: }
193:
194: /**
195: * Confirm that cloning works.
196: */
197: public void testCloning() {
198: PeriodAxisLabelInfo info1 = new PeriodAxisLabelInfo(Day.class,
199: new SimpleDateFormat("d"));
200: PeriodAxisLabelInfo info2 = null;
201: try {
202: info2 = (PeriodAxisLabelInfo) info1.clone();
203: } catch (CloneNotSupportedException e) {
204: System.err.println("Failed to clone.");
205: }
206: assertTrue(info1 != info2);
207: assertTrue(info1.getClass() == info2.getClass());
208: assertTrue(info1.equals(info2));
209: }
210:
211: /**
212: * Serialize an instance, restore it, and check for equality.
213: */
214: public void testSerialization() {
215: PeriodAxisLabelInfo info1 = new PeriodAxisLabelInfo(Day.class,
216: new SimpleDateFormat("d"));
217: PeriodAxisLabelInfo info2 = null;
218: try {
219: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
220: ObjectOutput out = new ObjectOutputStream(buffer);
221: out.writeObject(info1);
222: out.close();
223:
224: ObjectInput in = new ObjectInputStream(
225: new ByteArrayInputStream(buffer.toByteArray()));
226: info2 = (PeriodAxisLabelInfo) in.readObject();
227: in.close();
228: } catch (Exception e) {
229: System.out.println(e.toString());
230: }
231: boolean b = info1.equals(info2);
232: assertTrue(b);
233: }
234:
235: }
|