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: * DefaultWindDatasetTests.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: DefaultWindDatasetTests.java,v 1.1.2.1 2006/10/03 15:41:37 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 12-Jul-2006 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.data.xy.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.time.Day;
057: import org.jfree.data.time.RegularTimePeriod;
058: import org.jfree.data.xy.DefaultWindDataset;
059:
060: /**
061: * Tests for {@link DefaultWindDataset}.
062: */
063: public class DefaultWindDatasetTests 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(DefaultWindDatasetTests.class);
072: }
073:
074: /**
075: * Constructs a new set of tests.
076: *
077: * @param name the name of the tests.
078: */
079: public DefaultWindDatasetTests(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: DefaultWindDataset d1 = new DefaultWindDataset();
088: DefaultWindDataset d2 = new DefaultWindDataset();
089: assertTrue(d1.equals(d2));
090: assertTrue(d2.equals(d1));
091:
092: d1 = createSampleDataset1();
093: assertFalse(d1.equals(d2));
094: d2 = createSampleDataset1();
095: assertTrue(d1.equals(d2));
096: }
097:
098: /**
099: * Confirm that cloning works.
100: */
101: public void testCloning() {
102: DefaultWindDataset d1 = new DefaultWindDataset();
103: DefaultWindDataset d2 = null;
104: try {
105: d2 = (DefaultWindDataset) d1.clone();
106: } catch (CloneNotSupportedException e) {
107: e.printStackTrace();
108: }
109: assertTrue(d1 != d2);
110: assertTrue(d1.getClass() == d2.getClass());
111: assertTrue(d1.equals(d2));
112:
113: // try a dataset with some content...
114: d1 = createSampleDataset1();
115: d2 = null;
116: try {
117: d2 = (DefaultWindDataset) d1.clone();
118: } catch (CloneNotSupportedException e) {
119: e.printStackTrace();
120: }
121: assertTrue(d1 != d2);
122: assertTrue(d1.getClass() == d2.getClass());
123: assertTrue(d1.equals(d2));
124: }
125:
126: /**
127: * Serialize an instance, restore it, and check for equality.
128: */
129: public void testSerialization() {
130: DefaultWindDataset d1 = new DefaultWindDataset();
131: DefaultWindDataset d2 = null;
132: try {
133: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
134: ObjectOutput out = new ObjectOutputStream(buffer);
135: out.writeObject(d1);
136: out.close();
137:
138: ObjectInput in = new ObjectInputStream(
139: new ByteArrayInputStream(buffer.toByteArray()));
140: d2 = (DefaultWindDataset) in.readObject();
141: in.close();
142: } catch (Exception e) {
143: e.printStackTrace();
144: }
145: assertEquals(d1, d2);
146:
147: // try a dataset with some content...
148: d1 = createSampleDataset1();
149: try {
150: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
151: ObjectOutput out = new ObjectOutputStream(buffer);
152: out.writeObject(d1);
153: out.close();
154:
155: ObjectInput in = new ObjectInputStream(
156: new ByteArrayInputStream(buffer.toByteArray()));
157: d2 = (DefaultWindDataset) in.readObject();
158: in.close();
159: } catch (Exception e) {
160: e.printStackTrace();
161: }
162: assertEquals(d1, d2);
163:
164: }
165:
166: /**
167: * Some checks for the getSeriesKey(int) method.
168: */
169: public void testGetSeriesKey() {
170: DefaultWindDataset d = createSampleDataset1();
171: assertEquals("Series 1", d.getSeriesKey(0));
172: assertEquals("Series 2", d.getSeriesKey(1));
173:
174: // check for series key out of bounds
175: boolean pass = false;
176: try {
177: /*Comparable k =*/d.getSeriesKey(-1);
178: } catch (IllegalArgumentException e) {
179: pass = true;
180: }
181: assertTrue(pass);
182:
183: pass = false;
184: try {
185: /*Comparable k =*/d.getSeriesKey(2);
186: } catch (IllegalArgumentException e) {
187: pass = true;
188: }
189: assertTrue(pass);
190: }
191:
192: /**
193: * Some checks for the indexOf(Comparable) method.
194: */
195: public void testIndexOf() {
196: DefaultWindDataset d = createSampleDataset1();
197: assertEquals(0, d.indexOf("Series 1"));
198: assertEquals(1, d.indexOf("Series 2"));
199: assertEquals(-1, d.indexOf("Green Eggs and Ham"));
200: assertEquals(-1, d.indexOf(null));
201: }
202:
203: /**
204: * Creates a sample dataset for testing.
205: *
206: * @return A sample dataset.
207: */
208: public DefaultWindDataset createSampleDataset1() {
209: Day t = new Day(1, 4, 2006);
210: Object[] item1 = createItem(t, 3, 7);
211: Object[] item2 = createItem(t.next(), 4, 8);
212: Object[] item3 = createItem(t.next(), 5, 9);
213: Object[][] series1 = new Object[][] { item1, item2, item3 };
214: Object[] item1b = createItem(t, 6, 10);
215: Object[] item2b = createItem(t.next(), 7, 11);
216: Object[] item3b = createItem(t.next(), 8, 12);
217: Object[][] series2 = new Object[][] { item1b, item2b, item3b };
218: Object[][][] data = new Object[][][] { series1, series2 };
219: return new DefaultWindDataset(data);
220: }
221:
222: /**
223: * Creates an array representing one item in a series.
224: *
225: * @param t the time period.
226: * @param dir the wind direction.
227: * @param force the wind force.
228: *
229: * @return An array containing the specified items.
230: */
231: private Object[] createItem(RegularTimePeriod t, int dir, int force) {
232: return new Object[] { new Long(t.getMiddleMillisecond()),
233: new Integer(dir), new Integer(force) };
234: }
235: }
|