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: * DialCapTests.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: DialCapTests.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.BasicStroke;
046: import java.awt.Color;
047: import java.awt.GradientPaint;
048: import java.io.ByteArrayInputStream;
049: import java.io.ByteArrayOutputStream;
050: import java.io.ObjectInput;
051: import java.io.ObjectInputStream;
052: import java.io.ObjectOutput;
053: import java.io.ObjectOutputStream;
054:
055: import junit.framework.Test;
056: import junit.framework.TestCase;
057: import junit.framework.TestSuite;
058:
059: import org.jfree.experimental.chart.plot.dial.DialCap;
060:
061: /**
062: * Tests for the {@link DialCap} class.
063: */
064: public class DialCapTests extends TestCase {
065:
066: /**
067: * Returns the tests as a test suite.
068: *
069: * @return The test suite.
070: */
071: public static Test suite() {
072: return new TestSuite(DialCapTests.class);
073: }
074:
075: /**
076: * Constructs a new set of tests.
077: *
078: * @param name the name of the tests.
079: */
080: public DialCapTests(String name) {
081: super (name);
082: }
083:
084: /**
085: * Confirm that the equals method can distinguish all the required fields.
086: */
087: public void testEquals() {
088: DialCap c1 = new DialCap();
089: DialCap c2 = new DialCap();
090: assertTrue(c1.equals(c2));
091:
092: // radius
093: c1.setRadius(0.5);
094: assertFalse(c1.equals(c2));
095: c2.setRadius(0.5);
096: assertTrue(c1.equals(c2));
097:
098: // fill paint
099: c1.setFillPaint(new GradientPaint(1.0f, 2.0f, Color.blue, 3.0f,
100: 4.0f, Color.green));
101: assertFalse(c1.equals(c2));
102: c2.setFillPaint(new GradientPaint(1.0f, 2.0f, Color.blue, 3.0f,
103: 4.0f, Color.green));
104:
105: // outline paint
106: c1.setOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.white,
107: 3.0f, 4.0f, Color.gray));
108: assertFalse(c1.equals(c2));
109: c2.setOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.white,
110: 3.0f, 4.0f, Color.gray));
111:
112: assertTrue(c1.equals(c2));
113:
114: // outline stroke
115: c1.setOutlineStroke(new BasicStroke(1.1f));
116: assertFalse(c1.equals(c2));
117: c2.setOutlineStroke(new BasicStroke(1.1f));
118: assertTrue(c1.equals(c2));
119: }
120:
121: /**
122: * Two objects that are equal are required to return the same hashCode.
123: */
124: public void testHashCode() {
125: DialCap c1 = new DialCap();
126: DialCap c2 = new DialCap();
127: assertTrue(c1.equals(c2));
128: int h1 = c1.hashCode();
129: int h2 = c2.hashCode();
130: assertEquals(h1, h2);
131: }
132:
133: /**
134: * Confirm that cloning works.
135: */
136: public void testCloning() {
137: // test a default instance
138: DialCap c1 = new DialCap();
139: DialCap c2 = null;
140: try {
141: c2 = (DialCap) c1.clone();
142: } catch (CloneNotSupportedException e) {
143: e.printStackTrace();
144: }
145: assertTrue(c1 != c2);
146: assertTrue(c1.getClass() == c2.getClass());
147: assertTrue(c1.equals(c2));
148:
149: // test a customised instance
150: c1 = new DialCap();
151: c1.setFillPaint(new GradientPaint(1.0f, 2.0f, Color.blue, 3.0f,
152: 4.0f, Color.green));
153: c1.setOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.white,
154: 3.0f, 4.0f, Color.gray));
155: c1.setOutlineStroke(new BasicStroke(2.0f));
156: c2 = null;
157: try {
158: c2 = (DialCap) c1.clone();
159: } catch (CloneNotSupportedException e) {
160: e.printStackTrace();
161: }
162: assertTrue(c1 != c2);
163: assertTrue(c1.getClass() == c2.getClass());
164: assertTrue(c1.equals(c2));
165: }
166:
167: /**
168: * Serialize an instance, restore it, and check for equality.
169: */
170: public void testSerialization() {
171: // test a default instance
172: DialCap c1 = new DialCap();
173: DialCap c2 = null;
174:
175: try {
176: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
177: ObjectOutput out = new ObjectOutputStream(buffer);
178: out.writeObject(c1);
179: out.close();
180:
181: ObjectInput in = new ObjectInputStream(
182: new ByteArrayInputStream(buffer.toByteArray()));
183: c2 = (DialCap) in.readObject();
184: in.close();
185: } catch (Exception e) {
186: e.printStackTrace();
187: }
188: assertEquals(c1, c2);
189:
190: // test a custom instance
191: c1 = new DialCap();
192: c1.setFillPaint(new GradientPaint(1.0f, 2.0f, Color.blue, 3.0f,
193: 4.0f, Color.green));
194: c1.setOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.white,
195: 3.0f, 4.0f, Color.gray));
196: c1.setOutlineStroke(new BasicStroke(2.0f));
197: c2 = null;
198:
199: try {
200: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
201: ObjectOutput out = new ObjectOutputStream(buffer);
202: out.writeObject(c1);
203: out.close();
204:
205: ObjectInput in = new ObjectInputStream(
206: new ByteArrayInputStream(buffer.toByteArray()));
207: c2 = (DialCap) in.readObject();
208: in.close();
209: } catch (Exception e) {
210: e.printStackTrace();
211: }
212: assertEquals(c1, c2);
213: }
214:
215: }
|