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: * LegendItemCollectionTests.java
029: * ------------------------------
030: * (C) Copyright 2005, 2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: LegendItemCollectionTests.java,v 1.1.2.2 2007/03/02 11:16:20 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 18-Apr-2005 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.chart.junit;
044:
045: import java.awt.BasicStroke;
046: import java.awt.Color;
047: import java.awt.geom.Line2D;
048: import java.awt.geom.Rectangle2D;
049: import java.io.ByteArrayInputStream;
050: import java.io.ByteArrayOutputStream;
051: import java.io.ObjectInput;
052: import java.io.ObjectInputStream;
053: import java.io.ObjectOutput;
054: import java.io.ObjectOutputStream;
055:
056: import junit.framework.Test;
057: import junit.framework.TestCase;
058: import junit.framework.TestSuite;
059:
060: import org.jfree.chart.LegendItem;
061: import org.jfree.chart.LegendItemCollection;
062:
063: /**
064: * Tests for the {@link LegendItemCollection} class.
065: */
066: public class LegendItemCollectionTests extends TestCase {
067:
068: /**
069: * Returns the tests as a test suite.
070: *
071: * @return The test suite.
072: */
073: public static Test suite() {
074: return new TestSuite(LegendItemCollectionTests.class);
075: }
076:
077: /**
078: * Constructs a new set of tests.
079: *
080: * @param name the name of the tests.
081: */
082: public LegendItemCollectionTests(String name) {
083: super (name);
084: }
085:
086: /**
087: * Confirm that the equals method can distinguish all the required fields.
088: */
089: public void testEquals() {
090:
091: LegendItemCollection c1 = new LegendItemCollection();
092: LegendItemCollection c2 = new LegendItemCollection();
093: assertTrue(c1.equals(c2));
094: assertTrue(c2.equals(c1));
095:
096: LegendItem item1 = new LegendItem("Label", "Description",
097: "ToolTip", "URL", true, new Rectangle2D.Double(1.0,
098: 2.0, 3.0, 4.0), true, Color.red, true,
099: Color.blue, new BasicStroke(1.2f), true,
100: new Line2D.Double(1.0, 2.0, 3.0, 4.0), new BasicStroke(
101: 2.1f), Color.green);
102: LegendItem item2 = new LegendItem("Label", "Description",
103: "ToolTip", "URL", true, new Rectangle2D.Double(1.0,
104: 2.0, 3.0, 4.0), true, Color.red, true,
105: Color.blue, new BasicStroke(1.2f), true,
106: new Line2D.Double(1.0, 2.0, 3.0, 4.0), new BasicStroke(
107: 2.1f), Color.green);
108: c1.add(item1);
109: c2.add(item2);
110: assertTrue(c1.equals(c2));
111:
112: }
113:
114: /**
115: * Serialize an instance, restore it, and check for equality.
116: */
117: public void testSerialization() {
118: LegendItemCollection c1 = new LegendItemCollection();
119: c1.add(new LegendItem("Item", "Description", "ToolTip", "URL",
120: new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0), Color.red));
121: LegendItemCollection c2 = null;
122: try {
123: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
124: ObjectOutput out = new ObjectOutputStream(buffer);
125: out.writeObject(c1);
126: out.close();
127:
128: ObjectInput in = new ObjectInputStream(
129: new ByteArrayInputStream(buffer.toByteArray()));
130: c2 = (LegendItemCollection) in.readObject();
131: in.close();
132: } catch (Exception e) {
133: e.printStackTrace();
134: }
135: assertEquals(c1, c2);
136: }
137:
138: /**
139: * Confirm that cloning works.
140: */
141: public void testCloning() {
142:
143: LegendItemCollection c1 = new LegendItemCollection();
144: c1.add(new LegendItem("Item", "Description", "ToolTip", "URL",
145: new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0), Color.red));
146: LegendItemCollection c2 = null;
147: try {
148: c2 = (LegendItemCollection) c1.clone();
149: } catch (CloneNotSupportedException e) {
150: e.printStackTrace();
151: }
152: assertTrue(c1 != c2);
153: assertTrue(c1.getClass() == c2.getClass());
154: assertTrue(c1.equals(c2));
155:
156: }
157:
158: }
|