1
/* ========================================================================
2
* JCommon : a free general purpose class library for the Java(tm) platform
3
* ========================================================================
5
* (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
7
* Project Info: http://www.jfree.org/jcommon/index.html
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.
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.
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,
24
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25
* in the United States and other countries.]
27
* --------------------
28
* ObjectListTests.java
29
* --------------------
30
* (C) Copyright 2003, 2004, by Object Refinery Limited and Contributors.
32
* Original Author: David Gilbert (for Object Refinery Limited);
35
* $Id: ObjectListTests.java,v 1.2 2005/10/18 13:25:14 mungady Exp $
39
* 13-Aug-2003 : Version 1 (DG);
40
* 17-Sep-2003 : Added new tests for equals and serialization (DG);
44
package org.jfree.util.junit;
46
import java.awt.Color;
47
import java.io.ByteArrayInputStream;
48
import java.io.ByteArrayOutputStream;
49
import java.io.ObjectInput;
50
import java.io.ObjectInputStream;
51
import java.io.ObjectOutput;
52
import java.io.ObjectOutputStream;
54
import junit.framework.Test;
55
import junit.framework.TestCase;
56
import junit.framework.TestSuite;
58
import org.jfree.util.ObjectList;
61
* Tests for the {@link ObjectList} class.
63
public class ObjectListTests extends TestCase {
66
* Returns the tests as a test suite.
68
* @return The test suite.
70
public static Test suite() {
71
return new TestSuite(ObjectListTests.class);
75
* Constructs a new set of tests.
77
* @param name the name of the tests.
79
public ObjectListTests(final String name) {
84
* Tests the equals() method.
86
public void testEquals() {
88
final ObjectList l1 = new ObjectList();
89
l1.set(0, Color.blue);
92
final ObjectList l2 = new ObjectList();
93
l2.set(0, Color.blue);
96
assertTrue(l1.equals(l2));
97
assertTrue(l2.equals(l2));
102
* Another test of the equals method. The capacity of the internal list shouldn't
105
public void testEquals2() {
107
final ObjectList l1 = new ObjectList(20);
108
l1.set(0, Color.blue);
109
l1.set(1, Color.red);
111
final ObjectList l2 = new ObjectList();
112
l2.set(0, Color.blue);
113
l2.set(1, Color.red);
115
assertTrue(l1.equals(l2));
116
assertTrue(l2.equals(l2));
121
* Confirm that cloning works.
123
public void testCloning() {
125
final ObjectList l1 = new ObjectList();
126
l1.set(0, Color.blue);
127
l1.set(1, Color.red);
129
ObjectList l2 = null;
131
l2 = (ObjectList) l1.clone();
133
catch (CloneNotSupportedException e) {
134
System.err.println("ObjectListTests.testCloning: failed to clone.");
136
assertTrue(l1 != l2);
137
assertTrue(l1.getClass() == l2.getClass());
138
assertTrue(l1.equals(l2));
140
l2.set(0, Color.green);
141
assertFalse(l1.equals(l2));
146
* Serialize an instance, restore it, and check for equality.
148
public void testSerialization() {
150
final ObjectList l1 = new ObjectList();
151
l1.set(0, Color.red);
152
l1.set(1, Color.blue);
155
ObjectList l2 = null;
158
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
159
final ObjectOutput out = new ObjectOutputStream(buffer);
163
final ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
164
l2 = (ObjectList) in.readObject();
167
catch (Exception e) {
168
System.out.println(e.toString());
170
assertEquals(l1, l2);
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.
178
public void testExpand() {
179
final ObjectList l1 = new ObjectList();
180
l1.set(10, Color.blue);
181
final Color c = (Color) l1.get(10);
182
assertTrue(c.equals(Color.blue));