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: * CombinedRangeXYPlotTests.java
029: * -----------------------------
030: * (C) Copyright 2003, 2004, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: CombinedRangeXYPlotTests.java,v 1.1.2.1 2006/10/03 15:41:27 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 21-Aug-2003 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.chart.plot.junit;
044:
045: import java.awt.Font;
046: import java.io.ByteArrayInputStream;
047: import java.io.ByteArrayOutputStream;
048: import java.io.ObjectInput;
049: import java.io.ObjectInputStream;
050: import java.io.ObjectOutput;
051: import java.io.ObjectOutputStream;
052: import java.util.List;
053:
054: import junit.framework.Test;
055: import junit.framework.TestCase;
056: import junit.framework.TestSuite;
057:
058: import org.jfree.chart.annotations.XYTextAnnotation;
059: import org.jfree.chart.axis.AxisLocation;
060: import org.jfree.chart.axis.NumberAxis;
061: import org.jfree.chart.plot.CombinedRangeXYPlot;
062: import org.jfree.chart.plot.PlotOrientation;
063: import org.jfree.chart.plot.XYPlot;
064: import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
065: import org.jfree.chart.renderer.xy.XYItemRenderer;
066: import org.jfree.data.xy.XYDataset;
067: import org.jfree.data.xy.XYSeries;
068: import org.jfree.data.xy.XYSeriesCollection;
069:
070: /**
071: * Tests for the {@link CombinedRangeXYPlot} class.
072: */
073: public class CombinedRangeXYPlotTests extends TestCase {
074:
075: /**
076: * Returns the tests as a test suite.
077: *
078: * @return The test suite.
079: */
080: public static Test suite() {
081: return new TestSuite(CombinedRangeXYPlotTests.class);
082: }
083:
084: /**
085: * Constructs a new set of tests.
086: *
087: * @param name the name of the tests.
088: */
089: public CombinedRangeXYPlotTests(String name) {
090: super (name);
091: }
092:
093: /**
094: * Test the equals method.
095: */
096: public void testEquals() {
097: CombinedRangeXYPlot plot1 = createPlot();
098: CombinedRangeXYPlot plot2 = createPlot();
099: assertTrue(plot1.equals(plot2));
100: assertTrue(plot2.equals(plot1));
101: }
102:
103: /**
104: * This is a test to replicate the bug report 987080.
105: */
106: public void testRemoveSubplot() {
107: CombinedRangeXYPlot plot = new CombinedRangeXYPlot();
108: XYPlot plot1 = new XYPlot();
109: XYPlot plot2 = new XYPlot();
110: plot.add(plot1);
111: plot.add(plot2);
112: // remove plot2, but plot1 is removed instead
113: plot.remove(plot2);
114: List plots = plot.getSubplots();
115: assertTrue(plots.get(0) == plot1);
116: }
117:
118: /**
119: * Confirm that cloning works.
120: */
121: public void testCloning() {
122: CombinedRangeXYPlot plot1 = createPlot();
123: CombinedRangeXYPlot plot2 = null;
124: try {
125: plot2 = (CombinedRangeXYPlot) plot1.clone();
126: } catch (CloneNotSupportedException e) {
127: System.err.println("Failed to clone.");
128: }
129: assertTrue(plot1 != plot2);
130: assertTrue(plot1.getClass() == plot2.getClass());
131: assertTrue(plot1.equals(plot2));
132: }
133:
134: /**
135: * Serialize an instance, restore it, and check for equality.
136: */
137: public void testSerialization() {
138:
139: CombinedRangeXYPlot plot1 = createPlot();
140: CombinedRangeXYPlot plot2 = null;
141:
142: try {
143: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
144: ObjectOutput out = new ObjectOutputStream(buffer);
145: out.writeObject(plot1);
146: out.close();
147:
148: ObjectInput in = new ObjectInputStream(
149: new ByteArrayInputStream(buffer.toByteArray()));
150: plot2 = (CombinedRangeXYPlot) in.readObject();
151: in.close();
152: } catch (Exception e) {
153: e.printStackTrace();
154: }
155: assertEquals(plot1, plot2);
156:
157: }
158:
159: /**
160: * Creates a sample dataset.
161: *
162: * @return Series 1.
163: */
164: private XYDataset createDataset1() {
165:
166: // create dataset 1...
167: XYSeries series1 = new XYSeries("Series 1");
168: series1.add(10.0, 12353.3);
169: series1.add(20.0, 13734.4);
170: series1.add(30.0, 14525.3);
171: series1.add(40.0, 13984.3);
172: series1.add(50.0, 12999.4);
173: series1.add(60.0, 14274.3);
174: series1.add(70.0, 15943.5);
175: series1.add(80.0, 14845.3);
176: series1.add(90.0, 14645.4);
177: series1.add(100.0, 16234.6);
178: series1.add(110.0, 17232.3);
179: series1.add(120.0, 14232.2);
180: series1.add(130.0, 13102.2);
181: series1.add(140.0, 14230.2);
182: series1.add(150.0, 11235.2);
183:
184: XYSeries series2 = new XYSeries("Series 2");
185: series2.add(10.0, 15000.3);
186: series2.add(20.0, 11000.4);
187: series2.add(30.0, 17000.3);
188: series2.add(40.0, 15000.3);
189: series2.add(50.0, 14000.4);
190: series2.add(60.0, 12000.3);
191: series2.add(70.0, 11000.5);
192: series2.add(80.0, 12000.3);
193: series2.add(90.0, 13000.4);
194: series2.add(100.0, 12000.6);
195: series2.add(110.0, 13000.3);
196: series2.add(120.0, 17000.2);
197: series2.add(130.0, 18000.2);
198: series2.add(140.0, 16000.2);
199: series2.add(150.0, 17000.2);
200:
201: XYSeriesCollection collection = new XYSeriesCollection();
202: collection.addSeries(series1);
203: collection.addSeries(series2);
204: return collection;
205:
206: }
207:
208: /**
209: * Creates a sample dataset.
210: *
211: * @return Series 2.
212: */
213: private XYDataset createDataset2() {
214:
215: // create dataset 2...
216: XYSeries series2 = new XYSeries("Series 3");
217:
218: series2.add(10.0, 16853.2);
219: series2.add(20.0, 19642.3);
220: series2.add(30.0, 18253.5);
221: series2.add(40.0, 15352.3);
222: series2.add(50.0, 13532.0);
223: series2.add(100.0, 12635.3);
224: series2.add(110.0, 13998.2);
225: series2.add(120.0, 11943.2);
226: series2.add(130.0, 16943.9);
227: series2.add(140.0, 17843.2);
228: series2.add(150.0, 16495.3);
229: series2.add(160.0, 17943.6);
230: series2.add(170.0, 18500.7);
231: series2.add(180.0, 19595.9);
232:
233: return new XYSeriesCollection(series2);
234:
235: }
236:
237: /**
238: * Creates a sample plot.
239: *
240: * @return A sample plot.
241: */
242: private CombinedRangeXYPlot createPlot() {
243: // create subplot 1...
244: XYDataset data1 = createDataset1();
245: XYItemRenderer renderer1 = new StandardXYItemRenderer();
246: NumberAxis rangeAxis1 = new NumberAxis("Range 1");
247: XYPlot subplot1 = new XYPlot(data1, null, rangeAxis1, renderer1);
248: subplot1.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
249:
250: XYTextAnnotation annotation = new XYTextAnnotation("Hello!",
251: 50.0, 10000.0);
252: annotation.setFont(new Font("SansSerif", Font.PLAIN, 9));
253: annotation.setRotationAngle(Math.PI / 4.0);
254: subplot1.addAnnotation(annotation);
255:
256: // create subplot 2...
257: XYDataset data2 = createDataset2();
258: XYItemRenderer renderer2 = new StandardXYItemRenderer();
259: NumberAxis rangeAxis2 = new NumberAxis("Range 2");
260: rangeAxis2.setAutoRangeIncludesZero(false);
261: XYPlot subplot2 = new XYPlot(data2, null, rangeAxis2, renderer2);
262: subplot2.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
263:
264: // parent plot...
265: CombinedRangeXYPlot plot = new CombinedRangeXYPlot(
266: new NumberAxis("Range"));
267: plot.setGap(10.0);
268:
269: // add the subplots...
270: plot.add(subplot1, 1);
271: plot.add(subplot2, 1);
272: plot.setOrientation(PlotOrientation.VERTICAL);
273: return plot;
274: }
275:
276: }
|