001: /* ========================================================================
002: * JCommon : a free general purpose class library for the Java(tm) platform
003: * ========================================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jcommon/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: * ObjectListTests.java
029: * --------------------
030: * (C) Copyright 2003, 2004, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: ObjectListTests.java,v 1.2 2005/10/18 13:25:14 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 13-Aug-2003 : Version 1 (DG);
040: * 17-Sep-2003 : Added new tests for equals and serialization (DG);
041: *
042: */
043:
044: package org.jfree.util.junit;
045:
046: import java.awt.Color;
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.util.ObjectList;
059:
060: /**
061: * Tests for the {@link ObjectList} class.
062: */
063: public class ObjectListTests extends TestCase {
064:
065: /**
066: * Returns the tests as a test suite.
067: *
068: * @return The test suite.
069: */
070: public static Test suite() {
071: return new TestSuite(ObjectListTests.class);
072: }
073:
074: /**
075: * Constructs a new set of tests.
076: *
077: * @param name the name of the tests.
078: */
079: public ObjectListTests(final String name) {
080: super (name);
081: }
082:
083: /**
084: * Tests the equals() method.
085: */
086: public void testEquals() {
087:
088: final ObjectList l1 = new ObjectList();
089: l1.set(0, Color.blue);
090: l1.set(1, Color.red);
091:
092: final ObjectList l2 = new ObjectList();
093: l2.set(0, Color.blue);
094: l2.set(1, Color.red);
095:
096: assertTrue(l1.equals(l2));
097: assertTrue(l2.equals(l2));
098:
099: }
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: } catch (CloneNotSupportedException e) {
133: System.err
134: .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 buffer = new ByteArrayOutputStream();
159: final ObjectOutput out = new ObjectOutputStream(buffer);
160: out.writeObject(l1);
161: out.close();
162:
163: final ObjectInput in = new ObjectInputStream(
164: new ByteArrayInputStream(buffer.toByteArray()));
165: l2 = (ObjectList) in.readObject();
166: in.close();
167: } catch (Exception 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 c = (Color) l1.get(10);
182: assertTrue(c.equals(Color.blue));
183: }
184:
185: }
|