001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, 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: * XYImageAnnotationTests.java
029: * ---------------------------
030: * (C) Copyright 2004-2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: XYImageAnnotationTests.java,v 1.1.2.4 2007/01/30 09:47:21 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 17-May-2004 : Version 1 (DG);
040: * 01-Dec-2006 : Updated testEquals() for new field (DG);
041: * 09-Jan-2007 : Comment out failing test (DG);
042: *
043: */
044:
045: package org.jfree.chart.annotations.junit;
046:
047: import java.awt.Image;
048:
049: import junit.framework.Test;
050: import junit.framework.TestCase;
051: import junit.framework.TestSuite;
052:
053: import org.jfree.chart.JFreeChart;
054: import org.jfree.chart.annotations.XYImageAnnotation;
055: import org.jfree.ui.RectangleAnchor;
056:
057: /**
058: * Tests for the {@link XYImageAnnotation} class.
059: */
060: public class XYImageAnnotationTests extends TestCase {
061:
062: /**
063: * Returns the tests as a test suite.
064: *
065: * @return The test suite.
066: */
067: public static Test suite() {
068: return new TestSuite(XYImageAnnotationTests.class);
069: }
070:
071: /**
072: * Constructs a new set of tests.
073: *
074: * @param name the name of the tests.
075: */
076: public XYImageAnnotationTests(String name) {
077: super (name);
078: }
079:
080: /**
081: * Confirm that the equals method can distinguish all the required fields.
082: */
083: public void testEquals() {
084: Image image = JFreeChart.INFO.getLogo();
085: XYImageAnnotation a1 = new XYImageAnnotation(10.0, 20.0, image);
086: XYImageAnnotation a2 = new XYImageAnnotation(10.0, 20.0, image);
087: assertTrue(a1.equals(a2));
088:
089: a1 = new XYImageAnnotation(10.0, 20.0, image,
090: RectangleAnchor.LEFT);
091: assertFalse(a1.equals(a2));
092: a2 = new XYImageAnnotation(10.0, 20.0, image,
093: RectangleAnchor.LEFT);
094: assertTrue(a1.equals(a2));
095: }
096:
097: /**
098: * Two objects that are equal are required to return the same hashCode.
099: */
100: public void testHashCode() {
101: Image image = JFreeChart.INFO.getLogo();
102: XYImageAnnotation a1 = new XYImageAnnotation(10.0, 20.0, image);
103: XYImageAnnotation a2 = new XYImageAnnotation(10.0, 20.0, image);
104: assertTrue(a1.equals(a2));
105: int h1 = a1.hashCode();
106: int h2 = a2.hashCode();
107: assertEquals(h1, h2);
108: }
109:
110: /**
111: * Confirm that cloning works.
112: */
113: public void testCloning() {
114: XYImageAnnotation a1 = new XYImageAnnotation(10.0, 20.0,
115: JFreeChart.INFO.getLogo());
116: XYImageAnnotation a2 = null;
117: try {
118: a2 = (XYImageAnnotation) a1.clone();
119: } catch (CloneNotSupportedException e) {
120: e.printStackTrace();
121: }
122: assertTrue(a1 != a2);
123: assertTrue(a1.getClass() == a2.getClass());
124: assertTrue(a1.equals(a2));
125: }
126:
127: // FIXME: Make this test pass
128: // /**
129: // * Serialize an instance, restore it, and check for equality.
130: // */
131: // public void testSerialization() {
132: // XYImageAnnotation a1 = new XYImageAnnotation(10.0, 20.0,
133: // JFreeChart.INFO.getLogo());
134: // XYImageAnnotation a2 = null;
135: // try {
136: // ByteArrayOutputStream buffer = new ByteArrayOutputStream();
137: // ObjectOutput out = new ObjectOutputStream(buffer);
138: // out.writeObject(a1);
139: // out.close();
140: //
141: // ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(
142: // buffer.toByteArray()));
143: // a2 = (XYImageAnnotation) in.readObject();
144: // in.close();
145: // }
146: // catch (Exception e) {
147: // e.printStackTrace();
148: // }
149: // assertEquals(a1, a2);
150: // }
151:
152: }
|