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: * SimpleDialScaleTests.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: StandardDialScaleTests.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.Font;
048: import java.awt.GradientPaint;
049: import java.io.ByteArrayInputStream;
050: import java.io.ByteArrayOutputStream;
051: import java.io.ObjectInput;
052: import java.io.ObjectInputStream;
053: import java.io.ObjectOutput;
054: import java.io.ObjectOutputStream;
055: import junit.framework.Test;
056: import junit.framework.TestCase;
057: import junit.framework.TestSuite;
058: import org.jfree.experimental.chart.plot.dial.StandardDialScale;
059:
060: /**
061: * Tests for the {@link StandardDialScale} class.
062: */
063: public class StandardDialScaleTests extends TestCase {
064:
065: /**
066: * Returns the tests as a test suite.
067: *
068: * @return The test suite.
069: */
070: public static Test suite() {
071: return new TestSuite(StandardDialScaleTests.class);
072: }
073:
074: /**
075: * Constructs a new set of tests.
076: *
077: * @param name the name of the tests.
078: */
079: public StandardDialScaleTests(String name) {
080: super (name);
081: }
082:
083: /**
084: * Confirm that the equals method can distinguish all the required fields.
085: */
086: public void testEquals() {
087: StandardDialScale s1 = new StandardDialScale();
088: StandardDialScale s2 = new StandardDialScale();
089: assertTrue(s1.equals(s2));
090:
091: // lowerBound
092: s1 = new StandardDialScale(10.0, 100.0, 0.0, 270.0);
093: assertFalse(s1.equals(s2));
094: s2 = new StandardDialScale(10.0, 100.0, 0.0, 270.0);
095: assertTrue(s1.equals(s2));
096:
097: // upperBound
098: s1 = new StandardDialScale(10.0, 200.0, 0.0, 270.0);
099: assertFalse(s1.equals(s2));
100: s2 = new StandardDialScale(10.0, 200.0, 0.0, 270.0);
101: assertTrue(s1.equals(s2));
102:
103: // startAngle
104: s1 = new StandardDialScale(10.0, 200.0, 20.0, 270.0);
105: assertFalse(s1.equals(s2));
106: s2 = new StandardDialScale(10.0, 200.0, 20.0, 270.0);
107: assertTrue(s1.equals(s2));
108:
109: // extent
110: s1 = new StandardDialScale(10.0, 200.0, 20.0, 99.0);
111: assertFalse(s1.equals(s2));
112: s2 = new StandardDialScale(10.0, 200.0, 20.0, 99.0);
113: assertTrue(s1.equals(s2));
114:
115: // tickRadius
116: s1.setTickRadius(0.99);
117: assertFalse(s1.equals(s2));
118: s2.setTickRadius(0.99);
119: assertTrue(s1.equals(s2));
120:
121: // majorTickIncrement
122: s1.setMajorTickIncrement(11.1);
123: assertFalse(s1.equals(s2));
124: s2.setMajorTickIncrement(11.1);
125: assertTrue(s1.equals(s2));
126:
127: // majorTickLength
128: s1.setMajorTickLength(0.09);
129: assertFalse(s1.equals(s2));
130: s2.setMajorTickLength(0.09);
131: assertTrue(s1.equals(s2));
132:
133: // majorTickPaint
134: s1.setMajorTickPaint(new GradientPaint(1.0f, 2.0f, Color.red,
135: 3.0f, 4.0f, Color.yellow));
136: assertFalse(s1.equals(s2));
137: s2.setMajorTickPaint(new GradientPaint(1.0f, 2.0f, Color.red,
138: 3.0f, 4.0f, Color.yellow));
139: assertTrue(s1.equals(s2));
140:
141: // majorTickStroke
142: s1.setMajorTickStroke(new BasicStroke(1.1f));
143: assertFalse(s1.equals(s2));
144: s2.setMajorTickStroke(new BasicStroke(1.1f));
145: assertTrue(s1.equals(s2));
146:
147: // minorTickCount
148: s1.setMinorTickCount(7);
149: assertFalse(s1.equals(s2));
150: s2.setMinorTickCount(7);
151: assertTrue(s1.equals(s2));
152:
153: // minorTickLength
154: s1.setMinorTickLength(0.09);
155: assertFalse(s1.equals(s2));
156: s2.setMinorTickLength(0.09);
157: assertTrue(s1.equals(s2));
158:
159: // tickLabelOffset
160: s1.setTickLabelOffset(0.11);
161: assertFalse(s1.equals(s2));
162: s2.setTickLabelOffset(0.11);
163: assertTrue(s1.equals(s2));
164:
165: // tickLabelFont
166: s1.setTickLabelFont(new Font("Dialog", Font.PLAIN, 15));
167: assertFalse(s1.equals(s2));
168: s2.setTickLabelFont(new Font("Dialog", Font.PLAIN, 15));
169: assertTrue(s1.equals(s2));
170:
171: // tickLabelPaint
172: s1.setTickLabelPaint(new GradientPaint(1.0f, 2.0f, Color.white,
173: 3.0f, 4.0f, Color.green));
174: assertFalse(s1.equals(s2));
175: s2.setTickLabelPaint(new GradientPaint(1.0f, 2.0f, Color.white,
176: 3.0f, 4.0f, Color.green));
177: assertTrue(s1.equals(s2));
178:
179: }
180:
181: /**
182: * Two objects that are equal are required to return the same hashCode.
183: */
184: public void testHashCode() {
185: StandardDialScale s1 = new StandardDialScale();
186: StandardDialScale s2 = new StandardDialScale();
187: assertTrue(s1.equals(s2));
188: int h1 = s1.hashCode();
189: int h2 = s2.hashCode();
190: assertEquals(h1, h2);
191: }
192:
193: /**
194: * Confirm that cloning works.
195: */
196: public void testCloning() {
197: // try a default instance
198: StandardDialScale s1 = new StandardDialScale();
199: StandardDialScale s2 = null;
200: try {
201: s2 = (StandardDialScale) s1.clone();
202: } catch (CloneNotSupportedException e) {
203: e.printStackTrace();
204: }
205: assertTrue(s1 != s2);
206: assertTrue(s1.getClass() == s2.getClass());
207: assertTrue(s1.equals(s2));
208:
209: // try a customised instance
210: s1 = new StandardDialScale();
211: s1.setExtent(123.4);
212: s1.setMajorTickPaint(new GradientPaint(1.0f, 2.0f, Color.red,
213: 3.0f, 4.0f, Color.white));
214: s1.setMajorTickStroke(new BasicStroke(2.0f));
215: s2 = null;
216: try {
217: s2 = (StandardDialScale) s1.clone();
218: } catch (CloneNotSupportedException e) {
219: e.printStackTrace();
220: }
221: assertTrue(s1 != s2);
222: assertTrue(s1.getClass() == s2.getClass());
223: assertTrue(s1.equals(s2));
224: }
225:
226: /**
227: * Serialize an instance, restore it, and check for equality.
228: */
229: public void testSerialization() {
230: // try a default instance
231: StandardDialScale s1 = new StandardDialScale();
232: StandardDialScale s2 = null;
233:
234: try {
235: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
236: ObjectOutput out = new ObjectOutputStream(buffer);
237: out.writeObject(s1);
238: out.close();
239:
240: ObjectInput in = new ObjectInputStream(
241: new ByteArrayInputStream(buffer.toByteArray()));
242: s2 = (StandardDialScale) in.readObject();
243: in.close();
244: } catch (Exception e) {
245: e.printStackTrace();
246: }
247: assertEquals(s1, s2);
248:
249: // try a customised instance
250: s1 = new StandardDialScale();
251: s1.setExtent(123.4);
252: s1.setMajorTickPaint(new GradientPaint(1.0f, 2.0f, Color.red,
253: 3.0f, 4.0f, Color.white));
254: s1.setMajorTickStroke(new BasicStroke(2.0f));
255: s2 = null;
256:
257: try {
258: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
259: ObjectOutput out = new ObjectOutputStream(buffer);
260: out.writeObject(s1);
261: out.close();
262:
263: ObjectInput in = new ObjectInputStream(
264: new ByteArrayInputStream(buffer.toByteArray()));
265: s2 = (StandardDialScale) in.readObject();
266: in.close();
267: } catch (Exception e) {
268: e.printStackTrace();
269: }
270: assertEquals(s1, s2);
271: }
272:
273: }
|