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: * LegendTitleTests.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: LegendTitleTests.java,v 1.1.2.2 2007/03/16 15:33:02 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 25-Feb-2005 : Version 1 (DG);
040: * 16-Mar-2005 : Extended testEquals() (DG);
041: *
042: */
043:
044: package org.jfree.chart.title.junit;
045:
046: import java.awt.Color;
047: import java.awt.Font;
048: import java.awt.GradientPaint;
049: import java.awt.geom.Rectangle2D;
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.plot.XYPlot;
062: import org.jfree.chart.title.LegendTitle;
063: import org.jfree.ui.RectangleAnchor;
064: import org.jfree.ui.RectangleEdge;
065:
066: /**
067: * Some tests for the {@link LegendTitle} class.
068: */
069: public class LegendTitleTests extends TestCase {
070:
071: /**
072: * Returns the tests as a test suite.
073: *
074: * @return The test suite.
075: */
076: public static Test suite() {
077: return new TestSuite(LegendTitleTests.class);
078: }
079:
080: /**
081: * Constructs a new set of tests.
082: *
083: * @param name the name of the tests.
084: */
085: public LegendTitleTests(String name) {
086: super (name);
087: }
088:
089: /**
090: * Check that the equals() method distinguishes all fields.
091: */
092: public void testEquals() {
093: XYPlot plot1 = new XYPlot();
094: LegendTitle t1 = new LegendTitle(plot1);
095: LegendTitle t2 = new LegendTitle(plot1);
096: assertEquals(t1, t2);
097:
098: t1.setBackgroundPaint(new GradientPaint(1.0f, 2.0f, Color.red,
099: 3.0f, 4.0f, Color.yellow));
100: assertFalse(t1.equals(t2));
101: t2.setBackgroundPaint(new GradientPaint(1.0f, 2.0f, Color.red,
102: 3.0f, 4.0f, Color.yellow));
103: assertTrue(t1.equals(t2));
104:
105: t1.setLegendItemGraphicEdge(RectangleEdge.BOTTOM);
106: assertFalse(t1.equals(t2));
107: t2.setLegendItemGraphicEdge(RectangleEdge.BOTTOM);
108: assertTrue(t1.equals(t2));
109:
110: t1.setLegendItemGraphicAnchor(RectangleAnchor.BOTTOM_LEFT);
111: assertFalse(t1.equals(t2));
112: t2.setLegendItemGraphicAnchor(RectangleAnchor.BOTTOM_LEFT);
113: assertTrue(t1.equals(t2));
114:
115: t1.setLegendItemGraphicLocation(RectangleAnchor.TOP_LEFT);
116: assertFalse(t1.equals(t2));
117: t2.setLegendItemGraphicLocation(RectangleAnchor.TOP_LEFT);
118: assertTrue(t1.equals(t2));
119:
120: t1.setItemFont(new Font("Dialog", Font.PLAIN, 19));
121: assertFalse(t1.equals(t2));
122: t2.setItemFont(new Font("Dialog", Font.PLAIN, 19));
123: assertTrue(t1.equals(t2));
124: }
125:
126: /**
127: * Two objects that are equal are required to return the same hashCode.
128: */
129: public void testHashcode() {
130: XYPlot plot1 = new XYPlot();
131: LegendTitle t1 = new LegendTitle(plot1);
132: LegendTitle t2 = new LegendTitle(plot1);
133: assertTrue(t1.equals(t2));
134: int h1 = t1.hashCode();
135: int h2 = t2.hashCode();
136: assertEquals(h1, h2);
137: }
138:
139: /**
140: * Confirm that cloning works.
141: */
142: public void testCloning() {
143: XYPlot plot = new XYPlot();
144: Rectangle2D bounds1 = new Rectangle2D.Double(10.0, 20.0, 30.0,
145: 40.0);
146: LegendTitle t1 = new LegendTitle(plot);
147: t1.setBackgroundPaint(new GradientPaint(1.0f, 2.0f, Color.red,
148: 3.0f, 4.0f, Color.yellow));
149: t1.setBounds(bounds1);
150: LegendTitle t2 = null;
151: try {
152: t2 = (LegendTitle) t1.clone();
153: } catch (CloneNotSupportedException e) {
154: e.printStackTrace();
155: }
156: assertTrue(t1 != t2);
157: assertTrue(t1.getClass() == t2.getClass());
158: assertTrue(t1.equals(t2));
159:
160: // check independence
161: bounds1.setFrame(40.0, 30.0, 20.0, 10.0);
162: assertFalse(t1.equals(t2));
163: t2.setBounds(new Rectangle2D.Double(40.0, 30.0, 20.0, 10.0));
164: assertTrue(t1.equals(t2));
165: }
166:
167: /**
168: * Serialize an instance, restore it, and check for equality.
169: */
170: public void testSerialization() {
171:
172: XYPlot plot = new XYPlot();
173: LegendTitle t1 = new LegendTitle(plot);
174: LegendTitle t2 = null;
175:
176: try {
177: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
178: ObjectOutput out = new ObjectOutputStream(buffer);
179: out.writeObject(t1);
180: out.close();
181:
182: ObjectInput in = new ObjectInputStream(
183: new ByteArrayInputStream(buffer.toByteArray()));
184: t2 = (LegendTitle) in.readObject();
185: in.close();
186: } catch (Exception e) {
187: fail(e.toString());
188: }
189: assertTrue(t1.equals(t2));
190: assertTrue(t2.getSources()[0].equals(plot));
191: }
192:
193: }
|