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: * ThermometerPlotTests.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: ThermometerPlotTests.java,v 1.1.2.3 2007/05/03 12:58:48 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 26-Mar-2003 : Version 1 (DG);
040: * 30-Apr-2007 : Added new serialization test (DG);
041: * 03-May-2007 : Added cloning test (DG);
042: *
043: */
044:
045: package org.jfree.chart.plot.junit;
046:
047: import java.awt.BasicStroke;
048: import java.awt.Color;
049: import java.awt.Font;
050: import java.awt.GradientPaint;
051: import java.io.ByteArrayInputStream;
052: import java.io.ByteArrayOutputStream;
053: import java.io.ObjectInput;
054: import java.io.ObjectInputStream;
055: import java.io.ObjectOutput;
056: import java.io.ObjectOutputStream;
057: import java.text.DecimalFormat;
058:
059: import junit.framework.Test;
060: import junit.framework.TestCase;
061: import junit.framework.TestSuite;
062:
063: import org.jfree.chart.plot.ThermometerPlot;
064: import org.jfree.ui.RectangleInsets;
065:
066: /**
067: * Tests for the {@link ThermometerPlot} class.
068: */
069: public class ThermometerPlotTests extends TestCase {
070:
071: /**
072: * Returns the tests as a test suite.
073: *
074: * @return The test suite.
075: */
076: public static Test suite() {
077: return new TestSuite(ThermometerPlotTests.class);
078: }
079:
080: /**
081: * Constructs a new set of tests.
082: *
083: * @param name the name of the tests.
084: */
085: public ThermometerPlotTests(String name) {
086: super (name);
087: }
088:
089: /**
090: * Some checks for the equals() method.
091: */
092: public void testEquals() {
093: ThermometerPlot p1 = new ThermometerPlot();
094: ThermometerPlot p2 = new ThermometerPlot();
095: assertTrue(p1.equals(p2));
096: assertTrue(p2.equals(p1));
097:
098: // padding
099: p1.setPadding(new RectangleInsets(1.0, 2.0, 3.0, 4.0));
100: assertFalse(p1.equals(p2));
101: p2.setPadding(new RectangleInsets(1.0, 2.0, 3.0, 4.0));
102: assertTrue(p2.equals(p1));
103:
104: // thermometerStroke
105: BasicStroke s = new BasicStroke(1.23f);
106: p1.setThermometerStroke(s);
107: assertFalse(p1.equals(p2));
108: p2.setThermometerStroke(s);
109: assertTrue(p2.equals(p1));
110:
111: // thermometerPaint
112: p1.setThermometerPaint(new GradientPaint(1.0f, 2.0f,
113: Color.blue, 3.0f, 4.0f, Color.red));
114: assertFalse(p1.equals(p2));
115: p2.setThermometerPaint(new GradientPaint(1.0f, 2.0f,
116: Color.blue, 3.0f, 4.0f, Color.red));
117: assertTrue(p2.equals(p1));
118:
119: // units
120: p1.setUnits(ThermometerPlot.UNITS_KELVIN);
121: assertFalse(p1.equals(p2));
122: p2.setUnits(ThermometerPlot.UNITS_KELVIN);
123: assertTrue(p2.equals(p1));
124:
125: // valueLocation
126: p1.setValueLocation(ThermometerPlot.LEFT);
127: assertFalse(p1.equals(p2));
128: p2.setValueLocation(ThermometerPlot.LEFT);
129: assertTrue(p2.equals(p1));
130:
131: // axisLocation
132: p1.setAxisLocation(ThermometerPlot.RIGHT);
133: assertFalse(p1.equals(p2));
134: p2.setAxisLocation(ThermometerPlot.RIGHT);
135: assertTrue(p2.equals(p1));
136:
137: // valueFont
138: p1.setValueFont(new Font("Serif", Font.PLAIN, 9));
139: assertFalse(p1.equals(p2));
140: p2.setValueFont(new Font("Serif", Font.PLAIN, 9));
141: assertTrue(p2.equals(p1));
142:
143: // valuePaint
144: p1.setValuePaint(new GradientPaint(4.0f, 5.0f, Color.red, 6.0f,
145: 7.0f, Color.white));
146: assertFalse(p1.equals(p2));
147: p2.setValuePaint(new GradientPaint(4.0f, 5.0f, Color.red, 6.0f,
148: 7.0f, Color.white));
149: assertTrue(p2.equals(p1));
150:
151: // valueFormat
152: p1.setValueFormat(new DecimalFormat("0.0000"));
153: assertFalse(p1.equals(p2));
154: p2.setValueFormat(new DecimalFormat("0.0000"));
155: assertTrue(p2.equals(p1));
156:
157: // mercuryPaint
158: p1.setMercuryPaint(new GradientPaint(9.0f, 8.0f, Color.red,
159: 7.0f, 6.0f, Color.blue));
160: assertFalse(p1.equals(p2));
161: p2.setMercuryPaint(new GradientPaint(9.0f, 8.0f, Color.red,
162: 7.0f, 6.0f, Color.blue));
163: assertTrue(p2.equals(p1));
164:
165: // showValueLines
166: p1.setShowValueLines(true);
167: assertFalse(p1.equals(p2));
168: p2.setShowValueLines(true);
169: assertTrue(p2.equals(p1));
170:
171: p1.setSubrange(1, 1.0, 2.0);
172: assertFalse(p1.equals(p2));
173: p2.setSubrange(1, 1.0, 2.0);
174: assertTrue(p2.equals(p1));
175:
176: p1.setSubrangePaint(1, new GradientPaint(1.0f, 2.0f, Color.red,
177: 3.0f, 4.0f, Color.yellow));
178: assertFalse(p1.equals(p2));
179: p2.setSubrangePaint(1, new GradientPaint(1.0f, 2.0f, Color.red,
180: 3.0f, 4.0f, Color.yellow));
181: assertTrue(p2.equals(p1));
182:
183: }
184:
185: /**
186: * Confirm that cloning works.
187: */
188: public void testCloning() {
189: ThermometerPlot p1 = new ThermometerPlot();
190: ThermometerPlot p2 = null;
191: try {
192: p2 = (ThermometerPlot) p1.clone();
193: } catch (CloneNotSupportedException e) {
194: e.printStackTrace();
195: }
196: assertTrue(p1 != p2);
197: assertTrue(p1.getClass() == p2.getClass());
198: assertTrue(p1.equals(p2));
199: }
200:
201: /**
202: * Serialize an instance, restore it, and check for equality.
203: */
204: public void testSerialization() {
205:
206: ThermometerPlot p1 = new ThermometerPlot();
207: ThermometerPlot p2 = null;
208:
209: try {
210: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
211: ObjectOutput out = new ObjectOutputStream(buffer);
212: out.writeObject(p1);
213: out.close();
214:
215: ObjectInput in = new ObjectInputStream(
216: new ByteArrayInputStream(buffer.toByteArray()));
217: p2 = (ThermometerPlot) in.readObject();
218: in.close();
219: } catch (Exception e) {
220: e.printStackTrace();
221: }
222: assertTrue(p1.equals(p2));
223:
224: }
225:
226: /**
227: * Serialize an instance, restore it, and check for equality.
228: */
229: public void testSerialization2() {
230: ThermometerPlot p1 = new ThermometerPlot();
231: p1.setSubrangePaint(1, new GradientPaint(1.0f, 2.0f, Color.red,
232: 3.0f, 4.0f, Color.blue));
233: ThermometerPlot p2 = null;
234:
235: try {
236: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
237: ObjectOutput out = new ObjectOutputStream(buffer);
238: out.writeObject(p1);
239: out.close();
240:
241: ObjectInput in = new ObjectInputStream(
242: new ByteArrayInputStream(buffer.toByteArray()));
243: p2 = (ThermometerPlot) in.readObject();
244: in.close();
245: } catch (Exception e) {
246: e.printStackTrace();
247: }
248: assertTrue(p1.equals(p2));
249: }
250:
251: /**
252: * Some checks for the setUnits() method.
253: */
254: public void testSetUnits() {
255: ThermometerPlot p1 = new ThermometerPlot();
256: assertEquals(ThermometerPlot.UNITS_CELCIUS, p1.getUnits());
257: p1.setUnits("FAHRENHEIT"); // this doesn't work
258: assertEquals(ThermometerPlot.UNITS_CELCIUS, p1.getUnits());
259: p1.setUnits("\u00B0F"); // ...but this does!
260: assertEquals(ThermometerPlot.UNITS_FAHRENHEIT, p1.getUnits());
261: }
262: }
|