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: * ObjectUtilsTests.java
029: * ---------------------
030: * (C) Copyright 2004, 2005, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: ObjectUtilitiesTests.java,v 1.3 2005/10/18 13:25:14 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 15-Sep-2004 : Version 1 (DG);
040: * 25-Nov-2004 : Added new checks (DG);
041: *
042: */
043:
044: package org.jfree.util.junit;
045:
046: import java.awt.Point;
047: import java.util.ArrayList;
048: import java.util.Collection;
049:
050: import junit.framework.Test;
051: import junit.framework.TestCase;
052: import junit.framework.TestSuite;
053:
054: import org.jfree.util.ObjectUtilities;
055:
056: /**
057: * Some tests for the {@link ObjectUtilities} class.
058: */
059: public class ObjectUtilitiesTests extends TestCase {
060:
061: /**
062: * Returns the tests as a test suite.
063: *
064: * @return The test suite.
065: */
066: public static Test suite() {
067: return new TestSuite(ObjectUtilitiesTests.class);
068: }
069:
070: /**
071: * Constructs a new set of tests.
072: *
073: * @param name the name of the tests.
074: */
075: public ObjectUtilitiesTests(String name) {
076: super (name);
077: }
078:
079: /**
080: * Some checks for the clone(Object) method. The method should
081: * have these properties:
082: *
083: * (1) Return a clone for any object that can be cloned;
084: * (2) Throw a NullPointerException for any object that cannot be cloned;
085: * (3) Throw an IllegalArgumentException for null.
086: *
087: * @throws CloneNotSupportedException if there is a problem cloning.
088: */
089: public void testCloneObject() throws CloneNotSupportedException {
090: Object obj;
091: Object clone;
092:
093: // check String (not Cloneable)
094: boolean pass = false;
095: obj = "Hello World";
096: try {
097: clone = ObjectUtilities.clone(obj);
098: pass = false;
099: } catch (CloneNotSupportedException e) {
100: pass = true;
101: }
102: assertTrue(pass);
103:
104: // check Integer (not Cloneable)
105: pass = false;
106: obj = new Integer(123);
107: try {
108: clone = ObjectUtilities.clone(obj);
109: pass = false;
110: } catch (CloneNotSupportedException e) {
111: pass = true;
112: }
113: assertTrue(pass);
114:
115: // check Point (Cloneable)
116: obj = new Point(1, 2);
117: clone = ObjectUtilities.clone(obj);
118: assertEquals(obj, clone);
119:
120: // check null (should throw an IllegalArgumentException)
121: obj = null;
122: try {
123: clone = ObjectUtilities.clone(obj);
124: pass = false;
125: } catch (IllegalArgumentException e) {
126: pass = true;
127: }
128: assertTrue(pass);
129: }
130:
131: /**
132: * Some checks for the deepClone(Collection) method.
133: */
134: public void testDeepClone() {
135: Collection c1 = new ArrayList();
136: Collection c2 = null;
137:
138: // empty list
139: try {
140: c2 = ObjectUtilities.deepClone(c1);
141: assertTrue(c2.isEmpty());
142: } catch (CloneNotSupportedException e) {
143: assertTrue(false);
144: }
145:
146: // list containing Cloneable objects
147: c1 = new ArrayList();
148: c1.add(new Point(1, 2));
149: c1.add(new Point(3, 4));
150: try {
151: c2 = ObjectUtilities.deepClone(c1);
152: assertEquals(2, c2.size());
153: assertTrue(c2.contains(new Point(1, 2)));
154: assertTrue(c2.contains(new Point(3, 4)));
155: } catch (CloneNotSupportedException e) {
156: assertTrue(false);
157: }
158:
159: // list containing Cloneable and null objects
160: c1 = new ArrayList();
161: c1.add(new Point(1, 2));
162: c1.add(null);
163: c1.add(new Point(3, 4));
164: try {
165: c2 = ObjectUtilities.deepClone(c1);
166: assertEquals(3, c2.size());
167: assertTrue(c2.contains(new Point(1, 2)));
168: assertTrue(c2.contains(new Point(3, 4)));
169: } catch (CloneNotSupportedException e) {
170: assertTrue(false);
171: }
172:
173: // list containing non-Cloneable objects
174: c1.clear();
175: c1.add("S1");
176: c1.add("S2");
177: try {
178: c2 = ObjectUtilities.deepClone(c1);
179: assertTrue(false); // if we get to here, the test has failed
180: } catch (CloneNotSupportedException e) {
181: assertTrue(true);
182: }
183:
184: // null list
185: try {
186: c2 = ObjectUtilities.deepClone(null);
187: assertTrue(false); // if we get to here, the test has failed
188: } catch (IllegalArgumentException e) {
189: assertTrue(true);
190: } catch (CloneNotSupportedException e) {
191: assertTrue(false);
192: }
193:
194: }
195:
196: }
|