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: * OHLCSeriesTests.java
029: * --------------------
030: * (C) Copyright 2006, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: OHLCSeriesTests.java,v 1.1.2.1 2006/12/04 17:08:36 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 04-Dec-2006 : Version 1, based on XYSeriesTests (DG);
040: *
041: */
042:
043: package org.jfree.data.time.ohlc.junit;
044:
045: import java.io.ByteArrayInputStream;
046: import java.io.ByteArrayOutputStream;
047: import java.io.ObjectInput;
048: import java.io.ObjectInputStream;
049: import java.io.ObjectOutput;
050: import java.io.ObjectOutputStream;
051:
052: import junit.framework.Test;
053: import junit.framework.TestCase;
054: import junit.framework.TestSuite;
055:
056: import org.jfree.data.general.SeriesException;
057: import org.jfree.data.time.Year;
058: import org.jfree.data.time.ohlc.OHLCSeries;
059:
060: /**
061: * Tests for the {@link OHLCSeries} class.
062: */
063: public class OHLCSeriesTests extends TestCase {
064:
065: /**
066: * Returns the tests as a test suite.
067: *
068: * @return The test suite.
069: */
070: public static Test suite() {
071: return new TestSuite(OHLCSeriesTests.class);
072: }
073:
074: /**
075: * Constructs a new set of tests.
076: *
077: * @param name the name of the tests.
078: */
079: public OHLCSeriesTests(String name) {
080: super (name);
081: }
082:
083: /**
084: * Confirm that the equals method can distinguish all the required fields.
085: */
086: public void testEquals() {
087: OHLCSeries s1 = new OHLCSeries("s1");
088: OHLCSeries s2 = new OHLCSeries("s1");
089: assertTrue(s1.equals(s2));
090:
091: // seriesKey
092: s1 = new OHLCSeries("s2");
093: assertFalse(s1.equals(s2));
094: s2 = new OHLCSeries("s2");
095: assertTrue(s1.equals(s2));
096:
097: // add a value
098: s1.add(new Year(2006), 2.0, 4.0, 1.0, 3.0);
099: assertFalse(s1.equals(s2));
100: s2.add(new Year(2006), 2.0, 4.0, 1.0, 3.0);
101: assertTrue(s2.equals(s1));
102:
103: // add another value
104: s1.add(new Year(2008), 2.0, 4.0, 1.0, 3.0);
105: assertFalse(s1.equals(s2));
106: s2.add(new Year(2008), 2.0, 4.0, 1.0, 3.0);
107: assertTrue(s2.equals(s1));
108:
109: // remove a value
110: s1.remove(new Year(2008));
111: assertFalse(s1.equals(s2));
112: s2.remove(new Year(2008));
113: assertTrue(s2.equals(s1));
114: }
115:
116: /**
117: * Confirm that cloning works.
118: */
119: public void testCloning() {
120: OHLCSeries s1 = new OHLCSeries("s1");
121: s1.add(new Year(2006), 2.0, 4.0, 1.0, 3.0);
122: OHLCSeries s2 = null;
123: try {
124: s2 = (OHLCSeries) s1.clone();
125: } catch (CloneNotSupportedException e) {
126: e.printStackTrace();
127: }
128: assertTrue(s1 != s2);
129: assertTrue(s1.getClass() == s2.getClass());
130: assertTrue(s1.equals(s2));
131: }
132:
133: /**
134: * Serialize an instance, restore it, and check for equality.
135: */
136: public void testSerialization() {
137:
138: OHLCSeries s1 = new OHLCSeries("s1");
139: s1.add(new Year(2006), 2.0, 4.0, 1.0, 3.0);
140: OHLCSeries s2 = null;
141:
142: try {
143: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
144: ObjectOutput out = new ObjectOutputStream(buffer);
145: out.writeObject(s1);
146: out.close();
147:
148: ObjectInput in = new ObjectInputStream(
149: new ByteArrayInputStream(buffer.toByteArray()));
150: s2 = (OHLCSeries) in.readObject();
151: in.close();
152: } catch (Exception e) {
153: e.printStackTrace();
154: }
155: assertEquals(s1, s2);
156:
157: }
158:
159: /**
160: * Simple test for the indexOf() method.
161: */
162: public void testIndexOf() {
163: OHLCSeries s1 = new OHLCSeries("s1");
164: s1.add(new Year(2006), 2.0, 4.0, 1.0, 3.0);
165: s1.add(new Year(2011), 2.0, 4.0, 1.0, 3.0);
166: s1.add(new Year(2010), 2.0, 4.0, 1.0, 3.0);
167: assertEquals(0, s1.indexOf(new Year(2006)));
168: assertEquals(1, s1.indexOf(new Year(2010)));
169: assertEquals(2, s1.indexOf(new Year(2011)));
170: }
171:
172: /**
173: * Simple test for the remove() method.
174: */
175: public void testRemove() {
176: OHLCSeries s1 = new OHLCSeries("s1");
177: s1.add(new Year(2006), 2.0, 4.0, 1.0, 3.0);
178: s1.add(new Year(2011), 2.1, 4.1, 1.1, 3.1);
179: s1.add(new Year(2010), 2.2, 4.2, 1.2, 3.2);
180: assertEquals(3, s1.getItemCount());
181:
182: s1.remove(new Year(2010));
183: assertEquals(new Year(2011), s1.getPeriod(1));
184:
185: s1.remove(new Year(2006));
186: assertEquals(new Year(2011), s1.getPeriod(0));
187: }
188:
189: /**
190: * If you add a duplicate period, an exception should be thrown.
191: */
192: public void testAdditionOfDuplicatePeriod() {
193: OHLCSeries s1 = new OHLCSeries("s1");
194: s1.add(new Year(2006), 1.0, 1.0, 1.0, 1.0);
195: boolean pass = false;
196: try {
197: s1.add(new Year(2006), 1.0, 1.0, 1.0, 1.0);
198: } catch (SeriesException e) {
199: pass = true;
200: }
201: assertTrue(pass);
202: }
203:
204: /**
205: * A simple check that the maximumItemCount attribute is working.
206: */
207: public void testSetMaximumItemCount() {
208: OHLCSeries s1 = new OHLCSeries("s1");
209: assertEquals(Integer.MAX_VALUE, s1.getMaximumItemCount());
210: s1.setMaximumItemCount(2);
211: assertEquals(2, s1.getMaximumItemCount());
212: s1.add(new Year(2006), 1.0, 1.1, 1.1, 1.1);
213: s1.add(new Year(2007), 2.0, 2.2, 2.2, 2.2);
214: s1.add(new Year(2008), 3.0, 3.3, 3.3, 3.3);
215: assertEquals(new Year(2007), s1.getPeriod(0));
216: assertEquals(new Year(2008), s1.getPeriod(1));
217: }
218:
219: /**
220: * Check that the maximum item count can be applied retrospectively.
221: */
222: public void testSetMaximumItemCount2() {
223: OHLCSeries s1 = new OHLCSeries("s1");
224: s1.add(new Year(2006), 1.0, 1.1, 1.1, 1.1);
225: s1.add(new Year(2007), 2.0, 2.2, 2.2, 2.2);
226: s1.add(new Year(2008), 3.0, 3.3, 3.3, 3.3);
227: s1.setMaximumItemCount(2);
228: assertEquals(new Year(2007), s1.getPeriod(0));
229: assertEquals(new Year(2008), s1.getPeriod(1));
230: }
231:
232: }
|