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: * StandardXYItemRendererTests.java
029: * --------------------------------
030: * (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: StandardXYItemRendererTests.java,v 1.1.2.4 2007/06/08 13:29:38 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 25-Mar-2003 : Version 1 (DG);
040: * 22-Oct-2003 : Added hashCode test (DG);
041: * 08-Oct-2004 : Strengthened test for equals() method (DG);
042: * 14-Mar-2007 : Added new checks in testEquals() and testCloning() (DG);
043: * 17-May-2007 : Added testGetLegendItemSeriesIndex() (DG);
044: * 08-Jun-2007 : Added testNoDisplayedItem() (DG);
045: *
046: */
047:
048: package org.jfree.chart.renderer.xy.junit;
049:
050: import java.awt.Graphics2D;
051: import java.awt.geom.Line2D;
052: import java.awt.geom.Rectangle2D;
053: import java.awt.image.BufferedImage;
054: import java.io.ByteArrayInputStream;
055: import java.io.ByteArrayOutputStream;
056: import java.io.ObjectInput;
057: import java.io.ObjectInputStream;
058: import java.io.ObjectOutput;
059: import java.io.ObjectOutputStream;
060:
061: import junit.framework.Test;
062: import junit.framework.TestCase;
063: import junit.framework.TestSuite;
064:
065: import org.jfree.chart.ChartFactory;
066: import org.jfree.chart.ChartRenderingInfo;
067: import org.jfree.chart.JFreeChart;
068: import org.jfree.chart.LegendItem;
069: import org.jfree.chart.axis.NumberAxis;
070: import org.jfree.chart.entity.EntityCollection;
071: import org.jfree.chart.entity.XYItemEntity;
072: import org.jfree.chart.junit.TestUtilities;
073: import org.jfree.chart.plot.PlotOrientation;
074: import org.jfree.chart.plot.XYPlot;
075: import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
076: import org.jfree.data.xy.XYSeries;
077: import org.jfree.data.xy.XYSeriesCollection;
078: import org.jfree.util.UnitType;
079:
080: /**
081: * Tests for the {@link StandardXYItemRenderer} class.
082: */
083: public class StandardXYItemRendererTests extends TestCase {
084:
085: /**
086: * Returns the tests as a test suite.
087: *
088: * @return The test suite.
089: */
090: public static Test suite() {
091: return new TestSuite(StandardXYItemRendererTests.class);
092: }
093:
094: /**
095: * Constructs a new set of tests.
096: *
097: * @param name the name of the tests.
098: */
099: public StandardXYItemRendererTests(String name) {
100: super (name);
101: }
102:
103: /**
104: * Test that the equals() method distinguishes all fields.
105: */
106: public void testEquals() {
107: StandardXYItemRenderer r1 = new StandardXYItemRenderer();
108: StandardXYItemRenderer r2 = new StandardXYItemRenderer();
109: assertEquals(r1, r2);
110:
111: r1.setBaseShapesVisible(true);
112: assertFalse(r1.equals(r2));
113: r2.setBaseShapesVisible(true);
114: assertTrue(r1.equals(r2));
115:
116: r1.setPlotLines(false);
117: assertFalse(r1.equals(r2));
118: r2.setPlotLines(false);
119: assertTrue(r1.equals(r2));
120:
121: r1.setPlotImages(true);
122: assertFalse(r1.equals(r2));
123: r2.setPlotImages(true);
124: assertTrue(r1.equals(r2));
125:
126: r1.setPlotDiscontinuous(true);
127: assertFalse(r1.equals(r2));
128: r2.setPlotDiscontinuous(true);
129: assertTrue(r1.equals(r2));
130:
131: r1.setGapThresholdType(UnitType.ABSOLUTE);
132: assertFalse(r1.equals(r2));
133: r2.setGapThresholdType(UnitType.ABSOLUTE);
134: assertTrue(r1.equals(r2));
135:
136: r1.setGapThreshold(1.23);
137: assertFalse(r1.equals(r2));
138: r2.setGapThreshold(1.23);
139: assertTrue(r1.equals(r2));
140:
141: r1.setLegendLine(new Line2D.Double(1.0, 2.0, 3.0, 4.0));
142: assertFalse(r1.equals(r2));
143: r2.setLegendLine(new Line2D.Double(1.0, 2.0, 3.0, 4.0));
144: assertTrue(r1.equals(r2));
145:
146: r1.setShapesFilled(false);
147: assertFalse(r1.equals(r2));
148: r2.setShapesFilled(false);
149: assertTrue(r1.equals(r2));
150:
151: r1.setSeriesShapesFilled(1, Boolean.TRUE);
152: assertFalse(r1.equals(r2));
153: r2.setSeriesShapesFilled(1, Boolean.TRUE);
154: assertTrue(r1.equals(r2));
155:
156: r1.setBaseShapesFilled(false);
157: assertFalse(r1.equals(r2));
158: r2.setBaseShapesFilled(false);
159: assertTrue(r1.equals(r2));
160:
161: r1.setDrawSeriesLineAsPath(true);
162: assertFalse(r1.equals(r2));
163: r2.setDrawSeriesLineAsPath(true);
164: assertTrue(r1.equals(r2));
165: }
166:
167: /**
168: * Two objects that are equal are required to return the same hashCode.
169: */
170: public void testHashcode() {
171: StandardXYItemRenderer r1 = new StandardXYItemRenderer();
172: StandardXYItemRenderer r2 = new StandardXYItemRenderer();
173: assertTrue(r1.equals(r2));
174: int h1 = r1.hashCode();
175: int h2 = r2.hashCode();
176: assertEquals(h1, h2);
177: }
178:
179: /**
180: * Confirm that cloning works.
181: */
182: public void testCloning() {
183: StandardXYItemRenderer r1 = new StandardXYItemRenderer();
184: Rectangle2D rect1 = new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0);
185: r1.setLegendLine(rect1);
186: StandardXYItemRenderer r2 = null;
187: try {
188: r2 = (StandardXYItemRenderer) r1.clone();
189: } catch (CloneNotSupportedException e) {
190: e.printStackTrace();
191: }
192: assertTrue(r1 != r2);
193: assertTrue(r1.getClass() == r2.getClass());
194: assertTrue(r1.equals(r2));
195:
196: // check independence
197: rect1.setRect(4.0, 3.0, 2.0, 1.0);
198: assertFalse(r1.equals(r2));
199: r2.setLegendLine(new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0));
200: assertTrue(r1.equals(r2));
201:
202: r1.setSeriesShapesFilled(1, Boolean.TRUE);
203: assertFalse(r1.equals(r2));
204: r2.setSeriesShapesFilled(1, Boolean.TRUE);
205: assertTrue(r1.equals(r2));
206:
207: }
208:
209: /**
210: * Serialize an instance, restore it, and check for equality.
211: */
212: public void testSerialization() {
213: StandardXYItemRenderer r1 = new StandardXYItemRenderer();
214: StandardXYItemRenderer r2 = null;
215: try {
216: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
217: ObjectOutput out = new ObjectOutputStream(buffer);
218: out.writeObject(r1);
219: out.close();
220:
221: ObjectInput in = new ObjectInputStream(
222: new ByteArrayInputStream(buffer.toByteArray()));
223: r2 = (StandardXYItemRenderer) in.readObject();
224: in.close();
225: } catch (Exception e) {
226: e.printStackTrace();
227: }
228: assertEquals(r1, r2);
229: }
230:
231: /**
232: * A check for the datasetIndex and seriesIndex fields in the LegendItem
233: * returned by the getLegendItem() method.
234: */
235: public void testGetLegendItemSeriesIndex() {
236: XYSeriesCollection d1 = new XYSeriesCollection();
237: XYSeries s1 = new XYSeries("S1");
238: s1.add(1.0, 1.1);
239: XYSeries s2 = new XYSeries("S2");
240: s2.add(1.0, 1.1);
241: d1.addSeries(s1);
242: d1.addSeries(s2);
243:
244: XYSeriesCollection d2 = new XYSeriesCollection();
245: XYSeries s3 = new XYSeries("S3");
246: s3.add(1.0, 1.1);
247: XYSeries s4 = new XYSeries("S4");
248: s4.add(1.0, 1.1);
249: XYSeries s5 = new XYSeries("S5");
250: s5.add(1.0, 1.1);
251: d2.addSeries(s3);
252: d2.addSeries(s4);
253: d2.addSeries(s5);
254:
255: StandardXYItemRenderer r = new StandardXYItemRenderer();
256: XYPlot plot = new XYPlot(d1, new NumberAxis("x"),
257: new NumberAxis("y"), r);
258: plot.setDataset(1, d2);
259: /*JFreeChart chart =*/new JFreeChart(plot);
260: LegendItem li = r.getLegendItem(1, 2);
261: assertEquals("S5", li.getLabel());
262: assertEquals(1, li.getDatasetIndex());
263: assertEquals(2, li.getSeriesIndex());
264: }
265:
266: /**
267: * A check to ensure that an item that falls outside the plot's data area
268: * does NOT generate an item entity.
269: */
270: public void testNoDisplayedItem() {
271: XYSeriesCollection dataset = new XYSeriesCollection();
272: XYSeries s1 = new XYSeries("S1");
273: s1.add(10.0, 10.0);
274: dataset.addSeries(s1);
275: JFreeChart chart = ChartFactory.createXYLineChart("Title", "X",
276: "Y", dataset, PlotOrientation.VERTICAL, false, true,
277: false);
278: XYPlot plot = (XYPlot) chart.getPlot();
279: plot.setRenderer(new StandardXYItemRenderer());
280: NumberAxis xAxis = (NumberAxis) plot.getDomainAxis();
281: xAxis.setRange(0.0, 5.0);
282: NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
283: yAxis.setRange(0.0, 5.0);
284: BufferedImage image = new BufferedImage(200, 100,
285: BufferedImage.TYPE_INT_RGB);
286: Graphics2D g2 = image.createGraphics();
287: ChartRenderingInfo info = new ChartRenderingInfo();
288: chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null,
289: info);
290: g2.dispose();
291: EntityCollection ec = info.getEntityCollection();
292: assertFalse(TestUtilities.containsInstanceOf(ec.getEntities(),
293: XYItemEntity.class));
294: }
295:
296: }
|