KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > util > junit > ObjectListTests


1 /* ========================================================================
2  * JCommon : a free general purpose class library for the Java(tm) platform
3  * ========================================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jcommon/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  * ObjectListTests.java
29  * --------------------
30  * (C) Copyright 2003, 2004, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: ObjectListTests.java,v 1.2 2005/10/18 13:25:14 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 13-Aug-2003 : Version 1 (DG);
40  * 17-Sep-2003 : Added new tests for equals and serialization (DG);
41  *
42  */

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

63 public class ObjectListTests extends TestCase {
64
65     /**
66      * Returns the tests as a test suite.
67      *
68      * @return The test suite.
69      */

70     public static Test suite() {
71         return new TestSuite(ObjectListTests.class);
72     }
73
74     /**
75      * Constructs a new set of tests.
76      *
77      * @param name the name of the tests.
78      */

79     public ObjectListTests(final String JavaDoc name) {
80         super(name);
81     }
82
83     /**
84      * Tests the equals() method.
85      */

86     public void testEquals() {
87         
88         final ObjectList l1 = new ObjectList();
89         l1.set(0, Color.blue);
90         l1.set(1, Color.red);
91         
92         final ObjectList l2 = new ObjectList();
93         l2.set(0, Color.blue);
94         l2.set(1, Color.red);
95         
96         assertTrue(l1.equals(l2));
97         assertTrue(l2.equals(l2));
98         
99     }
100     
101     /**
102      * Another test of the equals method. The capacity of the internal list shouldn't
103      * be a factor.
104      */

105     public void testEquals2() {
106         
107         final ObjectList l1 = new ObjectList(20);
108         l1.set(0, Color.blue);
109         l1.set(1, Color.red);
110         
111         final ObjectList l2 = new ObjectList();
112         l2.set(0, Color.blue);
113         l2.set(1, Color.red);
114         
115         assertTrue(l1.equals(l2));
116         assertTrue(l2.equals(l2));
117         
118     }
119     
120     /**
121      * Confirm that cloning works.
122      */

123     public void testCloning() {
124         
125         final ObjectList l1 = new ObjectList();
126         l1.set(0, Color.blue);
127         l1.set(1, Color.red);
128         
129         ObjectList l2 = null;
130         try {
131             l2 = (ObjectList) l1.clone();
132         }
133         catch (CloneNotSupportedException JavaDoc e) {
134             System.err.println("ObjectListTests.testCloning: failed to clone.");
135         }
136         assertTrue(l1 != l2);
137         assertTrue(l1.getClass() == l2.getClass());
138         assertTrue(l1.equals(l2));
139         
140         l2.set(0, Color.green);
141         assertFalse(l1.equals(l2));
142         
143     }
144     
145     /**
146      * Serialize an instance, restore it, and check for equality.
147      */

148     public void testSerialization() {
149
150         final ObjectList l1 = new ObjectList();
151         l1.set(0, Color.red);
152         l1.set(1, Color.blue);
153         l1.set(2, null);
154         
155         ObjectList l2 = null;
156
157         try {
158             final ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
159             final ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
160             out.writeObject(l1);
161             out.close();
162
163             final ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
164             l2 = (ObjectList) in.readObject();
165             in.close();
166         }
167         catch (Exception JavaDoc e) {
168             System.out.println(e.toString());
169         }
170         assertEquals(l1, l2);
171
172     }
173         
174     /**
175      * Tests the expand method. This test reproduces a bug where the list was not expanded beyond
176      * the initial default size of 8. This bug is now fixed.
177      */

178     public void testExpand() {
179         final ObjectList l1 = new ObjectList();
180         l1.set(10, Color.blue);
181         final Color JavaDoc c = (Color JavaDoc) l1.get(10);
182         assertTrue(c.equals(Color.blue));
183     }
184
185 }
186
Popular Tags