001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, 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: * XYTitleAnnotationTests.java
029: * ---------------------------
030: * (C) Copyright 2007, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: XYTitleAnnotationTests.java,v 1.1.2.1 2007/04/30 20:49:05 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 30-Apr-2007 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.experimental.chart.annotations.junit;
044:
045: import java.io.ByteArrayInputStream;
046: import java.io.ByteArrayOutputStream;
047: import java.io.ObjectInput;
048: import java.io.ObjectInputStream;
049: import java.io.ObjectOutput;
050: import java.io.ObjectOutputStream;
051:
052: import junit.framework.Test;
053: import junit.framework.TestCase;
054: import junit.framework.TestSuite;
055:
056: import org.jfree.chart.title.TextTitle;
057: import org.jfree.experimental.chart.annotations.XYTitleAnnotation;
058:
059: /**
060: * Tests for the {@link XYTitleAnnotation} class.
061: */
062: public class XYTitleAnnotationTests extends TestCase {
063:
064: /**
065: * Returns the tests as a test suite.
066: *
067: * @return The test suite.
068: */
069: public static Test suite() {
070: return new TestSuite(XYTitleAnnotationTests.class);
071: }
072:
073: /**
074: * Constructs a new set of tests.
075: *
076: * @param name the name of the tests.
077: */
078: public XYTitleAnnotationTests(String name) {
079: super (name);
080: }
081:
082: /**
083: * Confirm that the equals method can distinguish all the required fields.
084: */
085: public void testEquals() {
086: TextTitle t = new TextTitle("Title");
087: XYTitleAnnotation a1 = new XYTitleAnnotation(1.0, 2.0, t);
088: XYTitleAnnotation a2 = new XYTitleAnnotation(1.0, 2.0, t);
089: assertTrue(a1.equals(a2));
090:
091: a1 = new XYTitleAnnotation(1.1, 2.0, t);
092: assertFalse(a1.equals(a2));
093: a2 = new XYTitleAnnotation(1.1, 2.0, t);
094: assertTrue(a1.equals(a2));
095:
096: a1 = new XYTitleAnnotation(1.1, 2.2, t);
097: assertFalse(a1.equals(a2));
098: a2 = new XYTitleAnnotation(1.1, 2.2, t);
099: assertTrue(a1.equals(a2));
100:
101: TextTitle t2 = new TextTitle("Title 2");
102: a1 = new XYTitleAnnotation(1.1, 2.2, t2);
103: assertFalse(a1.equals(a2));
104: a2 = new XYTitleAnnotation(1.1, 2.2, t2);
105: assertTrue(a1.equals(a2));
106: }
107:
108: /**
109: * Two objects that are equal are required to return the same hashCode.
110: */
111: public void testHashCode() {
112: TextTitle t = new TextTitle("Title");
113: XYTitleAnnotation a1 = new XYTitleAnnotation(1.0, 2.0, t);
114: XYTitleAnnotation a2 = new XYTitleAnnotation(1.0, 2.0, t);
115: assertTrue(a1.equals(a2));
116: int h1 = a1.hashCode();
117: int h2 = a2.hashCode();
118: assertEquals(h1, h2);
119: }
120:
121: /**
122: * Confirm that cloning works.
123: */
124: public void testCloning() {
125: TextTitle t = new TextTitle("Title");
126: XYTitleAnnotation a1 = new XYTitleAnnotation(1.0, 2.0, t);
127: XYTitleAnnotation a2 = null;
128: try {
129: a2 = (XYTitleAnnotation) a1.clone();
130: } catch (CloneNotSupportedException e) {
131: e.printStackTrace();
132: }
133: assertTrue(a1 != a2);
134: assertTrue(a1.getClass() == a2.getClass());
135: assertTrue(a1.equals(a2));
136: }
137:
138: /**
139: * Serialize an instance, restore it, and check for equality.
140: */
141: public void testSerialization() {
142: TextTitle t = new TextTitle("Title");
143: XYTitleAnnotation a1 = new XYTitleAnnotation(1.0, 2.0, t);
144: XYTitleAnnotation a2 = null;
145: try {
146: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
147: ObjectOutput out = new ObjectOutputStream(buffer);
148: out.writeObject(a1);
149: out.close();
150:
151: ObjectInput in = new ObjectInputStream(
152: new ByteArrayInputStream(buffer.toByteArray()));
153: a2 = (XYTitleAnnotation) in.readObject();
154: in.close();
155: } catch (Exception e) {
156: e.printStackTrace();
157: }
158: assertEquals(a1, a2);
159: }
160:
161: }
|