001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, 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: * XYLineAndShapeRendererTests.java
029: * --------------------------------
030: * (C) Copyright 2004, 2005, 2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: XYLineAndShapeRendererTests.java,v 1.1.2.4 2007/05/17 16:23:31 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 27-Jan-2004 : Version 1 (DG);
040: * 07-Jan-2005 : Added check for findRangeBounds() method (DG);
041: * 21-Feb-2007 : Check independence in testCloning() (DG);
042: * 17-May-2007 : Added testGetLegendItemSeriesIndex() (DG);
043: *
044: */
045:
046: package org.jfree.chart.renderer.xy.junit;
047:
048: import java.awt.geom.Line2D;
049: import java.awt.geom.Rectangle2D;
050: import java.io.ByteArrayInputStream;
051: import java.io.ByteArrayOutputStream;
052: import java.io.ObjectInput;
053: import java.io.ObjectInputStream;
054: import java.io.ObjectOutput;
055: import java.io.ObjectOutputStream;
056:
057: import junit.framework.Test;
058: import junit.framework.TestCase;
059: import junit.framework.TestSuite;
060:
061: import org.jfree.chart.ChartFactory;
062: import org.jfree.chart.JFreeChart;
063: import org.jfree.chart.LegendItem;
064: import org.jfree.chart.axis.NumberAxis;
065: import org.jfree.chart.plot.PlotOrientation;
066: import org.jfree.chart.plot.XYPlot;
067: import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
068: import org.jfree.chart.urls.TimeSeriesURLGenerator;
069: import org.jfree.data.Range;
070: import org.jfree.data.xy.TableXYDataset;
071: import org.jfree.data.xy.XYSeries;
072: import org.jfree.data.xy.XYSeriesCollection;
073:
074: /**
075: * Tests for the {@link XYLineAndShapeRenderer} class.
076: */
077: public class XYLineAndShapeRendererTests extends TestCase {
078:
079: /**
080: * Returns the tests as a test suite.
081: *
082: * @return The test suite.
083: */
084: public static Test suite() {
085: return new TestSuite(XYLineAndShapeRendererTests.class);
086: }
087:
088: /**
089: * Constructs a new set of tests.
090: *
091: * @param name the name of the tests.
092: */
093: public XYLineAndShapeRendererTests(String name) {
094: super (name);
095: }
096:
097: /**
098: * Test that the equals() method distinguishes all fields.
099: */
100: public void testEquals() {
101:
102: XYLineAndShapeRenderer r1 = new XYLineAndShapeRenderer();
103: XYLineAndShapeRenderer r2 = new XYLineAndShapeRenderer();
104: assertEquals(r1, r2);
105: assertEquals(r2, r1);
106:
107: r1.setLinesVisible(true);
108: assertFalse(r1.equals(r2));
109: r2.setLinesVisible(true);
110: assertTrue(r1.equals(r2));
111:
112: r1.setSeriesLinesVisible(3, true);
113: assertFalse(r1.equals(r2));
114: r2.setSeriesLinesVisible(3, true);
115: assertTrue(r1.equals(r2));
116:
117: r1.setBaseLinesVisible(false);
118: assertFalse(r1.equals(r2));
119: r2.setBaseLinesVisible(false);
120: assertTrue(r1.equals(r2));
121:
122: r1.setLegendLine(new Line2D.Double(1.0, 2.0, 3.0, 4.0));
123: assertFalse(r1.equals(r2));
124: r2.setLegendLine(new Line2D.Double(1.0, 2.0, 3.0, 4.0));
125: assertTrue(r1.equals(r2));
126:
127: r1.setShapesVisible(true);
128: assertFalse(r1.equals(r2));
129: r2.setShapesVisible(true);
130: assertTrue(r1.equals(r2));
131:
132: r1.setSeriesShapesVisible(3, true);
133: assertFalse(r1.equals(r2));
134: r2.setSeriesShapesVisible(3, true);
135: assertTrue(r1.equals(r2));
136:
137: r1.setBaseShapesVisible(false);
138: assertFalse(r1.equals(r2));
139: r2.setBaseShapesVisible(false);
140: assertTrue(r1.equals(r2));
141:
142: r1.setShapesFilled(true);
143: assertFalse(r1.equals(r2));
144: r2.setShapesFilled(true);
145: assertTrue(r1.equals(r2));
146:
147: r1.setSeriesShapesFilled(3, true);
148: assertFalse(r1.equals(r2));
149: r2.setSeriesShapesFilled(3, true);
150: assertTrue(r1.equals(r2));
151:
152: r1.setBaseShapesFilled(false);
153: assertFalse(r1.equals(r2));
154: r2.setBaseShapesFilled(false);
155: assertTrue(r1.equals(r2));
156:
157: r1.setDrawOutlines(!r1.getDrawOutlines());
158: assertFalse(r1.equals(r2));
159: r2.setDrawOutlines(r1.getDrawOutlines());
160: assertTrue(r1.equals(r2));
161:
162: r1.setUseOutlinePaint(true);
163: assertFalse(r1.equals(r2));
164: r2.setUseOutlinePaint(true);
165: assertTrue(r1.equals(r2));
166:
167: r1.setUseFillPaint(true);
168: assertFalse(r1.equals(r2));
169: r2.setUseFillPaint(true);
170: assertTrue(r1.equals(r2));
171:
172: r1.setDrawSeriesLineAsPath(true);
173: assertFalse(r1.equals(r2));
174: r2.setDrawSeriesLineAsPath(true);
175: assertTrue(r1.equals(r2));
176: }
177:
178: /**
179: * Test that the equals() method works for a TimeSeriesURLGenerator.
180: */
181: public void testEquals2() {
182: XYLineAndShapeRenderer r1 = new XYLineAndShapeRenderer();
183: XYLineAndShapeRenderer r2 = new XYLineAndShapeRenderer();
184: assertEquals(r1, r2);
185: assertEquals(r2, r1);
186:
187: r1.setURLGenerator(new TimeSeriesURLGenerator());
188: assertFalse(r1.equals(r2));
189: r2.setURLGenerator(new TimeSeriesURLGenerator());
190: assertTrue(r1.equals(r2));
191: }
192:
193: /**
194: * Two objects that are equal are required to return the same hashCode.
195: */
196: public void testHashcode() {
197: XYLineAndShapeRenderer r1 = new XYLineAndShapeRenderer();
198: XYLineAndShapeRenderer r2 = new XYLineAndShapeRenderer();
199: assertTrue(r1.equals(r2));
200: int h1 = r1.hashCode();
201: int h2 = r2.hashCode();
202: assertEquals(h1, h2);
203: }
204:
205: /**
206: * Confirm that cloning works.
207: */
208: public void testCloning() {
209: Rectangle2D legendShape = new Rectangle2D.Double(1.0, 2.0, 3.0,
210: 4.0);
211: XYLineAndShapeRenderer r1 = new XYLineAndShapeRenderer();
212: r1.setLegendLine(legendShape);
213: XYLineAndShapeRenderer r2 = null;
214: try {
215: r2 = (XYLineAndShapeRenderer) r1.clone();
216: } catch (CloneNotSupportedException e) {
217: e.printStackTrace();
218: }
219: assertTrue(r1 != r2);
220: assertTrue(r1.getClass() == r2.getClass());
221: assertTrue(r1.equals(r2));
222:
223: r1.setSeriesLinesVisible(0, false);
224: assertFalse(r1.equals(r2));
225: r2.setSeriesLinesVisible(0, false);
226: assertTrue(r1.equals(r2));
227:
228: legendShape.setRect(4.0, 3.0, 2.0, 1.0);
229: assertFalse(r1.equals(r2));
230: r2.setLegendLine(new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0));
231: assertTrue(r1.equals(r2));
232:
233: r1.setSeriesShapesVisible(1, true);
234: assertFalse(r1.equals(r2));
235: r2.setSeriesShapesVisible(1, true);
236: assertTrue(r1.equals(r2));
237:
238: r1.setSeriesShapesFilled(1, true);
239: assertFalse(r1.equals(r2));
240: r2.setSeriesShapesFilled(1, true);
241: assertTrue(r1.equals(r2));
242: }
243:
244: /**
245: * Serialize an instance, restore it, and check for equality.
246: */
247: public void testSerialization() {
248:
249: XYLineAndShapeRenderer r1 = new XYLineAndShapeRenderer();
250: XYLineAndShapeRenderer r2 = null;
251: try {
252: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
253: ObjectOutput out = new ObjectOutputStream(buffer);
254: out.writeObject(r1);
255: out.close();
256:
257: ObjectInput in = new ObjectInputStream(
258: new ByteArrayInputStream(buffer.toByteArray()));
259: r2 = (XYLineAndShapeRenderer) in.readObject();
260: in.close();
261: } catch (Exception e) {
262: e.printStackTrace();
263: }
264: assertEquals(r1, r2);
265:
266: }
267:
268: /**
269: * Check that the renderer is calculating the domain bounds correctly.
270: */
271: public void testFindDomainBounds() {
272: XYSeriesCollection dataset = RendererXYPackageTests
273: .createTestXYSeriesCollection();
274: JFreeChart chart = ChartFactory.createXYLineChart("Test Chart",
275: "X", "Y", dataset, PlotOrientation.VERTICAL, false,
276: false, false);
277: XYPlot plot = (XYPlot) chart.getPlot();
278: NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
279: domainAxis.setAutoRangeIncludesZero(false);
280: Range bounds = domainAxis.getRange();
281: assertFalse(bounds.contains(0.9));
282: assertTrue(bounds.contains(1.0));
283: assertTrue(bounds.contains(2.0));
284: assertFalse(bounds.contains(2.10));
285: }
286:
287: /**
288: * Check that the renderer is calculating the range bounds correctly.
289: */
290: public void testFindRangeBounds() {
291: TableXYDataset dataset = RendererXYPackageTests
292: .createTestTableXYDataset();
293: JFreeChart chart = ChartFactory.createXYLineChart("Test Chart",
294: "X", "Y", dataset, PlotOrientation.VERTICAL, false,
295: false, false);
296: XYPlot plot = (XYPlot) chart.getPlot();
297: NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
298: rangeAxis.setAutoRangeIncludesZero(false);
299: Range bounds = rangeAxis.getRange();
300: assertFalse(bounds.contains(1.0));
301: assertTrue(bounds.contains(2.0));
302: assertTrue(bounds.contains(5.0));
303: assertFalse(bounds.contains(6.0));
304: }
305:
306: /**
307: * A check for the datasetIndex and seriesIndex fields in the LegendItem
308: * returned by the getLegendItem() method.
309: */
310: public void testGetLegendItemSeriesIndex() {
311: XYSeriesCollection d1 = new XYSeriesCollection();
312: XYSeries s1 = new XYSeries("S1");
313: s1.add(1.0, 1.1);
314: XYSeries s2 = new XYSeries("S2");
315: s2.add(1.0, 1.1);
316: d1.addSeries(s1);
317: d1.addSeries(s2);
318:
319: XYSeriesCollection d2 = new XYSeriesCollection();
320: XYSeries s3 = new XYSeries("S3");
321: s3.add(1.0, 1.1);
322: XYSeries s4 = new XYSeries("S4");
323: s4.add(1.0, 1.1);
324: XYSeries s5 = new XYSeries("S5");
325: s5.add(1.0, 1.1);
326: d2.addSeries(s3);
327: d2.addSeries(s4);
328: d2.addSeries(s5);
329:
330: XYLineAndShapeRenderer r = new XYLineAndShapeRenderer();
331: XYPlot plot = new XYPlot(d1, new NumberAxis("x"),
332: new NumberAxis("y"), r);
333: plot.setDataset(1, d2);
334: /*JFreeChart chart =*/new JFreeChart(plot);
335: LegendItem li = r.getLegendItem(1, 2);
336: assertEquals("S5", li.getLabel());
337: assertEquals(1, li.getDatasetIndex());
338: assertEquals(2, li.getSeriesIndex());
339: }
340:
341: }
|