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: * LegendItemTests.java
029: * --------------------
030: * (C) Copyright 2004-2006, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: LegendItemTests.java,v 1.1.2.2 2006/12/13 11:23:39 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 04-Jun-2004 : Version 1 (DG);
040: * 10-Dec-2005 : Addded new test to cover bug report 1374328 (DG);
041: * 13-Dec-2006 : Extended testEquals() for new fillPaintTransformer
042: * attribute (DG);
043: *
044: */
045:
046: package org.jfree.chart.junit;
047:
048: import java.awt.BasicStroke;
049: import java.awt.Color;
050: import java.awt.Font;
051: import java.awt.font.TextAttribute;
052: import java.awt.geom.Line2D;
053: import java.awt.geom.Rectangle2D;
054: import java.io.ByteArrayInputStream;
055: import java.io.ByteArrayOutputStream;
056: import java.io.ObjectInput;
057: import java.io.ObjectInputStream;
058: import java.io.ObjectOutput;
059: import java.io.ObjectOutputStream;
060: import java.text.AttributedString;
061:
062: import junit.framework.Test;
063: import junit.framework.TestCase;
064: import junit.framework.TestSuite;
065:
066: import org.jfree.chart.LegendItem;
067: import org.jfree.ui.GradientPaintTransformType;
068: import org.jfree.ui.StandardGradientPaintTransformer;
069:
070: /**
071: * Tests for the {@link LegendItem} class.
072: */
073: public class LegendItemTests extends TestCase {
074:
075: /**
076: * Returns the tests as a test suite.
077: *
078: * @return The test suite.
079: */
080: public static Test suite() {
081: return new TestSuite(LegendItemTests.class);
082: }
083:
084: /**
085: * Constructs a new set of tests.
086: *
087: * @param name the name of the tests.
088: */
089: public LegendItemTests(String name) {
090: super (name);
091: }
092:
093: /**
094: * Confirm that the equals method can distinguish all the required fields.
095: */
096: public void testEquals() {
097:
098: LegendItem item1 = new LegendItem("Label", "Description",
099: "ToolTip", "URL", true, new Rectangle2D.Double(1.0,
100: 2.0, 3.0, 4.0), true, Color.red, true,
101: Color.blue, new BasicStroke(1.2f), true,
102: new Line2D.Double(1.0, 2.0, 3.0, 4.0), new BasicStroke(
103: 2.1f), Color.green);
104: LegendItem item2 = new LegendItem("Label", "Description",
105: "ToolTip", "URL", true, new Rectangle2D.Double(1.0,
106: 2.0, 3.0, 4.0), true, Color.red, true,
107: Color.blue, new BasicStroke(1.2f), true,
108: new Line2D.Double(1.0, 2.0, 3.0, 4.0), new BasicStroke(
109: 2.1f), Color.green);
110: assertTrue(item1.equals(item2));
111: assertTrue(item2.equals(item1));
112:
113: item1 = new LegendItem("Label2", "Description", "ToolTip",
114: "URL", true,
115: new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0), true,
116: Color.red, true, Color.blue, new BasicStroke(1.2f),
117: true, new Line2D.Double(1.0, 2.0, 3.0, 4.0),
118: new BasicStroke(2.1f), Color.green);
119: assertFalse(item1.equals(item2));
120: item2 = new LegendItem("Label2", "Description", "ToolTip",
121: "URL", true,
122: new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0), true,
123: Color.red, true, Color.blue, new BasicStroke(1.2f),
124: true, new Line2D.Double(1.0, 2.0, 3.0, 4.0),
125: new BasicStroke(2.1f), Color.green);
126: assertTrue(item1.equals(item2));
127:
128: item1 = new LegendItem("Label2", "Description2", "ToolTip",
129: "URL", true,
130: new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0), true,
131: Color.red, true, Color.blue, new BasicStroke(1.2f),
132: true, new Line2D.Double(1.0, 2.0, 3.0, 4.0),
133: new BasicStroke(2.1f), Color.green);
134: assertFalse(item1.equals(item2));
135: item2 = new LegendItem("Label2", "Description2", "ToolTip",
136: "URL", true,
137: new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0), true,
138: Color.red, true, Color.blue, new BasicStroke(1.2f),
139: true, new Line2D.Double(1.0, 2.0, 3.0, 4.0),
140: new BasicStroke(2.1f), Color.green);
141: assertTrue(item1.equals(item2));
142:
143: item1 = new LegendItem("Label2", "Description2", "ToolTip",
144: "URL", false,
145: new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0), true,
146: Color.red, true, Color.blue, new BasicStroke(1.2f),
147: true, new Line2D.Double(1.0, 2.0, 3.0, 4.0),
148: new BasicStroke(2.1f), Color.green);
149: assertFalse(item1.equals(item2));
150: item2 = new LegendItem("Label2", "Description2", "ToolTip",
151: "URL", false,
152: new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0), true,
153: Color.red, true, Color.blue, new BasicStroke(1.2f),
154: true, new Line2D.Double(1.0, 2.0, 3.0, 4.0),
155: new BasicStroke(2.1f), Color.green);
156: assertTrue(item1.equals(item2));
157:
158: item1 = new LegendItem("Label2", "Description2", "ToolTip",
159: "URL", false,
160: new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), true,
161: Color.red, true, Color.blue, new BasicStroke(1.2f),
162: true, new Line2D.Double(1.0, 2.0, 3.0, 4.0),
163: new BasicStroke(2.1f), Color.green);
164: assertFalse(item1.equals(item2));
165: item2 = new LegendItem("Label2", "Description2", "ToolTip",
166: "URL", false,
167: new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), true,
168: Color.red, true, Color.blue, new BasicStroke(1.2f),
169: true, new Line2D.Double(1.0, 2.0, 3.0, 4.0),
170: new BasicStroke(2.1f), Color.green);
171: assertTrue(item1.equals(item2));
172:
173: item1 = new LegendItem("Label2", "Description2", "ToolTip",
174: "URL", false,
175: new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
176: Color.red, true, Color.blue, new BasicStroke(1.2f),
177: true, new Line2D.Double(1.0, 2.0, 3.0, 4.0),
178: new BasicStroke(2.1f), Color.green);
179: assertFalse(item1.equals(item2));
180: item2 = new LegendItem("Label2", "Description2", "ToolTip",
181: "URL", false,
182: new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
183: Color.red, true, Color.blue, new BasicStroke(1.2f),
184: true, new Line2D.Double(1.0, 2.0, 3.0, 4.0),
185: new BasicStroke(2.1f), Color.green);
186: assertTrue(item1.equals(item2));
187:
188: item1 = new LegendItem("Label2", "Description2", "ToolTip",
189: "URL", false,
190: new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
191: Color.black, true, Color.blue, new BasicStroke(1.2f),
192: true, new Line2D.Double(1.0, 2.0, 3.0, 4.0),
193: new BasicStroke(2.1f), Color.green);
194: assertFalse(item1.equals(item2));
195: item2 = new LegendItem("Label2", "Description2", "ToolTip",
196: "URL", false,
197: new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
198: Color.black, true, Color.blue, new BasicStroke(1.2f),
199: true, new Line2D.Double(1.0, 2.0, 3.0, 4.0),
200: new BasicStroke(2.1f), Color.green);
201: assertTrue(item1.equals(item2));
202:
203: item1 = new LegendItem("Label2", "Description2", "ToolTip",
204: "URL", false,
205: new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
206: Color.black, false, Color.blue, new BasicStroke(1.2f),
207: true, new Line2D.Double(1.0, 2.0, 3.0, 4.0),
208: new BasicStroke(2.1f), Color.green);
209: assertFalse(item1.equals(item2));
210: item2 = new LegendItem("Label2", "Description2", "ToolTip",
211: "URL", false,
212: new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
213: Color.black, false, Color.blue, new BasicStroke(1.2f),
214: true, new Line2D.Double(1.0, 2.0, 3.0, 4.0),
215: new BasicStroke(2.1f), Color.green);
216: assertTrue(item1.equals(item2));
217:
218: item1 = new LegendItem("Label2", "Description2", "ToolTip",
219: "URL", false,
220: new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
221: Color.black, false, Color.yellow,
222: new BasicStroke(1.2f), true, new Line2D.Double(1.0,
223: 2.0, 3.0, 4.0), new BasicStroke(2.1f),
224: Color.green);
225: assertFalse(item1.equals(item2));
226: item2 = new LegendItem("Label2", "Description2", "ToolTip",
227: "URL", false,
228: new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
229: Color.black, false, Color.yellow,
230: new BasicStroke(1.2f), true, new Line2D.Double(1.0,
231: 2.0, 3.0, 4.0), new BasicStroke(2.1f),
232: Color.green);
233: assertTrue(item1.equals(item2));
234:
235: item1 = new LegendItem("Label2", "Description2", "ToolTip",
236: "URL", false,
237: new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
238: Color.black, false, Color.yellow,
239: new BasicStroke(2.1f), true, new Line2D.Double(1.0,
240: 2.0, 3.0, 4.0), new BasicStroke(2.1f),
241: Color.green);
242: assertFalse(item1.equals(item2));
243: item2 = new LegendItem("Label2", "Description2", "ToolTip",
244: "URL", false,
245: new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
246: Color.black, false, Color.yellow,
247: new BasicStroke(2.1f), true, new Line2D.Double(1.0,
248: 2.0, 3.0, 4.0), new BasicStroke(2.1f),
249: Color.green);
250: assertTrue(item1.equals(item2));
251:
252: item1 = new LegendItem("Label2", "Description2", "ToolTip",
253: "URL", false,
254: new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
255: Color.black, false, Color.yellow,
256: new BasicStroke(2.1f), false, new Line2D.Double(1.0,
257: 2.0, 3.0, 4.0), new BasicStroke(2.1f),
258: Color.green);
259: assertFalse(item1.equals(item2));
260: item2 = new LegendItem("Label2", "Description2", "ToolTip",
261: "URL", false,
262: new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
263: Color.black, false, Color.yellow,
264: new BasicStroke(2.1f), false, new Line2D.Double(1.0,
265: 2.0, 3.0, 4.0), new BasicStroke(2.1f),
266: Color.green);
267: assertTrue(item1.equals(item2));
268:
269: item1 = new LegendItem("Label2", "Description2", "ToolTip",
270: "URL", false,
271: new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
272: Color.black, false, Color.yellow,
273: new BasicStroke(2.1f), false, new Line2D.Double(4.0,
274: 3.0, 2.0, 1.0), new BasicStroke(2.1f),
275: Color.green);
276: assertFalse(item1.equals(item2));
277: item2 = new LegendItem("Label2", "Description2", "ToolTip",
278: "URL", false,
279: new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
280: Color.black, false, Color.yellow,
281: new BasicStroke(2.1f), false, new Line2D.Double(4.0,
282: 3.0, 2.0, 1.0), new BasicStroke(2.1f),
283: Color.green);
284: assertTrue(item1.equals(item2));
285:
286: item1 = new LegendItem("Label2", "Description2", "ToolTip",
287: "URL", false,
288: new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
289: Color.black, false, Color.yellow,
290: new BasicStroke(2.1f), false, new Line2D.Double(4.0,
291: 3.0, 2.0, 1.0), new BasicStroke(3.3f),
292: Color.green);
293: assertFalse(item1.equals(item2));
294: item2 = new LegendItem("Label2", "Description2", "ToolTip",
295: "URL", false,
296: new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
297: Color.black, false, Color.yellow,
298: new BasicStroke(2.1f), false, new Line2D.Double(4.0,
299: 3.0, 2.0, 1.0), new BasicStroke(3.3f),
300: Color.green);
301: assertTrue(item1.equals(item2));
302:
303: item1 = new LegendItem("Label2", "Description2", "ToolTip",
304: "URL", false,
305: new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
306: Color.black, false, Color.yellow,
307: new BasicStroke(2.1f), false, new Line2D.Double(4.0,
308: 3.0, 2.0, 1.0), new BasicStroke(3.3f),
309: Color.white);
310: assertFalse(item1.equals(item2));
311: item2 = new LegendItem("Label2", "Description2", "ToolTip",
312: "URL", false,
313: new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
314: Color.black, false, Color.yellow,
315: new BasicStroke(2.1f), false, new Line2D.Double(4.0,
316: 3.0, 2.0, 1.0), new BasicStroke(3.3f),
317: Color.white);
318: assertTrue(item1.equals(item2));
319:
320: // fillPaintTransformer
321: item1
322: .setFillPaintTransformer(new StandardGradientPaintTransformer(
323: GradientPaintTransformType.CENTER_VERTICAL));
324: assertFalse(item1.equals(item2));
325: item2
326: .setFillPaintTransformer(new StandardGradientPaintTransformer(
327: GradientPaintTransformType.CENTER_VERTICAL));
328: assertTrue(item1.equals(item2));
329: }
330:
331: /**
332: * Serialize an instance, restore it, and check for equality.
333: */
334: public void testSerialization() {
335: LegendItem item1 = new LegendItem("Item", "Description",
336: "ToolTip", "URL", new Rectangle2D.Double(1.0, 2.0, 3.0,
337: 4.0), Color.red);
338: LegendItem item2 = null;
339: try {
340: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
341: ObjectOutput out = new ObjectOutputStream(buffer);
342: out.writeObject(item1);
343: out.close();
344:
345: ObjectInput in = new ObjectInputStream(
346: new ByteArrayInputStream(buffer.toByteArray()));
347: item2 = (LegendItem) in.readObject();
348: in.close();
349: } catch (Exception e) {
350: e.printStackTrace();
351: }
352: assertEquals(item1, item2);
353: }
354:
355: /**
356: * Serialize an instance, restore it, and check for equality.
357: */
358: public void testSerialization2() {
359: AttributedString as = new AttributedString("Test String");
360: as.addAttribute(TextAttribute.FONT, new Font("Dialog",
361: Font.PLAIN, 12));
362: LegendItem item1 = new LegendItem(as, "Description", "ToolTip",
363: "URL", new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0),
364: Color.red);
365: LegendItem item2 = null;
366: try {
367: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
368: ObjectOutput out = new ObjectOutputStream(buffer);
369: out.writeObject(item1);
370: out.close();
371:
372: ObjectInput in = new ObjectInputStream(
373: new ByteArrayInputStream(buffer.toByteArray()));
374: item2 = (LegendItem) in.readObject();
375: in.close();
376: } catch (Exception e) {
377: e.printStackTrace();
378: }
379: assertEquals(item1, item2);
380: }
381:
382: /**
383: * Cloning is not required for this class as it is designed to be
384: * immutable.
385: */
386: public void testCloning() {
387: LegendItem item = new LegendItem("Item", "Description",
388: "ToolTip", "URL", new Rectangle2D.Double(1.0, 2.0, 3.0,
389: 4.0), Color.red);
390: assertFalse(item instanceof Cloneable);
391: }
392:
393: }
|