KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > junit > ComparableObjectSeriesTests


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * --------------------------------
28  * ComparableObjectSeriesTests.java
29  * --------------------------------
30  * (C) Copyright 2006, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: ComparableObjectSeriesTests.java,v 1.1.2.2 2006/10/23 09:21:58 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 20-Oct-2006 : Version 1 (DG);
40  *
41  */

42
43 package org.jfree.data.junit;
44
45 import java.io.ByteArrayInputStream JavaDoc;
46 import java.io.ByteArrayOutputStream JavaDoc;
47 import java.io.ObjectInput JavaDoc;
48 import java.io.ObjectInputStream JavaDoc;
49 import java.io.ObjectOutput JavaDoc;
50 import java.io.ObjectOutputStream JavaDoc;
51
52 import junit.framework.Test;
53 import junit.framework.TestCase;
54 import junit.framework.TestSuite;
55 import org.jfree.data.ComparableObjectItem;
56 import org.jfree.data.ComparableObjectSeries;
57
58 /**
59  * Tests for the {@link ComparableObjectSeries} class.
60  */

61 public class ComparableObjectSeriesTests extends TestCase {
62
63     static class MyComparableObjectSeries extends ComparableObjectSeries {
64         /**
65          * Creates a new instance.
66          *
67          * @param key the series key.
68          */

69         public MyComparableObjectSeries(Comparable JavaDoc key) {
70             super(key);
71         }
72         /**
73          * Creates a new instance.
74          *
75          * @param key the series key.
76          * @param autoSort automatically sort by x-value?
77          * @param allowDuplicateXValues allow duplicate values?
78          */

79         public MyComparableObjectSeries(Comparable JavaDoc key, boolean autoSort,
80                 boolean allowDuplicateXValues) {
81             super(key, autoSort, allowDuplicateXValues);
82         }
83         public void add(Comparable JavaDoc x, Object JavaDoc y) {
84             super.add(x, y);
85         }
86
87         public ComparableObjectItem remove(Comparable JavaDoc x) {
88             return super.remove(x);
89         }
90     }
91     
92     /**
93      * Returns the tests as a test suite.
94      *
95      * @return The test suite.
96      */

97     public static Test suite() {
98         return new TestSuite(ComparableObjectSeriesTests.class);
99     }
100
101     /**
102      * Constructs a new set of tests.
103      *
104      * @param name the name of the tests.
105      */

106     public ComparableObjectSeriesTests(String JavaDoc name) {
107         super(name);
108     }
109
110     /**
111      * Some checks for the constructor.
112      */

113     public void testConstructor1() {
114         ComparableObjectSeries s1 = new ComparableObjectSeries("s1");
115         assertEquals("s1", s1.getKey());
116         assertNull(s1.getDescription());
117         assertTrue(s1.getAllowDuplicateXValues());
118         assertTrue(s1.getAutoSort());
119         assertEquals(0, s1.getItemCount());
120         assertEquals(Integer.MAX_VALUE, s1.getMaximumItemCount());
121         
122         // try null key
123
boolean pass = false;
124         try {
125             s1 = new ComparableObjectSeries(null);
126         }
127         catch (IllegalArgumentException JavaDoc e) {
128             pass = true;
129         }
130         assertTrue(pass);
131     }
132     
133     /**
134      * Confirm that the equals method can distinguish all the required fields.
135      */

136     public void testEquals() {
137         MyComparableObjectSeries s1 = new MyComparableObjectSeries("A");
138         MyComparableObjectSeries s2 = new MyComparableObjectSeries("A");
139         assertTrue(s1.equals(s2));
140         assertTrue(s2.equals(s1));
141
142         // key
143
s1 = new MyComparableObjectSeries("B");
144         assertFalse(s1.equals(s2));
145         s2 = new MyComparableObjectSeries("B");
146         assertTrue(s1.equals(s2));
147         
148         // autoSort
149
s1 = new MyComparableObjectSeries("B", false, true);
150         assertFalse(s1.equals(s2));
151         s2 = new MyComparableObjectSeries("B", false, true);
152         assertTrue(s1.equals(s2));
153
154         // allowDuplicateXValues
155
s1 = new MyComparableObjectSeries("B", false, false);
156         assertFalse(s1.equals(s2));
157         s2 = new MyComparableObjectSeries("B", false, false);
158         assertTrue(s1.equals(s2));
159
160         // add a value
161
s1.add(new Integer JavaDoc(1), "ABC");
162         assertFalse(s1.equals(s2));
163         s2.add(new Integer JavaDoc(1), "ABC");
164         assertTrue(s1.equals(s2));
165         
166         // add another value
167
s1.add(new Integer JavaDoc(0), "DEF");
168         assertFalse(s1.equals(s2));
169         s2.add(new Integer JavaDoc(0), "DEF");
170         assertTrue(s1.equals(s2));
171         
172         // remove an item
173
s1.remove(new Integer JavaDoc(1));
174         assertFalse(s1.equals(s2));
175         s2.remove(new Integer JavaDoc(1));
176         assertTrue(s1.equals(s2));
177     }
178
179     /**
180      * Some checks for the clone() method.
181      */

182     public void testCloning() {
183         MyComparableObjectSeries s1 = new MyComparableObjectSeries("A");
184         s1.add(new Integer JavaDoc(1), "ABC");
185         MyComparableObjectSeries s2 = null;
186         try {
187             s2 = (MyComparableObjectSeries) s1.clone();
188         }
189         catch (CloneNotSupportedException JavaDoc e) {
190             e.printStackTrace();
191         }
192         assertTrue(s1 != s2);
193         assertTrue(s1.getClass() == s2.getClass());
194         assertTrue(s1.equals(s2));
195     }
196
197     /**
198      * Serialize an instance, restore it, and check for equality.
199      */

200     public void testSerialization() {
201         MyComparableObjectSeries s1 = new MyComparableObjectSeries("A");
202         s1.add(new Integer JavaDoc(1), "ABC");
203         MyComparableObjectSeries s2 = null;
204         try {
205             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
206             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
207             out.writeObject(s1);
208             out.close();
209
210             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
211                     new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
212             s2 = (MyComparableObjectSeries) in.readObject();
213             in.close();
214         }
215         catch (Exception JavaDoc e) {
216             e.printStackTrace();
217         }
218         assertEquals(s1, s2);
219     }
220
221 }
222
Popular Tags