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: * AbstractBlockTests.java
029: * -----------------------
030: * (C) Copyright 2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: AbstractBlockTests.java,v 1.1.2.2 2007/03/20 09:31:43 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 16-Mar-2007 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.chart.block.junit;
044:
045: import java.awt.Color;
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.block.AbstractBlock;
059: import org.jfree.chart.block.BlockBorder;
060: import org.jfree.chart.block.EmptyBlock;
061: import org.jfree.ui.RectangleInsets;
062:
063: /**
064: * Tests for the {@link AbstractBlock} class.
065: */
066: public class AbstractBlockTests 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(AbstractBlockTests.class);
075: }
076:
077: /**
078: * Constructs a new set of tests.
079: *
080: * @param name the name of the tests.
081: */
082: public AbstractBlockTests(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: EmptyBlock b1 = new EmptyBlock(1.0, 2.0);
091: EmptyBlock b2 = new EmptyBlock(1.0, 2.0);
092: assertTrue(b1.equals(b2));
093: assertTrue(b2.equals(b2));
094:
095: b1.setID("Test");
096: assertFalse(b1.equals(b2));
097: b2.setID("Test");
098: assertTrue(b1.equals(b2));
099:
100: b1.setMargin(new RectangleInsets(1.0, 2.0, 3.0, 4.0));
101: assertFalse(b1.equals(b2));
102: b2.setMargin(new RectangleInsets(1.0, 2.0, 3.0, 4.0));
103: assertTrue(b1.equals(b2));
104:
105: b1.setFrame(new BlockBorder(Color.red));
106: assertFalse(b1.equals(b2));
107: b2.setFrame(new BlockBorder(Color.red));
108: assertTrue(b1.equals(b2));
109:
110: b1.setPadding(new RectangleInsets(2.0, 4.0, 6.0, 8.0));
111: assertFalse(b1.equals(b2));
112: b2.setPadding(new RectangleInsets(2.0, 4.0, 6.0, 8.0));
113: assertTrue(b1.equals(b2));
114:
115: b1.setWidth(1.23);
116: assertFalse(b1.equals(b2));
117: b2.setWidth(1.23);
118: assertTrue(b1.equals(b2));
119:
120: b1.setHeight(4.56);
121: assertFalse(b1.equals(b2));
122: b2.setHeight(4.56);
123: assertTrue(b1.equals(b2));
124:
125: b1.setBounds(new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0));
126: assertFalse(b1.equals(b2));
127: b2.setBounds(new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0));
128: assertTrue(b1.equals(b2));
129:
130: b1 = new EmptyBlock(1.1, 2.0);
131: assertFalse(b1.equals(b2));
132: b2 = new EmptyBlock(1.1, 2.0);
133: assertTrue(b1.equals(b2));
134:
135: b1 = new EmptyBlock(1.1, 2.2);
136: assertFalse(b1.equals(b2));
137: b2 = new EmptyBlock(1.1, 2.2);
138: assertTrue(b1.equals(b2));
139: }
140:
141: /**
142: * Confirm that cloning works.
143: */
144: public void testCloning() {
145: EmptyBlock b1 = new EmptyBlock(1.0, 2.0);
146: Rectangle2D bounds1 = new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0);
147: b1.setBounds(bounds1);
148: EmptyBlock b2 = null;
149:
150: try {
151: b2 = (EmptyBlock) b1.clone();
152: } catch (CloneNotSupportedException e) {
153: fail(e.toString());
154: }
155: assertTrue(b1 != b2);
156: assertTrue(b1.getClass() == b2.getClass());
157: assertTrue(b1.equals(b2));
158:
159: bounds1.setFrame(2.0, 4.0, 6.0, 8.0);
160: assertFalse(b1.equals(b2));
161: b2.setBounds(new Rectangle2D.Double(2.0, 4.0, 6.0, 8.0));
162: assertTrue(b1.equals(b2));
163: }
164:
165: /**
166: * Serialize an instance, restore it, and check for equality.
167: */
168: public void testSerialization() {
169: EmptyBlock b1 = new EmptyBlock(1.0, 2.0);
170: EmptyBlock b2 = null;
171: try {
172: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
173: ObjectOutput out = new ObjectOutputStream(buffer);
174: out.writeObject(b1);
175: out.close();
176:
177: ObjectInput in = new ObjectInputStream(
178: new ByteArrayInputStream(buffer.toByteArray()));
179: b2 = (EmptyBlock) in.readObject();
180: in.close();
181: } catch (Exception e) {
182: fail(e.toString());
183: }
184: assertEquals(b1, b2);
185: }
186:
187: }
|