KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ObjectUtilsTests.java
29  * ---------------------
30  * (C) Copyright 2004, 2005, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: ObjectUtilitiesTests.java,v 1.3 2005/10/18 13:25:14 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 15-Sep-2004 : Version 1 (DG);
40  * 25-Nov-2004 : Added new checks (DG);
41  *
42  */

43
44 package org.jfree.util.junit;
45
46 import java.awt.Point JavaDoc;
47 import java.util.ArrayList JavaDoc;
48 import java.util.Collection JavaDoc;
49
50 import junit.framework.Test;
51 import junit.framework.TestCase;
52 import junit.framework.TestSuite;
53
54 import org.jfree.util.ObjectUtilities;
55
56 /**
57  * Some tests for the {@link ObjectUtilities} class.
58  */

59 public class ObjectUtilitiesTests extends TestCase {
60
61     /**
62      * Returns the tests as a test suite.
63      *
64      * @return The test suite.
65      */

66     public static Test suite() {
67         return new TestSuite(ObjectUtilitiesTests.class);
68     }
69
70     /**
71      * Constructs a new set of tests.
72      *
73      * @param name the name of the tests.
74      */

75     public ObjectUtilitiesTests(String JavaDoc name) {
76         super(name);
77     }
78
79     /**
80      * Some checks for the clone(Object) method. The method should
81      * have these properties:
82      *
83      * (1) Return a clone for any object that can be cloned;
84      * (2) Throw a NullPointerException for any object that cannot be cloned;
85      * (3) Throw an IllegalArgumentException for null.
86      *
87      * @throws CloneNotSupportedException if there is a problem cloning.
88      */

89     public void testCloneObject() throws CloneNotSupportedException JavaDoc {
90         Object JavaDoc obj;
91         Object JavaDoc clone;
92         
93         // check String (not Cloneable)
94
boolean pass = false;
95         obj = "Hello World";
96         try {
97             clone = ObjectUtilities.clone(obj);
98             pass = false;
99         }
100         catch (CloneNotSupportedException JavaDoc e) {
101             pass = true;
102         }
103         assertTrue(pass);
104         
105         // check Integer (not Cloneable)
106
pass = false;
107         obj = new Integer JavaDoc(123);
108         try {
109             clone = ObjectUtilities.clone(obj);
110             pass = false;
111         }
112         catch (CloneNotSupportedException JavaDoc e) {
113             pass = true;
114         }
115         assertTrue(pass);
116         
117         // check Point (Cloneable)
118
obj = new Point JavaDoc(1, 2);
119         clone = ObjectUtilities.clone(obj);
120         assertEquals(obj, clone);
121
122         // check null (should throw an IllegalArgumentException)
123
obj = null;
124         try {
125             clone = ObjectUtilities.clone(obj);
126             pass = false;
127         }
128         catch (IllegalArgumentException JavaDoc e) {
129             pass = true;
130         }
131         assertTrue(pass);
132     }
133     
134     /**
135      * Some checks for the deepClone(Collection) method.
136      */

137     public void testDeepClone() {
138         Collection JavaDoc c1 = new ArrayList JavaDoc();
139         Collection JavaDoc c2 = null;
140         
141         // empty list
142
try {
143             c2 = ObjectUtilities.deepClone(c1);
144             assertTrue(c2.isEmpty());
145         }
146         catch (CloneNotSupportedException JavaDoc e) {
147             assertTrue(false);
148         }
149         
150         // list containing Cloneable objects
151
c1 = new ArrayList JavaDoc();
152         c1.add(new Point JavaDoc(1, 2));
153         c1.add(new Point JavaDoc(3, 4));
154         try {
155             c2 = ObjectUtilities.deepClone(c1);
156             assertEquals(2, c2.size());
157             assertTrue(c2.contains(new Point JavaDoc(1, 2)));
158             assertTrue(c2.contains(new Point JavaDoc(3, 4)));
159         }
160         catch (CloneNotSupportedException JavaDoc e) {
161             assertTrue(false);
162         }
163
164         // list containing Cloneable and null objects
165
c1 = new ArrayList JavaDoc();
166         c1.add(new Point JavaDoc(1, 2));
167         c1.add(null);
168         c1.add(new Point JavaDoc(3, 4));
169         try {
170             c2 = ObjectUtilities.deepClone(c1);
171             assertEquals(3, c2.size());
172             assertTrue(c2.contains(new Point JavaDoc(1, 2)));
173             assertTrue(c2.contains(new Point JavaDoc(3, 4)));
174         }
175         catch (CloneNotSupportedException JavaDoc e) {
176             assertTrue(false);
177         }
178         
179         // list containing non-Cloneable objects
180
c1.clear();
181         c1.add("S1");
182         c1.add("S2");
183         try {
184             c2 = ObjectUtilities.deepClone(c1);
185             assertTrue(false); // if we get to here, the test has failed
186
}
187         catch (CloneNotSupportedException JavaDoc e) {
188             assertTrue(true);
189         }
190         
191         // null list
192
try {
193             c2 = ObjectUtilities.deepClone(null);
194             assertTrue(false); // if we get to here, the test has failed
195
}
196         catch (IllegalArgumentException JavaDoc e) {
197             assertTrue(true);
198         }
199         catch (CloneNotSupportedException JavaDoc e) {
200             assertTrue(false);
201         }
202
203     }
204
205 }
206
Popular Tags