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: * TimePeriodValuesCollectionTests.java
029: * ------------------------------------
030: * (C) Copyright 2005-2007, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: TimePeriodValuesCollectionTests.java,v 1.1.2.3 2007/06/11 10:19:04 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 11-Mar-2005 : Version 1 (DG);
040: * 08-Mar-2007 : Added testGetSeries() (DG);
041: * 11-Jun-2007 : Added tests for getDomainBounds() (DG);
042: *
043: */
044:
045: package org.jfree.data.time.junit;
046:
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.data.Range;
059: import org.jfree.data.time.Day;
060: import org.jfree.data.time.SimpleTimePeriod;
061: import org.jfree.data.time.TimePeriodAnchor;
062: import org.jfree.data.time.TimePeriodValues;
063: import org.jfree.data.time.TimePeriodValuesCollection;
064:
065: /**
066: * Some tests for the {@link TimePeriodValuesCollection} class.
067: */
068: public class TimePeriodValuesCollectionTests extends TestCase {
069:
070: /**
071: * Returns the tests as a test suite.
072: *
073: * @return The test suite.
074: */
075: public static Test suite() {
076: return new TestSuite(TimePeriodValuesCollectionTests.class);
077: }
078:
079: /**
080: * Constructs a new set of tests.
081: *
082: * @param name the name of the tests.
083: */
084: public TimePeriodValuesCollectionTests(String name) {
085: super (name);
086: }
087:
088: /**
089: * Common test setup.
090: */
091: protected void setUp() {
092: // no setup
093: }
094:
095: /**
096: * A test for bug report 1161340. I wasn't able to reproduce the problem
097: * with this test.
098: */
099: public void test1161340() {
100: TimePeriodValuesCollection dataset = new TimePeriodValuesCollection();
101: TimePeriodValues v1 = new TimePeriodValues("V1");
102: v1.add(new Day(11, 3, 2005), 1.2);
103: v1.add(new Day(12, 3, 2005), 3.4);
104: dataset.addSeries(v1);
105: assertEquals(1, dataset.getSeriesCount());
106: dataset.removeSeries(v1);
107: assertEquals(0, dataset.getSeriesCount());
108:
109: TimePeriodValues v2 = new TimePeriodValues("V2");
110: v1.add(new Day(5, 3, 2005), 1.2);
111: v1.add(new Day(6, 3, 2005), 3.4);
112: dataset.addSeries(v2);
113: assertEquals(1, dataset.getSeriesCount());
114: }
115:
116: /**
117: * Tests the equals() method.
118: */
119: public void testEquals() {
120:
121: TimePeriodValuesCollection c1 = new TimePeriodValuesCollection();
122: TimePeriodValuesCollection c2 = new TimePeriodValuesCollection();
123: assertTrue(c1.equals(c2));
124:
125: c1.setDomainIsPointsInTime(!c1.getDomainIsPointsInTime());
126: assertFalse(c1.equals(c2));
127: c2.setDomainIsPointsInTime(c1.getDomainIsPointsInTime());
128: assertTrue(c1.equals(c2));
129:
130: c1.setXPosition(TimePeriodAnchor.END);
131: assertFalse(c1.equals(c2));
132: c2.setXPosition(TimePeriodAnchor.END);
133: assertTrue(c1.equals(c2));
134:
135: TimePeriodValues v1 = new TimePeriodValues("Test");
136: TimePeriodValues v2 = new TimePeriodValues("Test");
137:
138: c1.addSeries(v1);
139: assertFalse(c1.equals(c2));
140: c2.addSeries(v2);
141: assertTrue(c1.equals(c2));
142: }
143:
144: /**
145: * Serialize an instance, restore it, and check for equality.
146: */
147: public void testSerialization() {
148: TimePeriodValuesCollection c1 = new TimePeriodValuesCollection();
149: TimePeriodValuesCollection c2 = null;
150: try {
151: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
152: ObjectOutput out = new ObjectOutputStream(buffer);
153: out.writeObject(c1);
154: out.close();
155:
156: ObjectInput in = new ObjectInputStream(
157: new ByteArrayInputStream(buffer.toByteArray()));
158: c2 = (TimePeriodValuesCollection) in.readObject();
159: in.close();
160: } catch (Exception e) {
161: e.printStackTrace();
162: }
163: assertEquals(c1, c2);
164: }
165:
166: /**
167: * Some basic checks for the getSeries() method.
168: */
169: public void testGetSeries() {
170: TimePeriodValuesCollection c1 = new TimePeriodValuesCollection();
171: TimePeriodValues s1 = new TimePeriodValues("Series 1");
172: c1.addSeries(s1);
173: assertEquals("Series 1", c1.getSeries(0).getKey());
174:
175: boolean pass = false;
176: try {
177: c1.getSeries(-1);
178: } catch (IllegalArgumentException e) {
179: pass = true;
180: }
181: assertTrue(pass);
182:
183: pass = false;
184: try {
185: c1.getSeries(1);
186: } catch (IllegalArgumentException e) {
187: pass = true;
188: }
189: assertTrue(pass);
190: }
191:
192: /**
193: * Some checks for the getDomainBounds() method.
194: */
195: public void testGetDomainBoundsWithoutInterval() {
196: // check empty dataset
197: TimePeriodValuesCollection dataset = new TimePeriodValuesCollection();
198: dataset.setDomainIsPointsInTime(false);
199: Range r = dataset.getDomainBounds(false);
200: assertNull(r);
201:
202: // check dataset with one time period
203: TimePeriodValues s1 = new TimePeriodValues("S1");
204: s1.add(new SimpleTimePeriod(1000L, 2000L), 1.0);
205: dataset.addSeries(s1);
206: r = dataset.getDomainBounds(false);
207: assertEquals(1500.0, r.getLowerBound());
208: assertEquals(1500.0, r.getUpperBound());
209:
210: // check dataset with two time periods
211: s1.add(new SimpleTimePeriod(1500L, 3000L), 2.0);
212: r = dataset.getDomainBounds(false);
213: assertEquals(1500.0, r.getLowerBound());
214: assertEquals(2250.0, r.getUpperBound());
215: }
216:
217: /**
218: * Some more checks for the getDomainBounds() method.
219: *
220: * @see #testGetDomainBoundsWithoutInterval()
221: */
222: public void testGetDomainBoundsWithInterval() {
223: // check empty dataset
224: TimePeriodValuesCollection dataset = new TimePeriodValuesCollection();
225: Range r = dataset.getDomainBounds(true);
226: assertNull(r);
227:
228: // check dataset with one time period
229: TimePeriodValues s1 = new TimePeriodValues("S1");
230: s1.add(new SimpleTimePeriod(1000L, 2000L), 1.0);
231: dataset.addSeries(s1);
232: r = dataset.getDomainBounds(true);
233: assertEquals(1000.0, r.getLowerBound());
234: assertEquals(2000.0, r.getUpperBound());
235:
236: // check dataset with two time periods
237: s1.add(new SimpleTimePeriod(1500L, 3000L), 2.0);
238: r = dataset.getDomainBounds(true);
239: assertEquals(1000.0, r.getLowerBound());
240: assertEquals(3000.0, r.getUpperBound());
241: }
242: }
|