001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2006, 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: * FastScatterPlotTests.java
029: * -------------------------
030: * (C) Copyright 2003, 2004, 2006, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: FastScatterPlotTests.java,v 1.1.2.2 2006/11/10 15:09:23 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 18-Mar-2003 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.chart.plot.junit;
044:
045: import java.awt.BasicStroke;
046: import java.awt.Color;
047: import java.awt.GradientPaint;
048: import java.awt.Stroke;
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:
056: import junit.framework.Test;
057: import junit.framework.TestCase;
058: import junit.framework.TestSuite;
059:
060: import org.jfree.chart.JFreeChart;
061: import org.jfree.chart.axis.NumberAxis;
062: import org.jfree.chart.axis.ValueAxis;
063: import org.jfree.chart.plot.FastScatterPlot;
064:
065: /**
066: * Tests for the {@link FastScatterPlot} class.
067: */
068: public class FastScatterPlotTests extends TestCase {
069:
070: /**
071: * Returns the tests as a test suite.
072: *
073: * @return The test suite.
074: */
075: public static Test suite() {
076: return new TestSuite(FastScatterPlotTests.class);
077: }
078:
079: /**
080: * Constructs a new set of tests.
081: *
082: * @param name the name of the tests.
083: */
084: public FastScatterPlotTests(String name) {
085: super (name);
086: }
087:
088: /**
089: * Some checks for the equals() method.
090: */
091: public void testEquals() {
092:
093: FastScatterPlot plot1 = new FastScatterPlot();
094: FastScatterPlot plot2 = new FastScatterPlot();
095: assertTrue(plot1.equals(plot2));
096: assertTrue(plot2.equals(plot1));
097:
098: plot1.setPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
099: 4.0f, Color.yellow));
100: assertFalse(plot1.equals(plot2));
101: plot2.setPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
102: 4.0f, Color.yellow));
103: assertTrue(plot1.equals(plot2));
104:
105: plot1.setDomainGridlinesVisible(false);
106: assertFalse(plot1.equals(plot2));
107: plot2.setDomainGridlinesVisible(false);
108: assertTrue(plot1.equals(plot2));
109:
110: plot1.setDomainGridlinePaint(new GradientPaint(1.0f, 2.0f,
111: Color.blue, 3.0f, 4.0f, Color.yellow));
112: assertFalse(plot1.equals(plot2));
113: plot2.setDomainGridlinePaint(new GradientPaint(1.0f, 2.0f,
114: Color.blue, 3.0f, 4.0f, Color.yellow));
115: assertTrue(plot1.equals(plot2));
116:
117: Stroke s = new BasicStroke(1.5f);
118: plot1.setDomainGridlineStroke(s);
119: assertFalse(plot1.equals(plot2));
120: plot2.setDomainGridlineStroke(s);
121: assertTrue(plot1.equals(plot2));
122:
123: plot1.setRangeGridlinesVisible(false);
124: assertFalse(plot1.equals(plot2));
125: plot2.setRangeGridlinesVisible(false);
126: assertTrue(plot1.equals(plot2));
127:
128: plot1.setRangeGridlinePaint(new GradientPaint(1.0f, 2.0f,
129: Color.green, 3.0f, 4.0f, Color.yellow));
130: assertFalse(plot1.equals(plot2));
131: plot2.setRangeGridlinePaint(new GradientPaint(1.0f, 2.0f,
132: Color.green, 3.0f, 4.0f, Color.yellow));
133: assertTrue(plot1.equals(plot2));
134:
135: Stroke s2 = new BasicStroke(1.5f);
136: plot1.setRangeGridlineStroke(s2);
137: assertFalse(plot1.equals(plot2));
138: plot2.setRangeGridlineStroke(s2);
139: assertTrue(plot1.equals(plot2));
140:
141: }
142:
143: /**
144: * Confirm that cloning works.
145: */
146: public void testCloning() {
147: FastScatterPlot p1 = new FastScatterPlot();
148: FastScatterPlot p2 = null;
149: try {
150: p2 = (FastScatterPlot) p1.clone();
151: } catch (CloneNotSupportedException e) {
152: e.printStackTrace();
153: System.err.println("Failed to clone.");
154: }
155: assertTrue(p1 != p2);
156: assertTrue(p1.getClass() == p2.getClass());
157: assertTrue(p1.equals(p2));
158: }
159:
160: /**
161: * Serialize an instance, restore it, and check for equality.
162: */
163: public void testSerialization() {
164:
165: float[][] data = createData();
166:
167: ValueAxis domainAxis = new NumberAxis("X");
168: ValueAxis rangeAxis = new NumberAxis("Y");
169: FastScatterPlot p1 = new FastScatterPlot(data, domainAxis,
170: rangeAxis);
171: FastScatterPlot p2 = null;
172:
173: try {
174: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
175: ObjectOutput out = new ObjectOutputStream(buffer);
176: out.writeObject(p1);
177: out.close();
178:
179: ObjectInput in = new ObjectInputStream(
180: new ByteArrayInputStream(buffer.toByteArray()));
181: p2 = (FastScatterPlot) in.readObject();
182: in.close();
183: } catch (Exception e) {
184: System.out.println(e.toString());
185: }
186: assertEquals(p1, p2);
187:
188: }
189:
190: /**
191: * Draws the chart with a <code>null</code> info object to make sure that
192: * no exceptions are thrown.
193: */
194: public void testDrawWithNullInfo() {
195: boolean success = false;
196: try {
197: float[][] data = createData();
198:
199: ValueAxis domainAxis = new NumberAxis("X");
200: ValueAxis rangeAxis = new NumberAxis("Y");
201: FastScatterPlot plot = new FastScatterPlot(data,
202: domainAxis, rangeAxis);
203: JFreeChart chart = new JFreeChart(plot);
204: /* BufferedImage image = */chart.createBufferedImage(300,
205: 200, null);
206: success = true;
207: } catch (NullPointerException e) {
208: e.printStackTrace();
209: success = false;
210: }
211: assertTrue(success);
212: }
213:
214: /**
215: * Populates the data array with random values.
216: *
217: * @return Random data.
218: */
219: private float[][] createData() {
220:
221: float[][] result = new float[2][1000];
222: for (int i = 0; i < result[0].length; i++) {
223:
224: float x = (float) i + 100;
225: result[0][i] = x;
226: result[1][i] = 100 + (float) Math.random() * 1000;
227: }
228: return result;
229:
230: }
231:
232: }
|