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: * ClusteredXYBarRendererTests.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: ClusteredXYBarRendererTests.java,v 1.1.2.2 2007/06/12 11:36:24 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 25-Mar-2003 : Version 1 (DG);
040: * 22-Oct-2003 : Added hashCode test (DG);
041: *
042: */
043:
044: package org.jfree.chart.renderer.xy.junit;
045:
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:
053: import junit.framework.Test;
054: import junit.framework.TestCase;
055: import junit.framework.TestSuite;
056:
057: import org.jfree.chart.renderer.xy.AbstractXYItemRenderer;
058: import org.jfree.chart.renderer.xy.ClusteredXYBarRenderer;
059: import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
060: import org.jfree.data.Range;
061: import org.jfree.data.xy.DefaultIntervalXYDataset;
062: import org.jfree.data.xy.XYDataset;
063:
064: /**
065: * Tests for the {@link ClusteredXYBarRenderer} class.
066: */
067: public class ClusteredXYBarRendererTests extends TestCase {
068:
069: /**
070: * Returns the tests as a test suite.
071: *
072: * @return The test suite.
073: */
074: public static Test suite() {
075: return new TestSuite(ClusteredXYBarRendererTests.class);
076: }
077:
078: /**
079: * Constructs a new set of tests.
080: *
081: * @param name the name of the tests.
082: */
083: public ClusteredXYBarRendererTests(String name) {
084: super (name);
085: }
086:
087: /**
088: * Check that the equals() method distinguishes all fields.
089: */
090: public void testEquals() {
091: ClusteredXYBarRenderer r1 = new ClusteredXYBarRenderer();
092: ClusteredXYBarRenderer r2 = new ClusteredXYBarRenderer();
093: assertEquals(r1, r2);
094: assertEquals(r2, r1);
095:
096: r1 = new ClusteredXYBarRenderer(1.2, false);
097: assertFalse(r1.equals(r2));
098: r2 = new ClusteredXYBarRenderer(1.2, false);
099: assertTrue(r1.equals(r2));
100:
101: r1 = new ClusteredXYBarRenderer(1.2, true);
102: assertFalse(r1.equals(r2));
103: r2 = new ClusteredXYBarRenderer(1.2, true);
104: assertTrue(r1.equals(r2));
105: }
106:
107: /**
108: * Two objects that are equal are required to return the same hashCode.
109: */
110: public void testHashcode() {
111: ClusteredXYBarRenderer r1 = new ClusteredXYBarRenderer();
112: ClusteredXYBarRenderer r2 = new ClusteredXYBarRenderer();
113: assertTrue(r1.equals(r2));
114: int h1 = r1.hashCode();
115: int h2 = r2.hashCode();
116: assertEquals(h1, h2);
117: }
118:
119: /**
120: * Confirm that cloning works.
121: */
122: public void testCloning() {
123: ClusteredXYBarRenderer r1 = new ClusteredXYBarRenderer();
124: ClusteredXYBarRenderer r2 = null;
125: try {
126: r2 = (ClusteredXYBarRenderer) r1.clone();
127: } catch (CloneNotSupportedException e) {
128: e.printStackTrace();
129: }
130: assertTrue(r1 != r2);
131: assertTrue(r1.getClass() == r2.getClass());
132: assertTrue(r1.equals(r2));
133: }
134:
135: /**
136: * Serialize an instance, restore it, and check for equality.
137: */
138: public void testSerialization() {
139:
140: ClusteredXYBarRenderer r1 = new ClusteredXYBarRenderer();
141: ClusteredXYBarRenderer r2 = null;
142:
143: try {
144: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
145: ObjectOutput out = new ObjectOutputStream(buffer);
146: out.writeObject(r1);
147: out.close();
148:
149: ObjectInput in = new ObjectInputStream(
150: new ByteArrayInputStream(buffer.toByteArray()));
151: r2 = (ClusteredXYBarRenderer) in.readObject();
152: in.close();
153: } catch (Exception e) {
154: e.printStackTrace();
155: }
156: assertEquals(r1, r2);
157:
158: }
159:
160: /**
161: * Some checks for the findDomainBounds() method (which requires special
162: * handling when the centerBarAtStartValue flag is set to true).
163: */
164: public void testFindDomainBounds() {
165: AbstractXYItemRenderer renderer = new ClusteredXYBarRenderer();
166: XYDataset dataset = createSampleDataset1();
167: Range r = renderer.findDomainBounds(dataset);
168: assertEquals(0.9, r.getLowerBound());
169: assertEquals(13.1, r.getUpperBound());
170:
171: renderer = new ClusteredXYBarRenderer(0.0, true);
172: r = renderer.findDomainBounds(dataset);
173: assertEquals(0.8, r.getLowerBound());
174: assertEquals(13.0, r.getUpperBound());
175:
176: // check that a null dataset returns null bounds
177: assertTrue(renderer.findDomainBounds(null) == null);
178: }
179:
180: /**
181: * Creates a sample dataset for testing.
182: *
183: * @return A sample dataset.
184: */
185: public DefaultIntervalXYDataset createSampleDataset1() {
186: DefaultIntervalXYDataset d = new DefaultIntervalXYDataset();
187: double[] x1 = new double[] { 1.0, 2.0, 3.0 };
188: double[] x1Start = new double[] { 0.9, 1.9, 2.9 };
189: double[] x1End = new double[] { 1.1, 2.1, 3.1 };
190: double[] y1 = new double[] { 4.0, 5.0, 6.0 };
191: double[] y1Start = new double[] { 1.09, 2.09, 3.09 };
192: double[] y1End = new double[] { 1.11, 2.11, 3.11 };
193: double[][] data1 = new double[][] { x1, x1Start, x1End, y1,
194: y1Start, y1End };
195: d.addSeries("S1", data1);
196:
197: double[] x2 = new double[] { 11.0, 12.0, 13.0 };
198: double[] x2Start = new double[] { 10.9, 11.9, 12.9 };
199: double[] x2End = new double[] { 11.1, 12.1, 13.1 };
200: double[] y2 = new double[] { 14.0, 15.0, 16.0 };
201: double[] y2Start = new double[] { 11.09, 12.09, 13.09 };
202: double[] y2End = new double[] { 11.11, 12.11, 13.11 };
203: double[][] data2 = new double[][] { x2, x2Start, x2End, y2,
204: y2Start, y2End };
205: d.addSeries("S2", data2);
206: return d;
207: }
208:
209: }
|