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: * LegendItemEntityTests.java
029: * --------------------------
030: * (C) Copyright 2004-2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: LegendItemEntityTests.java,v 1.1.2.2 2007/05/18 10:28:36 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 20-May-2004 : Version 1 (DG);
040: * 18-May-2007 : Added checks for new fields (DG);
041: *
042: */
043:
044: package org.jfree.chart.entity.junit;
045:
046: import java.awt.geom.Rectangle2D;
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.entity.LegendItemEntity;
059: import org.jfree.data.category.DefaultCategoryDataset;
060:
061: /**
062: * Tests for the {@link LegendItemEntity} class.
063: */
064: public class LegendItemEntityTests extends TestCase {
065:
066: /**
067: * Returns the tests as a test suite.
068: *
069: * @return The test suite.
070: */
071: public static Test suite() {
072: return new TestSuite(LegendItemEntityTests.class);
073: }
074:
075: /**
076: * Constructs a new set of tests.
077: *
078: * @param name the name of the tests.
079: */
080: public LegendItemEntityTests(String name) {
081: super (name);
082: }
083:
084: /**
085: * Confirm that the equals method can distinguish all the required fields.
086: */
087: public void testEquals() {
088: LegendItemEntity e1 = new LegendItemEntity(
089: new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0));
090: LegendItemEntity e2 = new LegendItemEntity(
091: new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0));
092: assertTrue(e1.equals(e2));
093:
094: e1.setArea(new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0));
095: assertFalse(e1.equals(e2));
096: e2.setArea(new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0));
097: assertTrue(e1.equals(e2));
098:
099: e1.setToolTipText("New ToolTip");
100: assertFalse(e1.equals(e2));
101: e2.setToolTipText("New ToolTip");
102: assertTrue(e1.equals(e2));
103:
104: e1.setURLText("New URL");
105: assertFalse(e1.equals(e2));
106: e2.setURLText("New URL");
107: assertTrue(e1.equals(e2));
108:
109: e1.setDataset(new DefaultCategoryDataset());
110: assertFalse(e1.equals(e2));
111: e2.setDataset(new DefaultCategoryDataset());
112: assertTrue(e1.equals(e2));
113:
114: e1.setSeriesKey("A");
115: assertFalse(e1.equals(e2));
116: e2.setSeriesKey("A");
117: assertTrue(e1.equals(e2));
118:
119: e1.setSeriesIndex(7);
120: assertFalse(e1.equals(e2));
121: e2.setSeriesIndex(7);
122: assertTrue(e1.equals(e2));
123:
124: }
125:
126: /**
127: * Confirm that cloning works.
128: */
129: public void testCloning() {
130: LegendItemEntity e1 = new LegendItemEntity(
131: new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0));
132: LegendItemEntity e2 = null;
133:
134: try {
135: e2 = (LegendItemEntity) e1.clone();
136: } catch (CloneNotSupportedException e) {
137: e.printStackTrace();
138: }
139: assertTrue(e1 != e2);
140: assertTrue(e1.getClass() == e2.getClass());
141: assertTrue(e1.equals(e2));
142: }
143:
144: /**
145: * Serialize an instance, restore it, and check for equality.
146: */
147: public void testSerialization() {
148: LegendItemEntity e1 = new LegendItemEntity(
149: new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0));
150: LegendItemEntity e2 = null;
151: try {
152: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
153: ObjectOutput out = new ObjectOutputStream(buffer);
154: out.writeObject(e1);
155: out.close();
156:
157: ObjectInput in = new ObjectInputStream(
158: new ByteArrayInputStream(buffer.toByteArray()));
159: e2 = (LegendItemEntity) in.readObject();
160: in.close();
161: } catch (Exception e) {
162: e.printStackTrace();
163: }
164: assertEquals(e1, e2);
165: }
166:
167: }
|