KickJava   Java API By Example, From Geeks To Geeks.

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


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  * FastScatterPlotTests.java
28  * -------------------------
29  * (C) Copyright 2003, 2004, by Object Refinery Limited and Contributors.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: FastScatterPlotTests.java,v 1.2 2005/03/16 09:42:35 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 18-Mar-2003 : Version 1 (DG);
39  *
40  */

41
42 package org.jfree.chart.plot.junit;
43
44 import java.awt.BasicStroke JavaDoc;
45 import java.awt.Color JavaDoc;
46 import java.awt.Stroke 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
54 import junit.framework.Test;
55 import junit.framework.TestCase;
56 import junit.framework.TestSuite;
57
58 import org.jfree.chart.axis.NumberAxis;
59 import org.jfree.chart.axis.ValueAxis;
60 import org.jfree.chart.plot.FastScatterPlot;
61
62 /**
63  * Tests for the {@link FastScatterPlot} class.
64  */

65 public class FastScatterPlotTests extends TestCase {
66
67     /**
68      * Returns the tests as a test suite.
69      *
70      * @return The test suite.
71      */

72     public static Test suite() {
73         return new TestSuite(FastScatterPlotTests.class);
74     }
75
76     /**
77      * Constructs a new set of tests.
78      *
79      * @param name the name of the tests.
80      */

81     public FastScatterPlotTests(String JavaDoc name) {
82         super(name);
83     }
84
85     /**
86      * Test the equals() method.
87      */

88     public void testEquals() {
89         
90         FastScatterPlot plot1 = new FastScatterPlot();
91         FastScatterPlot plot2 = new FastScatterPlot();
92         assertTrue(plot1.equals(plot2));
93         assertTrue(plot2.equals(plot1));
94         
95         plot1.setPaint(Color.yellow);
96         assertFalse(plot1.equals(plot2));
97         plot2.setPaint(Color.yellow);
98         assertTrue(plot1.equals(plot2));
99         
100         plot1.setDomainGridlinesVisible(false);
101         assertFalse(plot1.equals(plot2));
102         plot2.setDomainGridlinesVisible(false);
103         assertTrue(plot1.equals(plot2));
104         
105         plot1.setDomainGridlinePaint(Color.red);
106         assertFalse(plot1.equals(plot2));
107         plot2.setDomainGridlinePaint(Color.red);
108         assertTrue(plot1.equals(plot2));
109         
110         Stroke JavaDoc s = new BasicStroke JavaDoc(1.5f);
111         plot1.setDomainGridlineStroke(s);
112         assertFalse(plot1.equals(plot2));
113         plot2.setDomainGridlineStroke(s);
114         assertTrue(plot1.equals(plot2));
115         
116         plot1.setRangeGridlinesVisible(false);
117         assertFalse(plot1.equals(plot2));
118         plot2.setRangeGridlinesVisible(false);
119         assertTrue(plot1.equals(plot2));
120         
121         plot1.setRangeGridlinePaint(Color.red);
122         assertFalse(plot1.equals(plot2));
123         plot2.setRangeGridlinePaint(Color.red);
124         assertTrue(plot1.equals(plot2));
125         
126         Stroke JavaDoc s2 = new BasicStroke JavaDoc(1.5f);
127         plot1.setRangeGridlineStroke(s2);
128         assertFalse(plot1.equals(plot2));
129         plot2.setRangeGridlineStroke(s2);
130         assertTrue(plot1.equals(plot2));
131         
132     }
133
134     /**
135      * Confirm that cloning works.
136      */

137     public void testCloning() {
138         FastScatterPlot p1 = new FastScatterPlot();
139         FastScatterPlot p2 = null;
140         try {
141             p2 = (FastScatterPlot) p1.clone();
142         }
143         catch (CloneNotSupportedException JavaDoc e) {
144             e.printStackTrace();
145             System.err.println("Failed to clone.");
146         }
147         assertTrue(p1 != p2);
148         assertTrue(p1.getClass() == p2.getClass());
149         assertTrue(p1.equals(p2));
150     }
151
152     /**
153      * Serialize an instance, restore it, and check for equality.
154      */

155     public void testSerialization() {
156
157         float[][] data = createData();
158
159         ValueAxis domainAxis = new NumberAxis("X");
160         ValueAxis rangeAxis = new NumberAxis("Y");
161         FastScatterPlot p1 = new FastScatterPlot(data, domainAxis, rangeAxis);
162         FastScatterPlot p2 = null;
163
164         try {
165             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
166             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
167             out.writeObject(p1);
168             out.close();
169
170             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
171                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
172             );
173             p2 = (FastScatterPlot) in.readObject();
174             in.close();
175         }
176         catch (Exception JavaDoc e) {
177             System.out.println(e.toString());
178         }
179         assertEquals(p1, p2);
180
181     }
182
183     /**
184      * Populates the data array with random values.
185      *
186      * @return Random data.
187      */

188     private float[][] createData() {
189
190         float[][] result = new float[2][1000];
191         for (int i = 0; i < result[0].length; i++) {
192
193             float x = (float) i + 100;
194             result[0][i] = x;
195             result[1][i] = 100 + (float) Math.random() * 1000;
196         }
197         return result;
198
199     }
200
201 }
202
Popular Tags