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: * XYShapeAnnotationTests.java
029: * ---------------------------
030: * (C) Copyright 2004-2006, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: XYShapeAnnotationTests.java,v 1.1.2.2 2006/10/24 13:57:52 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 29-Sep-2004 : Version 1 (DG);
040: * 07-Jan-2005 : Added hashCode() test (DG);
041: *
042: */
043:
044: package org.jfree.chart.annotations.junit;
045:
046: import java.awt.BasicStroke;
047: import java.awt.Color;
048: import java.awt.GradientPaint;
049: import java.awt.geom.Rectangle2D;
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.XYShapeAnnotation;
062:
063: /**
064: * Some tests for the {@link XYShapeAnnotation} class.
065: */
066: public class XYShapeAnnotationTests extends TestCase {
067:
068: /**
069: * Returns the tests as a test suite.
070: *
071: * @return The test suite.
072: */
073: public static Test suite() {
074: return new TestSuite(XYShapeAnnotationTests.class);
075: }
076:
077: /**
078: * Constructs a new set of tests.
079: *
080: * @param name the name of the tests.
081: */
082: public XYShapeAnnotationTests(String name) {
083: super (name);
084: }
085:
086: /**
087: * Confirm that the equals method can distinguish all the required fields.
088: */
089: public void testEquals() {
090:
091: XYShapeAnnotation a1 = new XYShapeAnnotation(
092: new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0),
093: new BasicStroke(1.2f), Color.red, Color.blue);
094: XYShapeAnnotation a2 = new XYShapeAnnotation(
095: new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0),
096: new BasicStroke(1.2f), Color.red, Color.blue);
097: assertTrue(a1.equals(a2));
098: assertTrue(a2.equals(a1));
099:
100: // shape
101: a1 = new XYShapeAnnotation(new Rectangle2D.Double(4.0, 3.0,
102: 2.0, 1.0), new BasicStroke(1.2f), Color.red, Color.blue);
103: assertFalse(a1.equals(a2));
104: a2 = new XYShapeAnnotation(new Rectangle2D.Double(4.0, 3.0,
105: 2.0, 1.0), new BasicStroke(1.2f), Color.red, Color.blue);
106: assertTrue(a1.equals(a2));
107:
108: // stroke
109: a1 = new XYShapeAnnotation(new Rectangle2D.Double(4.0, 3.0,
110: 2.0, 1.0), new BasicStroke(2.3f), Color.red, Color.blue);
111: assertFalse(a1.equals(a2));
112: a2 = new XYShapeAnnotation(new Rectangle2D.Double(4.0, 3.0,
113: 2.0, 1.0), new BasicStroke(2.3f), Color.red, Color.blue);
114: assertTrue(a1.equals(a2));
115:
116: GradientPaint gp1a = new GradientPaint(1.0f, 2.0f, Color.blue,
117: 3.0f, 4.0f, Color.red);
118: GradientPaint gp1b = new GradientPaint(1.0f, 2.0f, Color.blue,
119: 3.0f, 4.0f, Color.red);
120: GradientPaint gp2a = new GradientPaint(5.0f, 6.0f, Color.pink,
121: 7.0f, 8.0f, Color.white);
122: GradientPaint gp2b = new GradientPaint(5.0f, 6.0f, Color.pink,
123: 7.0f, 8.0f, Color.white);
124:
125: // outlinePaint
126: a1 = new XYShapeAnnotation(new Rectangle2D.Double(4.0, 3.0,
127: 2.0, 1.0), new BasicStroke(2.3f), gp1a, Color.blue);
128: assertFalse(a1.equals(a2));
129: a2 = new XYShapeAnnotation(new Rectangle2D.Double(4.0, 3.0,
130: 2.0, 1.0), new BasicStroke(2.3f), gp1b, Color.blue);
131: assertTrue(a1.equals(a2));
132:
133: // fillPaint
134: a1 = new XYShapeAnnotation(new Rectangle2D.Double(4.0, 3.0,
135: 2.0, 1.0), new BasicStroke(2.3f), gp1a, gp2a);
136: assertFalse(a1.equals(a2));
137: a2 = new XYShapeAnnotation(new Rectangle2D.Double(4.0, 3.0,
138: 2.0, 1.0), new BasicStroke(2.3f), gp1b, gp2b);
139: assertTrue(a1.equals(a2));
140: }
141:
142: /**
143: * Two objects that are equal are required to return the same hashCode.
144: */
145: public void testHashCode() {
146: XYShapeAnnotation a1 = new XYShapeAnnotation(
147: new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0),
148: new BasicStroke(1.2f), Color.red, Color.blue);
149: XYShapeAnnotation a2 = new XYShapeAnnotation(
150: new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0),
151: new BasicStroke(1.2f), Color.red, Color.blue);
152: assertTrue(a1.equals(a2));
153: int h1 = a1.hashCode();
154: int h2 = a2.hashCode();
155: assertEquals(h1, h2);
156: }
157:
158: /**
159: * Confirm that cloning works.
160: */
161: public void testCloning() {
162:
163: XYShapeAnnotation a1 = new XYShapeAnnotation(
164: new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0),
165: new BasicStroke(1.2f), Color.red, Color.blue);
166: XYShapeAnnotation a2 = null;
167: try {
168: a2 = (XYShapeAnnotation) a1.clone();
169: } catch (CloneNotSupportedException e) {
170: e.printStackTrace();
171: }
172: assertTrue(a1 != a2);
173: assertTrue(a1.getClass() == a2.getClass());
174: assertTrue(a1.equals(a2));
175: }
176:
177: /**
178: * Serialize an instance, restore it, and check for equality.
179: */
180: public void testSerialization() {
181: XYShapeAnnotation a1 = new XYShapeAnnotation(
182: new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0),
183: new BasicStroke(1.2f), Color.red, Color.blue);
184: XYShapeAnnotation a2 = null;
185:
186: try {
187: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
188: ObjectOutput out = new ObjectOutputStream(buffer);
189: out.writeObject(a1);
190: out.close();
191:
192: ObjectInput in = new ObjectInputStream(
193: new ByteArrayInputStream(buffer.toByteArray()));
194: a2 = (XYShapeAnnotation) in.readObject();
195: in.close();
196: } catch (Exception e) {
197: e.printStackTrace();
198: }
199: assertEquals(a1, a2);
200: }
201:
202: }
|