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: * XYDrawableAnnotationTests.java
029: * ------------------------------
030: * (C) Copyright 2003-2005, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: XYDrawableAnnotationTests.java,v 1.1.2.1 2006/10/03 15:41:41 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 19-Aug-2003 : Version 1 (DG);
040: * 01-Oct-2004 : Fixed bugs in tests (DG);
041: * 07-Jan-2005 : Added hashCode() test (DG);
042: *
043: */
044:
045: package org.jfree.chart.annotations.junit;
046:
047: import java.awt.Graphics2D;
048: import java.awt.geom.Rectangle2D;
049: import java.io.ByteArrayInputStream;
050: import java.io.ByteArrayOutputStream;
051: import java.io.ObjectInput;
052: import java.io.ObjectInputStream;
053: import java.io.ObjectOutput;
054: import java.io.ObjectOutputStream;
055: import java.io.Serializable;
056:
057: import junit.framework.Test;
058: import junit.framework.TestCase;
059: import junit.framework.TestSuite;
060:
061: import org.jfree.chart.annotations.XYDrawableAnnotation;
062: import org.jfree.ui.Drawable;
063:
064: /**
065: * Tests for the {@link XYDrawableAnnotation} class.
066: */
067: public class XYDrawableAnnotationTests extends TestCase {
068:
069: static class TestDrawable implements Drawable, Cloneable,
070: Serializable {
071: /**
072: * Default constructor.
073: */
074: public TestDrawable() {
075: }
076:
077: /**
078: * Draws something.
079: * @param g2 the graphics device.
080: * @param area the area in which to draw.
081: */
082: public void draw(Graphics2D g2, Rectangle2D area) {
083: // do nothing
084: }
085:
086: /**
087: * Tests this object for equality with an arbitrary object.
088: * @param obj the object to test against (<code>null</code> permitted).
089: * @return A boolean.
090: */
091: public boolean equals(Object obj) {
092: if (obj == this ) {
093: return true;
094: }
095: if (!(obj instanceof TestDrawable)) {
096: return false;
097: }
098: return true;
099: }
100: }
101:
102: /**
103: * Returns the tests as a test suite.
104: *
105: * @return The test suite.
106: */
107: public static Test suite() {
108: return new TestSuite(XYDrawableAnnotationTests.class);
109: }
110:
111: /**
112: * Constructs a new set of tests.
113: *
114: * @param name the name of the tests.
115: */
116: public XYDrawableAnnotationTests(String name) {
117: super (name);
118: }
119:
120: /**
121: * Confirm that the equals method can distinguish all the required fields.
122: */
123: public void testEquals() {
124: XYDrawableAnnotation a1 = new XYDrawableAnnotation(10.0, 20.0,
125: 100.0, 200.0, new TestDrawable());
126: XYDrawableAnnotation a2 = new XYDrawableAnnotation(10.0, 20.0,
127: 100.0, 200.0, new TestDrawable());
128: assertTrue(a1.equals(a2));
129: }
130:
131: /**
132: * Two objects that are equal are required to return the same hashCode.
133: */
134: public void testHashCode() {
135: XYDrawableAnnotation a1 = new XYDrawableAnnotation(10.0, 20.0,
136: 100.0, 200.0, new TestDrawable());
137: XYDrawableAnnotation a2 = new XYDrawableAnnotation(10.0, 20.0,
138: 100.0, 200.0, new TestDrawable());
139: assertTrue(a1.equals(a2));
140: int h1 = a1.hashCode();
141: int h2 = a2.hashCode();
142: assertEquals(h1, h2);
143: }
144:
145: /**
146: * Confirm that cloning works.
147: */
148: public void testCloning() {
149: XYDrawableAnnotation a1 = new XYDrawableAnnotation(10.0, 20.0,
150: 100.0, 200.0, new TestDrawable());
151: XYDrawableAnnotation a2 = null;
152: try {
153: a2 = (XYDrawableAnnotation) a1.clone();
154: } catch (CloneNotSupportedException e) {
155: System.err.println("Failed to clone.");
156: }
157: assertTrue(a1 != a2);
158: assertTrue(a1.getClass() == a2.getClass());
159: assertTrue(a1.equals(a2));
160: }
161:
162: /**
163: * Serialize an instance, restore it, and check for equality.
164: */
165: public void testSerialization() {
166:
167: XYDrawableAnnotation a1 = new XYDrawableAnnotation(10.0, 20.0,
168: 100.0, 200.0, new TestDrawable());
169: XYDrawableAnnotation a2 = null;
170:
171: try {
172: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
173: ObjectOutput out = new ObjectOutputStream(buffer);
174: out.writeObject(a1);
175: out.close();
176:
177: ObjectInput in = new ObjectInputStream(
178: new ByteArrayInputStream(buffer.toByteArray()));
179: a2 = (XYDrawableAnnotation) in.readObject();
180: in.close();
181: } catch (Exception e) {
182: System.out.println(e.toString());
183: }
184: assertEquals(a1, a2);
185:
186: }
187:
188: }
|