001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2005, 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: * CompositeTitleTests.java
029: * ------------------------
030: * (C) Copyright 2005, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: CompositeTitleTests.java,v 1.1.2.1 2006/10/03 15:41:29 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 04-Feb-2005 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.chart.title.junit;
044:
045: import java.awt.Color;
046: import java.io.ByteArrayInputStream;
047: import java.io.ByteArrayOutputStream;
048: import java.io.ObjectInput;
049: import java.io.ObjectInputStream;
050: import java.io.ObjectOutput;
051: import java.io.ObjectOutputStream;
052:
053: import junit.framework.Test;
054: import junit.framework.TestCase;
055: import junit.framework.TestSuite;
056:
057: import org.jfree.chart.block.BlockBorder;
058: import org.jfree.chart.block.BlockContainer;
059: import org.jfree.chart.title.CompositeTitle;
060: import org.jfree.chart.title.TextTitle;
061: import org.jfree.ui.RectangleInsets;
062:
063: /**
064: * Tests for the {@link CompositeTitle} class.
065: */
066: public class CompositeTitleTests 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(CompositeTitleTests.class);
075: }
076:
077: /**
078: * Constructs a new set of tests.
079: *
080: * @param name the name of the tests.
081: */
082: public CompositeTitleTests(String name) {
083: super (name);
084: }
085:
086: /**
087: * Check that the equals() method distinguishes all fields.
088: */
089: public void testEquals() {
090: CompositeTitle t1 = new CompositeTitle(new BlockContainer());
091: CompositeTitle t2 = new CompositeTitle(new BlockContainer());
092: assertEquals(t1, t2);
093: assertEquals(t2, t1);
094:
095: // margin
096: t1.setMargin(new RectangleInsets(1.0, 2.0, 3.0, 4.0));
097: assertFalse(t1.equals(t2));
098: t2.setMargin(new RectangleInsets(1.0, 2.0, 3.0, 4.0));
099: assertTrue(t1.equals(t2));
100:
101: // border
102: t1.setBorder(new BlockBorder(Color.red));
103: assertFalse(t1.equals(t2));
104: t2.setBorder(new BlockBorder(Color.red));
105: assertTrue(t1.equals(t2));
106:
107: // padding
108: t1.setPadding(new RectangleInsets(1.0, 2.0, 3.0, 4.0));
109: assertFalse(t1.equals(t2));
110: t2.setPadding(new RectangleInsets(1.0, 2.0, 3.0, 4.0));
111: assertTrue(t1.equals(t2));
112:
113: // contained titles
114: t1.getContainer().add(new TextTitle("T1"));
115: assertFalse(t1.equals(t2));
116: t2.getContainer().add(new TextTitle("T1"));
117: assertTrue(t1.equals(t2));
118:
119: }
120:
121: /**
122: * Two objects that are equal are required to return the same hashCode.
123: */
124: public void testHashcode() {
125: CompositeTitle t1 = new CompositeTitle(new BlockContainer());
126: t1.getContainer().add(new TextTitle("T1"));
127: CompositeTitle t2 = new CompositeTitle(new BlockContainer());
128: t2.getContainer().add(new TextTitle("T1"));
129: assertTrue(t1.equals(t2));
130: int h1 = t1.hashCode();
131: int h2 = t2.hashCode();
132: assertEquals(h1, h2);
133: }
134:
135: /**
136: * Confirm that cloning works.
137: */
138: public void testCloning() {
139: CompositeTitle t1 = new CompositeTitle(new BlockContainer());
140: t1.getContainer().add(new TextTitle("T1"));
141: CompositeTitle t2 = null;
142: try {
143: t2 = (CompositeTitle) t1.clone();
144: } catch (CloneNotSupportedException e) {
145: fail(e.toString());
146: }
147: assertTrue(t1 != t2);
148: assertTrue(t1.getClass() == t2.getClass());
149: assertTrue(t1.equals(t2));
150: }
151:
152: /**
153: * Serialize an instance, restore it, and check for equality.
154: */
155: public void testSerialization() {
156: CompositeTitle t1 = new CompositeTitle(new BlockContainer());
157: t1.getContainer().add(new TextTitle("T1"));
158: CompositeTitle t2 = null;
159: try {
160: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
161: ObjectOutput out = new ObjectOutputStream(buffer);
162: out.writeObject(t1);
163: out.close();
164:
165: ObjectInput in = new ObjectInputStream(
166: new ByteArrayInputStream(buffer.toByteArray()));
167: t2 = (CompositeTitle) in.readObject();
168: in.close();
169: } catch (Exception e) {
170: System.out.println(e.toString());
171: }
172: assertEquals(t1, t2);
173: }
174:
175: }
|