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: * LabelBlockTests.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: LabelBlockTests.java,v 1.1.2.2 2007/03/16 11:28:16 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 01-Sep-2005 : Version 1 (DG);
040: * 16-Mar-2007 : Check GradientPaint in testSerialization() (DG);
041: *
042: */
043:
044: package org.jfree.chart.block.junit;
045:
046: import java.awt.Color;
047: import java.awt.Font;
048: import java.awt.GradientPaint;
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.block.LabelBlock;
061:
062: /**
063: * Some tests for the {@link LabelBlock} class.
064: */
065: public class LabelBlockTests extends TestCase {
066:
067: /**
068: * Returns the tests as a test suite.
069: *
070: * @return The test suite.
071: */
072: public static Test suite() {
073: return new TestSuite(LabelBlockTests.class);
074: }
075:
076: /**
077: * Constructs a new set of tests.
078: *
079: * @param name the name of the tests.
080: */
081: public LabelBlockTests(String name) {
082: super (name);
083: }
084:
085: /**
086: * Confirm that the equals() method can distinguish all the required fields.
087: */
088: public void testEquals() {
089: LabelBlock b1 = new LabelBlock("ABC", new Font("Dialog",
090: Font.PLAIN, 12), Color.red);
091: LabelBlock b2 = new LabelBlock("ABC", new Font("Dialog",
092: Font.PLAIN, 12), Color.red);
093: assertTrue(b1.equals(b2));
094: assertTrue(b2.equals(b2));
095:
096: b1 = new LabelBlock("XYZ", new Font("Dialog", Font.PLAIN, 12),
097: Color.red);
098: assertFalse(b1.equals(b2));
099: b2 = new LabelBlock("XYZ", new Font("Dialog", Font.PLAIN, 12),
100: Color.red);
101: assertTrue(b1.equals(b2));
102:
103: b1 = new LabelBlock("XYZ", new Font("Dialog", Font.BOLD, 12),
104: Color.red);
105: assertFalse(b1.equals(b2));
106: b2 = new LabelBlock("XYZ", new Font("Dialog", Font.BOLD, 12),
107: Color.red);
108: assertTrue(b1.equals(b2));
109:
110: b1 = new LabelBlock("XYZ", new Font("Dialog", Font.BOLD, 12),
111: Color.blue);
112: assertFalse(b1.equals(b2));
113: b2 = new LabelBlock("XYZ", new Font("Dialog", Font.BOLD, 12),
114: Color.blue);
115: assertTrue(b1.equals(b2));
116:
117: b1.setToolTipText("Tooltip");
118: assertFalse(b1.equals(b2));
119: b2.setToolTipText("Tooltip");
120: assertTrue(b1.equals(b2));
121:
122: b1.setURLText("URL");
123: assertFalse(b1.equals(b2));
124: b2.setURLText("URL");
125: assertTrue(b1.equals(b2));
126: }
127:
128: /**
129: * Confirm that cloning works.
130: */
131: public void testCloning() {
132: LabelBlock b1 = new LabelBlock("ABC", new Font("Dialog",
133: Font.PLAIN, 12), Color.red);
134: LabelBlock b2 = null;
135:
136: try {
137: b2 = (LabelBlock) b1.clone();
138: } catch (CloneNotSupportedException e) {
139: e.printStackTrace();
140: }
141: assertTrue(b1 != b2);
142: assertTrue(b1.getClass() == b2.getClass());
143: assertTrue(b1.equals(b2));
144: }
145:
146: /**
147: * Serialize an instance, restore it, and check for equality.
148: */
149: public void testSerialization() {
150: GradientPaint gp = new GradientPaint(1.0f, 2.0f, Color.red,
151: 3.0f, 4.0f, Color.blue);
152: LabelBlock b1 = new LabelBlock("ABC", new Font("Dialog",
153: Font.PLAIN, 12), gp);
154: LabelBlock b2 = null;
155: try {
156: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
157: ObjectOutput out = new ObjectOutputStream(buffer);
158: out.writeObject(b1);
159: out.close();
160:
161: ObjectInput in = new ObjectInputStream(
162: new ByteArrayInputStream(buffer.toByteArray()));
163: b2 = (LabelBlock) in.readObject();
164: in.close();
165: } catch (Exception e) {
166: e.printStackTrace();
167: }
168: assertEquals(b1, b2);
169: }
170:
171: }
|