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: * CandlestickRendererTests.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: CandlestickRendererTests.java,v 1.1.2.2 2007/03/05 14:40:33 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 25-Mar-2003 : Version 1 (DG);
040: * 22-Oct-2003 : Added hashCode test (DG);
041: * 17-Aug-2006 : Strengthened testEquals() and added testFindRangeBounds()
042: * method (DG);
043: * 05-Mar-2007 : Added new field to testEquals() (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.io.ByteArrayInputStream;
052: import java.io.ByteArrayOutputStream;
053: import java.io.ObjectInput;
054: import java.io.ObjectInputStream;
055: import java.io.ObjectOutput;
056: import java.io.ObjectOutputStream;
057: import java.util.Date;
058:
059: import junit.framework.Test;
060: import junit.framework.TestCase;
061: import junit.framework.TestSuite;
062:
063: import org.jfree.chart.renderer.xy.CandlestickRenderer;
064: import org.jfree.data.Range;
065: import org.jfree.data.xy.DefaultOHLCDataset;
066: import org.jfree.data.xy.OHLCDataItem;
067: import org.jfree.data.xy.OHLCDataset;
068:
069: /**
070: * Tests for the {@link CandlestickRenderer} class.
071: */
072: public class CandlestickRendererTests extends TestCase {
073:
074: /**
075: * Returns the tests as a test suite.
076: *
077: * @return The test suite.
078: */
079: public static Test suite() {
080: return new TestSuite(CandlestickRendererTests.class);
081: }
082:
083: /**
084: * Constructs a new set of tests.
085: *
086: * @param name the name of the tests.
087: */
088: public CandlestickRendererTests(String name) {
089: super (name);
090: }
091:
092: /**
093: * Check that the equals() method distinguishes all fields.
094: */
095: public void testEquals() {
096: CandlestickRenderer r1 = new CandlestickRenderer();
097: CandlestickRenderer r2 = new CandlestickRenderer();
098: assertEquals(r1, r2);
099:
100: // upPaint
101: r1.setUpPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
102: 4.0f, Color.white));
103: assertFalse(r1.equals(r2));
104: r2.setUpPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
105: 4.0f, Color.white));
106: assertTrue(r1.equals(r2));
107:
108: // downPaint
109: r1.setDownPaint(new GradientPaint(5.0f, 6.0f, Color.green,
110: 7.0f, 8.0f, Color.yellow));
111: assertFalse(r1.equals(r2));
112: r2.setDownPaint(new GradientPaint(5.0f, 6.0f, Color.green,
113: 7.0f, 8.0f, Color.yellow));
114: assertTrue(r1.equals(r2));
115:
116: // drawVolume
117: r1.setDrawVolume(false);
118: assertFalse(r1.equals(r2));
119: r2.setDrawVolume(false);
120: assertTrue(r1.equals(r2));
121:
122: // candleWidth
123: r1.setCandleWidth(3.3);
124: assertFalse(r1.equals(r2));
125: r2.setCandleWidth(3.3);
126: assertTrue(r1.equals(r2));
127:
128: // maxCandleWidthInMilliseconds
129: r1.setMaxCandleWidthInMilliseconds(123);
130: assertFalse(r1.equals(r2));
131: r2.setMaxCandleWidthInMilliseconds(123);
132: assertTrue(r1.equals(r2));
133:
134: // autoWidthMethod
135: r1.setAutoWidthMethod(CandlestickRenderer.WIDTHMETHOD_SMALLEST);
136: assertFalse(r1.equals(r2));
137: r2.setAutoWidthMethod(CandlestickRenderer.WIDTHMETHOD_SMALLEST);
138: assertTrue(r1.equals(r2));
139:
140: // autoWidthFactor
141: r1.setAutoWidthFactor(0.22);
142: assertFalse(r1.equals(r2));
143: r2.setAutoWidthFactor(0.22);
144: assertTrue(r1.equals(r2));
145:
146: // autoWidthGap
147: r1.setAutoWidthGap(1.1);
148: assertFalse(r1.equals(r2));
149: r2.setAutoWidthGap(1.1);
150: assertTrue(r1.equals(r2));
151:
152: r1.setUseOutlinePaint(true);
153: assertFalse(r1.equals(r2));
154: r2.setUseOutlinePaint(true);
155: assertTrue(r1.equals(r2));
156: }
157:
158: /**
159: * Two objects that are equal are required to return the same hashCode.
160: */
161: public void testHashcode() {
162: CandlestickRenderer r1 = new CandlestickRenderer();
163: CandlestickRenderer r2 = new CandlestickRenderer();
164: assertTrue(r1.equals(r2));
165: int h1 = r1.hashCode();
166: int h2 = r2.hashCode();
167: assertEquals(h1, h2);
168: }
169:
170: /**
171: * Confirm that cloning works.
172: */
173: public void testCloning() {
174: CandlestickRenderer r1 = new CandlestickRenderer();
175: CandlestickRenderer r2 = null;
176: try {
177: r2 = (CandlestickRenderer) r1.clone();
178: } catch (CloneNotSupportedException e) {
179: e.printStackTrace();
180: }
181: assertTrue(r1 != r2);
182: assertTrue(r1.getClass() == r2.getClass());
183: assertTrue(r1.equals(r2));
184: }
185:
186: /**
187: * Serialize an instance, restore it, and check for equality.
188: */
189: public void testSerialization() {
190:
191: CandlestickRenderer r1 = new CandlestickRenderer();
192: CandlestickRenderer r2 = null;
193:
194: try {
195: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
196: ObjectOutput out = new ObjectOutputStream(buffer);
197: out.writeObject(r1);
198: out.close();
199:
200: ObjectInput in = new ObjectInputStream(
201: new ByteArrayInputStream(buffer.toByteArray()));
202: r2 = (CandlestickRenderer) in.readObject();
203: in.close();
204: } catch (Exception e) {
205: e.printStackTrace();
206: }
207: assertEquals(r1, r2);
208:
209: }
210:
211: /**
212: * Some checks for the findRangeBounds() method.
213: */
214: public void testFindRangeBounds() {
215: CandlestickRenderer renderer = new CandlestickRenderer();
216:
217: OHLCDataItem item1 = new OHLCDataItem(new Date(1L), 2.0, 4.0,
218: 1.0, 3.0, 100);
219: OHLCDataset dataset = new DefaultOHLCDataset("S1",
220: new OHLCDataItem[] { item1 });
221: Range range = renderer.findRangeBounds(dataset);
222: assertEquals(new Range(1.0, 4.0), range);
223:
224: OHLCDataItem item2 = new OHLCDataItem(new Date(1L), -1.0, 3.0,
225: -1.0, 3.0, 100);
226: dataset = new DefaultOHLCDataset("S1", new OHLCDataItem[] {
227: item1, item2 });
228: range = renderer.findRangeBounds(dataset);
229: assertEquals(new Range(-1.0, 4.0), range);
230:
231: // try an empty dataset - should return a null range
232: dataset = new DefaultOHLCDataset("S1", new OHLCDataItem[] {});
233: range = renderer.findRangeBounds(dataset);
234: assertNull(range);
235:
236: // try a null dataset - should return a null range
237: range = renderer.findRangeBounds(null);
238: assertNull(range);
239: }
240:
241: }
|