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: * XYPointerAnnotationTests.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: XYPointerAnnotationTests.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: * 13-Oct-2003 : Expanded test for equals() method (DG);
041: * 07-Jan-2005 : Added hashCode() test (DG);
042: * 20-Feb-2006 : Added 'x' and 'y' checks to testEquals() (DG);
043: *
044: */
045:
046: package org.jfree.chart.annotations.junit;
047:
048: import java.awt.BasicStroke;
049: import java.awt.Color;
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:
058: import junit.framework.Test;
059: import junit.framework.TestCase;
060: import junit.framework.TestSuite;
061:
062: import org.jfree.chart.annotations.XYPointerAnnotation;
063:
064: /**
065: * Tests for the {@link XYPointerAnnotation} class.
066: */
067: public class XYPointerAnnotationTests 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(XYPointerAnnotationTests.class);
076: }
077:
078: /**
079: * Constructs a new set of tests.
080: *
081: * @param name the name of the tests.
082: */
083: public XYPointerAnnotationTests(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:
092: XYPointerAnnotation a1 = new XYPointerAnnotation("Label", 10.0,
093: 20.0, Math.PI);
094: XYPointerAnnotation a2 = new XYPointerAnnotation("Label", 10.0,
095: 20.0, Math.PI);
096: assertTrue(a1.equals(a2));
097:
098: a1 = new XYPointerAnnotation("Label2", 10.0, 20.0, Math.PI);
099: assertFalse(a1.equals(a2));
100: a2 = new XYPointerAnnotation("Label2", 10.0, 20.0, Math.PI);
101: assertTrue(a1.equals(a2));
102:
103: a1.setX(11.0);
104: assertFalse(a1.equals(a2));
105: a2.setX(11.0);
106: assertTrue(a1.equals(a2));
107:
108: a1.setY(22.0);
109: assertFalse(a1.equals(a2));
110: a2.setY(22.0);
111: assertTrue(a1.equals(a2));
112:
113: //private double angle;
114: a1.setAngle(Math.PI / 4.0);
115: assertFalse(a1.equals(a2));
116: a2.setAngle(Math.PI / 4.0);
117: assertTrue(a1.equals(a2));
118:
119: //private double tipRadius;
120: a1.setTipRadius(20.0);
121: assertFalse(a1.equals(a2));
122: a2.setTipRadius(20.0);
123: assertTrue(a1.equals(a2));
124:
125: //private double baseRadius;
126: a1.setBaseRadius(5.0);
127: assertFalse(a1.equals(a2));
128: a2.setBaseRadius(5.0);
129: assertTrue(a1.equals(a2));
130:
131: //private double arrowLength;
132: a1.setArrowLength(33.0);
133: assertFalse(a1.equals(a2));
134: a2.setArrowLength(33.0);
135: assertTrue(a1.equals(a2));
136:
137: //private double arrowWidth;
138: a1.setArrowWidth(9.0);
139: assertFalse(a1.equals(a2));
140: a2.setArrowWidth(9.0);
141: assertTrue(a1.equals(a2));
142:
143: //private Stroke arrowStroke;
144: Stroke stroke = new BasicStroke(1.5f);
145: a1.setArrowStroke(stroke);
146: assertFalse(a1.equals(a2));
147: a2.setArrowStroke(stroke);
148: assertTrue(a1.equals(a2));
149:
150: //private Paint arrowPaint;
151: a1.setArrowPaint(Color.blue);
152: assertFalse(a1.equals(a2));
153: a2.setArrowPaint(Color.blue);
154: assertTrue(a1.equals(a2));
155:
156: //private double labelOffset;
157: a1.setLabelOffset(10.0);
158: assertFalse(a1.equals(a2));
159: a2.setLabelOffset(10.0);
160: assertTrue(a1.equals(a2));
161:
162: }
163:
164: /**
165: * Two objects that are equal are required to return the same hashCode.
166: */
167: public void testHashCode() {
168: XYPointerAnnotation a1 = new XYPointerAnnotation("Label", 10.0,
169: 20.0, Math.PI);
170: XYPointerAnnotation a2 = new XYPointerAnnotation("Label", 10.0,
171: 20.0, Math.PI);
172: assertTrue(a1.equals(a2));
173: int h1 = a1.hashCode();
174: int h2 = a2.hashCode();
175: assertEquals(h1, h2);
176: }
177:
178: /**
179: * Confirm that cloning works.
180: */
181: public void testCloning() {
182:
183: XYPointerAnnotation a1 = new XYPointerAnnotation("Label", 10.0,
184: 20.0, Math.PI);
185: XYPointerAnnotation a2 = null;
186: try {
187: a2 = (XYPointerAnnotation) a1.clone();
188: } catch (CloneNotSupportedException e) {
189: System.err.println("Failed to clone.");
190: }
191: assertTrue(a1 != a2);
192: assertTrue(a1.getClass() == a2.getClass());
193: assertTrue(a1.equals(a2));
194: }
195:
196: /**
197: * Serialize an instance, restore it, and check for equality.
198: */
199: public void testSerialization() {
200:
201: XYPointerAnnotation a1 = new XYPointerAnnotation("Label", 10.0,
202: 20.0, Math.PI);
203: XYPointerAnnotation a2 = null;
204:
205: try {
206: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
207: ObjectOutput out = new ObjectOutputStream(buffer);
208: out.writeObject(a1);
209: out.close();
210:
211: ObjectInput in = new ObjectInputStream(
212: new ByteArrayInputStream(buffer.toByteArray()));
213: a2 = (XYPointerAnnotation) in.readObject();
214: in.close();
215: } catch (Exception e) {
216: System.out.println(e.toString());
217: }
218: assertEquals(a1, a2);
219:
220: }
221:
222: }
|