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: * CategoryPointerAnnotationTests.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: CategoryPointerAnnotationTests.java,v 1.1.2.1 2006/10/03 15:41:41 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 02-Oct-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.Stroke;
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.CategoryPointerAnnotation;
060:
061: /**
062: * Tests for the {@link CategoryPointerAnnotation} class.
063: */
064: public class CategoryPointerAnnotationTests 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(CategoryPointerAnnotationTests.class);
073: }
074:
075: /**
076: * Constructs a new set of tests.
077: *
078: * @param name the name of the tests.
079: */
080: public CategoryPointerAnnotationTests(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: CategoryPointerAnnotation a1 = new CategoryPointerAnnotation(
090: "Label", "Key 1", 20.0, Math.PI);
091: CategoryPointerAnnotation a2 = new CategoryPointerAnnotation(
092: "Label", "Key 1", 20.0, Math.PI);
093: assertTrue(a1.equals(a2));
094:
095: a1 = new CategoryPointerAnnotation("Label2", "Key 1", 20.0,
096: Math.PI);
097: assertFalse(a1.equals(a2));
098: a2 = new CategoryPointerAnnotation("Label2", "Key 1", 20.0,
099: Math.PI);
100: assertTrue(a1.equals(a2));
101:
102: a1.setCategory("Key 2");
103: assertFalse(a1.equals(a2));
104: a2.setCategory("Key 2");
105: assertTrue(a1.equals(a2));
106:
107: a1.setValue(22.0);
108: assertFalse(a1.equals(a2));
109: a2.setValue(22.0);
110: assertTrue(a1.equals(a2));
111:
112: //private double angle;
113: a1.setAngle(Math.PI / 4.0);
114: assertFalse(a1.equals(a2));
115: a2.setAngle(Math.PI / 4.0);
116: assertTrue(a1.equals(a2));
117:
118: //private double tipRadius;
119: a1.setTipRadius(20.0);
120: assertFalse(a1.equals(a2));
121: a2.setTipRadius(20.0);
122: assertTrue(a1.equals(a2));
123:
124: //private double baseRadius;
125: a1.setBaseRadius(5.0);
126: assertFalse(a1.equals(a2));
127: a2.setBaseRadius(5.0);
128: assertTrue(a1.equals(a2));
129:
130: //private double arrowLength;
131: a1.setArrowLength(33.0);
132: assertFalse(a1.equals(a2));
133: a2.setArrowLength(33.0);
134: assertTrue(a1.equals(a2));
135:
136: //private double arrowWidth;
137: a1.setArrowWidth(9.0);
138: assertFalse(a1.equals(a2));
139: a2.setArrowWidth(9.0);
140: assertTrue(a1.equals(a2));
141:
142: //private Stroke arrowStroke;
143: Stroke stroke = new BasicStroke(1.5f);
144: a1.setArrowStroke(stroke);
145: assertFalse(a1.equals(a2));
146: a2.setArrowStroke(stroke);
147: assertTrue(a1.equals(a2));
148:
149: //private Paint arrowPaint;
150: a1.setArrowPaint(Color.blue);
151: assertFalse(a1.equals(a2));
152: a2.setArrowPaint(Color.blue);
153: assertTrue(a1.equals(a2));
154:
155: //private double labelOffset;
156: a1.setLabelOffset(10.0);
157: assertFalse(a1.equals(a2));
158: a2.setLabelOffset(10.0);
159: assertTrue(a1.equals(a2));
160:
161: }
162:
163: /**
164: * Two objects that are equal are required to return the same hashCode.
165: */
166: public void testHashCode() {
167: CategoryPointerAnnotation a1 = new CategoryPointerAnnotation(
168: "Label", "A", 20.0, Math.PI);
169: CategoryPointerAnnotation a2 = new CategoryPointerAnnotation(
170: "Label", "A", 20.0, Math.PI);
171: assertTrue(a1.equals(a2));
172: int h1 = a1.hashCode();
173: int h2 = a2.hashCode();
174: assertEquals(h1, h2);
175: }
176:
177: /**
178: * Confirm that cloning works.
179: */
180: public void testCloning() {
181: CategoryPointerAnnotation a1 = new CategoryPointerAnnotation(
182: "Label", "A", 20.0, Math.PI);
183: CategoryPointerAnnotation a2 = null;
184: try {
185: a2 = (CategoryPointerAnnotation) a1.clone();
186: } catch (CloneNotSupportedException e) {
187: e.printStackTrace();
188: }
189: assertTrue(a1 != a2);
190: assertTrue(a1.getClass() == a2.getClass());
191: assertTrue(a1.equals(a2));
192: }
193:
194: /**
195: * Serialize an instance, restore it, and check for equality.
196: */
197: public void testSerialization() {
198:
199: CategoryPointerAnnotation a1 = new CategoryPointerAnnotation(
200: "Label", "A", 20.0, Math.PI);
201: CategoryPointerAnnotation a2 = null;
202:
203: try {
204: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
205: ObjectOutput out = new ObjectOutputStream(buffer);
206: out.writeObject(a1);
207: out.close();
208:
209: ObjectInput in = new ObjectInputStream(
210: new ByteArrayInputStream(buffer.toByteArray()));
211: a2 = (CategoryPointerAnnotation) in.readObject();
212: in.close();
213: } catch (Exception e) {
214: e.printStackTrace();
215: }
216: assertEquals(a1, a2);
217:
218: }
219:
220: }
|