001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2006, 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: * HighLowRendererTests.java
029: * -------------------------
030: * (C) Copyright 2003-2006, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: HighLowRendererTests.java,v 1.1.2.1 2006/10/03 15:41:45 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 25-Mar-2003 : Version 1 (DG);
040: * 22-Oct-2003 : Added hashCode test (DG);
041: * 01-Nov-2005 : Added tests for new fields (DG);
042: * 17-Aug-2006 : Added testFindRangeBounds() method (DG);
043: *
044: */
045:
046: package org.jfree.chart.renderer.xy.junit;
047:
048: import java.awt.Color;
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: import java.util.Date;
056:
057: import junit.framework.Test;
058: import junit.framework.TestCase;
059: import junit.framework.TestSuite;
060:
061: import org.jfree.chart.renderer.xy.HighLowRenderer;
062: import org.jfree.data.Range;
063: import org.jfree.data.xy.DefaultOHLCDataset;
064: import org.jfree.data.xy.OHLCDataItem;
065: import org.jfree.data.xy.OHLCDataset;
066:
067: /**
068: * Tests for the {@link HighLowRenderer} class.
069: */
070: public class HighLowRendererTests extends TestCase {
071:
072: /**
073: * Returns the tests as a test suite.
074: *
075: * @return The test suite.
076: */
077: public static Test suite() {
078: return new TestSuite(HighLowRendererTests.class);
079: }
080:
081: /**
082: * Constructs a new set of tests.
083: *
084: * @param name the name of the tests.
085: */
086: public HighLowRendererTests(String name) {
087: super (name);
088: }
089:
090: /**
091: * Check that the equals() method distinguishes all fields.
092: */
093: public void testEquals() {
094: HighLowRenderer r1 = new HighLowRenderer();
095: HighLowRenderer r2 = new HighLowRenderer();
096: assertEquals(r1, r2);
097:
098: // drawOpenTicks
099: r1.setDrawOpenTicks(false);
100: assertFalse(r1.equals(r2));
101: r2.setDrawOpenTicks(false);
102: assertTrue(r1.equals(r2));
103:
104: // drawCloseTicks
105: r1.setDrawCloseTicks(false);
106: assertFalse(r1.equals(r2));
107: r2.setDrawCloseTicks(false);
108: assertTrue(r1.equals(r2));
109:
110: // openTickPaint
111: r1.setOpenTickPaint(Color.red);
112: assertFalse(r1.equals(r2));
113: r2.setOpenTickPaint(Color.red);
114: assertTrue(r1.equals(r2));
115:
116: // closeTickPaint
117: r1.setCloseTickPaint(Color.blue);
118: assertFalse(r1.equals(r2));
119: r2.setCloseTickPaint(Color.blue);
120: assertTrue(r1.equals(r2));
121: }
122:
123: /**
124: * Two objects that are equal are required to return the same hashCode.
125: */
126: public void testHashcode() {
127: HighLowRenderer r1 = new HighLowRenderer();
128: HighLowRenderer r2 = new HighLowRenderer();
129: assertTrue(r1.equals(r2));
130: int h1 = r1.hashCode();
131: int h2 = r2.hashCode();
132: assertEquals(h1, h2);
133: }
134:
135: /**
136: * Confirm that cloning works.
137: */
138: public void testCloning() {
139: HighLowRenderer r1 = new HighLowRenderer();
140: r1.setCloseTickPaint(Color.green);
141: HighLowRenderer r2 = null;
142: try {
143: r2 = (HighLowRenderer) r1.clone();
144: } catch (CloneNotSupportedException e) {
145: e.printStackTrace();
146: }
147: assertTrue(r1 != r2);
148: assertTrue(r1.getClass() == r2.getClass());
149: assertTrue(r1.equals(r2));
150: }
151:
152: /**
153: * Serialize an instance, restore it, and check for equality.
154: */
155: public void testSerialization() {
156:
157: HighLowRenderer r1 = new HighLowRenderer();
158: r1.setCloseTickPaint(Color.green);
159: HighLowRenderer r2 = null;
160:
161: try {
162: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
163: ObjectOutput out = new ObjectOutputStream(buffer);
164: out.writeObject(r1);
165: out.close();
166:
167: ObjectInput in = new ObjectInputStream(
168: new ByteArrayInputStream(buffer.toByteArray()));
169: r2 = (HighLowRenderer) in.readObject();
170: in.close();
171: } catch (Exception e) {
172: e.printStackTrace();
173: }
174: assertEquals(r1, r2);
175:
176: }
177:
178: /**
179: * Some checks for the findRangeBounds() method.
180: */
181: public void testFindRangeBounds() {
182: HighLowRenderer renderer = new HighLowRenderer();
183:
184: OHLCDataItem item1 = new OHLCDataItem(new Date(1L), 2.0, 4.0,
185: 1.0, 3.0, 100);
186: OHLCDataset dataset = new DefaultOHLCDataset("S1",
187: new OHLCDataItem[] { item1 });
188: Range range = renderer.findRangeBounds(dataset);
189: assertEquals(new Range(1.0, 4.0), range);
190:
191: OHLCDataItem item2 = new OHLCDataItem(new Date(1L), -1.0, 3.0,
192: -1.0, 3.0, 100);
193: dataset = new DefaultOHLCDataset("S1", new OHLCDataItem[] {
194: item1, item2 });
195: range = renderer.findRangeBounds(dataset);
196: assertEquals(new Range(-1.0, 4.0), range);
197:
198: // try an empty dataset - should return a null range
199: dataset = new DefaultOHLCDataset("S1", new OHLCDataItem[] {});
200: range = renderer.findRangeBounds(dataset);
201: assertNull(range);
202:
203: // try a null dataset - should return a null range
204: range = renderer.findRangeBounds(null);
205: assertNull(range);
206: }
207:
208: }
|