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: * LegendGraphicTests.java
029: * -----------------------
030: * (C) Copyright 2005, 2006, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: LegendGraphicTests.java,v 1.1.2.2 2006/12/13 11:23:39 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 01-Sep-2005 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.chart.title.junit;
044:
045: import java.awt.BasicStroke;
046: import java.awt.Color;
047: import java.awt.Rectangle;
048: import java.awt.Stroke;
049: import java.awt.geom.Line2D;
050: import java.awt.geom.Rectangle2D;
051: import java.io.ByteArrayInputStream;
052: import java.io.ByteArrayOutputStream;
053: import java.io.ObjectInput;
054: import java.io.ObjectInputStream;
055: import java.io.ObjectOutput;
056: import java.io.ObjectOutputStream;
057:
058: import junit.framework.Test;
059: import junit.framework.TestCase;
060: import junit.framework.TestSuite;
061:
062: import org.jfree.chart.title.LegendGraphic;
063: import org.jfree.ui.GradientPaintTransformType;
064: import org.jfree.ui.RectangleAnchor;
065: import org.jfree.ui.StandardGradientPaintTransformer;
066:
067: /**
068: * Tests for the {@link LegendGraphic} class.
069: */
070: public class LegendGraphicTests extends TestCase {
071:
072: /**
073: * Returns the tests as a test suite.
074: *
075: * @return The test suite.
076: */
077: public static Test suite() {
078: return new TestSuite(LegendGraphicTests.class);
079: }
080:
081: /**
082: * Constructs a new set of tests.
083: *
084: * @param name the name of the tests.
085: */
086: public LegendGraphicTests(String name) {
087: super (name);
088: }
089:
090: /**
091: * Check that the equals() method distinguishes all fields.
092: */
093: public void testEquals() {
094: LegendGraphic g1 = new LegendGraphic(new Rectangle2D.Double(
095: 1.0, 2.0, 3.0, 4.0), Color.black);
096: LegendGraphic g2 = new LegendGraphic(new Rectangle2D.Double(
097: 1.0, 2.0, 3.0, 4.0), Color.black);
098: assertEquals(g1, g2);
099: assertEquals(g2, g1);
100:
101: // shapeVisible
102: g1.setShapeVisible(!g1.isShapeVisible());
103: assertFalse(g1.equals(g2));
104: g2.setShapeVisible(!g2.isShapeVisible());
105: assertTrue(g1.equals(g2));
106:
107: // shape
108: g1.setShape(new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0));
109: assertFalse(g1.equals(g2));
110: g2.setShape(new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0));
111: assertTrue(g1.equals(g2));
112:
113: // shapeFilled
114: g1.setShapeFilled(!g1.isShapeFilled());
115: assertFalse(g1.equals(g2));
116: g2.setShapeFilled(!g2.isShapeFilled());
117: assertTrue(g1.equals(g2));
118:
119: // fillPaint
120: g1.setFillPaint(Color.green);
121: assertFalse(g1.equals(g2));
122: g2.setFillPaint(Color.green);
123: assertTrue(g1.equals(g2));
124:
125: // shapeOutlineVisible
126: g1.setShapeOutlineVisible(!g1.isShapeOutlineVisible());
127: assertFalse(g1.equals(g2));
128: g2.setShapeOutlineVisible(!g2.isShapeOutlineVisible());
129: assertTrue(g1.equals(g2));
130:
131: // outlinePaint
132: g1.setOutlinePaint(Color.green);
133: assertFalse(g1.equals(g2));
134: g2.setOutlinePaint(Color.green);
135: assertTrue(g1.equals(g2));
136:
137: // outlineStroke
138: g1.setOutlineStroke(new BasicStroke(1.23f));
139: assertFalse(g1.equals(g2));
140: g2.setOutlineStroke(new BasicStroke(1.23f));
141: assertTrue(g1.equals(g2));
142:
143: // shapeAnchor
144: g1.setShapeAnchor(RectangleAnchor.BOTTOM_RIGHT);
145: assertFalse(g1.equals(g2));
146: g2.setShapeAnchor(RectangleAnchor.BOTTOM_RIGHT);
147: assertTrue(g1.equals(g2));
148:
149: // shapeLocation
150: g1.setShapeLocation(RectangleAnchor.BOTTOM_RIGHT);
151: assertFalse(g1.equals(g2));
152: g2.setShapeLocation(RectangleAnchor.BOTTOM_RIGHT);
153: assertTrue(g1.equals(g2));
154:
155: // lineVisible
156: g1.setLineVisible(!g1.isLineVisible());
157: assertFalse(g1.equals(g2));
158: g2.setLineVisible(!g2.isLineVisible());
159: assertTrue(g1.equals(g2));
160:
161: // line
162: g1.setLine(new Line2D.Double(1.0, 2.0, 3.0, 4.0));
163: assertFalse(g1.equals(g2));
164: g2.setLine(new Line2D.Double(1.0, 2.0, 3.0, 4.0));
165: assertTrue(g1.equals(g2));
166:
167: // linePaint
168: g1.setLinePaint(Color.green);
169: assertFalse(g1.equals(g2));
170: g2.setLinePaint(Color.green);
171: assertTrue(g1.equals(g2));
172:
173: // lineStroke
174: g1.setLineStroke(new BasicStroke(1.23f));
175: assertFalse(g1.equals(g2));
176: g2.setLineStroke(new BasicStroke(1.23f));
177: assertTrue(g1.equals(g2));
178:
179: // fillPaintTransformer
180: g1
181: .setFillPaintTransformer(new StandardGradientPaintTransformer(
182: GradientPaintTransformType.CENTER_HORIZONTAL));
183: assertFalse(g1.equals(g2));
184: g2
185: .setFillPaintTransformer(new StandardGradientPaintTransformer(
186: GradientPaintTransformType.CENTER_HORIZONTAL));
187: assertTrue(g1.equals(g2));
188:
189: }
190:
191: /**
192: * Two objects that are equal are required to return the same hashCode.
193: */
194: public void testHashcode() {
195: LegendGraphic g1 = new LegendGraphic(new Rectangle2D.Double(
196: 1.0, 2.0, 3.0, 4.0), Color.black);
197: LegendGraphic g2 = new LegendGraphic(new Rectangle2D.Double(
198: 1.0, 2.0, 3.0, 4.0), Color.black);
199: assertTrue(g1.equals(g2));
200: int h1 = g1.hashCode();
201: int h2 = g2.hashCode();
202: assertEquals(h1, h2);
203: }
204:
205: /**
206: * Confirm that cloning works.
207: */
208: public void testCloning() {
209: Rectangle r = new Rectangle(1, 2, 3, 4);
210: LegendGraphic g1 = new LegendGraphic(r, Color.black);
211: LegendGraphic g2 = null;
212: try {
213: g2 = (LegendGraphic) g1.clone();
214: } catch (CloneNotSupportedException e) {
215: e.printStackTrace();
216: }
217: assertTrue(g1 != g2);
218: assertTrue(g1.getClass() == g2.getClass());
219: assertTrue(g1.equals(g2));
220:
221: // check independence
222: r.setBounds(4, 3, 2, 1);
223: assertFalse(g1.equals(g2));
224: }
225:
226: /**
227: * A test for cloning - checks that the line shape is cloned correctly.
228: */
229: public void testCloning2() {
230: Rectangle r = new Rectangle(1, 2, 3, 4);
231: LegendGraphic g1 = new LegendGraphic(r, Color.black);
232: Line2D l = new Line2D.Double(1.0, 2.0, 3.0, 4.0);
233: g1.setLine(l);
234: LegendGraphic g2 = null;
235: try {
236: g2 = (LegendGraphic) g1.clone();
237: } catch (CloneNotSupportedException e) {
238: e.printStackTrace();
239: }
240: assertTrue(g1 != g2);
241: assertTrue(g1.getClass() == g2.getClass());
242: assertTrue(g1.equals(g2));
243:
244: // check independence
245: l.setLine(4.0, 3.0, 2.0, 1.0);
246: assertFalse(g1.equals(g2));
247:
248: }
249:
250: /**
251: * Serialize an instance, restore it, and check for equality.
252: */
253: public void testSerialization() {
254:
255: Stroke s = new BasicStroke(1.23f);
256: LegendGraphic g1 = new LegendGraphic(new Rectangle2D.Double(
257: 1.0, 2.0, 3.0, 4.0), Color.black);
258: g1.setOutlineStroke(s);
259: LegendGraphic g2 = null;
260:
261: try {
262: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
263: ObjectOutput out = new ObjectOutputStream(buffer);
264: out.writeObject(g1);
265: out.close();
266:
267: ObjectInput in = new ObjectInputStream(
268: new ByteArrayInputStream(buffer.toByteArray()));
269: g2 = (LegendGraphic) in.readObject();
270: in.close();
271: } catch (Exception e) {
272: System.out.println(e.toString());
273: }
274: assertTrue(g1.equals(g2));
275:
276: }
277:
278: }
|