001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2005, 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: * XYStepRendererTests.java
029: * ------------------------
030: * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: XYStepRendererTests.java,v 1.1.2.2 2007/05/17 16:17:43 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 25-Mar-2003 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.chart.renderer.xy.junit;
044:
045: import java.io.ByteArrayInputStream;
046: import java.io.ByteArrayOutputStream;
047: import java.io.ObjectInput;
048: import java.io.ObjectInputStream;
049: import java.io.ObjectOutput;
050: import java.io.ObjectOutputStream;
051:
052: import junit.framework.Test;
053: import junit.framework.TestCase;
054: import junit.framework.TestSuite;
055:
056: import org.jfree.chart.JFreeChart;
057: import org.jfree.chart.axis.NumberAxis;
058: import org.jfree.chart.plot.XYPlot;
059: import org.jfree.chart.renderer.xy.XYStepRenderer;
060: import org.jfree.data.xy.DefaultTableXYDataset;
061: import org.jfree.data.xy.XYSeries;
062:
063: /**
064: * Tests for the {@link XYStepRenderer} class.
065: */
066: public class XYStepRendererTests extends TestCase {
067:
068: /**
069: * Returns the tests as a test suite.
070: *
071: * @return The test suite.
072: */
073: public static Test suite() {
074: return new TestSuite(XYStepRendererTests.class);
075: }
076:
077: /**
078: * Constructs a new set of tests.
079: *
080: * @param name the name of the tests.
081: */
082: public XYStepRendererTests(String name) {
083: super (name);
084: }
085:
086: /**
087: * Check that the equals() method distinguishes all fields.
088: */
089: public void testEquals() {
090: XYStepRenderer r1 = new XYStepRenderer();
091: XYStepRenderer r2 = new XYStepRenderer();
092: assertEquals(r1, r2);
093: }
094:
095: /**
096: * Two objects that are equal are required to return the same hashCode.
097: */
098: public void testHashcode() {
099: XYStepRenderer r1 = new XYStepRenderer();
100: XYStepRenderer r2 = new XYStepRenderer();
101: assertTrue(r1.equals(r2));
102: int h1 = r1.hashCode();
103: int h2 = r2.hashCode();
104: assertEquals(h1, h2);
105: }
106:
107: /**
108: * Confirm that cloning works.
109: */
110: public void testCloning() {
111: XYStepRenderer r1 = new XYStepRenderer();
112: XYStepRenderer r2 = null;
113: try {
114: r2 = (XYStepRenderer) r1.clone();
115: } catch (CloneNotSupportedException e) {
116: System.err.println("Failed to clone.");
117: }
118: assertTrue(r1 != r2);
119: assertTrue(r1.getClass() == r2.getClass());
120: assertTrue(r1.equals(r2));
121: }
122:
123: /**
124: * Serialize an instance, restore it, and check for equality.
125: */
126: public void testSerialization() {
127:
128: XYStepRenderer r1 = new XYStepRenderer();
129: XYStepRenderer r2 = null;
130:
131: try {
132: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
133: ObjectOutput out = new ObjectOutputStream(buffer);
134: out.writeObject(r1);
135: out.close();
136:
137: ObjectInput in = new ObjectInputStream(
138: new ByteArrayInputStream(buffer.toByteArray()));
139: r2 = (XYStepRenderer) in.readObject();
140: in.close();
141: } catch (Exception e) {
142: System.out.println(e.toString());
143: }
144: assertEquals(r1, r2);
145:
146: }
147:
148: /**
149: * Draws the chart with a <code>null</code> info object to make sure that
150: * no exceptions are thrown (particularly by code in the renderer).
151: */
152: public void testDrawWithNullInfo() {
153: boolean success = false;
154: try {
155: DefaultTableXYDataset dataset = new DefaultTableXYDataset();
156:
157: XYSeries s1 = new XYSeries("Series 1", true, false);
158: s1.add(5.0, 5.0);
159: s1.add(10.0, 15.5);
160: s1.add(15.0, 9.5);
161: s1.add(20.0, 7.5);
162: dataset.addSeries(s1);
163:
164: XYSeries s2 = new XYSeries("Series 2", true, false);
165: s2.add(5.0, 5.0);
166: s2.add(10.0, 15.5);
167: s2.add(15.0, 9.5);
168: s2.add(20.0, 3.5);
169: dataset.addSeries(s2);
170: XYPlot plot = new XYPlot(dataset, new NumberAxis("X"),
171: new NumberAxis("Y"), new XYStepRenderer());
172: JFreeChart chart = new JFreeChart(plot);
173: /* BufferedImage image = */chart.createBufferedImage(300,
174: 200, null);
175: success = true;
176: } catch (NullPointerException e) {
177: e.printStackTrace();
178: success = false;
179: }
180: assertTrue(success);
181: }
182:
183: }
|