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: * XYPolygonAnnotationTests.java
029: * -----------------------------
030: * (C) Copyright 2006, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: XYPolygonAnnotationTests.java,v 1.1.2.1 2006/10/03 15:41:41 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 10-Jul-2006 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.chart.annotations.junit;
044:
045: import java.awt.BasicStroke;
046: import java.awt.Color;
047: import java.awt.GradientPaint;
048: import java.awt.Stroke;
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:
056: import junit.framework.Test;
057: import junit.framework.TestCase;
058: import junit.framework.TestSuite;
059:
060: import org.jfree.chart.annotations.XYPolygonAnnotation;
061:
062: /**
063: * Tests for the {@link XYPolygonAnnotation} class.
064: */
065: public class XYPolygonAnnotationTests extends TestCase {
066:
067: /**
068: * Returns the tests as a test suite.
069: *
070: * @return The test suite.
071: */
072: public static Test suite() {
073: return new TestSuite(XYPolygonAnnotationTests.class);
074: }
075:
076: /**
077: * Constructs a new set of tests.
078: *
079: * @param name the name of the tests.
080: */
081: public XYPolygonAnnotationTests(String name) {
082: super (name);
083: }
084:
085: /**
086: * Confirm that the equals method can distinguish all the required fields.
087: */
088: public void testEquals() {
089: Stroke stroke1 = new BasicStroke(2.0f);
090: Stroke stroke2 = new BasicStroke(2.5f);
091: XYPolygonAnnotation a1 = new XYPolygonAnnotation(new double[] {
092: 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 }, stroke1, Color.red,
093: Color.blue);
094: XYPolygonAnnotation a2 = new XYPolygonAnnotation(new double[] {
095: 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 }, stroke1, Color.red,
096: Color.blue);
097: assertTrue(a1.equals(a2));
098: assertTrue(a2.equals(a1));
099:
100: a1 = new XYPolygonAnnotation(new double[] { 99.0, 2.0, 3.0,
101: 4.0, 5.0, 6.0 }, stroke1, Color.red, Color.blue);
102: assertFalse(a1.equals(a2));
103: a2 = new XYPolygonAnnotation(new double[] { 99.0, 2.0, 3.0,
104: 4.0, 5.0, 6.0 }, stroke1, Color.red, Color.blue);
105: assertTrue(a1.equals(a2));
106:
107: a1 = new XYPolygonAnnotation(new double[] { 99.0, 2.0, 3.0,
108: 4.0, 5.0, 6.0 }, stroke2, Color.red, Color.blue);
109: assertFalse(a1.equals(a2));
110: a2 = new XYPolygonAnnotation(new double[] { 99.0, 2.0, 3.0,
111: 4.0, 5.0, 6.0 }, stroke2, Color.red, Color.blue);
112: assertTrue(a1.equals(a2));
113:
114: GradientPaint gp1 = new GradientPaint(1.0f, 2.0f, Color.yellow,
115: 3.0f, 4.0f, Color.white);
116: GradientPaint gp2 = new GradientPaint(1.0f, 2.0f, Color.yellow,
117: 3.0f, 4.0f, Color.white);
118: a1 = new XYPolygonAnnotation(new double[] { 99.0, 2.0, 3.0,
119: 4.0, 5.0, 6.0 }, stroke2, gp1, Color.blue);
120: assertFalse(a1.equals(a2));
121: a2 = new XYPolygonAnnotation(new double[] { 99.0, 2.0, 3.0,
122: 4.0, 5.0, 6.0 }, stroke2, gp2, Color.blue);
123: assertTrue(a1.equals(a2));
124:
125: GradientPaint gp3 = new GradientPaint(1.0f, 2.0f, Color.green,
126: 3.0f, 4.0f, Color.white);
127: GradientPaint gp4 = new GradientPaint(1.0f, 2.0f, Color.green,
128: 3.0f, 4.0f, Color.white);
129: a1 = new XYPolygonAnnotation(new double[] { 99.0, 2.0, 3.0,
130: 4.0, 5.0, 6.0 }, stroke2, gp1, gp3);
131: assertFalse(a1.equals(a2));
132: a2 = new XYPolygonAnnotation(new double[] { 99.0, 2.0, 3.0,
133: 4.0, 5.0, 6.0 }, stroke2, gp2, gp4);
134: assertTrue(a1.equals(a2));
135: }
136:
137: /**
138: * Two objects that are equal are required to return the same hashCode.
139: */
140: public void testHashCode() {
141: Stroke stroke = new BasicStroke(2.0f);
142: XYPolygonAnnotation a1 = new XYPolygonAnnotation(new double[] {
143: 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 }, stroke, Color.red,
144: Color.blue);
145: XYPolygonAnnotation a2 = new XYPolygonAnnotation(new double[] {
146: 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 }, stroke, Color.red,
147: Color.blue);
148: assertTrue(a1.equals(a2));
149: int h1 = a1.hashCode();
150: int h2 = a2.hashCode();
151: assertEquals(h1, h2);
152: }
153:
154: /**
155: * Confirm that cloning works.
156: */
157: public void testCloning() {
158: Stroke stroke1 = new BasicStroke(2.0f);
159: XYPolygonAnnotation a1 = new XYPolygonAnnotation(new double[] {
160: 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 }, stroke1, Color.red,
161: Color.blue);
162: XYPolygonAnnotation a2 = null;
163: try {
164: a2 = (XYPolygonAnnotation) a1.clone();
165: } catch (CloneNotSupportedException e) {
166: e.printStackTrace();
167: }
168: assertTrue(a1 != a2);
169: assertTrue(a1.getClass() == a2.getClass());
170: assertTrue(a1.equals(a2));
171: }
172:
173: /**
174: * Serialize an instance, restore it, and check for equality.
175: */
176: public void testSerialization() {
177:
178: Stroke stroke1 = new BasicStroke(2.0f);
179: XYPolygonAnnotation a1 = new XYPolygonAnnotation(new double[] {
180: 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 }, stroke1, Color.red,
181: Color.blue);
182: XYPolygonAnnotation 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 = (XYPolygonAnnotation) in.readObject();
193: in.close();
194: } catch (Exception e) {
195: e.printStackTrace();
196: }
197: assertEquals(a1, a2);
198:
199: }
200:
201: }
|