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: * XYStepAreaRendererTests.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): Matthias Rose;
034: *
035: * $Id: XYStepAreaRendererTests.java,v 1.1.2.2 2007/02/14 13:54:14 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 25-Mar-2003 : Version 1 (DG);
040: * 26-Sep-2003 : copied XYStepRendererTests.java and used for
041: * testing XYStepAreaRenderer (MR);
042: * 14-Feb-2007 : Extended testEquals() (DG);
043: *
044: */
045:
046: package org.jfree.chart.renderer.xy.junit;
047:
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.chart.JFreeChart;
060: import org.jfree.chart.axis.NumberAxis;
061: import org.jfree.chart.plot.XYPlot;
062: import org.jfree.chart.renderer.xy.XYStepAreaRenderer;
063: import org.jfree.data.xy.DefaultTableXYDataset;
064: import org.jfree.data.xy.XYSeries;
065:
066: /**
067: * Tests for the {@link XYStepAreaRenderer} class.
068: */
069: public class XYStepAreaRendererTests 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(XYStepAreaRendererTests.class);
078: }
079:
080: /**
081: * Constructs a new set of tests.
082: *
083: * @param name the name of the tests.
084: */
085: public XYStepAreaRendererTests(String name) {
086: super (name);
087: }
088:
089: /**
090: * Check that the equals() method distinguishes all fields.
091: */
092: public void testEquals() {
093: XYStepAreaRenderer r1 = new XYStepAreaRenderer();
094: XYStepAreaRenderer r2 = new XYStepAreaRenderer();
095: assertEquals(r1, r2);
096:
097: r1.setOutline(true);
098: assertFalse(r1.equals(r2));
099: r2.setOutline(true);
100: assertTrue(r1.equals(r2));
101:
102: r1.setShapesVisible(true);
103: assertFalse(r1.equals(r2));
104: r2.setShapesVisible(true);
105: assertTrue(r1.equals(r2));
106:
107: r1.setShapesFilled(true);
108: assertFalse(r1.equals(r2));
109: r2.setShapesFilled(true);
110: assertTrue(r1.equals(r2));
111:
112: r1.setPlotArea(false);
113: assertFalse(r1.equals(r2));
114: r2.setPlotArea(false);
115: assertTrue(r1.equals(r2));
116:
117: r1.setRangeBase(-1.0);
118: assertFalse(r1.equals(r2));
119: r2.setRangeBase(-1.0);
120: assertTrue(r1.equals(r2));
121: }
122:
123: /**
124: * Two objects that are equal are required to return the same hashCode.
125: */
126: public void testHashcode() {
127: XYStepAreaRenderer r1 = new XYStepAreaRenderer();
128: XYStepAreaRenderer r2 = new XYStepAreaRenderer();
129: assertTrue(r1.equals(r2));
130: int h1 = r1.hashCode();
131: int h2 = r2.hashCode();
132: assertEquals(h1, h2);
133: }
134:
135: /**
136: * Confirm that cloning works.
137: */
138: public void testCloning() {
139: XYStepAreaRenderer r1 = new XYStepAreaRenderer();
140: XYStepAreaRenderer r2 = null;
141: try {
142: r2 = (XYStepAreaRenderer) r1.clone();
143: } catch (CloneNotSupportedException e) {
144: e.printStackTrace();
145: }
146: assertTrue(r1 != r2);
147: assertTrue(r1.getClass() == r2.getClass());
148: assertTrue(r1.equals(r2));
149: }
150:
151: /**
152: * Serialize an instance, restore it, and check for equality.
153: */
154: public void testSerialization() {
155:
156: XYStepAreaRenderer r1 = new XYStepAreaRenderer();
157: XYStepAreaRenderer r2 = null;
158:
159: try {
160: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
161: ObjectOutput out = new ObjectOutputStream(buffer);
162: out.writeObject(r1);
163: out.close();
164:
165: ObjectInput in = new ObjectInputStream(
166: new ByteArrayInputStream(buffer.toByteArray()));
167: r2 = (XYStepAreaRenderer) in.readObject();
168: in.close();
169: } catch (Exception e) {
170: e.printStackTrace();
171: }
172: assertEquals(r1, r2);
173: }
174:
175: /**
176: * Draws the chart with a <code>null</code> info object to make sure that
177: * no exceptions are thrown (particularly by code in the renderer).
178: */
179: public void testDrawWithNullInfo() {
180: boolean success = false;
181: try {
182: DefaultTableXYDataset dataset = new DefaultTableXYDataset();
183:
184: XYSeries s1 = new XYSeries("Series 1", true, false);
185: s1.add(5.0, 5.0);
186: s1.add(10.0, 15.5);
187: s1.add(15.0, 9.5);
188: s1.add(20.0, 7.5);
189: dataset.addSeries(s1);
190:
191: XYSeries s2 = new XYSeries("Series 2", true, false);
192: s2.add(5.0, 5.0);
193: s2.add(10.0, 15.5);
194: s2.add(15.0, 9.5);
195: s2.add(20.0, 3.5);
196: dataset.addSeries(s2);
197: XYPlot plot = new XYPlot(dataset, new NumberAxis("X"),
198: new NumberAxis("Y"), new XYStepAreaRenderer());
199: JFreeChart chart = new JFreeChart(plot);
200: /* BufferedImage image = */chart.createBufferedImage(300,
201: 200, null);
202: success = true;
203: } catch (NullPointerException e) {
204: e.printStackTrace();
205: success = false;
206: }
207: assertTrue(success);
208: }
209:
210: }
|