001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2006, 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: * XYTextAnnotationTests.java
029: * --------------------------
030: * (C) Copyright 2003-2006, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: XYTextAnnotationTests.java,v 1.1.2.1 2006/10/03 15:41:40 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 19-Aug-2003 : Version 1 (DG);
040: * 07-Jan-2005 : Added hashCode() test (DG);
041: * 26-Jan-2006 : Extended equals() test (DG);
042: *
043: */
044:
045: package org.jfree.chart.annotations.junit;
046:
047: import java.awt.Color;
048: import java.awt.Font;
049: import java.awt.GradientPaint;
050: import java.io.ByteArrayInputStream;
051: import java.io.ByteArrayOutputStream;
052: import java.io.ObjectInput;
053: import java.io.ObjectInputStream;
054: import java.io.ObjectOutput;
055: import java.io.ObjectOutputStream;
056:
057: import junit.framework.Test;
058: import junit.framework.TestCase;
059: import junit.framework.TestSuite;
060:
061: import org.jfree.chart.annotations.XYTextAnnotation;
062: import org.jfree.ui.TextAnchor;
063:
064: /**
065: * Tests for the {@link XYTextAnnotation} class.
066: */
067: public class XYTextAnnotationTests extends TestCase {
068:
069: /**
070: * Returns the tests as a test suite.
071: *
072: * @return The test suite.
073: */
074: public static Test suite() {
075: return new TestSuite(XYTextAnnotationTests.class);
076: }
077:
078: /**
079: * Constructs a new set of tests.
080: *
081: * @param name the name of the tests.
082: */
083: public XYTextAnnotationTests(String name) {
084: super (name);
085: }
086:
087: /**
088: * Confirm that the equals method can distinguish all the required fields.
089: */
090: public void testEquals() {
091: XYTextAnnotation a1 = new XYTextAnnotation("Text", 10.0, 20.0);
092: XYTextAnnotation a2 = new XYTextAnnotation("Text", 10.0, 20.0);
093: assertTrue(a1.equals(a2));
094:
095: // text
096: a1 = new XYTextAnnotation("ABC", 10.0, 20.0);
097: assertFalse(a1.equals(a2));
098: a2 = new XYTextAnnotation("ABC", 10.0, 20.0);
099: assertTrue(a1.equals(a2));
100:
101: // x
102: a1 = new XYTextAnnotation("ABC", 11.0, 20.0);
103: assertFalse(a1.equals(a2));
104: a2 = new XYTextAnnotation("ABC", 11.0, 20.0);
105: assertTrue(a1.equals(a2));
106:
107: // y
108: a1 = new XYTextAnnotation("ABC", 11.0, 22.0);
109: assertFalse(a1.equals(a2));
110: a2 = new XYTextAnnotation("ABC", 11.0, 22.0);
111: assertTrue(a1.equals(a2));
112:
113: // font
114: a1.setFont(new Font("Serif", Font.PLAIN, 23));
115: assertFalse(a1.equals(a2));
116: a2.setFont(new Font("Serif", Font.PLAIN, 23));
117: assertTrue(a1.equals(a2));
118:
119: // paint
120: GradientPaint gp1 = new GradientPaint(1.0f, 2.0f, Color.red,
121: 3.0f, 4.0f, Color.yellow);
122: GradientPaint gp2 = new GradientPaint(1.0f, 2.0f, Color.red,
123: 3.0f, 4.0f, Color.yellow);
124: a1.setPaint(gp1);
125: assertFalse(a1.equals(a2));
126: a2.setPaint(gp2);
127: assertTrue(a1.equals(a2));
128:
129: // rotation anchor
130: a1.setRotationAnchor(TextAnchor.BASELINE_RIGHT);
131: assertFalse(a1.equals(a2));
132: a2.setRotationAnchor(TextAnchor.BASELINE_RIGHT);
133: assertTrue(a1.equals(a2));
134:
135: // rotation angle
136: a1.setRotationAngle(12.3);
137: assertFalse(a1.equals(a2));
138: a2.setRotationAngle(12.3);
139: assertTrue(a1.equals(a2));
140:
141: // text anchor
142: a1.setTextAnchor(TextAnchor.BASELINE_RIGHT);
143: assertFalse(a1.equals(a2));
144: a2.setTextAnchor(TextAnchor.BASELINE_RIGHT);
145: assertTrue(a1.equals(a2));
146: }
147:
148: /**
149: * Two objects that are equal are required to return the same hashCode.
150: */
151: public void testHashCode() {
152: XYTextAnnotation a1 = new XYTextAnnotation("Text", 10.0, 20.0);
153: XYTextAnnotation a2 = new XYTextAnnotation("Text", 10.0, 20.0);
154: assertTrue(a1.equals(a2));
155: int h1 = a1.hashCode();
156: int h2 = a2.hashCode();
157: assertEquals(h1, h2);
158: }
159:
160: /**
161: * Confirm that cloning works.
162: */
163: public void testCloning() {
164: XYTextAnnotation a1 = new XYTextAnnotation("Text", 10.0, 20.0);
165: XYTextAnnotation a2 = null;
166: try {
167: a2 = (XYTextAnnotation) a1.clone();
168: } catch (CloneNotSupportedException e) {
169: System.err.println("Failed to clone.");
170: }
171: assertTrue(a1 != a2);
172: assertTrue(a1.getClass() == a2.getClass());
173: assertTrue(a1.equals(a2));
174: }
175:
176: /**
177: * Serialize an instance, restore it, and check for equality.
178: */
179: public void testSerialization() {
180:
181: XYTextAnnotation a1 = new XYTextAnnotation("Text", 10.0, 20.0);
182: XYTextAnnotation a2 = null;
183:
184: try {
185: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
186: ObjectOutput out = new ObjectOutputStream(buffer);
187: out.writeObject(a1);
188: out.close();
189:
190: ObjectInput in = new ObjectInputStream(
191: new ByteArrayInputStream(buffer.toByteArray()));
192: a2 = (XYTextAnnotation) in.readObject();
193: in.close();
194: } catch (Exception e) {
195: System.out.println(e.toString());
196: }
197: assertEquals(a1, a2);
198:
199: }
200:
201: }
|