KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > plot > junit > MeterPlotTests


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * -------------------
27  * MeterPlotTests.java
28  * -------------------
29  * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: MeterPlotTests.java,v 1.4 2005/03/23 12:04:07 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 27-Mar-2003 : Version 1 (DG);
39  * 12-May-2004 : Updated testEquals();
40  *
41  */

42
43 package org.jfree.chart.plot.junit;
44
45 import java.awt.Color JavaDoc;
46 import java.awt.Font JavaDoc;
47 import java.io.ByteArrayInputStream JavaDoc;
48 import java.io.ByteArrayOutputStream JavaDoc;
49 import java.io.ObjectInput JavaDoc;
50 import java.io.ObjectInputStream JavaDoc;
51 import java.io.ObjectOutput JavaDoc;
52 import java.io.ObjectOutputStream JavaDoc;
53 import java.text.DecimalFormat JavaDoc;
54
55 import junit.framework.Test;
56 import junit.framework.TestCase;
57 import junit.framework.TestSuite;
58
59 import org.jfree.chart.plot.DialShape;
60 import org.jfree.chart.plot.MeterInterval;
61 import org.jfree.chart.plot.MeterPlot;
62 import org.jfree.data.Range;
63 import org.jfree.data.general.DefaultValueDataset;
64
65 /**
66  * Tests for the {@link MeterPlot} class.
67  */

68 public class MeterPlotTests extends TestCase {
69
70     /**
71      * Returns the tests as a test suite.
72      *
73      * @return The test suite.
74      */

75     public static Test suite() {
76         return new TestSuite(MeterPlotTests.class);
77     }
78
79     /**
80      * Constructs a new set of tests.
81      *
82      * @param name the name of the tests.
83      */

84     public MeterPlotTests(String JavaDoc name) {
85         super(name);
86     }
87
88     /**
89      * Test the equals method to ensure that it can distinguish the required
90      * fields. Note that the dataset is NOT considered in the equals test.
91      */

92     public void testEquals() {
93         MeterPlot plot1 = new MeterPlot();
94         MeterPlot plot2 = new MeterPlot();
95         assertTrue(plot1.equals(plot2));
96         
97         // units
98
plot1.setUnits("mph");
99         assertFalse(plot1.equals(plot2));
100         plot2.setUnits("mph");
101         assertTrue(plot1.equals(plot2));
102         
103         // range
104
plot1.setRange(new Range(50.0, 70.0));
105         assertFalse(plot1.equals(plot2));
106         plot2.setRange(new Range(50.0, 70.0));
107         assertTrue(plot1.equals(plot2));
108         
109         // interval
110
plot1.addInterval(new MeterInterval("Normal", new Range(55.0, 60.0)));
111         assertFalse(plot1.equals(plot2));
112         plot2.addInterval(new MeterInterval("Normal", new Range(55.0, 60.0)));
113         assertTrue(plot1.equals(plot2));
114         
115         // dial outline paint
116
plot1.setDialOutlinePaint(Color.red);
117         assertFalse(plot1.equals(plot2));
118         plot2.setDialOutlinePaint(Color.red);
119         assertTrue(plot1.equals(plot2));
120         
121         // dial shape
122
plot1.setDialShape(DialShape.CHORD);
123         assertFalse(plot1.equals(plot2));
124         plot2.setDialShape(DialShape.CHORD);
125         assertTrue(plot1.equals(plot2));
126         
127         // dial background paint
128
plot1.setDialBackgroundPaint(Color.yellow);
129         assertFalse(plot1.equals(plot2));
130         plot2.setDialBackgroundPaint(Color.yellow);
131         assertTrue(plot1.equals(plot2));
132              
133         // needle paint
134
plot1.setNeedlePaint(Color.black);
135         assertFalse(plot1.equals(plot2));
136         plot2.setNeedlePaint(Color.black);
137         assertTrue(plot1.equals(plot2));
138         
139         // value font
140
plot1.setValueFont(new Font JavaDoc("Serif", Font.PLAIN, 6));
141         assertFalse(plot1.equals(plot2));
142         plot2.setValueFont(new Font JavaDoc("Serif", Font.PLAIN, 6));
143         assertTrue(plot1.equals(plot2));
144         
145         // value paint
146
plot1.setValuePaint(Color.black);
147         assertFalse(plot1.equals(plot2));
148         plot2.setValuePaint(Color.black);
149         assertTrue(plot1.equals(plot2));
150         
151         // tick labels visible
152
plot1.setTickLabelsVisible(false);
153         assertFalse(plot1.equals(plot2));
154         plot2.setTickLabelsVisible(false);
155         assertTrue(plot1.equals(plot2));
156         
157         // tick label font
158
plot1.setTickLabelFont(new Font JavaDoc("Serif", Font.PLAIN, 6));
159         assertFalse(plot1.equals(plot2));
160         plot2.setTickLabelFont(new Font JavaDoc("Serif", Font.PLAIN, 6));
161         assertTrue(plot1.equals(plot2));
162         
163         // tick label format
164
plot1.setTickLabelFormat(new DecimalFormat JavaDoc("0"));
165         assertFalse(plot1.equals(plot2));
166         plot2.setTickLabelFormat(new DecimalFormat JavaDoc("0"));
167         assertTrue(plot1.equals(plot2));
168         
169         // draw border
170
plot1.setDrawBorder(!plot1.getDrawBorder());
171         assertFalse(plot1.equals(plot2));
172         plot2.setDrawBorder(plot1.getDrawBorder());
173         assertTrue(plot1.equals(plot2));
174         
175         // meter angle
176
plot1.setMeterAngle(22);
177         assertFalse(plot1.equals(plot2));
178         plot2.setMeterAngle(22);
179         assertTrue(plot1.equals(plot2));
180         
181     }
182
183     /**
184      * Confirm that cloning works.
185      */

186     public void testCloning() {
187         MeterPlot p1 = new MeterPlot();
188         MeterPlot p2 = null;
189         try {
190             p2 = (MeterPlot) p1.clone();
191         }
192         catch (CloneNotSupportedException JavaDoc e) {
193             e.printStackTrace();
194             System.err.println("Failed to clone.");
195         }
196         assertTrue(p1 != p2);
197         assertTrue(p1.getClass() == p2.getClass());
198         assertTrue(p1.equals(p2));
199         
200         // the clone and the original share a reference to the SAME dataset
201
assertTrue(p1.getDataset() == p2.getDataset());
202         
203         // try a few checks to ensure that the clone is independent of the
204
// original
205
p1.getTickLabelFormat().setMinimumIntegerDigits(99);
206         assertFalse(p1.equals(p2));
207         p2.getTickLabelFormat().setMinimumIntegerDigits(99);
208         assertTrue(p1.equals(p2));
209         
210         p1.addInterval(new MeterInterval("Test", new Range(1.234, 5.678)));
211         assertFalse(p1.equals(p2));
212         p2.addInterval(new MeterInterval("Test", new Range(1.234, 5.678)));
213         assertTrue(p1.equals(p2));
214         
215     }
216     
217     /**
218      * Serialize an instance, restore it, and check for equality.
219      */

220     public void testSerialization1() {
221         MeterPlot p1 = new MeterPlot(null);
222         MeterPlot p2 = null;
223         try {
224             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
225             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
226             out.writeObject(p1);
227             out.close();
228
229             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
230                  new ByteArrayInputStream JavaDoc(buffer.toByteArray())
231             );
232             p2 = (MeterPlot) in.readObject();
233             in.close();
234         }
235         catch (Exception JavaDoc e) {
236             System.out.println(e.toString());
237         }
238         assertEquals(p1, p2);
239     }
240
241     /**
242      * Serialize an instance, restore it, and check for equality.
243      */

244     public void testSerialization2() {
245         MeterPlot p1 = new MeterPlot(new DefaultValueDataset(1.23));
246         MeterPlot p2 = null;
247         try {
248             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
249             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
250             out.writeObject(p1);
251             out.close();
252
253             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
254                  new ByteArrayInputStream JavaDoc(buffer.toByteArray())
255             );
256             p2 = (MeterPlot) in.readObject();
257             in.close();
258         }
259         catch (Exception JavaDoc e) {
260             e.printStackTrace();
261         }
262         assertEquals(p1, p2);
263
264     }
265     
266 }
267
Popular Tags