001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, 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: * MeterPlotTests.java
029: * -------------------
030: * (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: MeterPlotTests.java,v 1.1.2.2 2007/03/21 10:25:00 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 27-Mar-2003 : Version 1 (DG);
040: * 12-May-2004 : Updated testEquals();
041: *
042: */
043:
044: package org.jfree.chart.plot.junit;
045:
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 java.text.DecimalFormat;
056:
057: import junit.framework.Test;
058: import junit.framework.TestCase;
059: import junit.framework.TestSuite;
060:
061: import org.jfree.chart.plot.DialShape;
062: import org.jfree.chart.plot.MeterInterval;
063: import org.jfree.chart.plot.MeterPlot;
064: import org.jfree.data.Range;
065: import org.jfree.data.general.DefaultValueDataset;
066:
067: /**
068: * Tests for the {@link MeterPlot} class.
069: */
070: public class MeterPlotTests extends TestCase {
071:
072: /**
073: * Returns the tests as a test suite.
074: *
075: * @return The test suite.
076: */
077: public static Test suite() {
078: return new TestSuite(MeterPlotTests.class);
079: }
080:
081: /**
082: * Constructs a new set of tests.
083: *
084: * @param name the name of the tests.
085: */
086: public MeterPlotTests(String name) {
087: super (name);
088: }
089:
090: /**
091: * Test the equals method to ensure that it can distinguish the required
092: * fields. Note that the dataset is NOT considered in the equals test.
093: */
094: public void testEquals() {
095: MeterPlot plot1 = new MeterPlot();
096: MeterPlot plot2 = new MeterPlot();
097: assertTrue(plot1.equals(plot2));
098:
099: // units
100: plot1.setUnits("mph");
101: assertFalse(plot1.equals(plot2));
102: plot2.setUnits("mph");
103: assertTrue(plot1.equals(plot2));
104:
105: // range
106: plot1.setRange(new Range(50.0, 70.0));
107: assertFalse(plot1.equals(plot2));
108: plot2.setRange(new Range(50.0, 70.0));
109: assertTrue(plot1.equals(plot2));
110:
111: // interval
112: plot1.addInterval(new MeterInterval("Normal", new Range(55.0,
113: 60.0)));
114: assertFalse(plot1.equals(plot2));
115: plot2.addInterval(new MeterInterval("Normal", new Range(55.0,
116: 60.0)));
117: assertTrue(plot1.equals(plot2));
118:
119: // dial outline paint
120: plot1.setDialOutlinePaint(new GradientPaint(1.0f, 2.0f,
121: Color.red, 3.0f, 4.0f, Color.blue));
122: assertFalse(plot1.equals(plot2));
123: plot2.setDialOutlinePaint(new GradientPaint(1.0f, 2.0f,
124: Color.red, 3.0f, 4.0f, Color.blue));
125: assertTrue(plot1.equals(plot2));
126:
127: // dial shape
128: plot1.setDialShape(DialShape.CHORD);
129: assertFalse(plot1.equals(plot2));
130: plot2.setDialShape(DialShape.CHORD);
131: assertTrue(plot1.equals(plot2));
132:
133: // dial background paint
134: plot1.setDialBackgroundPaint(new GradientPaint(9.0f, 8.0f,
135: Color.red, 7.0f, 6.0f, Color.blue));
136: assertFalse(plot1.equals(plot2));
137: plot2.setDialBackgroundPaint(new GradientPaint(9.0f, 8.0f,
138: Color.red, 7.0f, 6.0f, Color.blue));
139: assertTrue(plot1.equals(plot2));
140:
141: // needle paint
142: plot1.setNeedlePaint(new GradientPaint(9.0f, 8.0f, Color.red,
143: 7.0f, 6.0f, Color.blue));
144: assertFalse(plot1.equals(plot2));
145: plot2.setNeedlePaint(new GradientPaint(9.0f, 8.0f, Color.red,
146: 7.0f, 6.0f, Color.blue));
147: assertTrue(plot1.equals(plot2));
148:
149: // value font
150: plot1.setValueFont(new Font("Serif", Font.PLAIN, 6));
151: assertFalse(plot1.equals(plot2));
152: plot2.setValueFont(new Font("Serif", Font.PLAIN, 6));
153: assertTrue(plot1.equals(plot2));
154:
155: // value paint
156: plot1.setValuePaint(new GradientPaint(1.0f, 2.0f, Color.black,
157: 3.0f, 4.0f, Color.white));
158: assertFalse(plot1.equals(plot2));
159: plot2.setValuePaint(new GradientPaint(1.0f, 2.0f, Color.black,
160: 3.0f, 4.0f, Color.white));
161: assertTrue(plot1.equals(plot2));
162:
163: // tick labels visible
164: plot1.setTickLabelsVisible(false);
165: assertFalse(plot1.equals(plot2));
166: plot2.setTickLabelsVisible(false);
167: assertTrue(plot1.equals(plot2));
168:
169: // tick label font
170: plot1.setTickLabelFont(new Font("Serif", Font.PLAIN, 6));
171: assertFalse(plot1.equals(plot2));
172: plot2.setTickLabelFont(new Font("Serif", Font.PLAIN, 6));
173: assertTrue(plot1.equals(plot2));
174:
175: // tick label paint
176: plot1.setTickLabelPaint(Color.red);
177: assertFalse(plot1.equals(plot2));
178: plot2.setTickLabelPaint(Color.red);
179: assertTrue(plot1.equals(plot2));
180:
181: // tick label format
182: plot1.setTickLabelFormat(new DecimalFormat("0"));
183: assertFalse(plot1.equals(plot2));
184: plot2.setTickLabelFormat(new DecimalFormat("0"));
185: assertTrue(plot1.equals(plot2));
186:
187: // tick paint
188: plot1.setTickPaint(Color.green);
189: assertFalse(plot1.equals(plot2));
190: plot2.setTickPaint(Color.green);
191: assertTrue(plot1.equals(plot2));
192:
193: // tick size
194: plot1.setTickSize(1.23);
195: assertFalse(plot1.equals(plot2));
196: plot2.setTickSize(1.23);
197: assertTrue(plot1.equals(plot2));
198:
199: // draw border
200: plot1.setDrawBorder(!plot1.getDrawBorder());
201: assertFalse(plot1.equals(plot2));
202: plot2.setDrawBorder(plot1.getDrawBorder());
203: assertTrue(plot1.equals(plot2));
204:
205: // meter angle
206: plot1.setMeterAngle(22);
207: assertFalse(plot1.equals(plot2));
208: plot2.setMeterAngle(22);
209: assertTrue(plot1.equals(plot2));
210:
211: }
212:
213: /**
214: * Confirm that cloning works.
215: */
216: public void testCloning() {
217: MeterPlot p1 = new MeterPlot();
218: MeterPlot p2 = null;
219: try {
220: p2 = (MeterPlot) p1.clone();
221: } catch (CloneNotSupportedException e) {
222: e.printStackTrace();
223: }
224: assertTrue(p1 != p2);
225: assertTrue(p1.getClass() == p2.getClass());
226: assertTrue(p1.equals(p2));
227:
228: // the clone and the original share a reference to the SAME dataset
229: assertTrue(p1.getDataset() == p2.getDataset());
230:
231: // try a few checks to ensure that the clone is independent of the
232: // original
233: p1.getTickLabelFormat().setMinimumIntegerDigits(99);
234: assertFalse(p1.equals(p2));
235: p2.getTickLabelFormat().setMinimumIntegerDigits(99);
236: assertTrue(p1.equals(p2));
237:
238: p1.addInterval(new MeterInterval("Test",
239: new Range(1.234, 5.678)));
240: assertFalse(p1.equals(p2));
241: p2.addInterval(new MeterInterval("Test",
242: new Range(1.234, 5.678)));
243: assertTrue(p1.equals(p2));
244:
245: }
246:
247: /**
248: * Serialize an instance, restore it, and check for equality.
249: */
250: public void testSerialization1() {
251: MeterPlot p1 = new MeterPlot(null);
252: p1.setDialBackgroundPaint(new GradientPaint(1.0f, 2.0f,
253: Color.red, 3.0f, 4.0f, Color.blue));
254: p1.setDialBackgroundPaint(new GradientPaint(1.0f, 2.0f,
255: Color.red, 3.0f, 4.0f, Color.blue));
256: p1.setNeedlePaint(new GradientPaint(1.0f, 2.0f, Color.red,
257: 3.0f, 4.0f, Color.blue));
258: p1.setTickLabelPaint(new GradientPaint(1.0f, 2.0f, Color.red,
259: 3.0f, 4.0f, Color.blue));
260: p1.setTickPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
261: 4.0f, Color.blue));
262: MeterPlot p2 = null;
263: try {
264: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
265: ObjectOutput out = new ObjectOutputStream(buffer);
266: out.writeObject(p1);
267: out.close();
268:
269: ObjectInput in = new ObjectInputStream(
270: new ByteArrayInputStream(buffer.toByteArray()));
271: p2 = (MeterPlot) in.readObject();
272: in.close();
273: } catch (Exception e) {
274: e.printStackTrace();
275: }
276: assertEquals(p1, p2);
277: }
278:
279: /**
280: * Serialize an instance, restore it, and check for equality.
281: */
282: public void testSerialization2() {
283: MeterPlot p1 = new MeterPlot(new DefaultValueDataset(1.23));
284: MeterPlot p2 = null;
285: try {
286: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
287: ObjectOutput out = new ObjectOutputStream(buffer);
288: out.writeObject(p1);
289: out.close();
290:
291: ObjectInput in = new ObjectInputStream(
292: new ByteArrayInputStream(buffer.toByteArray()));
293: p2 = (MeterPlot) in.readObject();
294: in.close();
295: } catch (Exception e) {
296: e.printStackTrace();
297: }
298: assertEquals(p1, p2);
299:
300: }
301:
302: }
|