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: * CategoryLineAnnotationTests.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: CategoryLineAnnotationTests.java,v 1.1.2.1 2006/10/03 15:41:40 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 29-Jul-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.io.ByteArrayInputStream;
048: import java.io.ByteArrayOutputStream;
049: import java.io.ObjectInput;
050: import java.io.ObjectInputStream;
051: import java.io.ObjectOutput;
052: import java.io.ObjectOutputStream;
053:
054: import junit.framework.Test;
055: import junit.framework.TestCase;
056: import junit.framework.TestSuite;
057:
058: import org.jfree.chart.annotations.CategoryLineAnnotation;
059:
060: /**
061: * Tests for the {@link CategoryLineAnnotationTests} class.
062: */
063: public class CategoryLineAnnotationTests extends TestCase {
064:
065: /**
066: * Returns the tests as a test suite.
067: *
068: * @return The test suite.
069: */
070: public static Test suite() {
071: return new TestSuite(CategoryLineAnnotationTests.class);
072: }
073:
074: /**
075: * Constructs a new set of tests.
076: *
077: * @param name the name of the tests.
078: */
079: public CategoryLineAnnotationTests(String name) {
080: super (name);
081: }
082:
083: /**
084: * Confirm that the equals method can distinguish all the required fields.
085: */
086: public void testEquals() {
087:
088: BasicStroke s1 = new BasicStroke(1.0f);
089: BasicStroke s2 = new BasicStroke(2.0f);
090: CategoryLineAnnotation a1 = new CategoryLineAnnotation(
091: "Category 1", 1.0, "Category 2", 2.0, Color.red, s1);
092: CategoryLineAnnotation a2 = new CategoryLineAnnotation(
093: "Category 1", 1.0, "Category 2", 2.0, Color.red, s1);
094: assertTrue(a1.equals(a2));
095: assertTrue(a2.equals(a1));
096:
097: // category 1
098: a1.setCategory1("Category A");
099: assertFalse(a1.equals(a2));
100: a2.setCategory1("Category A");
101: assertTrue(a1.equals(a2));
102:
103: // value 1
104: a1.setValue1(0.15);
105: assertFalse(a1.equals(a2));
106: a2.setValue1(0.15);
107: assertTrue(a1.equals(a2));
108:
109: // category 2
110: a1.setCategory2("Category B");
111: assertFalse(a1.equals(a2));
112: a2.setCategory2("Category B");
113: assertTrue(a1.equals(a2));
114:
115: // value 2
116: a1.setValue2(0.25);
117: assertFalse(a1.equals(a2));
118: a2.setValue2(0.25);
119: assertTrue(a1.equals(a2));
120:
121: // paint
122: a1.setPaint(Color.yellow);
123: assertFalse(a1.equals(a2));
124: a2.setPaint(Color.yellow);
125: assertTrue(a1.equals(a2));
126:
127: // stroke
128: a1.setStroke(s2);
129: assertFalse(a1.equals(a2));
130: a2.setStroke(s2);
131: assertTrue(a1.equals(a2));
132: }
133:
134: /**
135: * Two objects that are equal are required to return the same hashCode.
136: */
137: public void testHashcode() {
138: CategoryLineAnnotation a1 = new CategoryLineAnnotation(
139: "Category 1", 1.0, "Category 2", 2.0, Color.red,
140: new BasicStroke(1.0f));
141: CategoryLineAnnotation a2 = new CategoryLineAnnotation(
142: "Category 1", 1.0, "Category 2", 2.0, Color.red,
143: new BasicStroke(1.0f));
144: assertTrue(a1.equals(a2));
145: int h1 = a1.hashCode();
146: int h2 = a2.hashCode();
147: assertEquals(h1, h2);
148: }
149:
150: /**
151: * Confirm that cloning works.
152: */
153: public void testCloning() {
154: CategoryLineAnnotation a1 = new CategoryLineAnnotation(
155: "Category 1", 1.0, "Category 2", 2.0, Color.red,
156: new BasicStroke(1.0f));
157: CategoryLineAnnotation a2 = null;
158: try {
159: a2 = (CategoryLineAnnotation) a1.clone();
160: } catch (CloneNotSupportedException e) {
161: e.printStackTrace();
162: }
163: assertTrue(a1 != a2);
164: assertTrue(a1.getClass() == a2.getClass());
165: assertTrue(a1.equals(a2));
166: }
167:
168: /**
169: * Serialize an instance, restore it, and check for equality.
170: */
171: public void testSerialization() {
172:
173: CategoryLineAnnotation a1 = new CategoryLineAnnotation(
174: "Category 1", 1.0, "Category 2", 2.0, Color.red,
175: new BasicStroke(1.0f));
176: CategoryLineAnnotation a2 = null;
177:
178: try {
179: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
180: ObjectOutput out = new ObjectOutputStream(buffer);
181: out.writeObject(a1);
182: out.close();
183:
184: ObjectInput in = new ObjectInputStream(
185: new ByteArrayInputStream(buffer.toByteArray()));
186: a2 = (CategoryLineAnnotation) in.readObject();
187: in.close();
188: } catch (Exception e) {
189: e.printStackTrace();
190: }
191: assertEquals(a1, a2);
192:
193: }
194:
195: }
|