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: * XYDifferenceRendererTests.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: XYDifferenceRendererTests.java,v 1.1.2.3 2007/05/17 16:23:31 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 22-Oct-2003 : Version 1 (DG);
040: * 04-May-2005 : Improved equals() test (DG);
041: * 24-Jan-2007 : Added 'roundXCoordinates' to testEquals(), and improved
042: * testClone() (DG);
043: * 17-May-2007 : Added testGetLegendItemSeriesIndex() (DG);
044: *
045: */
046:
047: package org.jfree.chart.renderer.xy.junit;
048:
049: import java.awt.Color;
050: import java.awt.GradientPaint;
051: import java.awt.Shape;
052: import java.awt.geom.Line2D;
053: import java.io.ByteArrayInputStream;
054: import java.io.ByteArrayOutputStream;
055: import java.io.ObjectInput;
056: import java.io.ObjectInputStream;
057: import java.io.ObjectOutput;
058: import java.io.ObjectOutputStream;
059:
060: import junit.framework.Test;
061: import junit.framework.TestCase;
062: import junit.framework.TestSuite;
063:
064: import org.jfree.chart.JFreeChart;
065: import org.jfree.chart.LegendItem;
066: import org.jfree.chart.axis.NumberAxis;
067: import org.jfree.chart.plot.XYPlot;
068: import org.jfree.chart.renderer.xy.XYDifferenceRenderer;
069: import org.jfree.data.xy.XYSeries;
070: import org.jfree.data.xy.XYSeriesCollection;
071:
072: /**
073: * Tests for the {@link XYDifferenceRenderer} class.
074: */
075: public class XYDifferenceRendererTests extends TestCase {
076:
077: /**
078: * Returns the tests as a test suite.
079: *
080: * @return The test suite.
081: */
082: public static Test suite() {
083: return new TestSuite(XYDifferenceRendererTests.class);
084: }
085:
086: /**
087: * Constructs a new set of tests.
088: *
089: * @param name the name of the tests.
090: */
091: public XYDifferenceRendererTests(String name) {
092: super (name);
093: }
094:
095: /**
096: * Check that the equals() method distinguishes all fields.
097: */
098: public void testEquals() {
099: XYDifferenceRenderer r1 = new XYDifferenceRenderer(Color.red,
100: Color.blue, false);
101: XYDifferenceRenderer r2 = new XYDifferenceRenderer(Color.red,
102: Color.blue, false);
103: assertEquals(r1, r2);
104:
105: // positive paint
106: r1.setPositivePaint(new GradientPaint(1.0f, 2.0f, Color.red,
107: 3.0f, 4.0f, Color.blue));
108: assertFalse(r1.equals(r2));
109: r2.setPositivePaint(new GradientPaint(1.0f, 2.0f, Color.red,
110: 3.0f, 4.0f, Color.blue));
111: assertTrue(r1.equals(r2));
112:
113: // negative paint
114: r1.setNegativePaint(new GradientPaint(1.0f, 2.0f, Color.yellow,
115: 3.0f, 4.0f, Color.blue));
116: assertFalse(r1.equals(r2));
117: r2.setNegativePaint(new GradientPaint(1.0f, 2.0f, Color.yellow,
118: 3.0f, 4.0f, Color.blue));
119: assertTrue(r1.equals(r2));
120:
121: // shapesVisible
122: r1 = new XYDifferenceRenderer(Color.green, Color.yellow, true);
123: assertFalse(r1.equals(r2));
124: r2 = new XYDifferenceRenderer(Color.green, Color.yellow, true);
125: assertTrue(r1.equals(r2));
126:
127: // legendLine
128: r1.setLegendLine(new Line2D.Double(1.0, 2.0, 3.0, 4.0));
129: assertFalse(r1.equals(r2));
130: r2.setLegendLine(new Line2D.Double(1.0, 2.0, 3.0, 4.0));
131: assertTrue(r1.equals(r2));
132:
133: // roundXCoordinates
134: r1.setRoundXCoordinates(true);
135: assertFalse(r1.equals(r2));
136: r2.setRoundXCoordinates(true);
137: assertTrue(r1.equals(r2));
138:
139: assertFalse(r1.equals(null));
140: }
141:
142: /**
143: * Two objects that are equal are required to return the same hashCode.
144: */
145: public void testHashcode() {
146: XYDifferenceRenderer r1 = new XYDifferenceRenderer(Color.red,
147: Color.blue, false);
148: XYDifferenceRenderer r2 = new XYDifferenceRenderer(Color.red,
149: Color.blue, false);
150: assertTrue(r1.equals(r2));
151: int h1 = r1.hashCode();
152: int h2 = r2.hashCode();
153: assertEquals(h1, h2);
154: }
155:
156: /**
157: * Confirm that cloning works.
158: */
159: public void testCloning() {
160: XYDifferenceRenderer r1 = new XYDifferenceRenderer(Color.red,
161: Color.blue, false);
162: XYDifferenceRenderer r2 = null;
163: try {
164: r2 = (XYDifferenceRenderer) r1.clone();
165: } catch (CloneNotSupportedException e) {
166: e.printStackTrace();
167: }
168: assertTrue(r1 != r2);
169: assertTrue(r1.getClass() == r2.getClass());
170: assertTrue(r1.equals(r2));
171:
172: // check independence
173: Shape s = r1.getLegendLine();
174: if (s instanceof Line2D) {
175: Line2D l = (Line2D) s;
176: l.setLine(1.0, 2.0, 3.0, 4.0);
177: assertFalse(r1.equals(r2));
178: }
179: }
180:
181: /**
182: * Serialize an instance, restore it, and check for equality.
183: */
184: public void testSerialization() {
185:
186: XYDifferenceRenderer r1 = new XYDifferenceRenderer(Color.red,
187: Color.blue, false);
188: XYDifferenceRenderer r2 = null;
189:
190: try {
191: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
192: ObjectOutput out = new ObjectOutputStream(buffer);
193: out.writeObject(r1);
194: out.close();
195:
196: ObjectInput in = new ObjectInputStream(
197: new ByteArrayInputStream(buffer.toByteArray()));
198: r2 = (XYDifferenceRenderer) in.readObject();
199: in.close();
200: } catch (Exception e) {
201: e.printStackTrace();
202: }
203: assertEquals(r1, r2);
204:
205: }
206:
207: /**
208: * A check for the datasetIndex and seriesIndex fields in the LegendItem
209: * returned by the getLegendItem() method.
210: */
211: public void testGetLegendItemSeriesIndex() {
212: XYSeriesCollection d1 = new XYSeriesCollection();
213: XYSeries s1 = new XYSeries("S1");
214: s1.add(1.0, 1.1);
215: XYSeries s2 = new XYSeries("S2");
216: s2.add(1.0, 1.1);
217: d1.addSeries(s1);
218: d1.addSeries(s2);
219:
220: XYSeriesCollection d2 = new XYSeriesCollection();
221: XYSeries s3 = new XYSeries("S3");
222: s3.add(1.0, 1.1);
223: XYSeries s4 = new XYSeries("S4");
224: s4.add(1.0, 1.1);
225: XYSeries s5 = new XYSeries("S5");
226: s5.add(1.0, 1.1);
227: d2.addSeries(s3);
228: d2.addSeries(s4);
229: d2.addSeries(s5);
230:
231: XYDifferenceRenderer r = new XYDifferenceRenderer();
232: XYPlot plot = new XYPlot(d1, new NumberAxis("x"),
233: new NumberAxis("y"), r);
234: plot.setDataset(1, d2);
235: /*JFreeChart chart =*/new JFreeChart(plot);
236: LegendItem li = r.getLegendItem(1, 2);
237: assertEquals("S5", li.getLabel());
238: assertEquals(1, li.getDatasetIndex());
239: assertEquals(2, li.getSeriesIndex());
240: }
241:
242: }
|