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: * XYBarRendererTests.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: XYBarRendererTests.java,v 1.1.2.3 2007/05/17 16:23:31 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 25-Mar-2003 : Version 1 (DG);
040: * 22-Oct-2003 : Added hashCode test (DG);
041: * 09-Feb-2007 : Added to 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.Rectangle2D;
049: import java.io.ByteArrayInputStream;
050: import java.io.ByteArrayOutputStream;
051: import java.io.ObjectInput;
052: import java.io.ObjectInputStream;
053: import java.io.ObjectOutput;
054: import java.io.ObjectOutputStream;
055:
056: import junit.framework.Test;
057: import junit.framework.TestCase;
058: import junit.framework.TestSuite;
059:
060: import org.jfree.chart.ChartFactory;
061: import org.jfree.chart.JFreeChart;
062: import org.jfree.chart.LegendItem;
063: import org.jfree.chart.axis.NumberAxis;
064: import org.jfree.chart.labels.ItemLabelPosition;
065: import org.jfree.chart.plot.PlotOrientation;
066: import org.jfree.chart.plot.XYPlot;
067: import org.jfree.chart.renderer.xy.XYBarRenderer;
068: import org.jfree.data.Range;
069: import org.jfree.data.xy.XYBarDataset;
070: import org.jfree.data.xy.XYSeries;
071: import org.jfree.data.xy.XYSeriesCollection;
072: import org.jfree.ui.GradientPaintTransformType;
073: import org.jfree.ui.StandardGradientPaintTransformer;
074:
075: /**
076: * Tests for the {@link XYBarRenderer} class.
077: */
078: public class XYBarRendererTests extends TestCase {
079:
080: /**
081: * Returns the tests as a test suite.
082: *
083: * @return The test suite.
084: */
085: public static Test suite() {
086: return new TestSuite(XYBarRendererTests.class);
087: }
088:
089: /**
090: * Constructs a new set of tests.
091: *
092: * @param name the name of the tests.
093: */
094: public XYBarRendererTests(String name) {
095: super (name);
096: }
097:
098: /**
099: * Test that the equals() method distinguishes all fields.
100: */
101: public void testEquals() {
102:
103: // default instances
104: XYBarRenderer r1 = new XYBarRenderer();
105: XYBarRenderer r2 = new XYBarRenderer();
106: assertTrue(r1.equals(r2));
107: assertTrue(r2.equals(r1));
108:
109: // setBase()
110: r1.setBase(1.0);
111: assertFalse(r1.equals(r2));
112: r2.setBase(1.0);
113: assertTrue(r1.equals(r2));
114:
115: // setUseYInterval
116: r1.setUseYInterval(!r1.getUseYInterval());
117: assertFalse(r1.equals(r2));
118: r2.setUseYInterval(!r2.getUseYInterval());
119: assertTrue(r1.equals(r2));
120:
121: // setMargin()
122: r1.setMargin(0.10);
123: assertFalse(r1.equals(r2));
124: r2.setMargin(0.10);
125: assertTrue(r1.equals(r2));
126:
127: // setDrawBarOutline()
128: r1.setDrawBarOutline(!r1.isDrawBarOutline());
129: assertFalse(r1.equals(r2));
130: r2.setDrawBarOutline(!r2.isDrawBarOutline());
131: assertTrue(r1.equals(r2));
132:
133: // setGradientPaintTransformer()
134: r1
135: .setGradientPaintTransformer(new StandardGradientPaintTransformer(
136: GradientPaintTransformType.CENTER_HORIZONTAL));
137: assertFalse(r1.equals(r2));
138: r2
139: .setGradientPaintTransformer(new StandardGradientPaintTransformer(
140: GradientPaintTransformType.CENTER_HORIZONTAL));
141: assertTrue(r1.equals(r2));
142:
143: // legendBar
144: r1.setLegendBar(new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0));
145: assertFalse(r1.equals(r2));
146: r2.setLegendBar(new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0));
147: assertTrue(r1.equals(r2));
148:
149: // positiveItemLabelFallbackPosition
150: r1
151: .setPositiveItemLabelPositionFallback(new ItemLabelPosition());
152: assertFalse(r1.equals(r2));
153: r2
154: .setPositiveItemLabelPositionFallback(new ItemLabelPosition());
155: assertTrue(r1.equals(r2));
156:
157: // negativeItemLabelFallbackPosition
158: r1
159: .setNegativeItemLabelPositionFallback(new ItemLabelPosition());
160: assertFalse(r1.equals(r2));
161: r2
162: .setNegativeItemLabelPositionFallback(new ItemLabelPosition());
163: assertTrue(r1.equals(r2));
164: }
165:
166: /**
167: * Two objects that are equal are required to return the same hashCode.
168: */
169: public void testHashcode() {
170: XYBarRenderer r1 = new XYBarRenderer();
171: XYBarRenderer r2 = new XYBarRenderer();
172: assertTrue(r1.equals(r2));
173: int h1 = r1.hashCode();
174: int h2 = r2.hashCode();
175: assertEquals(h1, h2);
176: }
177:
178: /**
179: * Confirm that cloning works.
180: */
181: public void testCloning() {
182: XYBarRenderer r1 = new XYBarRenderer();
183: Rectangle2D rect = new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0);
184: r1.setLegendBar(rect);
185: XYBarRenderer r2 = null;
186: try {
187: r2 = (XYBarRenderer) r1.clone();
188: } catch (CloneNotSupportedException e) {
189: e.printStackTrace();
190: }
191: assertTrue(r1 != r2);
192: assertTrue(r1.getClass() == r2.getClass());
193: assertTrue(r1.equals(r2));
194:
195: // check independence
196: rect.setRect(4.0, 3.0, 2.0, 1.0);
197: assertFalse(r1.equals(r2));
198: r2.setLegendBar(new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0));
199: assertTrue(r1.equals(r2));
200: }
201:
202: /**
203: * Serialize an instance, restore it, and check for equality.
204: */
205: public void testSerialization() {
206:
207: XYBarRenderer r1 = new XYBarRenderer();
208: XYBarRenderer r2 = null;
209:
210: try {
211: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
212: ObjectOutput out = new ObjectOutputStream(buffer);
213: out.writeObject(r1);
214: out.close();
215:
216: ObjectInput in = new ObjectInputStream(
217: new ByteArrayInputStream(buffer.toByteArray()));
218: r2 = (XYBarRenderer) in.readObject();
219: in.close();
220: } catch (Exception e) {
221: System.out.println(e.toString());
222: }
223: assertEquals(r1, r2);
224:
225: }
226:
227: /**
228: * Serialize an instance, restore it, and check for equality.
229: */
230: public void testSerialization2() {
231:
232: XYBarRenderer r1 = new XYBarRenderer();
233: r1
234: .setPositiveItemLabelPositionFallback(new ItemLabelPosition());
235: XYBarRenderer r2 = null;
236:
237: try {
238: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
239: ObjectOutput out = new ObjectOutputStream(buffer);
240: out.writeObject(r1);
241: out.close();
242:
243: ObjectInput in = new ObjectInputStream(
244: new ByteArrayInputStream(buffer.toByteArray()));
245: r2 = (XYBarRenderer) in.readObject();
246: in.close();
247: } catch (Exception e) {
248: e.printStackTrace();
249: }
250: assertEquals(r1, r2);
251:
252: }
253:
254: /**
255: * Check that the renderer is calculating the domain bounds correctly.
256: */
257: public void testFindDomainBounds() {
258: XYSeriesCollection dataset = RendererXYPackageTests
259: .createTestXYSeriesCollection();
260: JFreeChart chart = ChartFactory.createXYBarChart("Test Chart",
261: "X", false, "Y", dataset, PlotOrientation.VERTICAL,
262: false, false, false);
263: XYPlot plot = (XYPlot) chart.getPlot();
264: NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
265: domainAxis.setAutoRangeIncludesZero(false);
266: Range bounds = domainAxis.getRange();
267: assertFalse(bounds.contains(0.3));
268: assertTrue(bounds.contains(0.5));
269: assertTrue(bounds.contains(2.5));
270: assertFalse(bounds.contains(2.8));
271: }
272:
273: /**
274: * A check for the datasetIndex and seriesIndex fields in the LegendItem
275: * returned by the getLegendItem() method.
276: */
277: public void testGetLegendItemSeriesIndex() {
278: XYSeriesCollection d1 = new XYSeriesCollection();
279: XYSeries s1 = new XYSeries("S1");
280: s1.add(1.0, 1.1);
281: XYSeries s2 = new XYSeries("S2");
282: s2.add(1.0, 1.1);
283: d1.addSeries(s1);
284: d1.addSeries(s2);
285:
286: XYSeriesCollection d2 = new XYSeriesCollection();
287: XYSeries s3 = new XYSeries("S3");
288: s3.add(1.0, 1.1);
289: XYSeries s4 = new XYSeries("S4");
290: s4.add(1.0, 1.1);
291: XYSeries s5 = new XYSeries("S5");
292: s5.add(1.0, 1.1);
293: d2.addSeries(s3);
294: d2.addSeries(s4);
295: d2.addSeries(s5);
296:
297: XYBarRenderer r = new XYBarRenderer();
298: XYPlot plot = new XYPlot(new XYBarDataset(d1, 1.0),
299: new NumberAxis("x"), new NumberAxis("y"), r);
300: plot.setDataset(1, new XYBarDataset(d2, 2.0));
301: /*JFreeChart chart =*/new JFreeChart(plot);
302: LegendItem li = r.getLegendItem(1, 2);
303: assertEquals("S5", li.getLabel());
304: assertEquals(1, li.getDatasetIndex());
305: assertEquals(2, li.getSeriesIndex());
306: }
307:
308: }
|