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: * XYLineAnnotationTests.java
029: * --------------------------
030: * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: XYLineAnnotationTests.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: * 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.Stroke;
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.XYLineAnnotation;
062:
063: /**
064: * Tests for the {@link XYLineAnnotation} class.
065: */
066: public class XYLineAnnotationTests 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(XYLineAnnotationTests.class);
075: }
076:
077: /**
078: * Constructs a new set of tests.
079: *
080: * @param name the name of the tests.
081: */
082: public XYLineAnnotationTests(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: Stroke stroke = new BasicStroke(2.0f);
091: XYLineAnnotation a1 = new XYLineAnnotation(10.0, 20.0, 100.0,
092: 200.0, stroke, Color.blue);
093: XYLineAnnotation a2 = new XYLineAnnotation(10.0, 20.0, 100.0,
094: 200.0, stroke, Color.blue);
095: assertTrue(a1.equals(a2));
096: assertTrue(a2.equals(a1));
097:
098: a1 = new XYLineAnnotation(11.0, 20.0, 100.0, 200.0, stroke,
099: Color.blue);
100: assertFalse(a1.equals(a2));
101: a2 = new XYLineAnnotation(11.0, 20.0, 100.0, 200.0, stroke,
102: Color.blue);
103: assertTrue(a1.equals(a2));
104:
105: a1 = new XYLineAnnotation(11.0, 21.0, 100.0, 200.0, stroke,
106: Color.blue);
107: assertFalse(a1.equals(a2));
108: a2 = new XYLineAnnotation(11.0, 21.0, 100.0, 200.0, stroke,
109: Color.blue);
110: assertTrue(a1.equals(a2));
111:
112: a1 = new XYLineAnnotation(11.0, 21.0, 101.0, 200.0, stroke,
113: Color.blue);
114: assertFalse(a1.equals(a2));
115: a2 = new XYLineAnnotation(11.0, 21.0, 101.0, 200.0, stroke,
116: Color.blue);
117: assertTrue(a1.equals(a2));
118:
119: a1 = new XYLineAnnotation(11.0, 21.0, 101.0, 201.0, stroke,
120: Color.blue);
121: assertFalse(a1.equals(a2));
122: a2 = new XYLineAnnotation(11.0, 21.0, 101.0, 201.0, stroke,
123: Color.blue);
124: assertTrue(a1.equals(a2));
125:
126: Stroke stroke2 = new BasicStroke(0.99f);
127: a1 = new XYLineAnnotation(11.0, 21.0, 101.0, 200.0, stroke2,
128: Color.blue);
129: assertFalse(a1.equals(a2));
130: a2 = new XYLineAnnotation(11.0, 21.0, 101.0, 200.0, stroke2,
131: Color.blue);
132: assertTrue(a1.equals(a2));
133:
134: GradientPaint g1 = new GradientPaint(1.0f, 2.0f, Color.red,
135: 3.0f, 4.0f, Color.white);
136: GradientPaint g2 = new GradientPaint(1.0f, 2.0f, Color.red,
137: 3.0f, 4.0f, Color.white);
138: a1 = new XYLineAnnotation(11.0, 21.0, 101.0, 200.0, stroke2, g1);
139: assertFalse(a1.equals(a2));
140: a2 = new XYLineAnnotation(11.0, 21.0, 101.0, 200.0, stroke2, g2);
141: assertTrue(a1.equals(a2));
142: }
143:
144: /**
145: * Two objects that are equal are required to return the same hashCode.
146: */
147: public void testHashCode() {
148: Stroke stroke = new BasicStroke(2.0f);
149: XYLineAnnotation a1 = new XYLineAnnotation(10.0, 20.0, 100.0,
150: 200.0, stroke, Color.blue);
151: XYLineAnnotation a2 = new XYLineAnnotation(10.0, 20.0, 100.0,
152: 200.0, stroke, Color.blue);
153: assertTrue(a1.equals(a2));
154: int h1 = a1.hashCode();
155: int h2 = a2.hashCode();
156: assertEquals(h1, h2);
157: }
158:
159: /**
160: * Confirm that cloning works.
161: */
162: public void testCloning() {
163: Stroke stroke = new BasicStroke(2.0f);
164: XYLineAnnotation a1 = new XYLineAnnotation(10.0, 20.0, 100.0,
165: 200.0, stroke, Color.blue);
166: XYLineAnnotation a2 = null;
167: try {
168: a2 = (XYLineAnnotation) a1.clone();
169: } catch (CloneNotSupportedException e) {
170: System.err.println("Failed to clone.");
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:
182: Stroke stroke = new BasicStroke(2.0f);
183: XYLineAnnotation a1 = new XYLineAnnotation(10.0, 20.0, 100.0,
184: 200.0, stroke, Color.blue);
185: XYLineAnnotation a2 = null;
186:
187: try {
188: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
189: ObjectOutput out = new ObjectOutputStream(buffer);
190: out.writeObject(a1);
191: out.close();
192:
193: ObjectInput in = new ObjectInputStream(
194: new ByteArrayInputStream(buffer.toByteArray()));
195: a2 = (XYLineAnnotation) in.readObject();
196: in.close();
197: } catch (Exception e) {
198: System.out.println(e.toString());
199: }
200: assertEquals(a1, a2);
201:
202: }
203:
204: }
|