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: * CombinedDomainXYPlotTests.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: CombinedDomainXYPlotTests.java,v 1.1.2.1 2006/10/03 15:41:26 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.CombinedDomainXYPlot;
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 CombinedDomainXYPlot} class.
072: */
073: public class CombinedDomainXYPlotTests 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(CombinedDomainXYPlotTests.class);
082: }
083:
084: /**
085: * Constructs a new set of tests.
086: *
087: * @param name the name of the tests.
088: */
089: public CombinedDomainXYPlotTests(String name) {
090: super (name);
091: }
092:
093: /**
094: * Confirm that the constructor will accept a null axis.
095: */
096: public void testConstructor1() {
097: CombinedDomainXYPlot plot = new CombinedDomainXYPlot(null);
098: assertEquals(null, plot.getDomainAxis());
099: }
100:
101: /**
102: * This is a test to replicate the bug report 987080.
103: */
104: public void testRemoveSubplot() {
105: CombinedDomainXYPlot plot = new CombinedDomainXYPlot();
106: XYPlot plot1 = new XYPlot();
107: XYPlot plot2 = new XYPlot();
108: plot.add(plot1);
109: plot.add(plot2);
110: // remove plot2, but plot1 is removed instead
111: plot.remove(plot2);
112: List plots = plot.getSubplots();
113: assertTrue(plots.get(0) == plot1);
114: }
115:
116: /**
117: * Tests the equals() method.
118: */
119: public void testEquals() {
120: CombinedDomainXYPlot plot1 = createPlot();
121: CombinedDomainXYPlot plot2 = createPlot();
122: assertTrue(plot1.equals(plot2));
123: assertTrue(plot2.equals(plot1));
124: }
125:
126: /**
127: * Confirm that cloning works.
128: */
129: public void testCloning() {
130: CombinedDomainXYPlot plot1 = createPlot();
131: CombinedDomainXYPlot plot2 = null;
132: try {
133: plot2 = (CombinedDomainXYPlot) plot1.clone();
134: } catch (CloneNotSupportedException e) {
135: System.err.println("Failed to clone.");
136: }
137: assertTrue(plot1 != plot2);
138: assertTrue(plot1.getClass() == plot2.getClass());
139: assertTrue(plot1.equals(plot2));
140: }
141:
142: /**
143: * Serialize an instance, restore it, and check for equality.
144: */
145: public void testSerialization() {
146:
147: CombinedDomainXYPlot plot1 = createPlot();
148: CombinedDomainXYPlot plot2 = null;
149:
150: try {
151: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
152: ObjectOutput out = new ObjectOutputStream(buffer);
153: out.writeObject(plot1);
154: out.close();
155:
156: ObjectInput in = new ObjectInputStream(
157: new ByteArrayInputStream(buffer.toByteArray()));
158: plot2 = (CombinedDomainXYPlot) in.readObject();
159: in.close();
160: } catch (Exception e) {
161: e.printStackTrace();
162: }
163: assertEquals(plot1, plot2);
164:
165: }
166:
167: /**
168: * Creates a sample dataset.
169: *
170: * @return Series 1.
171: */
172: private XYDataset createDataset1() {
173:
174: // create dataset 1...
175: XYSeries series1 = new XYSeries("Series 1");
176: series1.add(10.0, 12353.3);
177: series1.add(20.0, 13734.4);
178: series1.add(30.0, 14525.3);
179: series1.add(40.0, 13984.3);
180: series1.add(50.0, 12999.4);
181: series1.add(60.0, 14274.3);
182: series1.add(70.0, 15943.5);
183: series1.add(80.0, 14845.3);
184: series1.add(90.0, 14645.4);
185: series1.add(100.0, 16234.6);
186: series1.add(110.0, 17232.3);
187: series1.add(120.0, 14232.2);
188: series1.add(130.0, 13102.2);
189: series1.add(140.0, 14230.2);
190: series1.add(150.0, 11235.2);
191:
192: XYSeries series2 = new XYSeries("Series 2");
193: series2.add(10.0, 15000.3);
194: series2.add(20.0, 11000.4);
195: series2.add(30.0, 17000.3);
196: series2.add(40.0, 15000.3);
197: series2.add(50.0, 14000.4);
198: series2.add(60.0, 12000.3);
199: series2.add(70.0, 11000.5);
200: series2.add(80.0, 12000.3);
201: series2.add(90.0, 13000.4);
202: series2.add(100.0, 12000.6);
203: series2.add(110.0, 13000.3);
204: series2.add(120.0, 17000.2);
205: series2.add(130.0, 18000.2);
206: series2.add(140.0, 16000.2);
207: series2.add(150.0, 17000.2);
208:
209: XYSeriesCollection collection = new XYSeriesCollection();
210: collection.addSeries(series1);
211: collection.addSeries(series2);
212: return collection;
213:
214: }
215:
216: /**
217: * Creates a sample dataset.
218: *
219: * @return Series 2.
220: */
221: private XYDataset createDataset2() {
222:
223: // create dataset 2...
224: XYSeries series2 = new XYSeries("Series 3");
225:
226: series2.add(10.0, 16853.2);
227: series2.add(20.0, 19642.3);
228: series2.add(30.0, 18253.5);
229: series2.add(40.0, 15352.3);
230: series2.add(50.0, 13532.0);
231: series2.add(100.0, 12635.3);
232: series2.add(110.0, 13998.2);
233: series2.add(120.0, 11943.2);
234: series2.add(130.0, 16943.9);
235: series2.add(140.0, 17843.2);
236: series2.add(150.0, 16495.3);
237: series2.add(160.0, 17943.6);
238: series2.add(170.0, 18500.7);
239: series2.add(180.0, 19595.9);
240:
241: return new XYSeriesCollection(series2);
242:
243: }
244:
245: /**
246: * Creates a sample plot.
247: *
248: * @return A sample plot.
249: */
250: private CombinedDomainXYPlot createPlot() {
251: // create subplot 1...
252: XYDataset data1 = createDataset1();
253: XYItemRenderer renderer1 = new StandardXYItemRenderer();
254: NumberAxis rangeAxis1 = new NumberAxis("Range 1");
255: XYPlot subplot1 = new XYPlot(data1, null, rangeAxis1, renderer1);
256: subplot1.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
257:
258: XYTextAnnotation annotation = new XYTextAnnotation("Hello!",
259: 50.0, 10000.0);
260: annotation.setFont(new Font("SansSerif", Font.PLAIN, 9));
261: annotation.setRotationAngle(Math.PI / 4.0);
262: subplot1.addAnnotation(annotation);
263:
264: // create subplot 2...
265: XYDataset data2 = createDataset2();
266: XYItemRenderer renderer2 = new StandardXYItemRenderer();
267: NumberAxis rangeAxis2 = new NumberAxis("Range 2");
268: rangeAxis2.setAutoRangeIncludesZero(false);
269: XYPlot subplot2 = new XYPlot(data2, null, rangeAxis2, renderer2);
270: subplot2.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
271:
272: // parent plot...
273: CombinedDomainXYPlot plot = new CombinedDomainXYPlot(
274: new NumberAxis("Domain"));
275: plot.setGap(10.0);
276:
277: // add the subplots...
278: plot.add(subplot1, 1);
279: plot.add(subplot2, 1);
280: plot.setOrientation(PlotOrientation.VERTICAL);
281: return plot;
282: }
283: }
|