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: * XYBlockRendererTests.java
029: * -------------------------
030: * (C) Copyright 2006, 2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: XYBlockRendererTests.java,v 1.1.2.2 2007/03/09 15:59:21 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 05-Jul-2006 : Version 1 (DG);
040: * 09-Mar-2007 : Added independence check to testCloning (DG);
041: *
042: */
043:
044: package org.jfree.chart.renderer.xy.junit;
045:
046: import java.awt.Color;
047: import java.io.ByteArrayInputStream;
048: import java.io.ByteArrayOutputStream;
049: import java.io.ObjectInput;
050: import java.io.ObjectInputStream;
051: import java.io.ObjectOutput;
052: import java.io.ObjectOutputStream;
053:
054: import junit.framework.Test;
055: import junit.framework.TestCase;
056: import junit.framework.TestSuite;
057:
058: import org.jfree.chart.renderer.GrayPaintScale;
059: import org.jfree.chart.renderer.LookupPaintScale;
060: import org.jfree.chart.renderer.xy.XYBlockRenderer;
061:
062: /**
063: * Tests for the {@link XYBlockRenderer} class.
064: */
065: public class XYBlockRendererTests extends TestCase {
066:
067: /**
068: * Returns the tests as a test suite.
069: *
070: * @return The test suite.
071: */
072: public static Test suite() {
073: return new TestSuite(XYBlockRendererTests.class);
074: }
075:
076: /**
077: * Constructs a new set of tests.
078: *
079: * @param name the name of the tests.
080: */
081: public XYBlockRendererTests(String name) {
082: super (name);
083: }
084:
085: /**
086: * Test that the equals() method distinguishes all fields.
087: */
088: public void testEquals() {
089:
090: // default instances
091: XYBlockRenderer r1 = new XYBlockRenderer();
092: XYBlockRenderer r2 = new XYBlockRenderer();
093: assertTrue(r1.equals(r2));
094: assertTrue(r2.equals(r1));
095:
096: // blockHeight
097: r1.setBlockHeight(2.0);
098: assertFalse(r1.equals(r2));
099: r2.setBlockHeight(2.0);
100: assertTrue(r1.equals(r2));
101:
102: // blockWidth
103: r1.setBlockWidth(2.0);
104: assertFalse(r1.equals(r2));
105: r2.setBlockWidth(2.0);
106: assertTrue(r1.equals(r2));
107:
108: // paintScale
109: r1.setPaintScale(new GrayPaintScale(0.0, 1.0));
110: assertFalse(r1.equals(r2));
111: r2.setPaintScale(new GrayPaintScale(0.0, 1.0));
112: assertTrue(r1.equals(r2));
113:
114: }
115:
116: /**
117: * Two objects that are equal are required to return the same hashCode.
118: */
119: public void testHashcode() {
120: XYBlockRenderer r1 = new XYBlockRenderer();
121: XYBlockRenderer r2 = new XYBlockRenderer();
122: assertTrue(r1.equals(r2));
123: int h1 = r1.hashCode();
124: int h2 = r2.hashCode();
125: assertEquals(h1, h2);
126: }
127:
128: /**
129: * Confirm that cloning works.
130: */
131: public void testCloning() {
132: XYBlockRenderer r1 = new XYBlockRenderer();
133: LookupPaintScale scale1 = new LookupPaintScale();
134: r1.setPaintScale(scale1);
135: XYBlockRenderer r2 = null;
136: try {
137: r2 = (XYBlockRenderer) r1.clone();
138: } catch (CloneNotSupportedException e) {
139: e.printStackTrace();
140: }
141: assertTrue(r1 != r2);
142: assertTrue(r1.getClass() == r2.getClass());
143: assertTrue(r1.equals(r2));
144:
145: // check independence
146: scale1.add(new Double(0.5), Color.red);
147: assertFalse(r1.equals(r2));
148: LookupPaintScale scale2 = (LookupPaintScale) r2.getPaintScale();
149: scale2.add(new Double(0.5), Color.red);
150: assertTrue(r1.equals(r2));
151: }
152:
153: /**
154: * Serialize an instance, restore it, and check for equality.
155: */
156: public void testSerialization() {
157: XYBlockRenderer r1 = new XYBlockRenderer();
158: XYBlockRenderer r2 = null;
159: try {
160: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
161: ObjectOutput out = new ObjectOutputStream(buffer);
162: out.writeObject(r1);
163: out.close();
164:
165: ObjectInput in = new ObjectInputStream(
166: new ByteArrayInputStream(buffer.toByteArray()));
167: r2 = (XYBlockRenderer) in.readObject();
168: in.close();
169: } catch (Exception e) {
170: e.printStackTrace();
171: }
172: assertEquals(r1, r2);
173: }
174:
175: }
|