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: * DialPlotTests.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: DialPlotTests.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.experimental.chart.plot.dial.DialCap;
060: import org.jfree.experimental.chart.plot.dial.DialPlot;
061: import org.jfree.experimental.chart.plot.dial.SimpleDialFrame;
062: import org.jfree.experimental.chart.plot.dial.StandardDialScale;
063:
064: /**
065: * Tests for the {@link DialPlot} class.
066: */
067: public class DialPlotTests extends TestCase {
068:
069: /**
070: * Returns the tests as a test suite.
071: *
072: * @return The test suite.
073: */
074: public static Test suite() {
075: return new TestSuite(DialPlotTests.class);
076: }
077:
078: /**
079: * Constructs a new set of tests.
080: *
081: * @param name the name of the tests.
082: */
083: public DialPlotTests(String name) {
084: super (name);
085: }
086:
087: /**
088: * Confirm that the equals method can distinguish all the required fields.
089: */
090: public void testEquals() {
091: DialPlot p1 = new DialPlot();
092: DialPlot p2 = new DialPlot();
093: assertTrue(p1.equals(p2));
094:
095: // background
096: p1.setBackground(new DialBackground(Color.green));
097: assertFalse(p1.equals(p2));
098: p2.setBackground(new DialBackground(Color.green));
099: assertTrue(p1.equals(p2));
100:
101: p1.setBackground(null);
102: assertFalse(p1.equals(p2));
103: p2.setBackground(null);
104: assertTrue(p1.equals(p2));
105:
106: // dial cap
107: DialCap cap1 = new DialCap();
108: cap1.setFillPaint(Color.red);
109: p1.setCap(cap1);
110: assertFalse(p1.equals(p2));
111: DialCap cap2 = new DialCap();
112: cap2.setFillPaint(Color.red);
113: p2.setCap(cap2);
114: assertTrue(p1.equals(p2));
115:
116: p1.setCap(null);
117: assertFalse(p1.equals(p2));
118: p2.setCap(null);
119: assertTrue(p1.equals(p2));
120:
121: // frame
122: SimpleDialFrame f1 = new SimpleDialFrame();
123: f1.setBackgroundPaint(new GradientPaint(1.0f, 2.0f, Color.red,
124: 3.0f, 4.0f, Color.white));
125: p1.setDialFrame(f1);
126: assertFalse(p1.equals(p2));
127: SimpleDialFrame f2 = new SimpleDialFrame();
128: f2.setBackgroundPaint(new GradientPaint(1.0f, 2.0f, Color.red,
129: 3.0f, 4.0f, Color.white));
130: p2.setDialFrame(f2);
131: assertTrue(p1.equals(p2));
132:
133: // view
134: p1.setView(0.2, 0.0, 0.8, 1.0);
135: assertFalse(p1.equals(p2));
136: p2.setView(0.2, 0.0, 0.8, 1.0);
137: assertTrue(p1.equals(p2));
138:
139: // layer
140: p1.addLayer(new StandardDialScale());
141: assertFalse(p1.equals(p2));
142: p2.addLayer(new StandardDialScale());
143: assertTrue(p1.equals(p2));
144: }
145:
146: /**
147: * Two objects that are equal are required to return the same hashCode.
148: */
149: public void testHashCode() {
150: DialPlot p1 = new DialPlot();
151: DialPlot p2 = new DialPlot();
152: assertTrue(p1.equals(p2));
153: int h1 = p1.hashCode();
154: int h2 = p2.hashCode();
155: assertEquals(h1, h2);
156: }
157:
158: /**
159: * Confirm that cloning works.
160: */
161: public void testCloning() {
162: DialPlot p1 = new DialPlot();
163: DialPlot p2 = null;
164: try {
165: p2 = (DialPlot) p1.clone();
166: } catch (CloneNotSupportedException e) {
167: e.printStackTrace();
168: }
169: assertTrue(p1 != p2);
170: assertTrue(p1.getClass() == p2.getClass());
171: assertTrue(p1.equals(p2));
172: }
173:
174: /**
175: * Serialize an instance, restore it, and check for equality.
176: */
177: public void testSerialization() {
178: DialPlot p1 = new DialPlot();
179: DialPlot p2 = null;
180:
181: try {
182: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
183: ObjectOutput out = new ObjectOutputStream(buffer);
184: out.writeObject(p1);
185: out.close();
186:
187: ObjectInput in = new ObjectInputStream(
188: new ByteArrayInputStream(buffer.toByteArray()));
189: p2 = (DialPlot) in.readObject();
190: in.close();
191: } catch (Exception e) {
192: e.printStackTrace();
193: }
194: assertEquals(p1, p2);
195: }
196:
197: }
|