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