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: * DialBackgroundTests.java
029: * ------------------------
030: * (C) Copyright 2006, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: DialBackgroundTests.java,v 1.1.2.2 2006/11/06 16:31:01 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 03-Nov-2006 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.experimental.chart.plot.dial.junit;
044:
045: import java.awt.Color;
046: import java.awt.GradientPaint;
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.experimental.chart.plot.dial.DialBackground;
059: import org.jfree.ui.GradientPaintTransformType;
060: import org.jfree.ui.StandardGradientPaintTransformer;
061:
062: /**
063: * Tests for the {@link DialBackground} class.
064: */
065: public class DialBackgroundTests 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(DialBackgroundTests.class);
074: }
075:
076: /**
077: * Constructs a new set of tests.
078: *
079: * @param name the name of the tests.
080: */
081: public DialBackgroundTests(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: DialBackground b1 = new DialBackground();
090: DialBackground b2 = new DialBackground();
091: assertTrue(b1.equals(b2));
092:
093: // paint
094: b1.setPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
095: 4.0f, Color.yellow));
096: assertFalse(b1.equals(b2));
097: b2.setPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
098: 4.0f, Color.yellow));
099: assertTrue(b1.equals(b2));
100:
101: // gradient paint transformer
102: b1
103: .setGradientPaintTransformer(new StandardGradientPaintTransformer(
104: GradientPaintTransformType.CENTER_VERTICAL));
105: assertFalse(b1.equals(b2));
106: b2
107: .setGradientPaintTransformer(new StandardGradientPaintTransformer(
108: GradientPaintTransformType.CENTER_VERTICAL));
109: assertTrue(b1.equals(b2));
110: }
111:
112: /**
113: * Two objects that are equal are required to return the same hashCode.
114: */
115: public void testHashCode() {
116: DialBackground b1 = new DialBackground(Color.red);
117: DialBackground b2 = new DialBackground(Color.red);
118: assertTrue(b1.equals(b2));
119: int h1 = b1.hashCode();
120: int h2 = b2.hashCode();
121: assertEquals(h1, h2);
122: }
123:
124: /**
125: * Confirm that cloning works.
126: */
127: public void testCloning() {
128: // test default instance
129: DialBackground b1 = new DialBackground();
130: DialBackground b2 = null;
131: try {
132: b2 = (DialBackground) b1.clone();
133: } catch (CloneNotSupportedException e) {
134: e.printStackTrace();
135: }
136: assertTrue(b1 != b2);
137: assertTrue(b1.getClass() == b2.getClass());
138: assertTrue(b1.equals(b2));
139:
140: // test a customised instance
141: b1 = new DialBackground();
142: b1.setPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
143: 4.0f, Color.green));
144: b1
145: .setGradientPaintTransformer(new StandardGradientPaintTransformer(
146: GradientPaintTransformType.CENTER_VERTICAL));
147: b2 = null;
148: try {
149: b2 = (DialBackground) b1.clone();
150: } catch (CloneNotSupportedException e) {
151: e.printStackTrace();
152: }
153: assertTrue(b1 != b2);
154: assertTrue(b1.getClass() == b2.getClass());
155: assertTrue(b1.equals(b2));
156: }
157:
158: /**
159: * Serialize an instance, restore it, and check for equality.
160: */
161: public void testSerialization() {
162: // test a default instance
163: DialBackground b1 = new DialBackground();
164: DialBackground b2 = null;
165:
166: try {
167: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
168: ObjectOutput out = new ObjectOutputStream(buffer);
169: out.writeObject(b1);
170: out.close();
171:
172: ObjectInput in = new ObjectInputStream(
173: new ByteArrayInputStream(buffer.toByteArray()));
174: b2 = (DialBackground) in.readObject();
175: in.close();
176: } catch (Exception e) {
177: e.printStackTrace();
178: }
179: assertEquals(b1, b2);
180:
181: // test a customised instance
182: b1 = new DialBackground();
183: b1.setPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
184: 4.0f, Color.green));
185: b1
186: .setGradientPaintTransformer(new StandardGradientPaintTransformer(
187: GradientPaintTransformType.CENTER_VERTICAL));
188: b2 = null;
189:
190: try {
191: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
192: ObjectOutput out = new ObjectOutputStream(buffer);
193: out.writeObject(b1);
194: out.close();
195:
196: ObjectInput in = new ObjectInputStream(
197: new ByteArrayInputStream(buffer.toByteArray()));
198: b2 = (DialBackground) in.readObject();
199: in.close();
200: } catch (Exception e) {
201: e.printStackTrace();
202: }
203: assertEquals(b1, b2);
204: }
205:
206: }
|