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: * ComparableObjectSeriesTests.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: ComparableObjectSeriesTests.java,v 1.1.2.3 2007/04/13 14:38:31 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 20-Oct-2006 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.data.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.ComparableObjectItem;
057: import org.jfree.data.ComparableObjectSeries;
058:
059: /**
060: * Tests for the {@link ComparableObjectSeries} class.
061: */
062: public class ComparableObjectSeriesTests extends TestCase {
063:
064: static class MyComparableObjectSeries extends
065: ComparableObjectSeries {
066: /**
067: * Creates a new instance.
068: *
069: * @param key the series key.
070: */
071: public MyComparableObjectSeries(Comparable key) {
072: super (key);
073: }
074:
075: /**
076: * Creates a new instance.
077: *
078: * @param key the series key.
079: * @param autoSort automatically sort by x-value?
080: * @param allowDuplicateXValues allow duplicate values?
081: */
082: public MyComparableObjectSeries(Comparable key,
083: boolean autoSort, boolean allowDuplicateXValues) {
084: super (key, autoSort, allowDuplicateXValues);
085: }
086:
087: public void add(Comparable x, Object y) {
088: super .add(x, y);
089: }
090:
091: public ComparableObjectItem remove(Comparable x) {
092: return super .remove(x);
093: }
094: }
095:
096: /**
097: * Returns the tests as a test suite.
098: *
099: * @return The test suite.
100: */
101: public static Test suite() {
102: return new TestSuite(ComparableObjectSeriesTests.class);
103: }
104:
105: /**
106: * Constructs a new set of tests.
107: *
108: * @param name the name of the tests.
109: */
110: public ComparableObjectSeriesTests(String name) {
111: super (name);
112: }
113:
114: /**
115: * Some checks for the constructor.
116: */
117: public void testConstructor1() {
118: ComparableObjectSeries s1 = new ComparableObjectSeries("s1");
119: assertEquals("s1", s1.getKey());
120: assertNull(s1.getDescription());
121: assertTrue(s1.getAllowDuplicateXValues());
122: assertTrue(s1.getAutoSort());
123: assertEquals(0, s1.getItemCount());
124: assertEquals(Integer.MAX_VALUE, s1.getMaximumItemCount());
125:
126: // try null key
127: boolean pass = false;
128: try {
129: s1 = new ComparableObjectSeries(null);
130: } catch (IllegalArgumentException e) {
131: pass = true;
132: }
133: assertTrue(pass);
134: }
135:
136: /**
137: * Confirm that the equals method can distinguish all the required fields.
138: */
139: public void testEquals() {
140: MyComparableObjectSeries s1 = new MyComparableObjectSeries("A");
141: MyComparableObjectSeries s2 = new MyComparableObjectSeries("A");
142: assertTrue(s1.equals(s2));
143: assertTrue(s2.equals(s1));
144:
145: // key
146: s1 = new MyComparableObjectSeries("B");
147: assertFalse(s1.equals(s2));
148: s2 = new MyComparableObjectSeries("B");
149: assertTrue(s1.equals(s2));
150:
151: // autoSort
152: s1 = new MyComparableObjectSeries("B", false, true);
153: assertFalse(s1.equals(s2));
154: s2 = new MyComparableObjectSeries("B", false, true);
155: assertTrue(s1.equals(s2));
156:
157: // allowDuplicateXValues
158: s1 = new MyComparableObjectSeries("B", false, false);
159: assertFalse(s1.equals(s2));
160: s2 = new MyComparableObjectSeries("B", false, false);
161: assertTrue(s1.equals(s2));
162:
163: // add a value
164: s1.add(new Integer(1), "ABC");
165: assertFalse(s1.equals(s2));
166: s2.add(new Integer(1), "ABC");
167: assertTrue(s1.equals(s2));
168:
169: // add another value
170: s1.add(new Integer(0), "DEF");
171: assertFalse(s1.equals(s2));
172: s2.add(new Integer(0), "DEF");
173: assertTrue(s1.equals(s2));
174:
175: // remove an item
176: s1.remove(new Integer(1));
177: assertFalse(s1.equals(s2));
178: s2.remove(new Integer(1));
179: assertTrue(s1.equals(s2));
180: }
181:
182: /**
183: * Some checks for the clone() method.
184: */
185: public void testCloning() {
186: MyComparableObjectSeries s1 = new MyComparableObjectSeries("A");
187: s1.add(new Integer(1), "ABC");
188: MyComparableObjectSeries s2 = null;
189: try {
190: s2 = (MyComparableObjectSeries) s1.clone();
191: } catch (CloneNotSupportedException e) {
192: e.printStackTrace();
193: }
194: assertTrue(s1 != s2);
195: assertTrue(s1.getClass() == s2.getClass());
196: assertTrue(s1.equals(s2));
197: }
198:
199: /**
200: * Serialize an instance, restore it, and check for equality.
201: */
202: public void testSerialization() {
203: MyComparableObjectSeries s1 = new MyComparableObjectSeries("A");
204: s1.add(new Integer(1), "ABC");
205: MyComparableObjectSeries s2 = null;
206: try {
207: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
208: ObjectOutput out = new ObjectOutputStream(buffer);
209: out.writeObject(s1);
210: out.close();
211:
212: ObjectInput in = new ObjectInputStream(
213: new ByteArrayInputStream(buffer.toByteArray()));
214: s2 = (MyComparableObjectSeries) in.readObject();
215: in.close();
216: } catch (Exception e) {
217: e.printStackTrace();
218: }
219: assertEquals(s1, s2);
220: }
221:
222: }
|